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']);
89 $this->client->setDebug($this->args['DEBUG']);
90 $this->client->request_compression = $this->request_compression;
91 $this->client->accepted_compression = $this->accepted_compression;
93 $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] );
95 if ($this->args['DEBUG'] == 1)
99 protected function tearDown()
101 if ($this->args['DEBUG'] != 1)
103 $out = ob_get_clean();
104 $status = $this->getStatus();
105 if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
106 || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
111 protected function send($msg, $errrorcode = 0, $return_response = false)
113 if ($this->collectCodeCoverageInformation) {
114 $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
117 $r = $this->client->send($msg, $this->timeout, $this->method);
118 // for multicall, return directly array of responses
122 if (is_array($errrorcode)) {
123 $this->assertContains($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
125 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
127 if (!$r->faultCode()) {
128 if ($return_response) {
138 public function testString()
140 $sendstring = "here are 3 \"entities\": < > & " .
141 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
142 " - isn't that great? \\\"hackery\\\" at it's best " .
143 " also don't want to miss out on \$item[0]. " .
144 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
145 "a simple CR here" . chr(13) .
146 "a simple LF here" . chr(10) .
147 "and then LFCR" . chr(10) . chr(13) .
148 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
149 $f = new xmlrpcmsg('examples.stringecho', array(
150 new xmlrpcval($sendstring, 'string'),
152 $v = $this->send($f);
154 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
155 // so we relax our tests...
156 $l1 = strlen($sendstring);
157 $l2 = strlen($v->scalarval());
159 $this->assertEquals($sendstring, $v->scalarval());
161 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
166 public function testLatin1String()
169 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
170 $f = '<?xml version="1.0" encoding="ISO-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
172 '</value></param></params></methodCall>';
173 $v = $this->send($f);
175 $this->assertEquals($sendstring, $v->scalarval());
179 public function testAddingDoubles()
181 // note that rounding errors mean we
182 // keep precision to sensible levels here ;-)
185 $f = new xmlrpcmsg('examples.addtwodouble', array(
186 new xmlrpcval($a, 'double'),
187 new xmlrpcval($b, 'double'),
189 $v = $this->send($f);
191 $this->assertEquals($a + $b, $v->scalarval());
195 public function testAdding()
197 $f = new xmlrpcmsg('examples.addtwo', array(
198 new xmlrpcval(12, 'int'),
199 new xmlrpcval(-23, 'int'),
201 $v = $this->send($f);
203 $this->assertEquals(12 - 23, $v->scalarval());
207 public function testInvalidNumber()
209 $f = new xmlrpcmsg('examples.addtwo', array(
210 new xmlrpcval('fred', 'int'),
211 new xmlrpcval("\"; exec('ls')", 'int'),
213 $v = $this->send($f);
214 /// @todo a fault condition should be generated here
215 /// by the server, which we pick up on
217 $this->assertEquals(0, $v->scalarval());
221 public function testBoolean()
223 $f = new xmlrpcmsg('examples.invertBooleans', array(
225 new xmlrpcval(true, 'boolean'),
226 new xmlrpcval(false, 'boolean'),
227 new xmlrpcval(1, 'boolean'),
228 new xmlrpcval(0, 'boolean'),
229 //new xmlrpcval('true', 'boolean'),
230 //new xmlrpcval('false', 'boolean')
235 $v = $this->send($f);
237 $sz = $v->arraysize();
239 for ($i = 0; $i < $sz; $i++) {
240 $b = $v->arraymem($i);
241 if ($b->scalarval()) {
247 $this->assertEquals($answer, $got);
251 public function testBase64()
253 $sendstring = 'Mary had a little lamb,
254 Whose fleece was white as snow,
255 And everywhere that Mary went
256 the lamb was sure to go.
258 Mary had a little lamb
259 She tied it to a pylon
260 Ten thousand volts went down its back
261 And turned it into nylon';
262 $f = new xmlrpcmsg('examples.decode64', array(
263 new xmlrpcval($sendstring, 'base64'),
265 $v = $this->send($f);
267 if (strlen($sendstring) == strlen($v->scalarval())) {
268 $this->assertEquals($sendstring, $v->scalarval());
270 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
275 public function testDateTime()
278 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
279 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
280 $this->assertEquals($t1->serialize(), $t2->serialize());
281 if (class_exists('DateTime')) {
282 $datetime = new DateTime();
283 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
284 if (is_callable(array($datetime, 'setTimestamp'))) {
285 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
286 $this->assertEquals($t1->serialize(), $t3->serialize());
291 public function testCountEntities()
293 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
294 $f = new xmlrpcmsg('validator1.countTheEntities', array(
295 new xmlrpcval($sendstring, 'string'),
297 $v = $this->send($f);
301 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
302 while (list(, $val) = each($expect_array)) {
303 $b = $v->structmem($val);
304 $got .= $b->me['int'];
306 $this->assertEquals($expected, $got);
310 public function _multicall_msg($method, $params)
312 $struct['methodName'] = new xmlrpcval($method, 'string');
313 $struct['params'] = new xmlrpcval($params, 'array');
315 return new xmlrpcval($struct, 'struct');
318 public function testServerMulticall()
320 // We manually construct a system.multicall() call to ensure
321 // that the server supports it.
323 // NB: This test will NOT pass if server does not support system.multicall.
325 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
326 $good1 = $this->_multicall_msg(
328 array(php_xmlrpc_encode('system.listMethods')));
329 $bad = $this->_multicall_msg(
331 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
332 $recursive = $this->_multicall_msg(
334 array(new xmlrpcval(array(), 'array')));
335 $good2 = $this->_multicall_msg(
336 'system.methodSignature',
337 array(php_xmlrpc_encode('system.listMethods')));
338 $arg = new xmlrpcval(
339 array($good1, $bad, $recursive, $good2),
343 $f = new xmlrpcmsg('system.multicall', array($arg));
344 $v = $this->send($f);
346 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
347 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
349 $r1 = $v->arraymem(0);
351 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
352 "did not get array of size 1 from good1"
355 $r2 = $v->arraymem(1);
357 $r2->kindOf() == 'struct',
361 $r3 = $v->arraymem(2);
363 $r3->kindOf() == 'struct',
364 "recursive system.multicall did not fail"
367 $r4 = $v->arraymem(3);
369 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
370 "did not get array of size 1 from good2"
375 public function testClientMulticall1()
377 // NB: This test will NOT pass if server does not support system.multicall.
379 $this->client->no_multicall = false;
381 $good1 = new xmlrpcmsg('system.methodHelp',
382 array(php_xmlrpc_encode('system.listMethods')));
383 $bad = new xmlrpcmsg('test.nosuch',
384 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
385 $recursive = new xmlrpcmsg('system.multicall',
386 array(new xmlrpcval(array(), 'array')));
387 $good2 = new xmlrpcmsg('system.methodSignature',
388 array(php_xmlrpc_encode('system.listMethods'))
391 $r = $this->send(array($good1, $bad, $recursive, $good2));
393 $this->assertTrue(count($r) == 4, "wrong number of return values");
396 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
397 if (!$r[0]->faultCode()) {
398 $val = $r[0]->value();
400 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
401 "good1 did not return string"
404 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
405 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
406 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
407 if (!$r[3]->faultCode()) {
408 $val = $r[3]->value();
409 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
411 // This is the only assert in this test which should fail
412 // if the test server does not support system.multicall.
413 $this->assertTrue($this->client->no_multicall == false,
414 "server does not support system.multicall"
418 public function testClientMulticall2()
420 // NB: This test will NOT pass if server does not support system.multicall.
422 $this->client->no_multicall = true;
424 $good1 = new xmlrpcmsg('system.methodHelp',
425 array(php_xmlrpc_encode('system.listMethods')));
426 $bad = new xmlrpcmsg('test.nosuch',
427 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
428 $recursive = new xmlrpcmsg('system.multicall',
429 array(new xmlrpcval(array(), 'array')));
430 $good2 = new xmlrpcmsg('system.methodSignature',
431 array(php_xmlrpc_encode('system.listMethods'))
434 $r = $this->send(array($good1, $bad, $recursive, $good2));
436 $this->assertTrue(count($r) == 4, "wrong number of return values");
439 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
440 if (!$r[0]->faultCode()) {
441 $val = $r[0]->value();
443 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
444 "good1 did not return string");
446 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
447 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
448 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
449 if (!$r[3]->faultCode()) {
450 $val = $r[3]->value();
451 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
455 public function testClientMulticall3()
457 // NB: This test will NOT pass if server does not support system.multicall.
459 $this->client->return_type = 'phpvals';
460 $this->client->no_multicall = false;
462 $good1 = new xmlrpcmsg('system.methodHelp',
463 array(php_xmlrpc_encode('system.listMethods')));
464 $bad = new xmlrpcmsg('test.nosuch',
465 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
466 $recursive = new xmlrpcmsg('system.multicall',
467 array(new xmlrpcval(array(), 'array')));
468 $good2 = new xmlrpcmsg('system.methodSignature',
469 array(php_xmlrpc_encode('system.listMethods'))
472 $r = $this->send(array($good1, $bad, $recursive, $good2));
474 $this->assertTrue(count($r) == 4, "wrong number of return values");
476 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
477 if (!$r[0]->faultCode()) {
478 $val = $r[0]->value();
480 is_string($val), "good1 did not return string");
482 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
483 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
484 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
485 if (!$r[3]->faultCode()) {
486 $val = $r[3]->value();
487 $this->assertTrue(is_array($val), "good2 did not return array");
489 $this->client->return_type = 'xmlrpcvals';
492 public function testCatchWarnings()
494 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
495 new xmlrpcval('whatever', 'string'),
497 $v = $this->send($f);
499 $this->assertEquals($v->scalarval(), true);
503 public function testCatchExceptions()
505 $f = new xmlrpcmsg('examples.raiseException', array(
506 new xmlrpcval('whatever', 'string'),
508 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
509 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
510 $v = $this->send($f, 1);
511 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
512 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
513 // as php will generate an http status code of either 200 or 500...
514 $v = $this->send($f, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
517 public function testZeroParams()
519 $f = new xmlrpcmsg('system.listMethods');
520 $v = $this->send($f);
523 public function testCodeInjectionServerSide()
525 $f = new xmlrpcmsg('system.MethodHelp');
526 $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>";
527 $v = $this->send($f);
528 //$v = $r->faultCode();
530 $this->assertEquals(0, $v->structsize());
534 public function testAutoRegisteredFunction()
536 $f = new xmlrpcmsg('examples.php.getStateName', array(
537 new xmlrpcval(23, 'int'),
539 $v = $this->send($f);
541 $this->assertEquals('Michigan', $v->scalarval());
543 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
547 public function testAutoRegisteredClass()
549 $f = new xmlrpcmsg('examples.php2.getStateName', array(
550 new xmlrpcval(23, 'int'),
552 $v = $this->send($f);
554 $this->assertEquals('Michigan', $v->scalarval());
555 $f = new xmlrpcmsg('examples.php3.getStateName', array(
556 new xmlrpcval(23, 'int'),
558 $v = $this->send($f);
560 $this->assertEquals('Michigan', $v->scalarval());
563 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
567 public function testAutoRegisteredMethod()
569 // make a 'deep client copy' as the original one might have many properties set
570 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
572 $this->fail('Registration of examples.getStateName failed');
575 // work around bug in current version of phpunit
577 $v = var_export($v, true);
579 $this->assertEquals('Michigan', $v);
583 public function testGetCookies()
585 // let server set to us some cookies we tell it
588 'c2' => array('value' => 'c2'),
589 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
590 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
591 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
593 $cookiesval = php_xmlrpc_encode($cookies);
594 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
595 $r = $this->send($f, 0, true);
598 $this->assertEquals(1, $v->scalarval());
599 // now check if we decoded the cookies as we had set them
600 $rcookies = $r->cookies();
601 // remove extra cookies which might have been set by proxies
602 foreach ($rcookies as $c => $v) {
603 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
604 unset($rcookies[$c]);
606 // Seems like we get this when using php-fpm and php 5.5+ ...
607 if (isset($rcookies[$c]['Max-Age'])) {
608 unset($rcookies[$c]['Max-Age']);
611 foreach ($cookies as $c => $v) {
612 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
613 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
614 if (isset($v['expires'])) {
615 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
616 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
618 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
623 $this->assertEquals($cookies, $rcookies);
627 public function testSetCookies()
629 // let server set to us some cookies we tell it
634 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
636 $f = new xmlrpcmsg('examples.getcookies', array());
637 foreach ($cookies as $cookie => $val) {
638 $this->client->setCookie($cookie, $val);
639 $cookies[$cookie] = (string)$cookies[$cookie];
641 $r = $this->client->send($f, $this->timeout, $this->method);
642 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
643 if (!$r->faultCode()) {
645 $v = php_xmlrpc_decode($v);
647 // take care for the extra cookie used for coverage collection
648 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
649 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
652 // on IIS and Apache getallheaders returns something slightly different...
653 $this->assertEquals($v, $cookies);
657 public function testSendTwiceSameMsg()
659 $f = new xmlrpcmsg('examples.stringecho', array(
660 new xmlrpcval('hello world', 'string'),
662 $v1 = $this->send($f);
663 $v2 = $this->send($f);
664 //$v = $r->faultCode();
666 $this->assertEquals($v2, $v1);