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 testAddingDoubles()
155 // note that rounding errors mean we
156 // keep precision to sensible levels here ;-)
159 $f = new xmlrpcmsg('examples.addtwodouble', array(
160 new xmlrpcval($a, 'double'),
161 new xmlrpcval($b, 'double'),
163 $v = $this->send($f);
165 $this->assertEquals($a + $b, $v->scalarval());
169 public function testAdding()
171 $f = new xmlrpcmsg('examples.addtwo', array(
172 new xmlrpcval(12, 'int'),
173 new xmlrpcval(-23, 'int'),
175 $v = $this->send($f);
177 $this->assertEquals(12 - 23, $v->scalarval());
181 public function testInvalidNumber()
183 $f = new xmlrpcmsg('examples.addtwo', array(
184 new xmlrpcval('fred', 'int'),
185 new xmlrpcval("\"; exec('ls')", 'int'),
187 $v = $this->send($f);
188 /// @todo a fault condition should be generated here
189 /// by the server, which we pick up on
191 $this->assertEquals(0, $v->scalarval());
195 public function testBoolean()
197 $f = new xmlrpcmsg('examples.invertBooleans', 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')
209 $v = $this->send($f);
211 $sz = $v->arraysize();
213 for ($i = 0; $i < $sz; $i++) {
214 $b = $v->arraymem($i);
215 if ($b->scalarval()) {
221 $this->assertEquals($answer, $got);
225 public function testBase64()
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.
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'),
239 $v = $this->send($f);
241 if (strlen($sendstring) == strlen($v->scalarval())) {
242 $this->assertEquals($sendstring, $v->scalarval());
244 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
249 public function testDateTime()
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());
265 public function testCountEntities()
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'),
271 $v = $this->send($f);
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'];
280 $this->assertEquals($expected, $got);
284 public function _multicall_msg($method, $params)
286 $struct['methodName'] = new xmlrpcval($method, 'string');
287 $struct['params'] = new xmlrpcval($params, 'array');
289 return new xmlrpcval($struct, 'struct');
292 public function testServerMulticall()
294 // We manually construct a system.multicall() call to ensure
295 // that the server supports it.
297 // NB: This test will NOT pass if server does not support system.multicall.
299 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
300 $good1 = $this->_multicall_msg(
302 array(php_xmlrpc_encode('system.listMethods')));
303 $bad = $this->_multicall_msg(
305 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
306 $recursive = $this->_multicall_msg(
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),
317 $f = new xmlrpcmsg('system.multicall', array($arg));
318 $v = $this->send($f);
320 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
321 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
323 $r1 = $v->arraymem(0);
325 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
326 "did not get array of size 1 from good1"
329 $r2 = $v->arraymem(1);
331 $r2->kindOf() == 'struct',
335 $r3 = $v->arraymem(2);
337 $r3->kindOf() == 'struct',
338 "recursive system.multicall did not fail"
341 $r4 = $v->arraymem(3);
343 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
344 "did not get array of size 1 from good2"
349 public function testClientMulticall1()
351 // NB: This test will NOT pass if server does not support system.multicall.
353 $this->client->no_multicall = false;
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'))
365 $r = $this->send(array($good1, $bad, $recursive, $good2));
367 $this->assertTrue(count($r) == 4, "wrong number of return values");
370 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
371 if (!$r[0]->faultCode()) {
372 $val = $r[0]->value();
374 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
375 "good1 did not return string"
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");
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"
392 public function testClientMulticall2()
394 // NB: This test will NOT pass if server does not support system.multicall.
396 $this->client->no_multicall = true;
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'))
408 $r = $this->send(array($good1, $bad, $recursive, $good2));
410 $this->assertTrue(count($r) == 4, "wrong number of return values");
413 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
414 if (!$r[0]->faultCode()) {
415 $val = $r[0]->value();
417 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
418 "good1 did not return string");
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");
429 public function testClientMulticall3()
431 // NB: This test will NOT pass if server does not support system.multicall.
433 $this->client->return_type = 'phpvals';
434 $this->client->no_multicall = false;
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'))
446 $r = $this->send(array($good1, $bad, $recursive, $good2));
448 $this->assertTrue(count($r) == 4, "wrong number of return values");
450 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
451 if (!$r[0]->faultCode()) {
452 $val = $r[0]->value();
454 is_string($val), "good1 did not return string");
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");
463 $this->client->return_type = 'xmlrpcvals';
466 public function testCatchWarnings()
468 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
469 new xmlrpcval('whatever', 'string'),
471 $v = $this->send($f);
473 $this->assertEquals($v->scalarval(), true);
477 public function testCatchExceptions()
479 $f = new xmlrpcmsg('examples.raiseException', array(
480 new xmlrpcval('whatever', 'string'),
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']));
491 public function testZeroParams()
493 $f = new xmlrpcmsg('system.listMethods');
494 $v = $this->send($f);
497 public function testCodeInjectionServerSide()
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();
504 $this->assertEquals(0, $v->structsize());
508 public function testAutoRegisteredFunction()
510 $f = new xmlrpcmsg('examples.php.getStateName', array(
511 new xmlrpcval(23, 'int'),
513 $v = $this->send($f);
515 $this->assertEquals('Michigan', $v->scalarval());
517 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
521 public function testAutoRegisteredClass()
523 $f = new xmlrpcmsg('examples.php2.getStateName', array(
524 new xmlrpcval(23, 'int'),
526 $v = $this->send($f);
528 $this->assertEquals('Michigan', $v->scalarval());
529 $f = new xmlrpcmsg('examples.php3.getStateName', array(
530 new xmlrpcval(23, 'int'),
532 $v = $this->send($f);
534 $this->assertEquals('Michigan', $v->scalarval());
537 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
541 public function testAutoRegisteredMethod()
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));
546 $this->fail('Registration of examples.getStateName failed');
549 // work around bug in current version of phpunit
551 $v = var_export($v, true);
553 $this->assertEquals('Michigan', $v);
557 public function testGetCookies()
559 // let server set to us some cookies we tell it
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'),
567 $cookiesval = php_xmlrpc_encode($cookies);
568 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
569 $r = $this->send($f, 0, true);
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]);
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']);
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']);
592 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
597 $this->assertEquals($cookies, $rcookies);
601 public function testSetCookies()
603 // let server set to us some cookies we tell it
608 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
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];
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()) {
619 $v = php_xmlrpc_decode($v);
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']);
626 // on IIS and Apache getallheaders returns something slightly different...
627 $this->assertEquals($v, $cookies);
631 public function testSendTwiceSameMsg()
633 $f = new xmlrpcmsg('examples.stringecho', array(
634 new xmlrpcval('hello world', 'string'),
636 $v1 = $this->send($f);
637 $v2 = $this->send($f);
638 //$v = $r->faultCode();
640 $this->assertEquals($v2, $v1);