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