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 $this->assertEquals($r->faultCode(), $errrorcode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
63 if (!$r->faultCode()) {
64 if ($return_response) {
74 public function testString()
76 $sendstring = "here are 3 \"entities\": < > & " .
77 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
78 " - isn't that great? \\\"hackery\\\" at it's best " .
79 " also don't want to miss out on \$item[0]. " .
80 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
81 "a simple CR here" . chr(13) .
82 "a simple LF here" . chr(10) .
83 "and then LFCR" . chr(10) . chr(13) .
84 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
85 $f = new xmlrpcmsg('examples.stringecho', array(
86 new xmlrpcval($sendstring, 'string'),
90 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
91 // so we relax our tests...
92 $l1 = strlen($sendstring);
93 $l2 = strlen($v->scalarval());
95 $this->assertEquals($sendstring, $v->scalarval());
97 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
102 public function testAddingDoubles()
104 // note that rounding errors mean we
105 // keep precision to sensible levels here ;-)
108 $f = new xmlrpcmsg('examples.addtwodouble', array(
109 new xmlrpcval($a, 'double'),
110 new xmlrpcval($b, 'double'),
112 $v = $this->send($f);
114 $this->assertEquals($a + $b, $v->scalarval());
118 public function testAdding()
120 $f = new xmlrpcmsg('examples.addtwo', array(
121 new xmlrpcval(12, 'int'),
122 new xmlrpcval(-23, 'int'),
124 $v = $this->send($f);
126 $this->assertEquals(12 - 23, $v->scalarval());
130 public function testInvalidNumber()
132 $f = new xmlrpcmsg('examples.addtwo', array(
133 new xmlrpcval('fred', 'int'),
134 new xmlrpcval("\"; exec('ls')", 'int'),
136 $v = $this->send($f);
137 /// @todo a fault condition should be generated here
138 /// by the server, which we pick up on
140 $this->assertEquals(0, $v->scalarval());
144 public function testBoolean()
146 $f = new xmlrpcmsg('examples.invertBooleans', array(
148 new xmlrpcval(true, 'boolean'),
149 new xmlrpcval(false, 'boolean'),
150 new xmlrpcval(1, 'boolean'),
151 new xmlrpcval(0, 'boolean'),
152 //new xmlrpcval('true', 'boolean'),
153 //new xmlrpcval('false', 'boolean')
158 $v = $this->send($f);
160 $sz = $v->arraysize();
162 for ($i = 0; $i < $sz; $i++) {
163 $b = $v->arraymem($i);
164 if ($b->scalarval()) {
170 $this->assertEquals($answer, $got);
174 public function testBase64()
176 $sendstring = 'Mary had a little lamb,
177 Whose fleece was white as snow,
178 And everywhere that Mary went
179 the lamb was sure to go.
181 Mary had a little lamb
182 She tied it to a pylon
183 Ten thousand volts went down its back
184 And turned it into nylon';
185 $f = new xmlrpcmsg('examples.decode64', array(
186 new xmlrpcval($sendstring, 'base64'),
188 $v = $this->send($f);
190 if (strlen($sendstring) == strlen($v->scalarval())) {
191 $this->assertEquals($sendstring, $v->scalarval());
193 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
198 public function testDateTime()
201 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
202 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
203 $this->assertEquals($t1->serialize(), $t2->serialize());
204 if (class_exists('DateTime')) {
205 $datetime = new DateTime();
206 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
207 if (is_callable(array($datetime, 'setTimestamp'))) {
208 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
209 $this->assertEquals($t1->serialize(), $t3->serialize());
214 public function testCountEntities()
216 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
217 $f = new xmlrpcmsg('validator1.countTheEntities', array(
218 new xmlrpcval($sendstring, 'string'),
220 $v = $this->send($f);
224 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
225 while (list(, $val) = each($expect_array)) {
226 $b = $v->structmem($val);
227 $got .= $b->me['int'];
229 $this->assertEquals($expected, $got);
233 public function _multicall_msg($method, $params)
235 $struct['methodName'] = new xmlrpcval($method, 'string');
236 $struct['params'] = new xmlrpcval($params, 'array');
238 return new xmlrpcval($struct, 'struct');
241 public function testServerMulticall()
243 // We manually construct a system.multicall() call to ensure
244 // that the server supports it.
246 // NB: This test will NOT pass if server does not support system.multicall.
248 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
249 $good1 = $this->_multicall_msg(
251 array(php_xmlrpc_encode('system.listMethods')));
252 $bad = $this->_multicall_msg(
254 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
255 $recursive = $this->_multicall_msg(
257 array(new xmlrpcval(array(), 'array')));
258 $good2 = $this->_multicall_msg(
259 'system.methodSignature',
260 array(php_xmlrpc_encode('system.listMethods')));
261 $arg = new xmlrpcval(
262 array($good1, $bad, $recursive, $good2),
266 $f = new xmlrpcmsg('system.multicall', array($arg));
267 $v = $this->send($f);
269 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
270 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
272 $r1 = $v->arraymem(0);
274 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
275 "did not get array of size 1 from good1"
278 $r2 = $v->arraymem(1);
280 $r2->kindOf() == 'struct',
284 $r3 = $v->arraymem(2);
286 $r3->kindOf() == 'struct',
287 "recursive system.multicall did not fail"
290 $r4 = $v->arraymem(3);
292 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
293 "did not get array of size 1 from good2"
298 public function testClientMulticall1()
300 // NB: This test will NOT pass if server does not support system.multicall.
302 $this->client->no_multicall = false;
304 $good1 = new xmlrpcmsg('system.methodHelp',
305 array(php_xmlrpc_encode('system.listMethods')));
306 $bad = new xmlrpcmsg('test.nosuch',
307 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
308 $recursive = new xmlrpcmsg('system.multicall',
309 array(new xmlrpcval(array(), 'array')));
310 $good2 = new xmlrpcmsg('system.methodSignature',
311 array(php_xmlrpc_encode('system.listMethods'))
314 $r = $this->send(array($good1, $bad, $recursive, $good2));
316 $this->assertTrue(count($r) == 4, "wrong number of return values");
319 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
320 if (!$r[0]->faultCode()) {
321 $val = $r[0]->value();
323 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
324 "good1 did not return string"
327 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
328 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
329 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
330 if (!$r[3]->faultCode()) {
331 $val = $r[3]->value();
332 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
334 // This is the only assert in this test which should fail
335 // if the test server does not support system.multicall.
336 $this->assertTrue($this->client->no_multicall == false,
337 "server does not support system.multicall"
341 public function testClientMulticall2()
343 // NB: This test will NOT pass if server does not support system.multicall.
345 $this->client->no_multicall = true;
347 $good1 = new xmlrpcmsg('system.methodHelp',
348 array(php_xmlrpc_encode('system.listMethods')));
349 $bad = new xmlrpcmsg('test.nosuch',
350 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
351 $recursive = new xmlrpcmsg('system.multicall',
352 array(new xmlrpcval(array(), 'array')));
353 $good2 = new xmlrpcmsg('system.methodSignature',
354 array(php_xmlrpc_encode('system.listMethods'))
357 $r = $this->send(array($good1, $bad, $recursive, $good2));
359 $this->assertTrue(count($r) == 4, "wrong number of return values");
362 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
363 if (!$r[0]->faultCode()) {
364 $val = $r[0]->value();
366 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
367 "good1 did not return string");
369 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
370 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
371 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
372 if (!$r[3]->faultCode()) {
373 $val = $r[3]->value();
374 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
378 public function testClientMulticall3()
380 // NB: This test will NOT pass if server does not support system.multicall.
382 $this->client->return_type = 'phpvals';
383 $this->client->no_multicall = false;
385 $good1 = new xmlrpcmsg('system.methodHelp',
386 array(php_xmlrpc_encode('system.listMethods')));
387 $bad = new xmlrpcmsg('test.nosuch',
388 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
389 $recursive = new xmlrpcmsg('system.multicall',
390 array(new xmlrpcval(array(), 'array')));
391 $good2 = new xmlrpcmsg('system.methodSignature',
392 array(php_xmlrpc_encode('system.listMethods'))
395 $r = $this->send(array($good1, $bad, $recursive, $good2));
397 $this->assertTrue(count($r) == 4, "wrong number of return values");
399 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
400 if (!$r[0]->faultCode()) {
401 $val = $r[0]->value();
403 is_string($val), "good1 did not return string");
405 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
406 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
407 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
408 if (!$r[3]->faultCode()) {
409 $val = $r[3]->value();
410 $this->assertTrue(is_array($val), "good2 did not return array");
412 $this->client->return_type = 'xmlrpcvals';
415 public function testCatchWarnings()
417 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
418 new xmlrpcval('whatever', 'string'),
420 $v = $this->send($f);
422 $this->assertEquals($v->scalarval(), true);
426 public function testCatchExceptions()
428 $f = new xmlrpcmsg('examples.raiseException', array(
429 new xmlrpcval('whatever', 'string'),
431 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
432 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=1';
433 $v = $this->send($f, 1);
434 $this->client->path = $this->args['URI'] . '?EXCEPTION_HANDLING=2';
435 $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
438 public function testZeroParams()
440 $f = new xmlrpcmsg('system.listMethods');
441 $v = $this->send($f);
444 public function testCodeInjectionServerSide()
446 $f = new xmlrpcmsg('system.MethodHelp');
447 $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>";
448 $v = $this->send($f);
449 //$v = $r->faultCode();
451 $this->assertEquals(0, $v->structsize());
455 public function testAutoRegisteredFunction()
457 $f = new xmlrpcmsg('examples.php.getStateName', array(
458 new xmlrpcval(23, 'int'),
460 $v = $this->send($f);
462 $this->assertEquals('Michigan', $v->scalarval());
464 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
468 public function testAutoRegisteredClass()
470 $f = new xmlrpcmsg('examples.php2.getStateName', array(
471 new xmlrpcval(23, 'int'),
473 $v = $this->send($f);
475 $this->assertEquals('Michigan', $v->scalarval());
476 $f = new xmlrpcmsg('examples.php3.getStateName', array(
477 new xmlrpcval(23, 'int'),
479 $v = $this->send($f);
481 $this->assertEquals('Michigan', $v->scalarval());
484 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
488 public function testAutoRegisteredMethod()
490 // make a 'deep client copy' as the original one might have many properties set
491 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 1));
493 $this->fail('Registration of examples.getStateName failed');
496 // work around bug in current version of phpunit
498 $v = var_export($v, true);
500 $this->assertEquals('Michigan', $v);
504 public function testGetCookies()
506 // let server set to us some cookies we tell it
509 'c2' => array('value' => 'c2'),
510 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
511 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
512 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
514 $cookiesval = php_xmlrpc_encode($cookies);
515 $f = new xmlrpcmsg('examples.setcookies', array($cookiesval));
516 $r = $this->send($f, 0, true);
519 $this->assertEquals(1, $v->scalarval());
520 // now check if we decoded the cookies as we had set them
521 $rcookies = $r->cookies();
522 // remove extra cookies which might have been set by proxies
523 foreach ($rcookies as $c => $v) {
524 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
525 unset($rcookies[$c]);
528 foreach ($cookies as $c => $v) {// format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
529 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
530 if (isset($v['expires'])) {
531 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
532 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
534 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
537 $this->assertEquals($cookies, $rcookies);
542 public function testSetCookies()
544 // let server set to us some cookies we tell it
549 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
551 $f = new xmlrpcmsg('examples.getcookies', array());
552 foreach ($cookies as $cookie => $val) {
553 $this->client->setCookie($cookie, $val);
554 $cookies[$cookie] = (string)$cookies[$cookie];
556 $r = $this->client->send($f, $this->timeout, $this->method);
557 $this->assertEquals($r->faultCode(), 0, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
558 if (!$r->faultCode()) {
560 $v = php_xmlrpc_decode($v);
561 // on IIS and Apache getallheaders returns something slightly different...
562 $this->assertEquals($v, $cookies);
566 public function testSendTwiceSameMsg()
568 $f = new xmlrpcmsg('examples.stringecho', array(
569 new xmlrpcval('hello world', 'string'),
571 $v1 = $this->send($f);
572 $v2 = $this->send($f);
573 //$v = $r->faultCode();
575 $this->assertEquals($v2, $v1);