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