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