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