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