2nd try at test stabilization
[plcapi.git] / tests / 3LocalhostTest.php
1 <?php
2
3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
5
6 include_once __DIR__ . '/parse_args.php';
7
8 /**
9  * Tests which involve interaction between the client and the server.
10  * They are run against the server found in demo/server.php
11  */
12 class LocalhostTest extends PHPUnit_Framework_TestCase
13 {
14     /** @var xmlrpc_client $client */
15     protected $client = null;
16     protected $method = 'http';
17     protected $timeout = 10;
18     protected $request_compression = null;
19     protected $accepted_compression = '';
20     protected $args = array();
21
22     protected static $failed_tests = array();
23
24     protected $testId;
25     /** @var boolean $collectCodeCoverageInformation */
26     protected $collectCodeCoverageInformation;
27     protected $coverageScriptUrl;
28
29     public static function fail($message = '')
30     {
31         // save in a static var that this particular test has failed
32         // (but only if not called from subclass objects / multitests)
33         if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
34             $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
35             for ($i = 0; $i < count($trace); $i++) {
36                 if (strpos($trace[$i]['function'], 'test') === 0) {
37                     self::$failed_tests[$trace[$i]['function']] = true;
38                     break;
39                 }
40             }
41         }
42
43         parent::fail($message);
44     }
45
46     /**
47      * Reimplemented to allow us to collect code coverage info from the target server.
48      * Code taken from PHPUnit_Extensions_Selenium2TestCase
49      *
50      * @param PHPUnit_Framework_TestResult $result
51      * @return PHPUnit_Framework_TestResult
52      * @throws Exception
53      */
54     public function run(PHPUnit_Framework_TestResult $result = NULL)
55     {
56         $this->testId = get_class($this) . '__' . $this->getName();
57
58         if ($result === NULL) {
59             $result = $this->createResult();
60         }
61
62         $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
63
64         parent::run($result);
65
66         if ($this->collectCodeCoverageInformation) {
67             $coverage = new PHPUnit_Extensions_SeleniumCommon_RemoteCoverage(
68                 $this->coverageScriptUrl,
69                 $this->testId
70             );
71             $result->getCodeCoverage()->append(
72                 $coverage->get(), $this
73             );
74         }
75
76         // do not call this before to give the time to the Listeners to run
77         //$this->getStrategy()->endOfTest($this->session);
78
79         return $result;
80     }
81
82     public function setUp()
83     {
84         $this->args = argParser::getArgs();
85
86         $server = explode(':', $this->args['LOCALSERVER']);
87         if (count($server) > 1) {
88             $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
89         } else {
90             $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
91         }
92
93         $this->client->setDebug($this->args['DEBUG']);
94         $this->client->request_compression = $this->request_compression;
95         $this->client->accepted_compression = $this->accepted_compression;
96
97         $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] );
98
99         if ($this->args['DEBUG'] == 1)
100             ob_start();
101     }
102
103     protected function tearDown()
104     {
105         if ($this->args['DEBUG'] != 1)
106             return;
107         $out = ob_get_clean();
108         $status = $this->getStatus();
109         if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
110             || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
111             echo $out;
112         }
113     }
114
115     /**
116      * @param PhpXmlRpc\Request|array $msg
117      * @param int|array $errorCode
118      * @param bool $returnResponse
119      * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|void
120      */
121     protected function send($msg, $errorCode = 0, $returnResponse = false)
122     {
123         if ($this->collectCodeCoverageInformation) {
124             $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
125         }
126
127         $r = $this->client->send($msg, $this->timeout, $this->method);
128         // for multicall, return directly array of responses
129         if (is_array($r)) {
130             return $r;
131         }
132         if (is_array($errorCode)) {
133             $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
134         } else {
135             $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
136         }
137         if (!$r->faultCode()) {
138             if ($returnResponse) {
139                 return $r;
140             } else {
141                 return $r->value();
142             }
143         } else {
144             return;
145         }
146     }
147
148     public function testString()
149     {
150         $sendString = "here are 3 \"entities\": < > & " .
151             "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
152             " - isn't that great? \\\"hackery\\\" at it's best " .
153             " also don't want to miss out on \$item[0]. " .
154             "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
155             "a simple CR here" . chr(13) .
156             "a simple LF here" . chr(10) .
157             "and then LFCR" . chr(10) . chr(13) .
158             "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
159         $m = new xmlrpcmsg('examples.stringecho', array(
160             new xmlrpcval($sendString, 'string'),
161         ));
162         $v = $this->send($m);
163         if ($v) {
164             // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
165             // so we relax our tests...
166             $l1 = strlen($sendString);
167             $l2 = strlen($v->scalarval());
168             if ($l1 == $l2) {
169                 $this->assertEquals($sendString, $v->scalarval());
170             } else {
171                 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
172             }
173         }
174     }
175
176     public function testLatin1String()
177     {
178         $sendString =
179             "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
180         $x = '<?xml version="1.0" encoding="ISO-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
181             $sendString.
182             '</value></param></params></methodCall>';
183         $v = $this->send($x);
184         if ($v) {
185             $this->assertEquals($sendString, $v->scalarval());
186         }
187     }
188
189     public function testExoticCharsetsRequests()
190     {
191         // note that we should disable this call also when mbstring is missing server-side
192         if (!function_exists('mb_convert_encoding')) {
193             $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
194             return;
195         }
196         $sendString = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
197         $str = '<?xml version="1.0" encoding="_ENC_"?>
198 <methodCall>
199     <methodName>examples.stringecho</methodName>
200     <params>
201         <param>
202         <value><string>'.$sendString.'</string></value>
203         </param>
204     </params>
205 </methodCall>';
206
207         $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UCS-4', $str), 'UCS-4', 'UTF-8'));
208         $this->assertEquals($sendString, $v->scalarval());
209         $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UTF-16', $str), 'UTF-16', 'UTF-8'));
210         $this->assertEquals($sendString, $v->scalarval());
211     }
212
213     /*public function testLatin1Method()
214     {
215         $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
216             new xmlrpcval('hello')
217         ));
218         $v = $this->send($f);
219         if ($v) {
220             $this->assertEquals('hello', $v->scalarval());
221         }
222     }*/
223
224     public function testUtf8Method()
225     {
226         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
227         $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
228             new xmlrpcval('hello')
229         ));
230         $v = $this->send($m);
231         if ($v) {
232             $this->assertEquals('hello', $v->scalarval());
233         }
234         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
235     }
236
237     public function testAddingDoubles()
238     {
239         // note that rounding errors mean we
240         // keep precision to sensible levels here ;-)
241         $a = 12.13;
242         $b = -23.98;
243         $m = new xmlrpcmsg('examples.addtwodouble', array(
244             new xmlrpcval($a, 'double'),
245             new xmlrpcval($b, 'double'),
246         ));
247         $v = $this->send($m);
248         if ($v) {
249             $this->assertEquals($a + $b, $v->scalarval());
250         }
251     }
252
253     public function testAdding()
254     {
255         $m = new xmlrpcmsg('examples.addtwo', array(
256             new xmlrpcval(12, 'int'),
257             new xmlrpcval(-23, 'int'),
258         ));
259         $v = $this->send($m);
260         if ($v) {
261             $this->assertEquals(12 - 23, $v->scalarval());
262         }
263     }
264
265     public function testInvalidNumber()
266     {
267         $m = new xmlrpcmsg('examples.addtwo', array(
268             new xmlrpcval('fred', 'int'),
269             new xmlrpcval("\"; exec('ls')", 'int'),
270         ));
271         $v = $this->send($m);
272         /// @todo a fault condition should be generated here
273         /// by the server, which we pick up on
274         if ($v) {
275             $this->assertEquals(0, $v->scalarval());
276         }
277     }
278
279     public function testBoolean()
280     {
281         $m = new xmlrpcmsg('examples.invertBooleans', array(
282             new xmlrpcval(array(
283                 new xmlrpcval(true, 'boolean'),
284                 new xmlrpcval(false, 'boolean'),
285                 new xmlrpcval(1, 'boolean'),
286                 new xmlrpcval(0, 'boolean')
287             ),
288                 'array'
289             ),));
290         $answer = '0101';
291         $v = $this->send($m);
292         if ($v) {
293             $sz = $v->arraysize();
294             $got = '';
295             for ($i = 0; $i < $sz; $i++) {
296                 $b = $v->arraymem($i);
297                 if ($b->scalarval()) {
298                     $got .= '1';
299                 } else {
300                     $got .= '0';
301                 }
302             }
303             $this->assertEquals($answer, $got);
304         }
305     }
306
307     public function testBase64()
308     {
309         $sendString = 'Mary had a little lamb,
310 Whose fleece was white as snow,
311 And everywhere that Mary went
312 the lamb was sure to go.
313
314 Mary had a little lamb
315 She tied it to a pylon
316 Ten thousand volts went down its back
317 And turned it into nylon';
318         $m = new xmlrpcmsg('examples.decode64', array(
319             new xmlrpcval($sendString, 'base64'),
320         ));
321         $v = $this->send($m);
322         if ($v) {
323             if (strlen($sendString) == strlen($v->scalarval())) {
324                 $this->assertEquals($sendString, $v->scalarval());
325             } else {
326                 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
327             }
328         }
329     }
330
331     public function testDateTime()
332     {
333         $time = time();
334         $t1 = new xmlrpcval($time, 'dateTime.iso8601');
335         $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
336         $this->assertEquals($t1->serialize(), $t2->serialize());
337         if (class_exists('DateTime')) {
338             $datetime = new DateTime();
339             // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
340             if (is_callable(array($datetime, 'setTimestamp'))) {
341                 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
342                 $this->assertEquals($t1->serialize(), $t3->serialize());
343             }
344         }
345     }
346
347     public function testCountEntities()
348     {
349         $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
350         $m = new xmlrpcmsg('validator1.countTheEntities', array(
351             new xmlrpcval($sendString, 'string'),
352         ));
353         $v = $this->send($m);
354         if ($v) {
355             $got = '';
356             $expected = '37210';
357             $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
358             while (list(, $val) = each($expect_array)) {
359                 $b = $v->structmem($val);
360                 $got .= $b->me['int'];
361             }
362             $this->assertEquals($expected, $got);
363         }
364     }
365
366     public function _multicall_msg($method, $params)
367     {
368         $struct['methodName'] = new xmlrpcval($method, 'string');
369         $struct['params'] = new xmlrpcval($params, 'array');
370
371         return new xmlrpcval($struct, 'struct');
372     }
373
374     public function testServerMulticall()
375     {
376         // We manually construct a system.multicall() call to ensure
377         // that the server supports it.
378
379         // NB: This test will NOT pass if server does not support system.multicall.
380
381         // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
382         $good1 = $this->_multicall_msg(
383             'system.methodHelp',
384             array(php_xmlrpc_encode('system.listMethods')));
385         $bad = $this->_multicall_msg(
386             'test.nosuch',
387             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
388         $recursive = $this->_multicall_msg(
389             'system.multicall',
390             array(new xmlrpcval(array(), 'array')));
391         $good2 = $this->_multicall_msg(
392             'system.methodSignature',
393             array(php_xmlrpc_encode('system.listMethods')));
394         $arg = new xmlrpcval(
395             array($good1, $bad, $recursive, $good2),
396             'array'
397         );
398
399         $m = new xmlrpcmsg('system.multicall', array($arg));
400         $v = $this->send($m);
401         if ($v) {
402             //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
403             $this->assertTrue($v->arraysize() == 4, "bad number of return values");
404
405             $r1 = $v->arraymem(0);
406             $this->assertTrue(
407                 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
408                 "did not get array of size 1 from good1"
409             );
410
411             $r2 = $v->arraymem(1);
412             $this->assertTrue(
413                 $r2->kindOf() == 'struct',
414                 "no fault from bad"
415             );
416
417             $r3 = $v->arraymem(2);
418             $this->assertTrue(
419                 $r3->kindOf() == 'struct',
420                 "recursive system.multicall did not fail"
421             );
422
423             $r4 = $v->arraymem(3);
424             $this->assertTrue(
425                 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
426                 "did not get array of size 1 from good2"
427             );
428         }
429     }
430
431     public function testClientMulticall1()
432     {
433         // NB: This test will NOT pass if server does not support system.multicall.
434
435         $this->client->no_multicall = false;
436
437         $good1 = new xmlrpcmsg('system.methodHelp',
438             array(php_xmlrpc_encode('system.listMethods')));
439         $bad = new xmlrpcmsg('test.nosuch',
440             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
441         $recursive = new xmlrpcmsg('system.multicall',
442             array(new xmlrpcval(array(), 'array')));
443         $good2 = new xmlrpcmsg('system.methodSignature',
444             array(php_xmlrpc_encode('system.listMethods'))
445         );
446
447         $r = $this->send(array($good1, $bad, $recursive, $good2));
448         if ($r) {
449             $this->assertTrue(count($r) == 4, "wrong number of return values");
450         }
451
452         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
453         if (!$r[0]->faultCode()) {
454             $val = $r[0]->value();
455             $this->assertTrue(
456                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
457                 "good1 did not return string"
458             );
459         }
460         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
461         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
462         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
463         if (!$r[3]->faultCode()) {
464             $val = $r[3]->value();
465             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
466         }
467         // This is the only assert in this test which should fail
468         // if the test server does not support system.multicall.
469         $this->assertTrue($this->client->no_multicall == false,
470             "server does not support system.multicall"
471         );
472     }
473
474     public function testClientMulticall2()
475     {
476         // NB: This test will NOT pass if server does not support system.multicall.
477
478         $this->client->no_multicall = true;
479
480         $good1 = new xmlrpcmsg('system.methodHelp',
481             array(php_xmlrpc_encode('system.listMethods')));
482         $bad = new xmlrpcmsg('test.nosuch',
483             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
484         $recursive = new xmlrpcmsg('system.multicall',
485             array(new xmlrpcval(array(), 'array')));
486         $good2 = new xmlrpcmsg('system.methodSignature',
487             array(php_xmlrpc_encode('system.listMethods'))
488         );
489
490         $r = $this->send(array($good1, $bad, $recursive, $good2));
491         if ($r) {
492             $this->assertTrue(count($r) == 4, "wrong number of return values");
493         }
494
495         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
496         if (!$r[0]->faultCode()) {
497             $val = $r[0]->value();
498             $this->assertTrue(
499                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
500                 "good1 did not return string");
501         }
502         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
503         $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
504         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
505         if (!$r[3]->faultCode()) {
506             $val = $r[3]->value();
507             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
508         }
509     }
510
511     public function testClientMulticall3()
512     {
513         // NB: This test will NOT pass if server does not support system.multicall.
514
515         $this->client->return_type = 'phpvals';
516         $this->client->no_multicall = false;
517
518         $good1 = new xmlrpcmsg('system.methodHelp',
519             array(php_xmlrpc_encode('system.listMethods')));
520         $bad = new xmlrpcmsg('test.nosuch',
521             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
522         $recursive = new xmlrpcmsg('system.multicall',
523             array(new xmlrpcval(array(), 'array')));
524         $good2 = new xmlrpcmsg('system.methodSignature',
525             array(php_xmlrpc_encode('system.listMethods'))
526         );
527
528         $r = $this->send(array($good1, $bad, $recursive, $good2));
529         if ($r) {
530             $this->assertTrue(count($r) == 4, "wrong number of return values");
531         }
532         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
533         if (!$r[0]->faultCode()) {
534             $val = $r[0]->value();
535             $this->assertTrue(
536                 is_string($val), "good1 did not return string");
537         }
538         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
539         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
540         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
541         if (!$r[3]->faultCode()) {
542             $val = $r[3]->value();
543             $this->assertTrue(is_array($val), "good2 did not return array");
544         }
545         $this->client->return_type = 'xmlrpcvals';
546     }
547
548     public function testCatchWarnings()
549     {
550         $m = new xmlrpcmsg('tests.generatePHPWarning', array(
551             new xmlrpcval('whatever', 'string'),
552         ));
553         $v = $this->send($m);
554         if ($v) {
555             $this->assertEquals(true, $v->scalarval());
556         }
557     }
558
559     public function testCatchExceptions()
560     {
561         $m = new xmlrpcmsg('tests.raiseException', array(
562             new xmlrpcval('whatever', 'string'),
563         ));
564         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
565         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
566         $v = $this->send($m, 1); // the error code of the expected exception
567         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
568         // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
569         // as php will generate an http status code of either 200 or 500...
570         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
571     }
572
573     public function testZeroParams()
574     {
575         $m = new xmlrpcmsg('system.listMethods');
576         $v = $this->send($m);
577     }
578
579     public function testNullParams()
580     {
581         $m = new xmlrpcmsg('tests.getStateName.12', array(
582             new xmlrpcval('whatever', 'null'),
583             new xmlrpcval(23, 'int'),
584         ));
585         $v = $this->send($m);
586         if ($v) {
587             $this->assertEquals('Michigan', $v->scalarval());
588         }
589         $m = new xmlrpcmsg('tests.getStateName.12', array(
590             new xmlrpcval(23, 'int'),
591             new xmlrpcval('whatever', 'null'),
592         ));
593         $v = $this->send($m);
594         if ($v) {
595             $this->assertEquals('Michigan', $v->scalarval());
596         }
597         $m = new xmlrpcmsg('tests.getStateName.12', array(
598             new xmlrpcval(23, 'int')
599         ));
600         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
601     }
602
603     public function testCodeInjectionServerSide()
604     {
605         $m = new xmlrpcmsg('system.MethodHelp');
606         $m->payload = "<?xml version=\"1.0\"?><methodCall><methodName>validator1.echoStructTest</methodName><params><param><value><struct><member><name>','')); echo('gotcha!'); die(); //</name></member></struct></value></param></params></methodCall>";
607         $v = $this->send($m);
608         if ($v) {
609             $this->assertEquals(0, $v->structsize());
610         }
611     }
612
613     public function testServerWrappedFunction()
614     {
615         $m = new xmlrpcmsg('tests.getStateName.2', array(
616             new xmlrpcval(23, 'int'),
617         ));
618         $v = $this->send($m);
619         $this->assertEquals('Michigan', $v->scalarval());
620
621         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
622         $m = new xmlrpcmsg('tests.getStateName.2', array(
623             new xmlrpcval(0, 'int'),
624         ));
625         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
626
627         // check if the generated function dispatch map is fine, by checking if the server registered it
628         $m = new xmlrpcmsg('system.methodSignature', array(
629             new xmlrpcval('tests.getStateName.2'),
630         ));
631         $v = $this->send($m);
632         $encoder = new \PhpXmlRpc\Encoder();
633         $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
634     }
635
636     public function testServerWrappedFunctionAsSource()
637     {
638         $m = new xmlrpcmsg('tests.getStateName.6', array(
639             new xmlrpcval(23, 'int'),
640         ));
641         $v = $this->send($m);
642         $this->assertEquals('Michigan', $v->scalarval());
643
644         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
645         $m = new xmlrpcmsg('tests.getStateName.6', array(
646             new xmlrpcval(0, 'int'),
647         ));
648         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
649     }
650
651     public function testServerWrappedObjectMethods()
652     {
653         $m = new xmlrpcmsg('tests.getStateName.3', array(
654             new xmlrpcval(23, 'int'),
655         ));
656         $v = $this->send($m);
657         $this->assertEquals('Michigan', $v->scalarval());
658
659         $m = new xmlrpcmsg('tests.getStateName.4', array(
660             new xmlrpcval(23, 'int'),
661         ));
662         $v = $this->send($m);
663         $this->assertEquals('Michigan', $v->scalarval());
664
665         $m = new xmlrpcmsg('tests.getStateName.5', array(
666             new xmlrpcval(23, 'int'),
667         ));
668         $v = $this->send($m);
669         $this->assertEquals('Michigan', $v->scalarval());
670
671         $m = new xmlrpcmsg('tests.getStateName.7', array(
672             new xmlrpcval(23, 'int'),
673         ));
674         $v = $this->send($m);
675         $this->assertEquals('Michigan', $v->scalarval());
676
677         $m = new xmlrpcmsg('tests.getStateName.8', array(
678             new xmlrpcval(23, 'int'),
679         ));
680         $v = $this->send($m);
681         $this->assertEquals('Michigan', $v->scalarval());
682
683         $m = new xmlrpcmsg('tests.getStateName.9', array(
684             new xmlrpcval(23, 'int'),
685         ));
686         $v = $this->send($m);
687         $this->assertEquals('Michigan', $v->scalarval());
688     }
689
690     public function testServerWrappedObjectMethodsAsSource()
691     {
692         $m = new xmlrpcmsg('tests.getStateName.7', array(
693             new xmlrpcval(23, 'int'),
694         ));
695         $v = $this->send($m);
696         $this->assertEquals('Michigan', $v->scalarval());
697
698         $m = new xmlrpcmsg('tests.getStateName.8', array(
699             new xmlrpcval(23, 'int'),
700         ));
701         $v = $this->send($m);
702         $this->assertEquals('Michigan', $v->scalarval());
703
704         $m = new xmlrpcmsg('tests.getStateName.9', array(
705             new xmlrpcval(23, 'int'),
706         ));
707         $v = $this->send($m);
708         $this->assertEquals('Michigan', $v->scalarval());
709     }
710
711     public function testServerClosure()
712     {
713         $m = new xmlrpcmsg('tests.getStateName.10', array(
714             new xmlrpcval(23, 'int'),
715         ));
716         $v = $this->send($m);
717         $this->assertEquals('Michigan', $v->scalarval());
718     }
719
720     public function testServerWrappedClosure()
721     {
722         $m = new xmlrpcmsg('tests.getStateName.11', array(
723             new xmlrpcval(23, 'int'),
724         ));
725         $v = $this->send($m);
726         $this->assertEquals('Michigan', $v->scalarval());
727     }
728
729     public function testServerWrappedClass()
730     {
731         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
732             new xmlrpcval(23, 'int'),
733         ));
734         $v = $this->send($m);
735         $this->assertEquals('Michigan', $v->scalarval());
736     }
737
738     public function testWrappedMethod()
739     {
740         // make a 'deep client copy' as the original one might have many properties set
741         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
742         if ($func == false) {
743             $this->fail('Registration of examples.getStateName failed');
744         } else {
745             $v = $func(23);
746             // work around bug in current (or old?) version of phpunit when reporting the error
747             /*if (is_object($v)) {
748                 $v = var_export($v, true);
749             }*/
750             $this->assertEquals('Michigan', $v);
751         }
752     }
753
754     public function testWrappedMethodAsSource()
755     {
756         // make a 'deep client copy' as the original one might have many properties set
757         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
758         if ($func == false) {
759             $this->fail('Registration of examples.getStateName failed');
760         } else {
761             eval($func['source']);
762             $func = $func['function'];
763             $v = $func(23);
764             // work around bug in current (or old?) version of phpunit when reporting the error
765             /*if (is_object($v)) {
766                 $v = var_export($v, true);
767             }*/
768             $this->assertEquals('Michigan', $v);
769         }
770     }
771
772     public function testWrappedClass()
773     {
774         // make a 'deep client copy' as the original one might have many properties set
775         // also for speed only wrap one method of the whole server
776         $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
777         if ($class == '') {
778             $this->fail('Registration of remote server failed');
779         } else {
780             $obj = new $class();
781             $v = $obj->examples_getStateName(23);
782             // work around bug in current (or old?) version of phpunit when reporting the error
783             /*if (is_object($v)) {
784                 $v = var_export($v, true);
785             }*/
786             $this->assertEquals('Michigan', $v);
787         }
788     }
789
790     public function testTransferOfObjectViaWrapping()
791     {
792         // make a 'deep client copy' as the original one might have many properties set
793         $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => true,
794             'decode_php_objs' => true));
795         if ($func == false) {
796             $this->fail('Registration of tests.returnPhpObject failed');
797         } else {
798             $v = $func();
799             $obj = new stdClass();
800             $obj->hello = 'world';
801             $this->assertEquals($obj, $v);
802         }
803     }
804
805     public function testGetCookies()
806     {
807         // let server set to us some cookies we tell it
808         $cookies = array(
809             //'c1' => array(),
810             'c2' => array('value' => 'c2'),
811             'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
812             'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
813             'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
814         );
815         $cookiesval = php_xmlrpc_encode($cookies);
816         $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
817         $r = $this->send($m, 0, true);
818         if ($r) {
819             $v = $r->value();
820             $this->assertEquals(1, $v->scalarval());
821             // now check if we decoded the cookies as we had set them
822             $rcookies = $r->cookies();
823             // remove extra cookies which might have been set by proxies
824             foreach ($rcookies as $c => $v) {
825                 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
826                     unset($rcookies[$c]);
827                 }
828                 // Seems like we get this when using php-fpm and php 5.5+ ...
829                 if (isset($rcookies[$c]['Max-Age'])) {
830                     unset($rcookies[$c]['Max-Age']);
831                 }
832             }
833             foreach ($cookies as $c => $v) {
834                 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
835                 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
836                 if (isset($v['expires'])) {
837                     if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
838                         $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
839                     } else {
840                         $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
841                     }
842                 }
843             }
844
845             $this->assertEquals($cookies, $rcookies);
846         }
847     }
848
849     public function testSetCookies()
850     {
851         // let server set to us some cookies we tell it
852         $cookies = array(
853             'c0' => null,
854             'c1' => 1,
855             'c2' => '2 3',
856             'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
857         );
858         $m = new xmlrpcmsg('examples.getcookies', array());
859         foreach ($cookies as $cookie => $val) {
860             $this->client->setCookie($cookie, $val);
861             $cookies[$cookie] = (string)$cookies[$cookie];
862         }
863         $r = $this->client->send($m, $this->timeout, $this->method);
864         $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
865         if (!$r->faultCode()) {
866             $v = $r->value();
867             $v = php_xmlrpc_decode($v);
868
869             // take care for the extra cookie used for coverage collection
870             if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
871                 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
872             }
873
874             // on IIS and Apache getallheaders returns something slightly different...
875             $this->assertEquals($cookies, $v);
876         }
877     }
878
879     public function testServerComments()
880     {
881         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
882             new xmlrpcval('hello world', 'string'),
883         ));
884         $r = $this->send($m, 0, true);
885         $this->assertContains('hello world', $r->raw_data);
886     }
887
888     public function testSendTwiceSameMsg()
889     {
890         $m = new xmlrpcmsg('examples.stringecho', array(
891             new xmlrpcval('hello world', 'string'),
892         ));
893         $v1 = $this->send($m);
894         $v2 = $this->send($m);
895         if ($v1 && $v2) {
896             $this->assertEquals($v1, $v2);
897         }
898     }
899 }