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 public $client = null;
11 public $method = 'http';
13 public $request_compression = null;
14 public $accepted_compression = '';
15 public $args = array();
17 public static function fail($message = '')
19 // save in global var that this particular test has failed
20 // (but only if not called from subclass objects / multitests)
21 if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
23 $trace = debug_backtrace();
24 for ($i = 0; $i < count($trace); $i++) {
25 if (strpos($trace[$i]['function'], 'test') === 0) {
26 $failed_tests[$trace[$i]['function']] = true;
32 parent::fail($message);
36 * @todo be smarter with setup, do not use global variables anymore
38 public function setUp()
40 $this->args = argParser::getArgs();
42 $server = explode(':', $this->args['LOCALSERVER']);
43 if (count($server) > 1) {
44 $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
46 $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
48 if ($this->args['DEBUG']) {
49 $this->client->setDebug($this->args['DEBUG']);
51 $this->client->request_compression = $this->request_compression;
52 $this->client->accepted_compression = $this->accepted_compression;
55 public function send($msg, $errrorcode = 0, $return_response = false)
57 $r = $this->client->send($msg, $this->timeout, $this->method);
58 // for multicall, return directly array of responses
62 if (is_array($errrorcode)) {
63 $this->assertContains($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
66 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
68 if (!$r->faultCode()) {
69 if ($return_response) {
79 public function testString()
81 $sendstring = "here are 3 \"entities\": < > & " .
82 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
83 " - isn't that great? \\\"hackery\\\" at it's best " .
84 " also don't want to miss out on \$item[0]. " .
85 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
86 "a simple CR here" . chr(13) .
87 "a simple LF here" . chr(10) .
88 "and then LFCR" . chr(10) . chr(13) .
89 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
90 $f = new xmlrpcmsg('examples.stringecho', array(
91 new xmlrpcval($sendstring, 'string'),
95 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
96 // so we relax our tests...
97 $l1 = strlen($sendstring);
98 $l2 = strlen($v->scalarval());
100 $this->assertEquals($sendstring, $v->scalarval());
102 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
107 public function testAddingDoubles()
109 // note that rounding errors mean we
110 // keep precision to sensible levels here ;-)
113 $f = new xmlrpcmsg('examples.addtwodouble', array(
114 new xmlrpcval($a, 'double'),
115 new xmlrpcval($b, 'double'),
117 $v = $this->send($f);
119 $this->assertEquals($a + $b, $v->scalarval());
123 public function testAdding()
125 $f = new xmlrpcmsg('examples.addtwo', array(
126 new xmlrpcval(12, 'int'),
127 new xmlrpcval(-23, 'int'),
129 $v = $this->send($f);
131 $this->assertEquals(12 - 23, $v->scalarval());
135 public function testInvalidNumber()
137 $f = new xmlrpcmsg('examples.addtwo', array(
138 new xmlrpcval('fred', 'int'),
139 new xmlrpcval("\"; exec('ls')", 'int'),
141 $v = $this->send($f);
142 /// @todo a fault condition should be generated here
143 /// by the server, which we pick up on
145 $this->assertEquals(0, $v->scalarval());
149 public function testBoolean()
151 $f = new xmlrpcmsg('examples.invertBooleans', array(
153 new xmlrpcval(true, 'boolean'),
154 new xmlrpcval(false, 'boolean'),
155 new xmlrpcval(1, 'boolean'),
156 new xmlrpcval(0, 'boolean'),
157 //new xmlrpcval('true', 'boolean'),
158 //new xmlrpcval('false', 'boolean')
163 $v = $this->send($f);
165 $sz = $v->arraysize();
167 for ($i = 0; $i < $sz; $i++) {
168 $b = $v->arraymem($i);
169 if ($b->scalarval()) {
175 $this->assertEquals($answer, $got);
179 public function testBase64()
181 $sendstring = 'Mary had a little lamb,
182 Whose fleece was white as snow,
183 And everywhere that Mary went
184 the lamb was sure to go.
186 Mary had a little lamb
187 She tied it to a pylon
188 Ten thousand volts went down its back
189 And turned it into nylon';
190 $f = new xmlrpcmsg('examples.decode64', array(
191 new xmlrpcval($sendstring, 'base64'),
193 $v = $this->send($f);
195 if (strlen($sendstring) == strlen($v->scalarval())) {
196 $this->assertEquals($sendstring, $v->scalarval());
198 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
203 public function testDateTime()
206 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
207 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
208 $this->assertEquals($t1->serialize(), $t2->serialize());
209 if (class_exists('DateTime')) {
210 $datetime = new DateTime();
211 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
212 if (is_callable(array($datetime, 'setTimestamp'))) {
213 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
214 $this->assertEquals($t1->serialize(), $t3->serialize());
219 public function testCountEntities()
221 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
222 $f = new xmlrpcmsg('validator1.countTheEntities', array(
223 new xmlrpcval($sendstring, 'string'),
225 $v = $this->send($f);
229 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
230 while (list(, $val) = each($expect_array)) {
231 $b = $v->structmem($val);
232 $got .= $b->me['int'];
234 $this->assertEquals($expected, $got);
238 public function _multicall_msg($method, $params)
240 $struct['methodName'] = new xmlrpcval($method, 'string');
241 $struct['params'] = new xmlrpcval($params, 'array');
243 return new xmlrpcval($struct, 'struct');
246 public function testServerMulticall()
248 // We manually construct a system.multicall() call to ensure
249 // that the server supports it.
251 // NB: This test will NOT pass if server does not support system.multicall.
253 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
254 $good1 = $this->_multicall_msg(
256 array(php_xmlrpc_encode('system.listMethods')));
257 $bad = $this->_multicall_msg(
259 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
260 $recursive = $this->_multicall_msg(
262 array(new xmlrpcval(array(), 'array')));
263 $good2 = $this->_multicall_msg(
264 'system.methodSignature',
265 array(php_xmlrpc_encode('system.listMethods')));
266 $arg = new xmlrpcval(
267 array($good1, $bad, $recursive, $good2),
271 $f = new xmlrpcmsg('system.multicall', array($arg));
272 $v = $this->send($f);
274 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
275 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
277 $r1 = $v->arraymem(0);
279 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
280 "did not get array of size 1 from good1"
283 $r2 = $v->arraymem(1);
285 $r2->kindOf() == 'struct',
289 $r3 = $v->arraymem(2);
291 $r3->kindOf() == 'struct',
292 "recursive system.multicall did not fail"
295 $r4 = $v->arraymem(3);
297 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
298 "did not get array of size 1 from good2"
303 public function testClientMulticall1()
305 // NB: This test will NOT pass if server does not support system.multicall.
307 $this->client->no_multicall = false;
309 $good1 = new xmlrpcmsg('system.methodHelp',
310 array(php_xmlrpc_encode('system.listMethods')));
311 $bad = new xmlrpcmsg('test.nosuch',
312 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
313 $recursive = new xmlrpcmsg('system.multicall',
314 array(new xmlrpcval(array(), 'array')));
315 $good2 = new xmlrpcmsg('system.methodSignature',
316 array(php_xmlrpc_encode('system.listMethods'))
319 $r = $this->send(array($good1, $bad, $recursive, $good2));
321 $this->assertTrue(count($r) == 4, "wrong number of return values");
324 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
325 if (!$r[0]->faultCode()) {
326 $val = $r[0]->value();
328 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
329 "good1 did not return string"
332 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
333 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
334 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
335 if (!$r[3]->faultCode()) {
336 $val = $r[3]->value();
337 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
339 // This is the only assert in this test which should fail
340 // if the test server does not support system.multicall.
341 $this->assertTrue($this->client->no_multicall == false,
342 "server does not support system.multicall"
346 public function testClientMulticall2()
348 // NB: This test will NOT pass if server does not support system.multicall.
350 $this->client->no_multicall = true;
352 $good1 = new xmlrpcmsg('system.methodHelp',
353 array(php_xmlrpc_encode('system.listMethods')));
354 $bad = new xmlrpcmsg('test.nosuch',
355 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
356 $recursive = new xmlrpcmsg('system.multicall',
357 array(new xmlrpcval(array(), 'array')));
358 $good2 = new xmlrpcmsg('system.methodSignature',
359 array(php_xmlrpc_encode('system.listMethods'))
362 $r = $this->send(array($good1, $bad, $recursive, $good2));
364 $this->assertTrue(count($r) == 4, "wrong number of return values");
367 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
368 if (!$r[0]->faultCode()) {
369 $val = $r[0]->value();
371 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
372 "good1 did not return string");
374 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
375 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
376 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
377 if (!$r[3]->faultCode()) {
378 $val = $r[3]->value();
379 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
383 public function testClientMulticall3()
385 // NB: This test will NOT pass if server does not support system.multicall.
387 $this->client->return_type = 'phpvals';
388 $this->client->no_multicall = false;
390 $good1 = new xmlrpcmsg('system.methodHelp',
391 array(php_xmlrpc_encode('system.listMethods')));
392 $bad = new xmlrpcmsg('test.nosuch',
393 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
394 $recursive = new xmlrpcmsg('system.multicall',
395 array(new xmlrpcval(array(), 'array')));
396 $good2 = new xmlrpcmsg('system.methodSignature',
397 array(php_xmlrpc_encode('system.listMethods'))
400 $r = $this->send(array($good1, $bad, $recursive, $good2));
402 $this->assertTrue(count($r) == 4, "wrong number of return values");
404 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
405 if (!$r[0]->faultCode()) {
406 $val = $r[0]->value();
408 is_string($val), "good1 did not return string");
410 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
411 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
412 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
413 if (!$r[3]->faultCode()) {
414 $val = $r[3]->value();
415 $this->assertTrue(is_array($val), "good2 did not return array");
417 $this->client->return_type = 'xmlrpcvals';
420 public function testCatchWarnings()
422 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
423 new xmlrpcval('whatever', 'string'),
425 $v = $this->send($f);
427 $this->assertEquals($v->scalarval(), true);
431 public function testCatchExceptions()
433 $f = new xmlrpcmsg('examples.raiseException', array(
434 new xmlrpcval('whatever', 'string'),
436 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
437 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
438 $v = $this->send($f, 1);
439 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
440 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
441 // as php will generate an http status code of either 200 or 500...
442 $v = $this->send($f, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
445 public function testZeroParams()
447 $f = new xmlrpcmsg('system.listMethods');
448 $v = $this->send($f);
451 public function testCodeInjectionServerSide()
453 $f = new xmlrpcmsg('system.MethodHelp');
454 $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>";
455 $v = $this->send($f);
456 //$v = $r->faultCode();
458 $this->assertEquals(0, $v->structsize());
462 public function testAutoRegisteredFunction()
464 $f = new xmlrpcmsg('examples.php.getStateName', array(
465 new xmlrpcval(23, 'int'),
467 $v = $this->send($f);
469 $this->assertEquals('Michigan', $v->scalarval());
471 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
475 public function testAutoRegisteredClass()
477 $f = new xmlrpcmsg('examples.php2.getStateName', array(
478 new xmlrpcval(23, 'int'),
480 $v = $this->send($f);
482 $this->assertEquals('Michigan', $v->scalarval());
483 $f = new xmlrpcmsg('examples.php3.getStateName', array(
484 new xmlrpcval(23, 'int'),
486 $v = $this->send($f);
488 $this->assertEquals('Michigan', $v->scalarval());
491 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
495 public function testAutoRegisteredMethod()
497 // make a 'deep client copy' as the original one might have many properties set
498 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
500 $this->fail('Registration of examples.getStateName failed');
503 // work around bug in current version of phpunit
505 $v = var_export($v, true);
507 $this->assertEquals('Michigan', $v);
511 public function testGetCookies()
513 // let server set to us some cookies we tell it
516 'c2' => array('value' => 'c2'),
517 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
518 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
519 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
521 $cookiesval = php_xmlrpc_encode($cookies);
522 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
523 $r = $this->send($f, 0, true);
526 $this->assertEquals(1, $v->scalarval());
527 // now check if we decoded the cookies as we had set them
528 $rcookies = $r->cookies();
529 // remove extra cookies which might have been set by proxies
530 foreach ($rcookies as $c => $v) {
531 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
532 unset($rcookies[$c]);
534 // Seems like we get this when using php-fpm and php 5.5+ ...
535 if (isset($rcookies[$c]['Max-Age'])) {
536 unset($rcookies[$c]['Max-Age']);
539 foreach ($cookies as $c => $v) {
540 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
541 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
542 if (isset($v['expires'])) {
543 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
544 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
546 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
551 $this->assertEquals($cookies, $rcookies);
555 public function testSetCookies()
557 // let server set to us some cookies we tell it
562 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
564 $f = new xmlrpcmsg('examples.getcookies', array());
565 foreach ($cookies as $cookie => $val) {
566 $this->client->setCookie($cookie, $val);
567 $cookies[$cookie] = (string)$cookies[$cookie];
569 $r = $this->client->send($f, $this->timeout, $this->method);
570 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
571 if (!$r->faultCode()) {
573 $v = php_xmlrpc_decode($v);
574 // on IIS and Apache getallheaders returns something slightly different...
575 $this->assertEquals($v, $cookies);
579 public function testSendTwiceSameMsg()
581 $f = new xmlrpcmsg('examples.stringecho', array(
582 new xmlrpcval('hello world', 'string'),
584 $v1 = $this->send($f);
585 $v2 = $this->send($f);
586 //$v = $r->faultCode();
588 $this->assertEquals($v2, $v1);