186dd59c0a32ffe16fb1095466567e13cad2520f
[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 testLatin1Method()
190     {
191         $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
192             new xmlrpcval('hello')
193         ));
194         $v = $this->send($f);
195         if ($v) {
196             $this->assertEquals('hello', $v->scalarval());
197         }
198     }*/
199
200     public function testUtf8Method()
201     {
202         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
203         $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
204             new xmlrpcval('hello')
205         ));
206         $v = $this->send($m);
207         if ($v) {
208             $this->assertEquals('hello', $v->scalarval());
209         }
210         PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
211     }
212
213     public function testAddingDoubles()
214     {
215         // note that rounding errors mean we
216         // keep precision to sensible levels here ;-)
217         $a = 12.13;
218         $b = -23.98;
219         $m = new xmlrpcmsg('examples.addtwodouble', array(
220             new xmlrpcval($a, 'double'),
221             new xmlrpcval($b, 'double'),
222         ));
223         $v = $this->send($m);
224         if ($v) {
225             $this->assertEquals($a + $b, $v->scalarval());
226         }
227     }
228
229     public function testAdding()
230     {
231         $m = new xmlrpcmsg('examples.addtwo', array(
232             new xmlrpcval(12, 'int'),
233             new xmlrpcval(-23, 'int'),
234         ));
235         $v = $this->send($m);
236         if ($v) {
237             $this->assertEquals(12 - 23, $v->scalarval());
238         }
239     }
240
241     public function testInvalidNumber()
242     {
243         $m = new xmlrpcmsg('examples.addtwo', array(
244             new xmlrpcval('fred', 'int'),
245             new xmlrpcval("\"; exec('ls')", 'int'),
246         ));
247         $v = $this->send($m);
248         /// @todo a fault condition should be generated here
249         /// by the server, which we pick up on
250         if ($v) {
251             $this->assertEquals(0, $v->scalarval());
252         }
253     }
254
255     public function testBoolean()
256     {
257         $m = new xmlrpcmsg('examples.invertBooleans', array(
258             new xmlrpcval(array(
259                 new xmlrpcval(true, 'boolean'),
260                 new xmlrpcval(false, 'boolean'),
261                 new xmlrpcval(1, 'boolean'),
262                 new xmlrpcval(0, 'boolean')
263             ),
264                 'array'
265             ),));
266         $answer = '0101';
267         $v = $this->send($m);
268         if ($v) {
269             $sz = $v->arraysize();
270             $got = '';
271             for ($i = 0; $i < $sz; $i++) {
272                 $b = $v->arraymem($i);
273                 if ($b->scalarval()) {
274                     $got .= '1';
275                 } else {
276                     $got .= '0';
277                 }
278             }
279             $this->assertEquals($answer, $got);
280         }
281     }
282
283     public function testBase64()
284     {
285         $sendString = 'Mary had a little lamb,
286 Whose fleece was white as snow,
287 And everywhere that Mary went
288 the lamb was sure to go.
289
290 Mary had a little lamb
291 She tied it to a pylon
292 Ten thousand volts went down its back
293 And turned it into nylon';
294         $m = new xmlrpcmsg('examples.decode64', array(
295             new xmlrpcval($sendString, 'base64'),
296         ));
297         $v = $this->send($m);
298         if ($v) {
299             if (strlen($sendString) == strlen($v->scalarval())) {
300                 $this->assertEquals($sendString, $v->scalarval());
301             } else {
302                 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
303             }
304         }
305     }
306
307     public function testDateTime()
308     {
309         $time = time();
310         $t1 = new xmlrpcval($time, 'dateTime.iso8601');
311         $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
312         $this->assertEquals($t1->serialize(), $t2->serialize());
313         if (class_exists('DateTime')) {
314             $datetime = new DateTime();
315             // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
316             if (is_callable(array($datetime, 'setTimestamp'))) {
317                 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
318                 $this->assertEquals($t1->serialize(), $t3->serialize());
319             }
320         }
321     }
322
323     public function testCountEntities()
324     {
325         $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
326         $m = new xmlrpcmsg('validator1.countTheEntities', array(
327             new xmlrpcval($sendString, 'string'),
328         ));
329         $v = $this->send($m);
330         if ($v) {
331             $got = '';
332             $expected = '37210';
333             $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
334             while (list(, $val) = each($expect_array)) {
335                 $b = $v->structmem($val);
336                 $got .= $b->me['int'];
337             }
338             $this->assertEquals($expected, $got);
339         }
340     }
341
342     public function _multicall_msg($method, $params)
343     {
344         $struct['methodName'] = new xmlrpcval($method, 'string');
345         $struct['params'] = new xmlrpcval($params, 'array');
346
347         return new xmlrpcval($struct, 'struct');
348     }
349
350     public function testServerMulticall()
351     {
352         // We manually construct a system.multicall() call to ensure
353         // that the server supports it.
354
355         // NB: This test will NOT pass if server does not support system.multicall.
356
357         // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
358         $good1 = $this->_multicall_msg(
359             'system.methodHelp',
360             array(php_xmlrpc_encode('system.listMethods')));
361         $bad = $this->_multicall_msg(
362             'test.nosuch',
363             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
364         $recursive = $this->_multicall_msg(
365             'system.multicall',
366             array(new xmlrpcval(array(), 'array')));
367         $good2 = $this->_multicall_msg(
368             'system.methodSignature',
369             array(php_xmlrpc_encode('system.listMethods')));
370         $arg = new xmlrpcval(
371             array($good1, $bad, $recursive, $good2),
372             'array'
373         );
374
375         $m = new xmlrpcmsg('system.multicall', array($arg));
376         $v = $this->send($m);
377         if ($v) {
378             //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
379             $this->assertTrue($v->arraysize() == 4, "bad number of return values");
380
381             $r1 = $v->arraymem(0);
382             $this->assertTrue(
383                 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
384                 "did not get array of size 1 from good1"
385             );
386
387             $r2 = $v->arraymem(1);
388             $this->assertTrue(
389                 $r2->kindOf() == 'struct',
390                 "no fault from bad"
391             );
392
393             $r3 = $v->arraymem(2);
394             $this->assertTrue(
395                 $r3->kindOf() == 'struct',
396                 "recursive system.multicall did not fail"
397             );
398
399             $r4 = $v->arraymem(3);
400             $this->assertTrue(
401                 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
402                 "did not get array of size 1 from good2"
403             );
404         }
405     }
406
407     public function testClientMulticall1()
408     {
409         // NB: This test will NOT pass if server does not support system.multicall.
410
411         $this->client->no_multicall = false;
412
413         $good1 = new xmlrpcmsg('system.methodHelp',
414             array(php_xmlrpc_encode('system.listMethods')));
415         $bad = new xmlrpcmsg('test.nosuch',
416             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
417         $recursive = new xmlrpcmsg('system.multicall',
418             array(new xmlrpcval(array(), 'array')));
419         $good2 = new xmlrpcmsg('system.methodSignature',
420             array(php_xmlrpc_encode('system.listMethods'))
421         );
422
423         $r = $this->send(array($good1, $bad, $recursive, $good2));
424         if ($r) {
425             $this->assertTrue(count($r) == 4, "wrong number of return values");
426         }
427
428         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
429         if (!$r[0]->faultCode()) {
430             $val = $r[0]->value();
431             $this->assertTrue(
432                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
433                 "good1 did not return string"
434             );
435         }
436         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
437         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
438         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
439         if (!$r[3]->faultCode()) {
440             $val = $r[3]->value();
441             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
442         }
443         // This is the only assert in this test which should fail
444         // if the test server does not support system.multicall.
445         $this->assertTrue($this->client->no_multicall == false,
446             "server does not support system.multicall"
447         );
448     }
449
450     public function testClientMulticall2()
451     {
452         // NB: This test will NOT pass if server does not support system.multicall.
453
454         $this->client->no_multicall = true;
455
456         $good1 = new xmlrpcmsg('system.methodHelp',
457             array(php_xmlrpc_encode('system.listMethods')));
458         $bad = new xmlrpcmsg('test.nosuch',
459             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
460         $recursive = new xmlrpcmsg('system.multicall',
461             array(new xmlrpcval(array(), 'array')));
462         $good2 = new xmlrpcmsg('system.methodSignature',
463             array(php_xmlrpc_encode('system.listMethods'))
464         );
465
466         $r = $this->send(array($good1, $bad, $recursive, $good2));
467         if ($r) {
468             $this->assertTrue(count($r) == 4, "wrong number of return values");
469         }
470
471         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
472         if (!$r[0]->faultCode()) {
473             $val = $r[0]->value();
474             $this->assertTrue(
475                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
476                 "good1 did not return string");
477         }
478         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
479         $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
480         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
481         if (!$r[3]->faultCode()) {
482             $val = $r[3]->value();
483             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
484         }
485     }
486
487     public function testClientMulticall3()
488     {
489         // NB: This test will NOT pass if server does not support system.multicall.
490
491         $this->client->return_type = 'phpvals';
492         $this->client->no_multicall = false;
493
494         $good1 = new xmlrpcmsg('system.methodHelp',
495             array(php_xmlrpc_encode('system.listMethods')));
496         $bad = new xmlrpcmsg('test.nosuch',
497             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
498         $recursive = new xmlrpcmsg('system.multicall',
499             array(new xmlrpcval(array(), 'array')));
500         $good2 = new xmlrpcmsg('system.methodSignature',
501             array(php_xmlrpc_encode('system.listMethods'))
502         );
503
504         $r = $this->send(array($good1, $bad, $recursive, $good2));
505         if ($r) {
506             $this->assertTrue(count($r) == 4, "wrong number of return values");
507         }
508         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
509         if (!$r[0]->faultCode()) {
510             $val = $r[0]->value();
511             $this->assertTrue(
512                 is_string($val), "good1 did not return string");
513         }
514         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
515         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
516         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
517         if (!$r[3]->faultCode()) {
518             $val = $r[3]->value();
519             $this->assertTrue(is_array($val), "good2 did not return array");
520         }
521         $this->client->return_type = 'xmlrpcvals';
522     }
523
524     public function testCatchWarnings()
525     {
526         $m = new xmlrpcmsg('tests.generatePHPWarning', array(
527             new xmlrpcval('whatever', 'string'),
528         ));
529         $v = $this->send($m);
530         if ($v) {
531             $this->assertEquals(true, $v->scalarval());
532         }
533     }
534
535     public function testCatchExceptions()
536     {
537         $m = new xmlrpcmsg('tests.raiseException', array(
538             new xmlrpcval('whatever', 'string'),
539         ));
540         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
541         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
542         $v = $this->send($m, 1); // the error code of the expected exception
543         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
544         // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
545         // as php will generate an http status code of either 200 or 500...
546         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
547     }
548
549     public function testZeroParams()
550     {
551         $m = new xmlrpcmsg('system.listMethods');
552         $v = $this->send($m);
553     }
554
555     public function testNullParams()
556     {
557         $m = new xmlrpcmsg('tests.getStateName.12', array(
558             new xmlrpcval('whatever', 'null'),
559             new xmlrpcval(23, 'int'),
560         ));
561         $v = $this->send($m);
562         if ($v) {
563             $this->assertEquals('Michigan', $v->scalarval());
564         }
565         $m = new xmlrpcmsg('tests.getStateName.12', array(
566             new xmlrpcval(23, 'int'),
567             new xmlrpcval('whatever', 'null'),
568         ));
569         $v = $this->send($m);
570         if ($v) {
571             $this->assertEquals('Michigan', $v->scalarval());
572         }
573         $m = new xmlrpcmsg('tests.getStateName.12', array(
574             new xmlrpcval(23, 'int')
575         ));
576         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
577     }
578
579     public function testCodeInjectionServerSide()
580     {
581         $m = new xmlrpcmsg('system.MethodHelp');
582         $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>";
583         $v = $this->send($m);
584         if ($v) {
585             $this->assertEquals(0, $v->structsize());
586         }
587     }
588
589     public function testServerWrappedFunction()
590     {
591         $m = new xmlrpcmsg('tests.getStateName.2', array(
592             new xmlrpcval(23, 'int'),
593         ));
594         $v = $this->send($m);
595         $this->assertEquals('Michigan', $v->scalarval());
596
597         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
598         $m = new xmlrpcmsg('tests.getStateName.2', array(
599             new xmlrpcval(0, 'int'),
600         ));
601         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
602
603         // check if the generated function dispatch map is fine, by checking if the server registered it
604         $m = new xmlrpcmsg('system.methodSignature', array(
605             new xmlrpcval('tests.getStateName.2'),
606         ));
607         $v = $this->send($m);
608         $encoder = new \PhpXmlRpc\Encoder();
609         $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
610     }
611
612     public function testServerWrappedFunctionAsSource()
613     {
614         $m = new xmlrpcmsg('tests.getStateName.6', array(
615             new xmlrpcval(23, 'int'),
616         ));
617         $v = $this->send($m);
618         $this->assertEquals('Michigan', $v->scalarval());
619
620         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
621         $m = new xmlrpcmsg('tests.getStateName.6', array(
622             new xmlrpcval(0, 'int'),
623         ));
624         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
625     }
626
627     public function testServerWrappedObjectMethods()
628     {
629         $m = new xmlrpcmsg('tests.getStateName.3', array(
630             new xmlrpcval(23, 'int'),
631         ));
632         $v = $this->send($m);
633         $this->assertEquals('Michigan', $v->scalarval());
634
635         $m = new xmlrpcmsg('tests.getStateName.4', array(
636             new xmlrpcval(23, 'int'),
637         ));
638         $v = $this->send($m);
639         $this->assertEquals('Michigan', $v->scalarval());
640
641         $m = new xmlrpcmsg('tests.getStateName.5', array(
642             new xmlrpcval(23, 'int'),
643         ));
644         $v = $this->send($m);
645         $this->assertEquals('Michigan', $v->scalarval());
646
647         $m = new xmlrpcmsg('tests.getStateName.7', array(
648             new xmlrpcval(23, 'int'),
649         ));
650         $v = $this->send($m);
651         $this->assertEquals('Michigan', $v->scalarval());
652
653         $m = new xmlrpcmsg('tests.getStateName.8', 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.9', array(
660             new xmlrpcval(23, 'int'),
661         ));
662         $v = $this->send($m);
663         $this->assertEquals('Michigan', $v->scalarval());
664     }
665
666     public function testServerWrappedObjectMethodsAsSource()
667     {
668         $m = new xmlrpcmsg('tests.getStateName.7', array(
669             new xmlrpcval(23, 'int'),
670         ));
671         $v = $this->send($m);
672         $this->assertEquals('Michigan', $v->scalarval());
673
674         $m = new xmlrpcmsg('tests.getStateName.8', array(
675             new xmlrpcval(23, 'int'),
676         ));
677         $v = $this->send($m);
678         $this->assertEquals('Michigan', $v->scalarval());
679
680         $m = new xmlrpcmsg('tests.getStateName.9', array(
681             new xmlrpcval(23, 'int'),
682         ));
683         $v = $this->send($m);
684         $this->assertEquals('Michigan', $v->scalarval());
685     }
686
687     public function testServerClosure()
688     {
689         $m = new xmlrpcmsg('tests.getStateName.10', array(
690             new xmlrpcval(23, 'int'),
691         ));
692         $v = $this->send($m);
693         $this->assertEquals('Michigan', $v->scalarval());
694     }
695
696     public function testServerWrappedClosure()
697     {
698         $m = new xmlrpcmsg('tests.getStateName.11', array(
699             new xmlrpcval(23, 'int'),
700         ));
701         $v = $this->send($m);
702         $this->assertEquals('Michigan', $v->scalarval());
703     }
704
705     public function testServerWrappedClass()
706     {
707         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
708             new xmlrpcval(23, 'int'),
709         ));
710         $v = $this->send($m);
711         $this->assertEquals('Michigan', $v->scalarval());
712     }
713
714     public function testWrappedMethod()
715     {
716         // make a 'deep client copy' as the original one might have many properties set
717         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
718         if ($func == false) {
719             $this->fail('Registration of examples.getStateName failed');
720         } else {
721             $v = $func(23);
722             // work around bug in current (or old?) version of phpunit when reporting the error
723             /*if (is_object($v)) {
724                 $v = var_export($v, true);
725             }*/
726             $this->assertEquals('Michigan', $v);
727         }
728     }
729
730     public function testWrappedMethodAsSource()
731     {
732         // make a 'deep client copy' as the original one might have many properties set
733         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
734         if ($func == false) {
735             $this->fail('Registration of examples.getStateName failed');
736         } else {
737             eval($func['source']);
738             $func = $func['function'];
739             $v = $func(23);
740             // work around bug in current (or old?) version of phpunit when reporting the error
741             /*if (is_object($v)) {
742                 $v = var_export($v, true);
743             }*/
744             $this->assertEquals('Michigan', $v);
745         }
746     }
747
748     public function testWrappedClass()
749     {
750         // make a 'deep client copy' as the original one might have many properties set
751         // also for speed only wrap one method of the whole server
752         $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
753         if ($class == '') {
754             $this->fail('Registration of remote server failed');
755         } else {
756             $obj = new $class();
757             $v = $obj->examples_getStateName(23);
758             // work around bug in current (or old?) version of phpunit when reporting the error
759             /*if (is_object($v)) {
760                 $v = var_export($v, true);
761             }*/
762             $this->assertEquals('Michigan', $v);
763         }
764     }
765
766     public function testTransferOfObjectViaWrapping()
767     {
768         // make a 'deep client copy' as the original one might have many properties set
769         $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => true,
770             'decode_php_objs' => true));
771         if ($func == false) {
772             $this->fail('Registration of tests.returnPhpObject failed');
773         } else {
774             $v = $func();
775             $obj = new stdClass();
776             $obj->hello = 'world';
777             $this->assertEquals($obj, $v);
778         }
779     }
780
781     public function testGetCookies()
782     {
783         // let server set to us some cookies we tell it
784         $cookies = array(
785             //'c1' => array(),
786             'c2' => array('value' => 'c2'),
787             'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
788             'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
789             'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
790         );
791         $cookiesval = php_xmlrpc_encode($cookies);
792         $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
793         $r = $this->send($m, 0, true);
794         if ($r) {
795             $v = $r->value();
796             $this->assertEquals(1, $v->scalarval());
797             // now check if we decoded the cookies as we had set them
798             $rcookies = $r->cookies();
799             // remove extra cookies which might have been set by proxies
800             foreach ($rcookies as $c => $v) {
801                 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
802                     unset($rcookies[$c]);
803                 }
804                 // Seems like we get this when using php-fpm and php 5.5+ ...
805                 if (isset($rcookies[$c]['Max-Age'])) {
806                     unset($rcookies[$c]['Max-Age']);
807                 }
808             }
809             foreach ($cookies as $c => $v) {
810                 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
811                 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
812                 if (isset($v['expires'])) {
813                     if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
814                         $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
815                     } else {
816                         $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
817                     }
818                 }
819             }
820
821             $this->assertEquals($cookies, $rcookies);
822         }
823     }
824
825     public function testSetCookies()
826     {
827         // let server set to us some cookies we tell it
828         $cookies = array(
829             'c0' => null,
830             'c1' => 1,
831             'c2' => '2 3',
832             'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
833         );
834         $m = new xmlrpcmsg('examples.getcookies', array());
835         foreach ($cookies as $cookie => $val) {
836             $this->client->setCookie($cookie, $val);
837             $cookies[$cookie] = (string)$cookies[$cookie];
838         }
839         $r = $this->client->send($m, $this->timeout, $this->method);
840         $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
841         if (!$r->faultCode()) {
842             $v = $r->value();
843             $v = php_xmlrpc_decode($v);
844
845             // take care for the extra cookie used for coverage collection
846             if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
847                 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
848             }
849
850             // on IIS and Apache getallheaders returns something slightly different...
851             $this->assertEquals($cookies, $v);
852         }
853     }
854
855     public function testServerComments()
856     {
857         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
858             new xmlrpcval('hello world', 'string'),
859         ));
860         $r = $this->send($m, 0, true);
861         $this->assertContains('hello world', $r->raw_data);
862     }
863
864     public function testSendTwiceSameMsg()
865     {
866         $m = new xmlrpcmsg('examples.stringecho', array(
867             new xmlrpcval('hello world', 'string'),
868         ));
869         $v1 = $this->send($m);
870         $v2 = $this->send($m);
871         if ($v1 && $v2) {
872             $this->assertEquals($v1, $v2);
873         }
874     }
875 }