One more attempt at fixing unit tests
[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         $noMultiCall = $this->client->no_multicall;
504         $this->client->no_multicall = false;
505
506         $good1 = new xmlrpcmsg('system.methodHelp',
507             array(php_xmlrpc_encode('system.listMethods')));
508         $bad = new xmlrpcmsg('test.nosuch',
509             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
510         $recursive = new xmlrpcmsg('system.multicall',
511             array(new xmlrpcval(array(), 'array')));
512         $good2 = new xmlrpcmsg('system.methodSignature',
513             array(php_xmlrpc_encode('system.listMethods'))
514         );
515
516         $r = $this->send(array($good1, $bad, $recursive, $good2));
517         if ($r) {
518             $this->assertTrue(count($r) == 4, "wrong number of return values");
519         }
520
521         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
522         if (!$r[0]->faultCode()) {
523             $val = $r[0]->value();
524             $this->assertTrue(
525                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
526                 "good1 did not return string"
527             );
528         }
529         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
530         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
531         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
532         if (!$r[3]->faultCode()) {
533             $val = $r[3]->value();
534             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
535         }
536         // This is the only assert in this test which should fail
537         // if the test server does not support system.multicall.
538         $this->assertTrue($this->client->no_multicall == false,
539             "server does not support system.multicall"
540         );
541
542         $this->client->no_multicall = $noMultiCall;
543     }
544
545     public function testClientMulticall2()
546     {
547         // NB: This test will NOT pass if server does not support system.multicall.
548
549         $noMultiCall = $this->client->no_multicall;
550         $this->client->no_multicall = true;
551
552         $good1 = new xmlrpcmsg('system.methodHelp',
553             array(php_xmlrpc_encode('system.listMethods')));
554         $bad = new xmlrpcmsg('test.nosuch',
555             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
556         $recursive = new xmlrpcmsg('system.multicall',
557             array(new xmlrpcval(array(), 'array')));
558         $good2 = new xmlrpcmsg('system.methodSignature',
559             array(php_xmlrpc_encode('system.listMethods'))
560         );
561
562         $r = $this->send(array($good1, $bad, $recursive, $good2));
563         if ($r) {
564             $this->assertTrue(count($r) == 4, "wrong number of return values");
565         }
566
567         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
568         if (!$r[0]->faultCode()) {
569             $val = $r[0]->value();
570             $this->assertTrue(
571                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
572                 "good1 did not return string");
573         }
574         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
575         $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
576         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
577         if (!$r[3]->faultCode()) {
578             $val = $r[3]->value();
579             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
580         }
581
582         $this->client->no_multicall = $noMultiCall;
583     }
584
585     public function testClientMulticall3()
586     {
587         // NB: This test will NOT pass if server does not support system.multicall.
588
589         $noMultiCall = $this->client->no_multicall;
590         $returnType = $this->client->return_type;
591
592         $this->client->return_type = 'phpvals';
593         $this->client->no_multicall = false;
594
595         $good1 = new xmlrpcmsg('system.methodHelp',
596             array(php_xmlrpc_encode('system.listMethods')));
597         $bad = new xmlrpcmsg('test.nosuch',
598             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
599         $recursive = new xmlrpcmsg('system.multicall',
600             array(new xmlrpcval(array(), 'array')));
601         $good2 = new xmlrpcmsg('system.methodSignature',
602             array(php_xmlrpc_encode('system.listMethods'))
603         );
604
605         $r = $this->send(array($good1, $bad, $recursive, $good2));
606         if ($r) {
607             $this->assertTrue(count($r) == 4, "wrong number of return values");
608         }
609         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
610         if (!$r[0]->faultCode()) {
611             $val = $r[0]->value();
612             $this->assertTrue(
613                 is_string($val), "good1 did not return string");
614         }
615         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
616         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
617         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
618         if (!$r[3]->faultCode()) {
619             $val = $r[3]->value();
620             $this->assertTrue(is_array($val), "good2 did not return array");
621         }
622
623         $this->client->return_type = $returnType;
624         $this->client->no_multicall = $noMultiCall;
625     }
626
627     public function testCatchWarnings()
628     {
629         $m = new xmlrpcmsg('tests.generatePHPWarning', array(
630             new xmlrpcval('whatever', 'string'),
631         ));
632         $v = $this->send($m);
633         if ($v) {
634             $this->assertEquals(true, $v->scalarval());
635         }
636     }
637
638     public function testCatchExceptions()
639     {
640         $m = new xmlrpcmsg('tests.raiseException', array(
641             new xmlrpcval('whatever', 'string'),
642         ));
643         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
644         $this->addQueryParams(array('EXCEPTION_HANDLING' => 1));
645         $v = $this->send($m, 1); // the error code of the expected exception
646         $this->addQueryParams(array('EXCEPTION_HANDLING' => 2));
647         // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
648         // as php will generate an http status code of either 200 or 500...
649         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
650     }
651
652     public function testZeroParams()
653     {
654         $m = new xmlrpcmsg('system.listMethods');
655         $v = $this->send($m);
656     }
657
658     public function testNullParams()
659     {
660         $m = new xmlrpcmsg('tests.getStateName.12', array(
661             new xmlrpcval('whatever', 'null'),
662             new xmlrpcval(23, 'int'),
663         ));
664         $v = $this->send($m);
665         if ($v) {
666             $this->assertEquals('Michigan', $v->scalarval());
667         }
668         $m = new xmlrpcmsg('tests.getStateName.12', array(
669             new xmlrpcval(23, 'int'),
670             new xmlrpcval('whatever', 'null'),
671         ));
672         $v = $this->send($m);
673         if ($v) {
674             $this->assertEquals('Michigan', $v->scalarval());
675         }
676         $m = new xmlrpcmsg('tests.getStateName.12', array(
677             new xmlrpcval(23, 'int')
678         ));
679         $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
680     }
681
682     public function testCodeInjectionServerSide()
683     {
684         $m = new xmlrpcmsg('system.MethodHelp');
685         $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>";
686         $v = $this->send($m);
687         if ($v) {
688             $this->assertEquals(0, $v->structsize());
689         }
690     }
691
692     public function testServerWrappedFunction()
693     {
694         $m = new xmlrpcmsg('tests.getStateName.2', array(
695             new xmlrpcval(23, 'int'),
696         ));
697         $v = $this->send($m);
698         $this->assertEquals('Michigan', $v->scalarval());
699
700         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
701         $m = new xmlrpcmsg('tests.getStateName.2', array(
702             new xmlrpcval(0, 'int'),
703         ));
704         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
705
706         // check if the generated function dispatch map is fine, by checking if the server registered it
707         $m = new xmlrpcmsg('system.methodSignature', array(
708             new xmlrpcval('tests.getStateName.2'),
709         ));
710         $v = $this->send($m);
711         $encoder = new \PhpXmlRpc\Encoder();
712         $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
713     }
714
715     public function testServerWrappedFunctionAsSource()
716     {
717         $m = new xmlrpcmsg('tests.getStateName.6', array(
718             new xmlrpcval(23, 'int'),
719         ));
720         $v = $this->send($m);
721         $this->assertEquals('Michigan', $v->scalarval());
722
723         // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
724         $m = new xmlrpcmsg('tests.getStateName.6', array(
725             new xmlrpcval(0, 'int'),
726         ));
727         $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
728     }
729
730     public function testServerWrappedObjectMethods()
731     {
732         $m = new xmlrpcmsg('tests.getStateName.3', array(
733             new xmlrpcval(23, 'int'),
734         ));
735         $v = $this->send($m);
736         $this->assertEquals('Michigan', $v->scalarval());
737
738         $m = new xmlrpcmsg('tests.getStateName.4', array(
739             new xmlrpcval(23, 'int'),
740         ));
741         $v = $this->send($m);
742         $this->assertEquals('Michigan', $v->scalarval());
743
744         $m = new xmlrpcmsg('tests.getStateName.5', array(
745             new xmlrpcval(23, 'int'),
746         ));
747         $v = $this->send($m);
748         $this->assertEquals('Michigan', $v->scalarval());
749
750         $m = new xmlrpcmsg('tests.getStateName.7', array(
751             new xmlrpcval(23, 'int'),
752         ));
753         $v = $this->send($m);
754         $this->assertEquals('Michigan', $v->scalarval());
755
756         $m = new xmlrpcmsg('tests.getStateName.8', array(
757             new xmlrpcval(23, 'int'),
758         ));
759         $v = $this->send($m);
760         $this->assertEquals('Michigan', $v->scalarval());
761
762         $m = new xmlrpcmsg('tests.getStateName.9', array(
763             new xmlrpcval(23, 'int'),
764         ));
765         $v = $this->send($m);
766         $this->assertEquals('Michigan', $v->scalarval());
767     }
768
769     public function testServerWrappedObjectMethodsAsSource()
770     {
771         $m = new xmlrpcmsg('tests.getStateName.7', array(
772             new xmlrpcval(23, 'int'),
773         ));
774         $v = $this->send($m);
775         $this->assertEquals('Michigan', $v->scalarval());
776
777         $m = new xmlrpcmsg('tests.getStateName.8', array(
778             new xmlrpcval(23, 'int'),
779         ));
780         $v = $this->send($m);
781         $this->assertEquals('Michigan', $v->scalarval());
782
783         $m = new xmlrpcmsg('tests.getStateName.9', array(
784             new xmlrpcval(23, 'int'),
785         ));
786         $v = $this->send($m);
787         $this->assertEquals('Michigan', $v->scalarval());
788     }
789
790     public function testServerClosure()
791     {
792         $m = new xmlrpcmsg('tests.getStateName.10', array(
793             new xmlrpcval(23, 'int'),
794         ));
795         $v = $this->send($m);
796         $this->assertEquals('Michigan', $v->scalarval());
797     }
798
799     public function testServerWrappedClosure()
800     {
801         $m = new xmlrpcmsg('tests.getStateName.11', array(
802             new xmlrpcval(23, 'int'),
803         ));
804         $v = $this->send($m);
805         $this->assertEquals('Michigan', $v->scalarval());
806     }
807
808     public function testServerWrappedClass()
809     {
810         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
811             new xmlrpcval(23, 'int'),
812         ));
813         $v = $this->send($m);
814         $this->assertEquals('Michigan', $v->scalarval());
815     }
816
817     public function testWrappedMethod()
818     {
819         // make a 'deep client copy' as the original one might have many properties set
820         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
821         if ($func == false) {
822             $this->fail('Registration of examples.getStateName failed');
823         } else {
824             $v = $func(23);
825             // work around bug in current (or old?) version of phpunit when reporting the error
826             /*if (is_object($v)) {
827                 $v = var_export($v, true);
828             }*/
829             $this->assertEquals('Michigan', $v);
830         }
831     }
832
833     public function testWrappedMethodAsSource()
834     {
835         // make a 'deep client copy' as the original one might have many properties set
836         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
837         if ($func == false) {
838             $this->fail('Registration of examples.getStateName failed');
839         } else {
840             eval($func['source']);
841             $func = $func['function'];
842             $v = $func(23);
843             // work around bug in current (or old?) version of phpunit when reporting the error
844             /*if (is_object($v)) {
845                 $v = var_export($v, true);
846             }*/
847             $this->assertEquals('Michigan', $v);
848         }
849     }
850
851     public function testWrappedClass()
852     {
853         // make a 'deep client copy' as the original one might have many properties set
854         // also for speed only wrap one method of the whole server
855         $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
856         if ($class == '') {
857             $this->fail('Registration of remote server failed');
858         } else {
859             $obj = new $class();
860             if (!is_callable(array($obj, 'examples_getStateName'))) {
861                 $this->fail('Registration of remote server failed to import method "examples_getStateName"');
862             } else {
863                 $v = $obj->examples_getStateName(23);
864                 // work around bug in current (or old?) version of phpunit when reporting the error
865                 /*if (is_object($v)) {
866                     $v = var_export($v, true);
867                 }*/
868                 $this->assertEquals('Michigan', $v);
869             }
870         }
871     }
872
873     public function testTransferOfObjectViaWrapping()
874     {
875         // make a 'deep client copy' as the original one might have many properties set
876         $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => 0,
877             'decode_php_objs' => true));
878         if ($func == false) {
879             $this->fail('Registration of tests.returnPhpObject failed');
880         } else {
881             $v = $func();
882             $obj = new stdClass();
883             $obj->hello = 'world';
884             $this->assertEquals($obj, $v);
885         }
886     }
887
888     public function testGetCookies()
889     {
890         // let server set to us some cookies we tell it
891         $cookies = array(
892             //'c1' => array(),
893             'c2' => array('value' => 'c2'),
894             'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
895             'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
896             'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
897         );
898         $cookiesval = php_xmlrpc_encode($cookies);
899         $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
900         $r = $this->send($m, 0, true);
901         if ($r) {
902             $v = $r->value();
903             $this->assertEquals(1, $v->scalarval());
904             // now check if we decoded the cookies as we had set them
905             $rcookies = $r->cookies();
906             // remove extra cookies which might have been set by proxies
907             foreach ($rcookies as $c => $v) {
908                 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
909                     unset($rcookies[$c]);
910                 }
911                 // Seems like we get this when using php-fpm and php 5.5+ ...
912                 if (isset($rcookies[$c]['Max-Age'])) {
913                     unset($rcookies[$c]['Max-Age']);
914                 }
915             }
916             foreach ($cookies as $c => $v) {
917                 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
918                 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
919                 if (isset($v['expires'])) {
920                     if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
921                         $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
922                     } else {
923                         $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
924                     }
925                 }
926             }
927
928             $this->assertEquals($cookies, $rcookies);
929         }
930     }
931
932     public function testSetCookies()
933     {
934         // let server set to us some cookies we tell it
935         $cookies = array(
936             'c0' => null,
937             'c1' => 1,
938             'c2' => '2 3',
939             'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
940         );
941         $m = new xmlrpcmsg('examples.getcookies', array());
942         foreach ($cookies as $cookie => $val) {
943             $this->client->setCookie($cookie, $val);
944             $cookies[$cookie] = (string)$cookies[$cookie];
945         }
946         $r = $this->client->send($m, $this->timeout, $this->method);
947         $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
948         if (!$r->faultCode()) {
949             $v = $r->value();
950             $v = php_xmlrpc_decode($v);
951
952             // take care for the extra cookie used for coverage collection
953             if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
954                 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
955             }
956
957             // on IIS and Apache getallheaders returns something slightly different...
958             $this->assertEquals($cookies, $v);
959         }
960     }
961
962     public function testServerComments()
963     {
964         $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
965             new xmlrpcval('hello world', 'string'),
966         ));
967         $r = $this->send($m, 0, true);
968         $this->assertContains('hello world', $r->raw_data);
969     }
970
971     public function testSendTwiceSameMsg()
972     {
973         $m = new xmlrpcmsg('examples.stringecho', array(
974             new xmlrpcval('hello world', 'string'),
975         ));
976         $v1 = $this->send($m);
977         $v2 = $this->send($m);
978         if ($v1 && $v2) {
979             $this->assertEquals($v1, $v2);
980         }
981     }
982 }