3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
6 include_once __DIR__ . '/parse_args.php';
8 class LocalhostTest extends PHPUnit_Framework_TestCase
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();
18 protected static $failed_tests = array();
21 /** @var boolean $collectCodeCoverageInformation */
22 protected $collectCodeCoverageInformation;
23 protected $coverageScriptUrl;
25 public static function fail($message = '')
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;
39 parent::fail($message);
43 * Reimplemented to allow us to collect code coverage info from the target server.
44 * Code taken from PHPUnit_Extensions_Selenium2TestCase
46 * @param PHPUnit_Framework_TestResult $result
47 * @return PHPUnit_Framework_TestResult
50 public function run(PHPUnit_Framework_TestResult $result = NULL)
52 $this->testId = get_class($this) . '__' . $this->getName();
54 if ($result === NULL) {
55 $result = $this->createResult();
58 $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
62 if ($this->collectCodeCoverageInformation) {
63 $coverage = new PHPUnit_Extensions_SeleniumCommon_RemoteCoverage(
64 $this->coverageScriptUrl,
67 $result->getCodeCoverage()->append(
68 $coverage->get(), $this
72 // do not call this before to give the time to the Listeners to run
73 //$this->getStrategy()->endOfTest($this->session);
78 public function setUp()
80 $this->args = argParser::getArgs();
82 $server = explode(':', $this->args['LOCALSERVER']);
83 if (count($server) > 1) {
84 $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
86 $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
88 if ($this->args['DEBUG']) {
89 $this->client->setDebug($this->args['DEBUG']);
91 $this->client->request_compression = $this->request_compression;
92 $this->client->accepted_compression = $this->accepted_compression;
94 $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] );
97 protected function send($msg, $errrorcode = 0, $return_response = false)
99 if ($this->collectCodeCoverageInformation) {
100 $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
103 $r = $this->client->send($msg, $this->timeout, $this->method);
104 // for multicall, return directly array of responses
108 if (is_array($errrorcode)) {
109 $this->assertContains($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
112 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
114 if (!$r->faultCode()) {
115 if ($return_response) {
125 public function testString()
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'),
139 $v = $this->send($f);
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());
146 $this->assertEquals($sendstring, $v->scalarval());
148 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
153 public function testLatin1String()
156 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
157 $f = '<?xml version="1.0" encoding="iso-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
159 '</value></param></params></methodCall>';
160 $v = $this->send($f);
162 $this->assertEquals($sendstring, $v->scalarval());
166 public function testAddingDoubles()
168 // note that rounding errors mean we
169 // keep precision to sensible levels here ;-)
172 $f = new xmlrpcmsg('examples.addtwodouble', array(
173 new xmlrpcval($a, 'double'),
174 new xmlrpcval($b, 'double'),
176 $v = $this->send($f);
178 $this->assertEquals($a + $b, $v->scalarval());
182 public function testAdding()
184 $f = new xmlrpcmsg('examples.addtwo', array(
185 new xmlrpcval(12, 'int'),
186 new xmlrpcval(-23, 'int'),
188 $v = $this->send($f);
190 $this->assertEquals(12 - 23, $v->scalarval());
194 public function testInvalidNumber()
196 $f = new xmlrpcmsg('examples.addtwo', array(
197 new xmlrpcval('fred', 'int'),
198 new xmlrpcval("\"; exec('ls')", 'int'),
200 $v = $this->send($f);
201 /// @todo a fault condition should be generated here
202 /// by the server, which we pick up on
204 $this->assertEquals(0, $v->scalarval());
208 public function testBoolean()
210 $f = new xmlrpcmsg('examples.invertBooleans', array(
212 new xmlrpcval(true, 'boolean'),
213 new xmlrpcval(false, 'boolean'),
214 new xmlrpcval(1, 'boolean'),
215 new xmlrpcval(0, 'boolean'),
216 //new xmlrpcval('true', 'boolean'),
217 //new xmlrpcval('false', 'boolean')
222 $v = $this->send($f);
224 $sz = $v->arraysize();
226 for ($i = 0; $i < $sz; $i++) {
227 $b = $v->arraymem($i);
228 if ($b->scalarval()) {
234 $this->assertEquals($answer, $got);
238 public function testBase64()
240 $sendstring = 'Mary had a little lamb,
241 Whose fleece was white as snow,
242 And everywhere that Mary went
243 the lamb was sure to go.
245 Mary had a little lamb
246 She tied it to a pylon
247 Ten thousand volts went down its back
248 And turned it into nylon';
249 $f = new xmlrpcmsg('examples.decode64', array(
250 new xmlrpcval($sendstring, 'base64'),
252 $v = $this->send($f);
254 if (strlen($sendstring) == strlen($v->scalarval())) {
255 $this->assertEquals($sendstring, $v->scalarval());
257 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
262 public function testDateTime()
265 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
266 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
267 $this->assertEquals($t1->serialize(), $t2->serialize());
268 if (class_exists('DateTime')) {
269 $datetime = new DateTime();
270 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
271 if (is_callable(array($datetime, 'setTimestamp'))) {
272 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
273 $this->assertEquals($t1->serialize(), $t3->serialize());
278 public function testCountEntities()
280 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
281 $f = new xmlrpcmsg('validator1.countTheEntities', array(
282 new xmlrpcval($sendstring, 'string'),
284 $v = $this->send($f);
288 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
289 while (list(, $val) = each($expect_array)) {
290 $b = $v->structmem($val);
291 $got .= $b->me['int'];
293 $this->assertEquals($expected, $got);
297 public function _multicall_msg($method, $params)
299 $struct['methodName'] = new xmlrpcval($method, 'string');
300 $struct['params'] = new xmlrpcval($params, 'array');
302 return new xmlrpcval($struct, 'struct');
305 public function testServerMulticall()
307 // We manually construct a system.multicall() call to ensure
308 // that the server supports it.
310 // NB: This test will NOT pass if server does not support system.multicall.
312 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
313 $good1 = $this->_multicall_msg(
315 array(php_xmlrpc_encode('system.listMethods')));
316 $bad = $this->_multicall_msg(
318 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
319 $recursive = $this->_multicall_msg(
321 array(new xmlrpcval(array(), 'array')));
322 $good2 = $this->_multicall_msg(
323 'system.methodSignature',
324 array(php_xmlrpc_encode('system.listMethods')));
325 $arg = new xmlrpcval(
326 array($good1, $bad, $recursive, $good2),
330 $f = new xmlrpcmsg('system.multicall', array($arg));
331 $v = $this->send($f);
333 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
334 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
336 $r1 = $v->arraymem(0);
338 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
339 "did not get array of size 1 from good1"
342 $r2 = $v->arraymem(1);
344 $r2->kindOf() == 'struct',
348 $r3 = $v->arraymem(2);
350 $r3->kindOf() == 'struct',
351 "recursive system.multicall did not fail"
354 $r4 = $v->arraymem(3);
356 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
357 "did not get array of size 1 from good2"
362 public function testClientMulticall1()
364 // NB: This test will NOT pass if server does not support system.multicall.
366 $this->client->no_multicall = false;
368 $good1 = new xmlrpcmsg('system.methodHelp',
369 array(php_xmlrpc_encode('system.listMethods')));
370 $bad = new xmlrpcmsg('test.nosuch',
371 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
372 $recursive = new xmlrpcmsg('system.multicall',
373 array(new xmlrpcval(array(), 'array')));
374 $good2 = new xmlrpcmsg('system.methodSignature',
375 array(php_xmlrpc_encode('system.listMethods'))
378 $r = $this->send(array($good1, $bad, $recursive, $good2));
380 $this->assertTrue(count($r) == 4, "wrong number of return values");
383 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
384 if (!$r[0]->faultCode()) {
385 $val = $r[0]->value();
387 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
388 "good1 did not return string"
391 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
392 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
393 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
394 if (!$r[3]->faultCode()) {
395 $val = $r[3]->value();
396 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
398 // This is the only assert in this test which should fail
399 // if the test server does not support system.multicall.
400 $this->assertTrue($this->client->no_multicall == false,
401 "server does not support system.multicall"
405 public function testClientMulticall2()
407 // NB: This test will NOT pass if server does not support system.multicall.
409 $this->client->no_multicall = true;
411 $good1 = new xmlrpcmsg('system.methodHelp',
412 array(php_xmlrpc_encode('system.listMethods')));
413 $bad = new xmlrpcmsg('test.nosuch',
414 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
415 $recursive = new xmlrpcmsg('system.multicall',
416 array(new xmlrpcval(array(), 'array')));
417 $good2 = new xmlrpcmsg('system.methodSignature',
418 array(php_xmlrpc_encode('system.listMethods'))
421 $r = $this->send(array($good1, $bad, $recursive, $good2));
423 $this->assertTrue(count($r) == 4, "wrong number of return values");
426 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
427 if (!$r[0]->faultCode()) {
428 $val = $r[0]->value();
430 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
431 "good1 did not return string");
433 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
434 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
435 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
436 if (!$r[3]->faultCode()) {
437 $val = $r[3]->value();
438 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
442 public function testClientMulticall3()
444 // NB: This test will NOT pass if server does not support system.multicall.
446 $this->client->return_type = 'phpvals';
447 $this->client->no_multicall = false;
449 $good1 = new xmlrpcmsg('system.methodHelp',
450 array(php_xmlrpc_encode('system.listMethods')));
451 $bad = new xmlrpcmsg('test.nosuch',
452 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
453 $recursive = new xmlrpcmsg('system.multicall',
454 array(new xmlrpcval(array(), 'array')));
455 $good2 = new xmlrpcmsg('system.methodSignature',
456 array(php_xmlrpc_encode('system.listMethods'))
459 $r = $this->send(array($good1, $bad, $recursive, $good2));
461 $this->assertTrue(count($r) == 4, "wrong number of return values");
463 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
464 if (!$r[0]->faultCode()) {
465 $val = $r[0]->value();
467 is_string($val), "good1 did not return string");
469 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
470 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
471 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
472 if (!$r[3]->faultCode()) {
473 $val = $r[3]->value();
474 $this->assertTrue(is_array($val), "good2 did not return array");
476 $this->client->return_type = 'xmlrpcvals';
479 public function testCatchWarnings()
481 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
482 new xmlrpcval('whatever', 'string'),
484 $v = $this->send($f);
486 $this->assertEquals($v->scalarval(), true);
490 public function testCatchExceptions()
492 $f = new xmlrpcmsg('examples.raiseException', array(
493 new xmlrpcval('whatever', 'string'),
495 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
496 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
497 $v = $this->send($f, 1);
498 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
499 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
500 // as php will generate an http status code of either 200 or 500...
501 $v = $this->send($f, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
504 public function testZeroParams()
506 $f = new xmlrpcmsg('system.listMethods');
507 $v = $this->send($f);
510 public function testCodeInjectionServerSide()
512 $f = new xmlrpcmsg('system.MethodHelp');
513 $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>";
514 $v = $this->send($f);
515 //$v = $r->faultCode();
517 $this->assertEquals(0, $v->structsize());
521 public function testAutoRegisteredFunction()
523 $f = new xmlrpcmsg('examples.php.getStateName', array(
524 new xmlrpcval(23, 'int'),
526 $v = $this->send($f);
528 $this->assertEquals('Michigan', $v->scalarval());
530 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
534 public function testAutoRegisteredClass()
536 $f = new xmlrpcmsg('examples.php2.getStateName', array(
537 new xmlrpcval(23, 'int'),
539 $v = $this->send($f);
541 $this->assertEquals('Michigan', $v->scalarval());
542 $f = new xmlrpcmsg('examples.php3.getStateName', array(
543 new xmlrpcval(23, 'int'),
545 $v = $this->send($f);
547 $this->assertEquals('Michigan', $v->scalarval());
550 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
554 public function testAutoRegisteredMethod()
556 // make a 'deep client copy' as the original one might have many properties set
557 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
559 $this->fail('Registration of examples.getStateName failed');
562 // work around bug in current version of phpunit
564 $v = var_export($v, true);
566 $this->assertEquals('Michigan', $v);
570 public function testGetCookies()
572 // let server set to us some cookies we tell it
575 'c2' => array('value' => 'c2'),
576 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
577 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
578 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
580 $cookiesval = php_xmlrpc_encode($cookies);
581 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
582 $r = $this->send($f, 0, true);
585 $this->assertEquals(1, $v->scalarval());
586 // now check if we decoded the cookies as we had set them
587 $rcookies = $r->cookies();
588 // remove extra cookies which might have been set by proxies
589 foreach ($rcookies as $c => $v) {
590 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
591 unset($rcookies[$c]);
593 // Seems like we get this when using php-fpm and php 5.5+ ...
594 if (isset($rcookies[$c]['Max-Age'])) {
595 unset($rcookies[$c]['Max-Age']);
598 foreach ($cookies as $c => $v) {
599 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
600 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
601 if (isset($v['expires'])) {
602 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
603 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
605 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
610 $this->assertEquals($cookies, $rcookies);
614 public function testSetCookies()
616 // let server set to us some cookies we tell it
621 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
623 $f = new xmlrpcmsg('examples.getcookies', array());
624 foreach ($cookies as $cookie => $val) {
625 $this->client->setCookie($cookie, $val);
626 $cookies[$cookie] = (string)$cookies[$cookie];
628 $r = $this->client->send($f, $this->timeout, $this->method);
629 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
630 if (!$r->faultCode()) {
632 $v = php_xmlrpc_decode($v);
634 // take care for the extra cookie used for coverage collection
635 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
636 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
639 // on IIS and Apache getallheaders returns something slightly different...
640 $this->assertEquals($v, $cookies);
644 public function testSendTwiceSameMsg()
646 $f = new xmlrpcmsg('examples.stringecho', array(
647 new xmlrpcval('hello world', 'string'),
649 $v1 = $this->send($f);
650 $v2 = $this->send($f);
651 //$v = $r->faultCode();
653 $this->assertEquals($v2, $v1);