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 public $client = null;
12 public $method = 'http';
14 public $request_compression = null;
15 public $accepted_compression = '';
16 public $args = array();
18 protected static $failed_tests = array();
20 public static function fail($message = '')
22 // save in global var that this particular test has failed
23 // (but only if not called from subclass objects / multitests)
24 if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
26 $trace = debug_backtrace();
27 for ($i = 0; $i < count($trace); $i++) {
28 if (strpos($trace[$i]['function'], 'test') === 0) {
29 self::$failed_tests[$trace[$i]['function']] = true;
35 parent::fail($message);
39 * @todo be smarter with setup, do not use global variables anymore
41 public function setUp()
43 $this->args = argParser::getArgs();
45 $server = explode(':', $this->args['LOCALSERVER']);
46 if (count($server) > 1) {
47 $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
49 $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
51 if ($this->args['DEBUG']) {
52 $this->client->setDebug($this->args['DEBUG']);
54 $this->client->request_compression = $this->request_compression;
55 $this->client->accepted_compression = $this->accepted_compression;
58 public function send($msg, $errrorcode = 0, $return_response = false)
60 $r = $this->client->send($msg, $this->timeout, $this->method);
61 // for multicall, return directly array of responses
65 if (is_array($errrorcode)) {
66 $this->assertContains($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
69 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
71 if (!$r->faultCode()) {
72 if ($return_response) {
82 public function testString()
84 $sendstring = "here are 3 \"entities\": < > & " .
85 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
86 " - isn't that great? \\\"hackery\\\" at it's best " .
87 " also don't want to miss out on \$item[0]. " .
88 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
89 "a simple CR here" . chr(13) .
90 "a simple LF here" . chr(10) .
91 "and then LFCR" . chr(10) . chr(13) .
92 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
93 $f = new xmlrpcmsg('examples.stringecho', array(
94 new xmlrpcval($sendstring, 'string'),
98 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
99 // so we relax our tests...
100 $l1 = strlen($sendstring);
101 $l2 = strlen($v->scalarval());
103 $this->assertEquals($sendstring, $v->scalarval());
105 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
110 public function testAddingDoubles()
112 // note that rounding errors mean we
113 // keep precision to sensible levels here ;-)
116 $f = new xmlrpcmsg('examples.addtwodouble', array(
117 new xmlrpcval($a, 'double'),
118 new xmlrpcval($b, 'double'),
120 $v = $this->send($f);
122 $this->assertEquals($a + $b, $v->scalarval());
126 public function testAdding()
128 $f = new xmlrpcmsg('examples.addtwo', array(
129 new xmlrpcval(12, 'int'),
130 new xmlrpcval(-23, 'int'),
132 $v = $this->send($f);
134 $this->assertEquals(12 - 23, $v->scalarval());
138 public function testInvalidNumber()
140 $f = new xmlrpcmsg('examples.addtwo', array(
141 new xmlrpcval('fred', 'int'),
142 new xmlrpcval("\"; exec('ls')", 'int'),
144 $v = $this->send($f);
145 /// @todo a fault condition should be generated here
146 /// by the server, which we pick up on
148 $this->assertEquals(0, $v->scalarval());
152 public function testBoolean()
154 $f = new xmlrpcmsg('examples.invertBooleans', array(
156 new xmlrpcval(true, 'boolean'),
157 new xmlrpcval(false, 'boolean'),
158 new xmlrpcval(1, 'boolean'),
159 new xmlrpcval(0, 'boolean'),
160 //new xmlrpcval('true', 'boolean'),
161 //new xmlrpcval('false', 'boolean')
166 $v = $this->send($f);
168 $sz = $v->arraysize();
170 for ($i = 0; $i < $sz; $i++) {
171 $b = $v->arraymem($i);
172 if ($b->scalarval()) {
178 $this->assertEquals($answer, $got);
182 public function testBase64()
184 $sendstring = 'Mary had a little lamb,
185 Whose fleece was white as snow,
186 And everywhere that Mary went
187 the lamb was sure to go.
189 Mary had a little lamb
190 She tied it to a pylon
191 Ten thousand volts went down its back
192 And turned it into nylon';
193 $f = new xmlrpcmsg('examples.decode64', array(
194 new xmlrpcval($sendstring, 'base64'),
196 $v = $this->send($f);
198 if (strlen($sendstring) == strlen($v->scalarval())) {
199 $this->assertEquals($sendstring, $v->scalarval());
201 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
206 public function testDateTime()
209 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
210 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
211 $this->assertEquals($t1->serialize(), $t2->serialize());
212 if (class_exists('DateTime')) {
213 $datetime = new DateTime();
214 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
215 if (is_callable(array($datetime, 'setTimestamp'))) {
216 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
217 $this->assertEquals($t1->serialize(), $t3->serialize());
222 public function testCountEntities()
224 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
225 $f = new xmlrpcmsg('validator1.countTheEntities', array(
226 new xmlrpcval($sendstring, 'string'),
228 $v = $this->send($f);
232 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
233 while (list(, $val) = each($expect_array)) {
234 $b = $v->structmem($val);
235 $got .= $b->me['int'];
237 $this->assertEquals($expected, $got);
241 public function _multicall_msg($method, $params)
243 $struct['methodName'] = new xmlrpcval($method, 'string');
244 $struct['params'] = new xmlrpcval($params, 'array');
246 return new xmlrpcval($struct, 'struct');
249 public function testServerMulticall()
251 // We manually construct a system.multicall() call to ensure
252 // that the server supports it.
254 // NB: This test will NOT pass if server does not support system.multicall.
256 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
257 $good1 = $this->_multicall_msg(
259 array(php_xmlrpc_encode('system.listMethods')));
260 $bad = $this->_multicall_msg(
262 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
263 $recursive = $this->_multicall_msg(
265 array(new xmlrpcval(array(), 'array')));
266 $good2 = $this->_multicall_msg(
267 'system.methodSignature',
268 array(php_xmlrpc_encode('system.listMethods')));
269 $arg = new xmlrpcval(
270 array($good1, $bad, $recursive, $good2),
274 $f = new xmlrpcmsg('system.multicall', array($arg));
275 $v = $this->send($f);
277 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
278 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
280 $r1 = $v->arraymem(0);
282 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
283 "did not get array of size 1 from good1"
286 $r2 = $v->arraymem(1);
288 $r2->kindOf() == 'struct',
292 $r3 = $v->arraymem(2);
294 $r3->kindOf() == 'struct',
295 "recursive system.multicall did not fail"
298 $r4 = $v->arraymem(3);
300 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
301 "did not get array of size 1 from good2"
306 public function testClientMulticall1()
308 // NB: This test will NOT pass if server does not support system.multicall.
310 $this->client->no_multicall = false;
312 $good1 = new xmlrpcmsg('system.methodHelp',
313 array(php_xmlrpc_encode('system.listMethods')));
314 $bad = new xmlrpcmsg('test.nosuch',
315 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
316 $recursive = new xmlrpcmsg('system.multicall',
317 array(new xmlrpcval(array(), 'array')));
318 $good2 = new xmlrpcmsg('system.methodSignature',
319 array(php_xmlrpc_encode('system.listMethods'))
322 $r = $this->send(array($good1, $bad, $recursive, $good2));
324 $this->assertTrue(count($r) == 4, "wrong number of return values");
327 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
328 if (!$r[0]->faultCode()) {
329 $val = $r[0]->value();
331 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
332 "good1 did not return string"
335 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
336 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
337 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
338 if (!$r[3]->faultCode()) {
339 $val = $r[3]->value();
340 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
342 // This is the only assert in this test which should fail
343 // if the test server does not support system.multicall.
344 $this->assertTrue($this->client->no_multicall == false,
345 "server does not support system.multicall"
349 public function testClientMulticall2()
351 // NB: This test will NOT pass if server does not support system.multicall.
353 $this->client->no_multicall = true;
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");
377 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
378 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
379 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
380 if (!$r[3]->faultCode()) {
381 $val = $r[3]->value();
382 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
386 public function testClientMulticall3()
388 // NB: This test will NOT pass if server does not support system.multicall.
390 $this->client->return_type = 'phpvals';
391 $this->client->no_multicall = false;
393 $good1 = new xmlrpcmsg('system.methodHelp',
394 array(php_xmlrpc_encode('system.listMethods')));
395 $bad = new xmlrpcmsg('test.nosuch',
396 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
397 $recursive = new xmlrpcmsg('system.multicall',
398 array(new xmlrpcval(array(), 'array')));
399 $good2 = new xmlrpcmsg('system.methodSignature',
400 array(php_xmlrpc_encode('system.listMethods'))
403 $r = $this->send(array($good1, $bad, $recursive, $good2));
405 $this->assertTrue(count($r) == 4, "wrong number of return values");
407 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
408 if (!$r[0]->faultCode()) {
409 $val = $r[0]->value();
411 is_string($val), "good1 did not return string");
413 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
414 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
415 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
416 if (!$r[3]->faultCode()) {
417 $val = $r[3]->value();
418 $this->assertTrue(is_array($val), "good2 did not return array");
420 $this->client->return_type = 'xmlrpcvals';
423 public function testCatchWarnings()
425 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
426 new xmlrpcval('whatever', 'string'),
428 $v = $this->send($f);
430 $this->assertEquals($v->scalarval(), true);
434 public function testCatchExceptions()
436 $f = new xmlrpcmsg('examples.raiseException', array(
437 new xmlrpcval('whatever', 'string'),
439 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
440 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
441 $v = $this->send($f, 1);
442 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
443 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
444 // as php will generate an http status code of either 200 or 500...
445 $v = $this->send($f, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
448 public function testZeroParams()
450 $f = new xmlrpcmsg('system.listMethods');
451 $v = $this->send($f);
454 public function testCodeInjectionServerSide()
456 $f = new xmlrpcmsg('system.MethodHelp');
457 $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>";
458 $v = $this->send($f);
459 //$v = $r->faultCode();
461 $this->assertEquals(0, $v->structsize());
465 public function testAutoRegisteredFunction()
467 $f = new xmlrpcmsg('examples.php.getStateName', array(
468 new xmlrpcval(23, 'int'),
470 $v = $this->send($f);
472 $this->assertEquals('Michigan', $v->scalarval());
474 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
478 public function testAutoRegisteredClass()
480 $f = new xmlrpcmsg('examples.php2.getStateName', array(
481 new xmlrpcval(23, 'int'),
483 $v = $this->send($f);
485 $this->assertEquals('Michigan', $v->scalarval());
486 $f = new xmlrpcmsg('examples.php3.getStateName', array(
487 new xmlrpcval(23, 'int'),
489 $v = $this->send($f);
491 $this->assertEquals('Michigan', $v->scalarval());
494 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
498 public function testAutoRegisteredMethod()
500 // make a 'deep client copy' as the original one might have many properties set
501 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
503 $this->fail('Registration of examples.getStateName failed');
506 // work around bug in current version of phpunit
508 $v = var_export($v, true);
510 $this->assertEquals('Michigan', $v);
514 public function testGetCookies()
516 // let server set to us some cookies we tell it
519 'c2' => array('value' => 'c2'),
520 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
521 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
522 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
524 $cookiesval = php_xmlrpc_encode($cookies);
525 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
526 $r = $this->send($f, 0, true);
529 $this->assertEquals(1, $v->scalarval());
530 // now check if we decoded the cookies as we had set them
531 $rcookies = $r->cookies();
532 // remove extra cookies which might have been set by proxies
533 foreach ($rcookies as $c => $v) {
534 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
535 unset($rcookies[$c]);
537 // Seems like we get this when using php-fpm and php 5.5+ ...
538 if (isset($rcookies[$c]['Max-Age'])) {
539 unset($rcookies[$c]['Max-Age']);
542 foreach ($cookies as $c => $v) {
543 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
544 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
545 if (isset($v['expires'])) {
546 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
547 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
549 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
554 $this->assertEquals($cookies, $rcookies);
558 public function testSetCookies()
560 // let server set to us some cookies we tell it
565 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
567 $f = new xmlrpcmsg('examples.getcookies', array());
568 foreach ($cookies as $cookie => $val) {
569 $this->client->setCookie($cookie, $val);
570 $cookies[$cookie] = (string)$cookies[$cookie];
572 $r = $this->client->send($f, $this->timeout, $this->method);
573 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
574 if (!$r->faultCode()) {
576 $v = php_xmlrpc_decode($v);
577 // on IIS and Apache getallheaders returns something slightly different...
578 $this->assertEquals($v, $cookies);
582 public function testSendTwiceSameMsg()
584 $f = new xmlrpcmsg('examples.stringecho', array(
585 new xmlrpcval('hello world', 'string'),
587 $v1 = $this->send($f);
588 $v2 = $this->send($f);
589 //$v = $r->faultCode();
591 $this->assertEquals($v2, $v1);