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());
111 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
113 if (!$r->faultCode()) {
114 if ($return_response) {
124 public function testString()
126 $sendstring = "here are 3 \"entities\": < > & " .
127 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
128 " - isn't that great? \\\"hackery\\\" at it's best " .
129 " also don't want to miss out on \$item[0]. " .
130 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
131 "a simple CR here" . chr(13) .
132 "a simple LF here" . chr(10) .
133 "and then LFCR" . chr(10) . chr(13) .
134 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
135 $f = new xmlrpcmsg('examples.stringecho', array(
136 new xmlrpcval($sendstring, 'string'),
138 $v = $this->send($f);
140 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
141 // so we relax our tests...
142 $l1 = strlen($sendstring);
143 $l2 = strlen($v->scalarval());
145 $this->assertEquals($sendstring, $v->scalarval());
147 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
152 public function testLatin1String()
155 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
156 $f = '<?xml version="1.0" encoding="iso-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
158 '</value></param></params></methodCall>';
159 $v = $this->send($f);
161 $this->assertEquals($sendstring, $v->scalarval());
165 public function testAddingDoubles()
167 // note that rounding errors mean we
168 // keep precision to sensible levels here ;-)
171 $f = new xmlrpcmsg('examples.addtwodouble', array(
172 new xmlrpcval($a, 'double'),
173 new xmlrpcval($b, 'double'),
175 $v = $this->send($f);
177 $this->assertEquals($a + $b, $v->scalarval());
181 public function testAdding()
183 $f = new xmlrpcmsg('examples.addtwo', array(
184 new xmlrpcval(12, 'int'),
185 new xmlrpcval(-23, 'int'),
187 $v = $this->send($f);
189 $this->assertEquals(12 - 23, $v->scalarval());
193 public function testInvalidNumber()
195 $f = new xmlrpcmsg('examples.addtwo', array(
196 new xmlrpcval('fred', 'int'),
197 new xmlrpcval("\"; exec('ls')", 'int'),
199 $v = $this->send($f);
200 /// @todo a fault condition should be generated here
201 /// by the server, which we pick up on
203 $this->assertEquals(0, $v->scalarval());
207 public function testBoolean()
209 $f = new xmlrpcmsg('examples.invertBooleans', array(
211 new xmlrpcval(true, 'boolean'),
212 new xmlrpcval(false, 'boolean'),
213 new xmlrpcval(1, 'boolean'),
214 new xmlrpcval(0, 'boolean'),
215 //new xmlrpcval('true', 'boolean'),
216 //new xmlrpcval('false', 'boolean')
221 $v = $this->send($f);
223 $sz = $v->arraysize();
225 for ($i = 0; $i < $sz; $i++) {
226 $b = $v->arraymem($i);
227 if ($b->scalarval()) {
233 $this->assertEquals($answer, $got);
237 public function testBase64()
239 $sendstring = 'Mary had a little lamb,
240 Whose fleece was white as snow,
241 And everywhere that Mary went
242 the lamb was sure to go.
244 Mary had a little lamb
245 She tied it to a pylon
246 Ten thousand volts went down its back
247 And turned it into nylon';
248 $f = new xmlrpcmsg('examples.decode64', array(
249 new xmlrpcval($sendstring, 'base64'),
251 $v = $this->send($f);
253 if (strlen($sendstring) == strlen($v->scalarval())) {
254 $this->assertEquals($sendstring, $v->scalarval());
256 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
261 public function testDateTime()
264 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
265 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
266 $this->assertEquals($t1->serialize(), $t2->serialize());
267 if (class_exists('DateTime')) {
268 $datetime = new DateTime();
269 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
270 if (is_callable(array($datetime, 'setTimestamp'))) {
271 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
272 $this->assertEquals($t1->serialize(), $t3->serialize());
277 public function testCountEntities()
279 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
280 $f = new xmlrpcmsg('validator1.countTheEntities', array(
281 new xmlrpcval($sendstring, 'string'),
283 $v = $this->send($f);
287 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
288 while (list(, $val) = each($expect_array)) {
289 $b = $v->structmem($val);
290 $got .= $b->me['int'];
292 $this->assertEquals($expected, $got);
296 public function _multicall_msg($method, $params)
298 $struct['methodName'] = new xmlrpcval($method, 'string');
299 $struct['params'] = new xmlrpcval($params, 'array');
301 return new xmlrpcval($struct, 'struct');
304 public function testServerMulticall()
306 // We manually construct a system.multicall() call to ensure
307 // that the server supports it.
309 // NB: This test will NOT pass if server does not support system.multicall.
311 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
312 $good1 = $this->_multicall_msg(
314 array(php_xmlrpc_encode('system.listMethods')));
315 $bad = $this->_multicall_msg(
317 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
318 $recursive = $this->_multicall_msg(
320 array(new xmlrpcval(array(), 'array')));
321 $good2 = $this->_multicall_msg(
322 'system.methodSignature',
323 array(php_xmlrpc_encode('system.listMethods')));
324 $arg = new xmlrpcval(
325 array($good1, $bad, $recursive, $good2),
329 $f = new xmlrpcmsg('system.multicall', array($arg));
330 $v = $this->send($f);
332 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
333 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
335 $r1 = $v->arraymem(0);
337 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
338 "did not get array of size 1 from good1"
341 $r2 = $v->arraymem(1);
343 $r2->kindOf() == 'struct',
347 $r3 = $v->arraymem(2);
349 $r3->kindOf() == 'struct',
350 "recursive system.multicall did not fail"
353 $r4 = $v->arraymem(3);
355 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
356 "did not get array of size 1 from good2"
361 public function testClientMulticall1()
363 // NB: This test will NOT pass if server does not support system.multicall.
365 $this->client->no_multicall = false;
367 $good1 = new xmlrpcmsg('system.methodHelp',
368 array(php_xmlrpc_encode('system.listMethods')));
369 $bad = new xmlrpcmsg('test.nosuch',
370 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
371 $recursive = new xmlrpcmsg('system.multicall',
372 array(new xmlrpcval(array(), 'array')));
373 $good2 = new xmlrpcmsg('system.methodSignature',
374 array(php_xmlrpc_encode('system.listMethods'))
377 $r = $this->send(array($good1, $bad, $recursive, $good2));
379 $this->assertTrue(count($r) == 4, "wrong number of return values");
382 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
383 if (!$r[0]->faultCode()) {
384 $val = $r[0]->value();
386 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
387 "good1 did not return string"
390 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
391 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
392 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
393 if (!$r[3]->faultCode()) {
394 $val = $r[3]->value();
395 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
397 // This is the only assert in this test which should fail
398 // if the test server does not support system.multicall.
399 $this->assertTrue($this->client->no_multicall == false,
400 "server does not support system.multicall"
404 public function testClientMulticall2()
406 // NB: This test will NOT pass if server does not support system.multicall.
408 $this->client->no_multicall = true;
410 $good1 = new xmlrpcmsg('system.methodHelp',
411 array(php_xmlrpc_encode('system.listMethods')));
412 $bad = new xmlrpcmsg('test.nosuch',
413 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
414 $recursive = new xmlrpcmsg('system.multicall',
415 array(new xmlrpcval(array(), 'array')));
416 $good2 = new xmlrpcmsg('system.methodSignature',
417 array(php_xmlrpc_encode('system.listMethods'))
420 $r = $this->send(array($good1, $bad, $recursive, $good2));
422 $this->assertTrue(count($r) == 4, "wrong number of return values");
425 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
426 if (!$r[0]->faultCode()) {
427 $val = $r[0]->value();
429 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
430 "good1 did not return string");
432 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
433 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
434 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
435 if (!$r[3]->faultCode()) {
436 $val = $r[3]->value();
437 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
441 public function testClientMulticall3()
443 // NB: This test will NOT pass if server does not support system.multicall.
445 $this->client->return_type = 'phpvals';
446 $this->client->no_multicall = false;
448 $good1 = new xmlrpcmsg('system.methodHelp',
449 array(php_xmlrpc_encode('system.listMethods')));
450 $bad = new xmlrpcmsg('test.nosuch',
451 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
452 $recursive = new xmlrpcmsg('system.multicall',
453 array(new xmlrpcval(array(), 'array')));
454 $good2 = new xmlrpcmsg('system.methodSignature',
455 array(php_xmlrpc_encode('system.listMethods'))
458 $r = $this->send(array($good1, $bad, $recursive, $good2));
460 $this->assertTrue(count($r) == 4, "wrong number of return values");
462 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
463 if (!$r[0]->faultCode()) {
464 $val = $r[0]->value();
466 is_string($val), "good1 did not return string");
468 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
469 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
470 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
471 if (!$r[3]->faultCode()) {
472 $val = $r[3]->value();
473 $this->assertTrue(is_array($val), "good2 did not return array");
475 $this->client->return_type = 'xmlrpcvals';
478 public function testCatchWarnings()
480 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
481 new xmlrpcval('whatever', 'string'),
483 $v = $this->send($f);
485 $this->assertEquals($v->scalarval(), true);
489 public function testCatchExceptions()
491 $f = new xmlrpcmsg('examples.raiseException', array(
492 new xmlrpcval('whatever', 'string'),
494 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
495 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
496 $v = $this->send($f, 1);
497 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
498 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
499 // as php will generate an http status code of either 200 or 500...
500 $v = $this->send($f, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
503 public function testZeroParams()
505 $f = new xmlrpcmsg('system.listMethods');
506 $v = $this->send($f);
509 public function testCodeInjectionServerSide()
511 $f = new xmlrpcmsg('system.MethodHelp');
512 $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>";
513 $v = $this->send($f);
514 //$v = $r->faultCode();
516 $this->assertEquals(0, $v->structsize());
520 public function testAutoRegisteredFunction()
522 $f = new xmlrpcmsg('examples.php.getStateName', array(
523 new xmlrpcval(23, 'int'),
525 $v = $this->send($f);
527 $this->assertEquals('Michigan', $v->scalarval());
529 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
533 public function testAutoRegisteredClass()
535 $f = new xmlrpcmsg('examples.php2.getStateName', array(
536 new xmlrpcval(23, 'int'),
538 $v = $this->send($f);
540 $this->assertEquals('Michigan', $v->scalarval());
541 $f = new xmlrpcmsg('examples.php3.getStateName', array(
542 new xmlrpcval(23, 'int'),
544 $v = $this->send($f);
546 $this->assertEquals('Michigan', $v->scalarval());
549 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
553 public function testAutoRegisteredMethod()
555 // make a 'deep client copy' as the original one might have many properties set
556 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
558 $this->fail('Registration of examples.getStateName failed');
561 // work around bug in current version of phpunit
563 $v = var_export($v, true);
565 $this->assertEquals('Michigan', $v);
569 public function testGetCookies()
571 // let server set to us some cookies we tell it
574 'c2' => array('value' => 'c2'),
575 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
576 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
577 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
579 $cookiesval = php_xmlrpc_encode($cookies);
580 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
581 $r = $this->send($f, 0, true);
584 $this->assertEquals(1, $v->scalarval());
585 // now check if we decoded the cookies as we had set them
586 $rcookies = $r->cookies();
587 // remove extra cookies which might have been set by proxies
588 foreach ($rcookies as $c => $v) {
589 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
590 unset($rcookies[$c]);
592 // Seems like we get this when using php-fpm and php 5.5+ ...
593 if (isset($rcookies[$c]['Max-Age'])) {
594 unset($rcookies[$c]['Max-Age']);
597 foreach ($cookies as $c => $v) {
598 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
599 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
600 if (isset($v['expires'])) {
601 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
602 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
604 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
609 $this->assertEquals($cookies, $rcookies);
613 public function testSetCookies()
615 // let server set to us some cookies we tell it
620 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
622 $f = new xmlrpcmsg('examples.getcookies', array());
623 foreach ($cookies as $cookie => $val) {
624 $this->client->setCookie($cookie, $val);
625 $cookies[$cookie] = (string)$cookies[$cookie];
627 $r = $this->client->send($f, $this->timeout, $this->method);
628 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
629 if (!$r->faultCode()) {
631 $v = php_xmlrpc_decode($v);
633 // take care for the extra cookie used for coverage collection
634 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
635 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
638 // on IIS and Apache getallheaders returns something slightly different...
639 $this->assertEquals($v, $cookies);
643 public function testSendTwiceSameMsg()
645 $f = new xmlrpcmsg('examples.stringecho', array(
646 new xmlrpcval('hello world', 'string'),
648 $v1 = $this->send($f);
649 $v2 = $this->send($f);
650 //$v = $r->faultCode();
652 $this->assertEquals($v2, $v1);