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