Fix one test failing with php 5.5 fcgi
[plcapi.git] / tests / LocalhostTest.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 class LocalhostTest extends PHPUnit_Framework_TestCase
9 {
10     public $client = null;
11     public $method = 'http';
12     public $timeout = 10;
13     public $request_compression = null;
14     public $accepted_compression = '';
15     public $args = array();
16
17     public static function fail($message = '')
18     {
19         // save in global var that this particular test has failed
20         // (but only if not called from subclass objects / multitests)
21         if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
22             global $failed_tests;
23             $trace = debug_backtrace();
24             for ($i = 0; $i < count($trace); $i++) {
25                 if (strpos($trace[$i]['function'], 'test') === 0) {
26                     $failed_tests[$trace[$i]['function']] = true;
27                     break;
28                 }
29             }
30         }
31
32         parent::fail($message);
33     }
34
35     /**
36      * @todo be smarter with setup, do not use global variables anymore
37      */
38     public function setUp()
39     {
40         $this->args = argParser::getArgs();
41
42         $server = explode(':', $this->args['LOCALSERVER']);
43         if (count($server) > 1) {
44             $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
45         } else {
46             $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
47         }
48         if ($this->args['DEBUG']) {
49             $this->client->setDebug($this->args['DEBUG']);
50         }
51         $this->client->request_compression = $this->request_compression;
52         $this->client->accepted_compression = $this->accepted_compression;
53     }
54
55     public function send($msg, $errrorcode = 0, $return_response = false)
56     {
57         $r = $this->client->send($msg, $this->timeout, $this->method);
58         // for multicall, return directly array of responses
59         if (is_array($r)) {
60             return $r;
61         }
62         $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
63         if (!$r->faultCode()) {
64             if ($return_response) {
65                 return $r;
66             } else {
67                 return $r->value();
68             }
69         } else {
70             return;
71         }
72     }
73
74     public function testString()
75     {
76         $sendstring = "here are 3 \"entities\": < > & " .
77             "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
78             " - isn't that great? \\\"hackery\\\" at it's best " .
79             " also don't want to miss out on \$item[0]. " .
80             "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
81             "a simple CR here" . chr(13) .
82             "a simple LF here" . chr(10) .
83             "and then LFCR" . chr(10) . chr(13) .
84             "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
85         $f = new xmlrpcmsg('examples.stringecho', array(
86             new xmlrpcval($sendstring, 'string'),
87         ));
88         $v = $this->send($f);
89         if ($v) {
90             // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
91             // so we relax our tests...
92             $l1 = strlen($sendstring);
93             $l2 = strlen($v->scalarval());
94             if ($l1 == $l2) {
95                 $this->assertEquals($sendstring, $v->scalarval());
96             } else {
97                 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
98             }
99         }
100     }
101
102     public function testAddingDoubles()
103     {
104         // note that rounding errors mean we
105         // keep precision to sensible levels here ;-)
106         $a = 12.13;
107         $b = -23.98;
108         $f = new xmlrpcmsg('examples.addtwodouble', array(
109             new xmlrpcval($a, 'double'),
110             new xmlrpcval($b, 'double'),
111         ));
112         $v = $this->send($f);
113         if ($v) {
114             $this->assertEquals($a + $b, $v->scalarval());
115         }
116     }
117
118     public function testAdding()
119     {
120         $f = new xmlrpcmsg('examples.addtwo', array(
121             new xmlrpcval(12, 'int'),
122             new xmlrpcval(-23, 'int'),
123         ));
124         $v = $this->send($f);
125         if ($v) {
126             $this->assertEquals(12 - 23, $v->scalarval());
127         }
128     }
129
130     public function testInvalidNumber()
131     {
132         $f = new xmlrpcmsg('examples.addtwo', array(
133             new xmlrpcval('fred', 'int'),
134             new xmlrpcval("\"; exec('ls')", 'int'),
135         ));
136         $v = $this->send($f);
137         /// @todo a fault condition should be generated here
138         /// by the server, which we pick up on
139         if ($v) {
140             $this->assertEquals(0, $v->scalarval());
141         }
142     }
143
144     public function testBoolean()
145     {
146         $f = new xmlrpcmsg('examples.invertBooleans', array(
147             new xmlrpcval(array(
148                 new xmlrpcval(true, 'boolean'),
149                 new xmlrpcval(false, 'boolean'),
150                 new xmlrpcval(1, 'boolean'),
151                 new xmlrpcval(0, 'boolean'),
152                 //new xmlrpcval('true', 'boolean'),
153                 //new xmlrpcval('false', 'boolean')
154             ),
155                 'array'
156             ),));
157         $answer = '0101';
158         $v = $this->send($f);
159         if ($v) {
160             $sz = $v->arraysize();
161             $got = '';
162             for ($i = 0; $i < $sz; $i++) {
163                 $b = $v->arraymem($i);
164                 if ($b->scalarval()) {
165                     $got .= '1';
166                 } else {
167                     $got .= '0';
168                 }
169             }
170             $this->assertEquals($answer, $got);
171         }
172     }
173
174     public function testBase64()
175     {
176         $sendstring = 'Mary had a little lamb,
177 Whose fleece was white as snow,
178 And everywhere that Mary went
179 the lamb was sure to go.
180
181 Mary had a little lamb
182 She tied it to a pylon
183 Ten thousand volts went down its back
184 And turned it into nylon';
185         $f = new xmlrpcmsg('examples.decode64', array(
186             new xmlrpcval($sendstring, 'base64'),
187         ));
188         $v = $this->send($f);
189         if ($v) {
190             if (strlen($sendstring) == strlen($v->scalarval())) {
191                 $this->assertEquals($sendstring, $v->scalarval());
192             } else {
193                 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
194             }
195         }
196     }
197
198     public function testDateTime()
199     {
200         $time = time();
201         $t1 = new xmlrpcval($time, 'dateTime.iso8601');
202         $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
203         $this->assertEquals($t1->serialize(), $t2->serialize());
204         if (class_exists('DateTime')) {
205             $datetime = new DateTime();
206             // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
207             if (is_callable(array($datetime, 'setTimestamp'))) {
208                 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
209                 $this->assertEquals($t1->serialize(), $t3->serialize());
210             }
211         }
212     }
213
214     public function testCountEntities()
215     {
216         $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
217         $f = new xmlrpcmsg('validator1.countTheEntities', array(
218             new xmlrpcval($sendstring, 'string'),
219         ));
220         $v = $this->send($f);
221         if ($v) {
222             $got = '';
223             $expected = '37210';
224             $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
225             while (list(, $val) = each($expect_array)) {
226                 $b = $v->structmem($val);
227                 $got .= $b->me['int'];
228             }
229             $this->assertEquals($expected, $got);
230         }
231     }
232
233     public function _multicall_msg($method, $params)
234     {
235         $struct['methodName'] = new xmlrpcval($method, 'string');
236         $struct['params'] = new xmlrpcval($params, 'array');
237
238         return new xmlrpcval($struct, 'struct');
239     }
240
241     public function testServerMulticall()
242     {
243         // We manually construct a system.multicall() call to ensure
244         // that the server supports it.
245
246         // NB: This test will NOT pass if server does not support system.multicall.
247
248         // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
249         $good1 = $this->_multicall_msg(
250             'system.methodHelp',
251             array(php_xmlrpc_encode('system.listMethods')));
252         $bad = $this->_multicall_msg(
253             'test.nosuch',
254             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
255         $recursive = $this->_multicall_msg(
256             'system.multicall',
257             array(new xmlrpcval(array(), 'array')));
258         $good2 = $this->_multicall_msg(
259             'system.methodSignature',
260             array(php_xmlrpc_encode('system.listMethods')));
261         $arg = new xmlrpcval(
262             array($good1, $bad, $recursive, $good2),
263             'array'
264         );
265
266         $f = new xmlrpcmsg('system.multicall', array($arg));
267         $v = $this->send($f);
268         if ($v) {
269             //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
270             $this->assertTrue($v->arraysize() == 4, "bad number of return values");
271
272             $r1 = $v->arraymem(0);
273             $this->assertTrue(
274                 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
275                 "did not get array of size 1 from good1"
276             );
277
278             $r2 = $v->arraymem(1);
279             $this->assertTrue(
280                 $r2->kindOf() == 'struct',
281                 "no fault from bad"
282             );
283
284             $r3 = $v->arraymem(2);
285             $this->assertTrue(
286                 $r3->kindOf() == 'struct',
287                 "recursive system.multicall did not fail"
288             );
289
290             $r4 = $v->arraymem(3);
291             $this->assertTrue(
292                 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
293                 "did not get array of size 1 from good2"
294             );
295         }
296     }
297
298     public function testClientMulticall1()
299     {
300         // NB: This test will NOT pass if server does not support system.multicall.
301
302         $this->client->no_multicall = false;
303
304         $good1 = new xmlrpcmsg('system.methodHelp',
305             array(php_xmlrpc_encode('system.listMethods')));
306         $bad = new xmlrpcmsg('test.nosuch',
307             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
308         $recursive = new xmlrpcmsg('system.multicall',
309             array(new xmlrpcval(array(), 'array')));
310         $good2 = new xmlrpcmsg('system.methodSignature',
311             array(php_xmlrpc_encode('system.listMethods'))
312         );
313
314         $r = $this->send(array($good1, $bad, $recursive, $good2));
315         if ($r) {
316             $this->assertTrue(count($r) == 4, "wrong number of return values");
317         }
318
319         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
320         if (!$r[0]->faultCode()) {
321             $val = $r[0]->value();
322             $this->assertTrue(
323                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
324                 "good1 did not return string"
325             );
326         }
327         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
328         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
329         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
330         if (!$r[3]->faultCode()) {
331             $val = $r[3]->value();
332             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
333         }
334         // This is the only assert in this test which should fail
335         // if the test server does not support system.multicall.
336         $this->assertTrue($this->client->no_multicall == false,
337             "server does not support system.multicall"
338         );
339     }
340
341     public function testClientMulticall2()
342     {
343         // NB: This test will NOT pass if server does not support system.multicall.
344
345         $this->client->no_multicall = true;
346
347         $good1 = new xmlrpcmsg('system.methodHelp',
348             array(php_xmlrpc_encode('system.listMethods')));
349         $bad = new xmlrpcmsg('test.nosuch',
350             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
351         $recursive = new xmlrpcmsg('system.multicall',
352             array(new xmlrpcval(array(), 'array')));
353         $good2 = new xmlrpcmsg('system.methodSignature',
354             array(php_xmlrpc_encode('system.listMethods'))
355         );
356
357         $r = $this->send(array($good1, $bad, $recursive, $good2));
358         if ($r) {
359             $this->assertTrue(count($r) == 4, "wrong number of return values");
360         }
361
362         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
363         if (!$r[0]->faultCode()) {
364             $val = $r[0]->value();
365             $this->assertTrue(
366                 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
367                 "good1 did not return string");
368         }
369         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
370         $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
371         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
372         if (!$r[3]->faultCode()) {
373             $val = $r[3]->value();
374             $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
375         }
376     }
377
378     public function testClientMulticall3()
379     {
380         // NB: This test will NOT pass if server does not support system.multicall.
381
382         $this->client->return_type = 'phpvals';
383         $this->client->no_multicall = false;
384
385         $good1 = new xmlrpcmsg('system.methodHelp',
386             array(php_xmlrpc_encode('system.listMethods')));
387         $bad = new xmlrpcmsg('test.nosuch',
388             array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
389         $recursive = new xmlrpcmsg('system.multicall',
390             array(new xmlrpcval(array(), 'array')));
391         $good2 = new xmlrpcmsg('system.methodSignature',
392             array(php_xmlrpc_encode('system.listMethods'))
393         );
394
395         $r = $this->send(array($good1, $bad, $recursive, $good2));
396         if ($r) {
397             $this->assertTrue(count($r) == 4, "wrong number of return values");
398         }
399         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
400         if (!$r[0]->faultCode()) {
401             $val = $r[0]->value();
402             $this->assertTrue(
403                 is_string($val), "good1 did not return string");
404         }
405         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
406         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
407         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
408         if (!$r[3]->faultCode()) {
409             $val = $r[3]->value();
410             $this->assertTrue(is_array($val), "good2 did not return array");
411         }
412         $this->client->return_type = 'xmlrpcvals';
413     }
414
415     public function testCatchWarnings()
416     {
417         $f = new xmlrpcmsg('examples.generatePHPWarning', array(
418             new xmlrpcval('whatever', 'string'),
419         ));
420         $v = $this->send($f);
421         if ($v) {
422             $this->assertEquals($v->scalarval(), true);
423         }
424     }
425
426     public function testCatchExceptions()
427     {
428         $f = new xmlrpcmsg('examples.raiseException', array(
429             new xmlrpcval('whatever', 'string'),
430         ));
431         $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
432         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
433         $v = $this->send($f, 1);
434         $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
435         $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
436     }
437
438     public function testZeroParams()
439     {
440         $f = new xmlrpcmsg('system.listMethods');
441         $v = $this->send($f);
442     }
443
444     public function testCodeInjectionServerSide()
445     {
446         $f = new xmlrpcmsg('system.MethodHelp');
447         $f->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>";
448         $v = $this->send($f);
449         //$v = $r->faultCode();
450         if ($v) {
451             $this->assertEquals(0, $v->structsize());
452         }
453     }
454
455     public function testAutoRegisteredFunction()
456     {
457         $f = new xmlrpcmsg('examples.php.getStateName', array(
458             new xmlrpcval(23, 'int'),
459         ));
460         $v = $this->send($f);
461         if ($v) {
462             $this->assertEquals('Michigan', $v->scalarval());
463         } else {
464             $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
465         }
466     }
467
468     public function testAutoRegisteredClass()
469     {
470         $f = new xmlrpcmsg('examples.php2.getStateName', array(
471             new xmlrpcval(23, 'int'),
472         ));
473         $v = $this->send($f);
474         if ($v) {
475             $this->assertEquals('Michigan', $v->scalarval());
476             $f = new xmlrpcmsg('examples.php3.getStateName', array(
477                 new xmlrpcval(23, 'int'),
478             ));
479             $v = $this->send($f);
480             if ($v) {
481                 $this->assertEquals('Michigan', $v->scalarval());
482             }
483         } else {
484             $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
485         }
486     }
487
488     public function testAutoRegisteredMethod()
489     {
490         // make a 'deep client copy' as the original one might have many properties set
491         $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
492         if ($func == '') {
493             $this->fail('Registration of examples.getStateName failed');
494         } else {
495             $v = $func(23);
496             // work around bug in current version of phpunit
497             if (is_object($v)) {
498                 $v = var_export($v, true);
499             }
500             $this->assertEquals('Michigan', $v);
501         }
502     }
503
504     public function testGetCookies()
505     {
506         // let server set to us some cookies we tell it
507         $cookies = array(
508             //'c1' => array(),
509             'c2' => array('value' => 'c2'),
510             'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
511             'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
512             'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
513         );
514         $cookiesval = php_xmlrpc_encode($cookies);
515         $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
516         $r = $this->send($f, 0, true);
517         if ($r) {
518             $v = $r->value();
519             $this->assertEquals(1, $v->scalarval());
520             // now check if we decoded the cookies as we had set them
521             $rcookies = $r->cookies();
522             // remove extra cookies which might have been set by proxies
523             foreach ($rcookies as $c => $v) {
524                 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
525                     unset($rcookies[$c]);
526                 }
527                 // Seems like we get this when using php-fpm and php 5.5+ ...
528                 if (isset($rcookies[$c]['Max-Age'])) {
529                     unset($rcookies[$c]['Max-Age']);
530                 }
531             }
532             foreach ($cookies as $c => $v) {
533                 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
534                 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
535                 if (isset($v['expires'])) {
536                     if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
537                         $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
538                     } else {
539                         $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
540                     }
541                 }
542             }
543
544             $this->assertEquals($cookies, $rcookies);
545         }
546     }
547
548     public function testSetCookies()
549     {
550         // let server set to us some cookies we tell it
551         $cookies = array(
552             'c0' => null,
553             'c1' => 1,
554             'c2' => '2 3',
555             'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
556         );
557         $f = new xmlrpcmsg('examples.getcookies', array());
558         foreach ($cookies as $cookie => $val) {
559             $this->client->setCookie($cookie, $val);
560             $cookies[$cookie] = (string)$cookies[$cookie];
561         }
562         $r = $this->client->send($f, $this->timeout, $this->method);
563         $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
564         if (!$r->faultCode()) {
565             $v = $r->value();
566             $v = php_xmlrpc_decode($v);
567             // on IIS and Apache getallheaders returns something slightly different...
568             $this->assertEquals($v, $cookies);
569         }
570     }
571
572     public function testSendTwiceSameMsg()
573     {
574         $f = new xmlrpcmsg('examples.stringecho', array(
575             new xmlrpcval('hello world', 'string'),
576         ));
577         $v1 = $this->send($f);
578         $v2 = $this->send($f);
579         //$v = $r->faultCode();
580         if ($v1 && $v2) {
581             $this->assertEquals($v2, $v1);
582         }
583     }
584 }