4 include(getcwd().'/parse_args.php');
6 require_once('xmlrpc.inc');
7 require_once('xmlrpcs.inc');
8 require_once('xmlrpc_wrappers.inc');
10 require_once 'phpunit.php';
11 //require_once 'PHPUnit/TestDecorator.php';
13 // let testuite run for the needed time
14 if ((int)ini_get('max_execution_time') < 180)
15 ini_set('max_execution_time', 180);
18 $suite = new PHPUnit_TestSuite();
20 // array with list of failed tests
21 $failed_tests = array();
23 class LocalhostTests extends PHPUnit_TestCase
28 var $request_compression = null;
29 var $accepted_compression = '';
31 function fail($message = '')
33 PHPUnit_TestCase::fail($message);
34 // save in global var that this particular test has failed
35 // (but only if not called from subclass objects / multitests)
36 if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests')
39 $trace = debug_backtrace();
40 for ($i = 0; $i < count($trace); $i++)
42 if (strpos($trace[$i]['function'], 'test') === 0)
44 $failed_tests[$trace[$i]['function']] = true;
53 global $DEBUG, $LOCALSERVER, $URI;
54 $server = split(':', $LOCALSERVER);
55 if(count($server) > 1)
57 $this->client=&new xmlrpc_client($URI, $server[0], $server[1]);
61 $this->client=&new xmlrpc_client($URI, $LOCALSERVER);
65 $this->client->setDebug($DEBUG);
67 $this->client->request_compression = $this->request_compression;
68 $this->client->accepted_compression = $this->accepted_compression;
71 function send($msg, $errrorcode=0, $return_response=false)
73 $r = $this->client->send($msg, $this->timeout, $this->method);
74 // for multicall, return directly array of responses
79 $this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
95 $sendstring="here are 3 \"entities\": < > &" .
96 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
97 " - isn't that great? \\\"hackery\\\" at it's best " .
98 " also don't want to miss out on \$item[0]. ".
99 "The real weird stuff follows: CRLF here".chr(13).chr(10).
100 "a simple CR here".chr(13).
101 "a simple LF here".chr(10).
102 "and then LFCR".chr(10).chr(13).
103 "last but not least weird names: Günter, Elène, and an xml comment closing tag: -->";
104 $f=new xmlrpcmsg('examples.stringecho', array(
105 new xmlrpcval($sendstring, 'string')
110 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
111 // so we relax our tests...
112 $l1 = strlen($sendstring);
113 $l2 = strlen($v->scalarval());
115 $this->assertEquals($sendstring, $v->scalarval());
117 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
121 function testAddingDoubles()
123 // note that rounding errors mean i
124 // keep precision to sensible levels here ;-)
126 $f=new xmlrpcmsg('examples.addtwodouble',array(
127 new xmlrpcval($a, 'double'),
128 new xmlrpcval($b, 'double')
133 $this->assertEquals($a+$b,$v->scalarval());
137 function testAdding()
139 $f=new xmlrpcmsg('examples.addtwo',array(
140 new xmlrpcval(12, 'int'),
141 new xmlrpcval(-23, 'int')
146 $this->assertEquals(12-23, $v->scalarval());
150 function testInvalidNumber()
152 $f=new xmlrpcmsg('examples.addtwo',array(
153 new xmlrpcval('fred', 'int'),
154 new xmlrpcval("\"; exec('ls')", 'int')
157 /// @todo a fault condition should be generated here
158 /// by the server, which we pick up on
161 $this->assertEquals(0, $v->scalarval());
165 function testBoolean()
167 $f=new xmlrpcmsg('examples.invertBooleans', array(
169 new xmlrpcval(true, 'boolean'),
170 new xmlrpcval(false, 'boolean'),
171 new xmlrpcval(1, 'boolean'),
172 new xmlrpcval(0, 'boolean'),
173 //new xmlrpcval('true', 'boolean'),
174 //new xmlrpcval('false', 'boolean')
184 for($i=0; $i<$sz; $i++)
196 $this->assertEquals($answer, $got);
200 function testBase64()
202 $sendstring='Mary had a little lamb,
203 Whose fleece was white as snow,
204 And everywhere that Mary went
205 the lamb was sure to go.
207 Mary had a little lamb
208 She tied it to a pylon
209 Ten thousand volts went down its back
210 And turned it into nylon';
211 $f=new xmlrpcmsg('examples.decode64',array(
212 new xmlrpcval($sendstring, 'base64')
217 if (strlen($sendstring) == strlen($v->scalarval()))
218 $this->assertEquals($sendstring, $v->scalarval());
220 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
224 function testCountEntities()
226 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
227 $f = new xmlrpcmsg('validator1.countTheEntities',array(
228 new xmlrpcval($sendstring, 'string')
230 $v = $this->send($f);
235 $expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
236 while(list(,$val) = each($expect_array))
238 $b = $v->structmem($val);
239 $got .= $b->me['int'];
241 $this->assertEquals($expected, $got);
245 function _multicall_msg($method, $params)
247 $struct['methodName'] = new xmlrpcval($method, 'string');
248 $struct['params'] = new xmlrpcval($params, 'array');
249 return new xmlrpcval($struct, 'struct');
252 function testServerMulticall()
254 // We manually construct a system.multicall() call to ensure
255 // that the server supports it.
257 // NB: This test will NOT pass if server does not support system.multicall.
259 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
260 $good1 = $this->_multicall_msg(
262 array(php_xmlrpc_encode('system.listMethods')));
263 $bad = $this->_multicall_msg(
265 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
266 $recursive = $this->_multicall_msg(
268 array(new xmlrpcval(array(), 'array')));
269 $good2 = $this->_multicall_msg(
270 'system.methodSignature',
271 array(php_xmlrpc_encode('system.listMethods')));
272 $arg = new xmlrpcval(
273 array($good1, $bad, $recursive, $good2),
277 $f = new xmlrpcmsg('system.multicall', array($arg));
278 $v = $this->send($f);
281 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
282 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
284 $r1 = $v->arraymem(0);
286 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
287 "did not get array of size 1 from good1"
290 $r2 = $v->arraymem(1);
292 $r2->kindOf() == 'struct',
296 $r3 = $v->arraymem(2);
298 $r3->kindOf() == 'struct',
299 "recursive system.multicall did not fail"
302 $r4 = $v->arraymem(3);
304 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
305 "did not get array of size 1 from good2"
310 function testClientMulticall1()
312 // NB: This test will NOT pass if server does not support system.multicall.
314 $this->client->no_multicall = false;
316 $good1 = new xmlrpcmsg('system.methodHelp',
317 array(php_xmlrpc_encode('system.listMethods')));
318 $bad = new xmlrpcmsg('test.nosuch',
319 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
320 $recursive = new xmlrpcmsg('system.multicall',
321 array(new xmlrpcval(array(), 'array')));
322 $good2 = new xmlrpcmsg('system.methodSignature',
323 array(php_xmlrpc_encode('system.listMethods'))
326 $r = $this->send(array($good1, $bad, $recursive, $good2));
329 $this->assertTrue(count($r) == 4, "wrong number of return values");
332 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
333 if(!$r[0]->faultCode())
335 $val = $r[0]->value();
337 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
338 "good1 did not return string"
341 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
342 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
343 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
344 if(!$r[3]->faultCode())
346 $val = $r[3]->value();
347 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
349 // This is the only assert in this test which should fail
350 // if the test server does not support system.multicall.
351 $this->assertTrue($this->client->no_multicall == false,
352 "server does not support system.multicall"
356 function testClientMulticall2()
358 // NB: This test will NOT pass if server does not support system.multicall.
360 $this->client->no_multicall = true;
362 $good1 = new xmlrpcmsg('system.methodHelp',
363 array(php_xmlrpc_encode('system.listMethods')));
364 $bad = new xmlrpcmsg('test.nosuch',
365 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
366 $recursive = new xmlrpcmsg('system.multicall',
367 array(new xmlrpcval(array(), 'array')));
368 $good2 = new xmlrpcmsg('system.methodSignature',
369 array(php_xmlrpc_encode('system.listMethods'))
372 $r = $this->send(array($good1, $bad, $recursive, $good2));
375 $this->assertTrue(count($r) == 4, "wrong number of return values");
378 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
379 if(!$r[0]->faultCode())
381 $val = $r[0]->value();
383 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
384 "good1 did not return string");
386 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
387 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
388 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
389 if(!$r[3]->faultCode())
391 $val = $r[3]->value();
392 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
396 function testClientMulticall3()
398 // NB: This test will NOT pass if server does not support system.multicall.
400 $this->client->return_type = 'phpvals';
401 $this->client->no_multicall = false;
403 $good1 = new xmlrpcmsg('system.methodHelp',
404 array(php_xmlrpc_encode('system.listMethods')));
405 $bad = new xmlrpcmsg('test.nosuch',
406 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
407 $recursive = new xmlrpcmsg('system.multicall',
408 array(new xmlrpcval(array(), 'array')));
409 $good2 = new xmlrpcmsg('system.methodSignature',
410 array(php_xmlrpc_encode('system.listMethods'))
413 $r = $this->send(array($good1, $bad, $recursive, $good2));
416 $this->assertTrue(count($r) == 4, "wrong number of return values");
418 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
419 if(!$r[0]->faultCode())
421 $val = $r[0]->value();
423 is_string($val) , "good1 did not return string");
425 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
426 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
427 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
428 if(!$r[3]->faultCode())
430 $val = $r[3]->value();
431 $this->assertTrue(is_array($val), "good2 did not return array");
433 $this->client->return_type = 'xmlrpcvals';
436 function testCatchWarnings()
438 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
439 new xmlrpcval('whatever', 'string')
441 $v = $this->send($f);
444 $this->assertEquals($v->scalarval(), true);
448 function testZeroParams()
450 $f = new xmlrpcmsg('system.listMethods');
451 $v = $this->send($f);
454 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();
462 $this->assertEquals(0, $v->structsize());
466 function testAutoRegisteredFunction()
468 $f=new xmlrpcmsg('examples.php.getStateName',array(
469 new xmlrpcval(23, 'int')
474 $this->assertEquals('Michigan', $v->scalarval());
478 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
482 function testAutoRegisteredClass()
484 $f=new xmlrpcmsg('examples.php2.getStateName',array(
485 new xmlrpcval(23, 'int')
490 $this->assertEquals('Michigan', $v->scalarval());
491 $f=new xmlrpcmsg('examples.php3.getStateName',array(
492 new xmlrpcval(23, 'int')
497 $this->assertEquals('Michigan', $v->scalarval());
502 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
506 function testAutoRegisteredMethod()
508 $func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
511 $this->fail('Registration of examples.getStateName failed');
516 $this->assertEquals('Michigan', $v);
520 function testGetCookies()
522 // let server set to us some cookies we tell it
525 'c2' => array('value' => 'c2'),
526 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
527 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
528 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
530 $cookiesval =& php_xmlrpc_encode($cookies);
531 $f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
532 $r=$this->send($f, 0, true);
536 $this->assertEquals(1, $v->scalarval());
537 // now check if we decoded the cookies as we had set them
538 $rcookies = $r->cookies();
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']))
544 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
546 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
550 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
553 $this->assertEquals($cookies, $rcookies);
557 function testSetCookies()
559 // let server set to us some cookies we tell it
564 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
566 $f=new xmlrpcmsg('examples.getcookies',array());
567 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());
577 $v = php_xmlrpc_decode($v);
578 // on IIS and Apache getallheaders returns something slightly different...
579 $this->assertEquals($v, $cookies);
583 function testSendTwiceSameMsg()
585 $f=new xmlrpcmsg('examples.stringecho', array(
586 new xmlrpcval('hello world', 'string')
588 $v1 = $this->send($f);
589 $v2 = $this->send($f);
590 //$v = $r->faultCode();
593 $this->assertEquals($v2, $v1);
598 class LocalHostMultiTests extends LocalhostTests
602 global $failed_tests;
603 foreach(get_class_methods('LocalhostTests') as $meth)
605 if(strpos($meth, 'test') === 0 && $meth != 'testHttps')
607 if (!isset($failed_tests[$meth]))
617 function testDeflate()
619 if(!function_exists('gzdeflate'))
621 $this->fail('Zlib missing: cannot test deflate functionality');
624 $this->client->accepted_compression = array('deflate');
625 $this->client->request_compression = 'deflate';
631 if(!function_exists('gzdeflate'))
633 $this->fail('Zlib missing: cannot test gzip functionality');
636 $this->client->accepted_compression = array('gzip');
637 $this->client->request_compression = 'gzip';
641 function testKeepAlives()
643 if(!function_exists('curl_init'))
645 $this->fail('CURL missing: cannot test http 1.1');
648 $this->method = 'http11';
649 $this->client->keepalive = true;
655 global $PROXYSERVER, $PROXYPORT;
658 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
662 $this->fail('PROXY definition missing: cannot test proxy');
665 function testHttp11()
667 if(!function_exists('curl_init'))
669 $this->fail('CURL missing: cannot test http 1.1');
672 $this->method = 'http11'; // not an error the double assignment!
673 $this->client->method = 'http11';
674 //$this->client->verifyhost = 0;
675 //$this->client->verifypeer = 0;
676 $this->client->keepalive = false;
680 function testHttp11Gzip()
682 if(!function_exists('curl_init'))
684 $this->fail('CURL missing: cannot test http 1.1');
687 $this->method = 'http11'; // not an error the double assignment!
688 $this->client->method = 'http11';
689 $this->client->keepalive = false;
690 $this->client->accepted_compression = array('gzip');
691 $this->client->request_compression = 'gzip';
695 function testHttp11Deflate()
697 if(!function_exists('curl_init'))
699 $this->fail('CURL missing: cannot test http 1.1');
702 $this->method = 'http11'; // not an error the double assignment!
703 $this->client->method = 'http11';
704 $this->client->keepalive = false;
705 $this->client->accepted_compression = array('deflate');
706 $this->client->request_compression = 'deflate';
710 function testHttp11Proxy()
712 global $PROXYSERVER, $PROXYPORT;
713 if(!function_exists('curl_init'))
715 $this->fail('CURL missing: cannot test http 1.1 w. proxy');
718 else if ($PROXYSERVER == '')
720 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
723 $this->method = 'http11'; // not an error the double assignment!
724 $this->client->method = 'http11';
725 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
726 //$this->client->verifyhost = 0;
727 //$this->client->verifypeer = 0;
728 $this->client->keepalive = false;
734 global $HTTPSSERVER, $HTTPSURI;
735 if(!function_exists('curl_init'))
737 $this->fail('CURL missing: cannot test https functionality');
740 $this->client->server = $HTTPSSERVER;
741 $this->method = 'https';
742 $this->client->method = 'https';
743 $this->client->path = $HTTPSURI;
747 function testHttpsProxy()
749 global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
750 if(!function_exists('curl_init'))
752 $this->fail('CURL missing: cannot test https functionality');
755 else if ($PROXYSERVER == '')
757 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
760 $this->client->server = $HTTPSSERVER;
761 $this->method = 'https';
762 $this->client->method = 'https';
763 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
764 $this->client->path = $HTTPSURI;
768 function testUTF8Responses()
771 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
772 $this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
776 function testUTF8Requests()
778 $this->client->request_charset_encoding = 'UTF-8';
782 function testISOResponses()
785 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
786 $this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
790 function testISORequests()
792 $this->client->request_charset_encoding = 'ISO-8859-1';
797 class ParsingBugsTests extends PHPUnit_TestCase
799 function testMinusOneString()
801 $v=new xmlrpcval('-1');
802 $u=new xmlrpcval('-1', 'string');
803 $this->assertEquals($u->scalarval(), $v->scalarval());
806 function testUnicodeInMemberName(){
807 $v = array('Günter, Elène' => new xmlrpcval(1));
808 $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
809 $r = $r->serialize();
810 $m = new xmlrpcmsg('dummy');
811 $r = $m->parseResponse($r);
813 $this->assertEquals($v->structmemexists('Günter, Elène'), true);
816 function testUnicodeInErrorString()
818 $response = utf8_encode(
819 '<?xml version="1.0"?>
821 <!-- found by G. giunta, covers what happens when lib receives
822 UTF8 chars in reponse text and comments -->
823 <!-- àüèàüè -->
829 <name>faultCode</name>
830 <value><int>888</int></value>
833 <name>faultString</name>
834 <value><string>àüèàüè</string></value>
840 $m=new xmlrpcmsg('dummy');
841 $r=$m->parseResponse($response);
842 $v=$r->faultString();
843 $this->assertEquals('àüèàüè', $v);
846 function testValidNumbers()
848 $m=new xmlrpcmsg('dummy');
850 '<?xml version="1.0"?>
857 <name>integer1</name>
858 <value><int>01</int></value>
862 <value><double>01.10</double></value>
865 <name>integer2</name>
866 <value><int>+1</int></value>
870 <value><double>+1.10</double></value>
874 <value><double>-1.10e2</double></value>
881 $r=$m->parseResponse($fp);
883 $s=$v->structmem('integer1');
884 $t=$v->structmem('float1');
885 $u=$v->structmem('integer2');
886 $w=$v->structmem('float2');
887 $x=$v->structmem('float3');
888 $this->assertEquals(1, $s->scalarval());
889 $this->assertEquals(1.1, $t->scalarval());
890 $this->assertEquals(1, $u->scalarval());
891 $this->assertEquals(1.1, $w->scalarval());
892 $this->assertEquals(-110.0, $x->scalarval());
895 function testAddScalarToStruct()
897 $v=new xmlrpcval(array('a' => 'b'), 'struct');
898 // use @ operator in case error_log gets on screen
899 $r= @$v->addscalar('c');
900 $this->assertEquals(0, $r);
903 function testAddStructToStruct()
905 $v=new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
906 $r=$v->addstruct(array('b' => new xmlrpcval('c')));
907 $this->assertEquals(2, $v->structsize());
908 $this->assertEquals(1, $r);
909 $r=$v->addstruct(array('b' => new xmlrpcval('b')));
910 $this->assertEquals(2, $v->structsize());
913 function testAddArrayToArray()
915 $v=new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
916 $r=$v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
917 $this->assertEquals(4, $v->arraysize());
918 $this->assertEquals(1, $r);
921 function testEncodeArray()
924 $v = php_xmlrpc_encode($r);
925 $this->assertEquals('array', $v->kindof());
928 function testEncodeRecursive()
930 $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
931 $this->assertEquals('scalar', $v->kindof());
934 function testBrokenRequests()
936 $s = new xmlrpc_server();
937 // omitting the 'params' tag: not tolerated by the lib anymore
938 $f = '<?xml version="1.0"?>
940 <methodName>system.methodHelp</methodName>
942 <value><string>system.methodHelp</string></value>
945 $r = $s->parserequest($f);
946 $this->assertEquals(15, $r->faultCode());
947 // omitting a 'param' tag
948 $f = '<?xml version="1.0"?>
950 <methodName>system.methodHelp</methodName>
952 <value><string>system.methodHelp</string></value>
955 $r = $s->parserequest($f);
956 $this->assertEquals(15, $r->faultCode());
957 // omitting a 'value' tag
958 $f = '<?xml version="1.0"?>
960 <methodName>system.methodHelp</methodName>
962 <param><string>system.methodHelp</string></param>
965 $r = $s->parserequest($f);
966 $this->assertEquals(15, $r->faultCode());
969 function testBrokenResponses()
971 $m=new xmlrpcmsg('dummy');
973 // omitting the 'params' tag: no more tolerated by the lib...
974 $f = '<?xml version="1.0"?>
977 <value><string>system.methodHelp</string></value>
980 $r = $m->parseResponse($f);
981 $this->assertEquals(2, $r->faultCode());
982 // omitting the 'param' tag: no more tolerated by the lib...
983 $f = '<?xml version="1.0"?>
986 <value><string>system.methodHelp</string></value>
989 $r = $m->parseResponse($f);
990 $this->assertEquals(2, $r->faultCode());
991 // omitting a 'value' tag: KO
992 $f = '<?xml version="1.0"?>
995 <param><string>system.methodHelp</string></param>
998 $r = $m->parseResponse($f);
999 $this->assertEquals(2, $r->faultCode());
1002 function testBuggyHttp()
1004 $s = new xmlrpcmsg('dummy');
1005 $f = 'HTTP/1.1 100 Welcome to the jungle
1008 X-Content-Marx-Brothers: Harpo
1010 Content-Length: who knows?
1014 <?xml version="1.0"?>
1015 <!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
1016 <methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1017 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1020 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1021 <script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
1023 $r = $s->parseResponse($f);
1025 $s = $v->structmem('content');
1026 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1029 function testStringBug()
1031 $s = new xmlrpcmsg('dummy');
1032 $f = '<?xml version="1.0"?>
1034 <!-- found by 2z69xks7bpy001@sneakemail.com, amongst others
1035 covers what happens when there\'s character data after </string>
1036 and before </value> -->
1043 <name>success</name>
1045 <boolean>1</boolean>
1049 <name>sessionID</name>
1051 <string>S300510007I</string>
1058 </methodResponse> ';
1059 $r = $s->parseResponse($f);
1061 $s = $v->structmem('sessionID');
1062 $this->assertEquals('S300510007I', $s->scalarval());
1065 function testWhiteSpace()
1067 $s = new xmlrpcmsg('dummy');
1068 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1069 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1072 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1074 $r = $s->parseResponse($f);
1076 $s = $v->structmem('content');
1077 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1080 function testDoubleDataInArrayTag()
1082 $s = new xmlrpcmsg('dummy');
1083 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1086 </array></value></param></params></methodResponse>
1088 $r = $s->parseResponse($f);
1089 $v = $r->faultCode();
1090 $this->assertEquals(2, $v);
1091 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1092 <data><value>Hello world</value></data>
1094 </array></value></param></params></methodResponse>
1096 $r = $s->parseResponse($f);
1097 $v = $r->faultCode();
1098 $this->assertEquals(2, $v);
1101 function testDoubleStuffInValueTag()
1103 $s = new xmlrpcmsg('dummy');
1104 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1105 <string>hello world</string>
1106 <array><data></data></array>
1107 </value></param></params></methodResponse>
1109 $r = $s->parseResponse($f);
1110 $v = $r->faultCode();
1111 $this->assertEquals(2, $v);
1112 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1113 <string>hello</string>
1114 <string>world</string>
1115 </value></param></params></methodResponse>
1117 $r = $s->parseResponse($f);
1118 $v = $r->faultCode();
1119 $this->assertEquals(2, $v);
1120 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1121 <string>hello</string>
1122 <struct><member><name>hello><value>world</value></member></struct>
1123 </value></param></params></methodResponse>
1125 $r = $s->parseResponse($f);
1126 $v = $r->faultCode();
1127 $this->assertEquals(2, $v);
1130 function testAutodecodeResponse()
1132 $s = new xmlrpcmsg('dummy');
1133 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1134 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1137 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1139 $r = $s->parseResponse($f, true, 'phpvals');
1142 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s);
1145 function testNoDecodeResponse()
1147 $s = new xmlrpcmsg('dummy');
1148 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1149 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1152 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
1153 $r = $s->parseResponse($f, true, 'xml');
1155 $this->assertEquals($f, $v);
1158 function testAutoCoDec()
1160 $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
1161 $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
1162 $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
1163 //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
1164 $v1 = php_xmlrpc_encode($data, array('auto_dates'));
1165 $v2 = php_xmlrpc_decode_xml($v1->serialize());
1166 $this->assertEquals($v1, $v2);
1167 $r1 =& new xmlrpcresp($v1);
1168 $r2 = php_xmlrpc_decode_xml($r1->serialize());
1169 $r2->serialize(); // needed to set internal member payload
1170 $this->assertEquals($r1, $r2);
1171 $m1 =& new xmlrpcmsg('hello dolly', array($v1));
1172 $m2 = php_xmlrpc_decode_xml($m1->serialize());
1173 $m2->serialize(); // needed to set internal member payload
1174 $this->assertEquals($m1, $m2);
1177 function testUTF8Request()
1179 $sendstring='κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
1180 $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
1181 $f = new xmlrpcval($sendstring, 'string');
1183 $this->assertEquals("<value><string>κόσμε</string></value>\n", $v);
1184 $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
1187 function testUTF8Response()
1189 $s = new xmlrpcmsg('dummy');
1190 $f = "HTTP/1.1 200 OK\r\nContent-type: text/xml; charset=UTF-8\r\n\r\n".'<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1191 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>'.utf8_encode('àüèàüè').'</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1193 $r = $s->parseResponse($f, false, 'phpvals');
1196 $this->assertEquals("àüèàüè", $v);
1197 $f = '<?xml version="1.0" encoding="utf-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1198 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>'.utf8_encode('àüèàüè').'</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1200 $r = $s->parseResponse($f, false, 'phpvals');
1203 $this->assertEquals("àüèàüè", $v);
1206 function testUTF8IntString()
1208 $v=new xmlrpcval(100, 'int');
1209 $s=$v->serialize('UTF-8');
1210 $this->assertequals("<value><int>100</int></value>\n", $s);
1213 function testStringInt()
1215 $v=new xmlrpcval('hello world', 'int');
1217 $this->assertequals("<value><int>0</int></value>\n", $s);
1220 function testStructMemExists()
1222 $v=php_xmlrpc_encode(array('hello' => 'world'));
1223 $b=$v->structmemexists('hello');
1224 $this->assertequals(true, $b);
1225 $b=$v->structmemexists('world');
1226 $this->assertequals(false, $b);
1229 function testNilvalue()
1231 // default case: we do not accept nil values received
1232 $v = new xmlrpcval('hello', 'null');
1233 $r = new xmlrpcresp($v);
1234 $s = $r->serialize();
1235 $m = new xmlrpcmsg('dummy');
1236 $r = $m->parseresponse($s);
1237 $this->assertequals(2, $r->faultCode());
1238 // enable reception of nil values
1239 $GLOBALS['xmlrpc_null_extension'] = true;
1240 $r = $m->parseresponse($s);
1242 $this->assertequals('null', $v->scalartyp());
1245 function TestLocale()
1247 $locale = setlocale(LC_NUMERIC,0);
1248 if ( setlocale(LC_NUMERIC,'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false )
1250 $v = new xmlrpcval(1.1,'double');
1251 $r = $v->serialize();
1252 $this->assertequals(false, strpos($r,','));
1253 $this->assertequals(1, strpos($v->scalarval(),','));
1254 setlocale(LC_NUMERIC,$locale);
1259 class InvalidHostTests extends PHPUnit_TestCase
1265 global $DEBUG,$LOCALSERVER;
1266 $this->client=new xmlrpc_client('/NOTEXIST.php', $LOCALSERVER, 80);
1269 $this->client->setDebug($DEBUG);
1275 $f = new xmlrpcmsg('examples.echo',array(
1276 new xmlrpcval('hello', 'string')
1278 $r = $this->client->send($f, 5);
1279 $this->assertEquals(5, $r->faultCode());
1282 function testSrvNotFound()
1284 $f = new xmlrpcmsg('examples.echo',array(
1285 new xmlrpcval('hello', 'string')
1287 $this->client->server .= 'XXX';
1288 $r = $this->client->send($f, 5);
1289 $this->assertEquals(5, $r->faultCode());
1292 function testCurlKAErr()
1294 global $LOCALSERVER, $URI;
1295 if(!function_exists('curl_init'))
1297 $this->fail('CURL missing: cannot test curl keepalive errors');
1300 $f = new xmlrpcmsg('examples.stringecho',array(
1301 new xmlrpcval('hello', 'string')
1303 // test 2 calls w. keepalive: 1st time connection ko, second time ok
1304 $this->client->server .= 'XXX';
1305 $this->client->keepalive = true;
1306 $r = $this->client->send($f, 5, 'http11');
1307 $this->assertEquals(8, $r->faultCode());
1309 // now test a successful connection
1310 $server = split(':', $LOCALSERVER);
1311 if(count($server) > 1)
1313 $this->client->port = $server[1];
1315 $this->client->server = $server[0];
1316 $this->client->path = $URI;
1318 $r = $this->client->send($f, 5, 'http11');
1320 $this->assertEquals('hello', $r->scalarVal());
1325 $suite->addTest(new LocalhostTests('testString'));
1326 $suite->addTest(new LocalhostTests('testAdding'));
1327 $suite->addTest(new LocalhostTests('testAddingDoubles'));
1328 $suite->addTest(new LocalhostTests('testInvalidNumber'));
1329 $suite->addTest(new LocalhostTests('testBoolean'));
1330 $suite->addTest(new LocalhostTests('testCountEntities'));
1331 $suite->addTest(new LocalhostTests('testBase64'));
1332 $suite->addTest(new LocalhostTests('testServerMulticall'));
1333 $suite->addTest(new LocalhostTests('testClientMulticall1'));
1334 $suite->addTest(new LocalhostTests('testClientMulticall2'));
1335 $suite->addTest(new LocalhostTests('testClientMulticall3'));
1336 $suite->addTest(new LocalhostTests('testCatchWarnings'));
1337 $suite->addTest(new LocalhostTests('testZeroParams'));
1338 $suite->addTest(new LocalhostTests('testCodeInjectionServerSide'));
1339 $suite->addTest(new LocalhostTests('testAutoRegisteredFunction'));
1340 $suite->addTest(new LocalhostTests('testAutoRegisteredMethod'));
1341 $suite->addTest(new LocalhostTests('testSetCookies'));
1342 $suite->addTest(new LocalhostTests('testGetCookies'));
1343 $suite->addTest(new LocalhostTests('testSendTwiceSameMsg'));
1345 $suite->addTest(new LocalhostMultiTests('testUTF8Requests'));
1346 $suite->addTest(new LocalhostMultiTests('testUTF8Responses'));
1347 $suite->addTest(new LocalhostMultiTests('testISORequests'));
1348 $suite->addTest(new LocalhostMultiTests('testISOResponses'));
1349 $suite->addTest(new LocalhostMultiTests('testGzip'));
1350 $suite->addTest(new LocalhostMultiTests('testDeflate'));
1351 $suite->addTest(new LocalhostMultiTests('testProxy'));
1352 $suite->addTest(new LocalhostMultiTests('testHttp11'));
1353 $suite->addTest(new LocalhostMultiTests('testHttp11Gzip'));
1354 $suite->addTest(new LocalhostMultiTests('testHttp11Deflate'));
1355 $suite->addTest(new LocalhostMultiTests('testKeepAlives'));
1356 $suite->addTest(new LocalhostMultiTests('testHttp11Proxy'));
1357 $suite->addTest(new LocalhostMultiTests('testHttps'));
1358 $suite->addTest(new LocalhostMultiTests('testHttpsProxy'));
1360 $suite->addTest(new InvalidHostTests('test404'));
1361 //$suite->addTest(new InvalidHostTests('testSrvNotFound'));
1362 $suite->addTest(new InvalidHostTests('testCurlKAErr'));
1364 $suite->addTest(new ParsingBugsTests('testMinusOneString'));
1365 $suite->addTest(new ParsingBugsTests('testUnicodeInMemberName'));
1366 $suite->addTest(new ParsingBugsTests('testUnicodeInErrorString'));
1367 $suite->addTest(new ParsingBugsTests('testValidNumbers'));
1368 $suite->addTest(new ParsingBugsTests('testAddScalarToStruct'));
1369 $suite->addTest(new ParsingBugsTests('testAddStructToStruct'));
1370 $suite->addTest(new ParsingBugsTests('testAddArrayToArray'));
1371 $suite->addTest(new ParsingBugsTests('testEncodeArray'));
1372 $suite->addTest(new ParsingBugsTests('testEncodeRecursive'));
1373 $suite->addTest(new ParsingBugsTests('testBrokenrequests'));
1374 $suite->addTest(new ParsingBugsTests('testBrokenresponses'));
1375 $suite->addTest(new ParsingBugsTests('testBuggyHttp'));
1376 $suite->addTest(new ParsingBugsTests('testStringBug'));
1377 $suite->addTest(new ParsingBugsTests('testWhiteSpace'));
1378 $suite->addTest(new ParsingBugsTests('testAutodecodeResponse'));
1379 $suite->addTest(new ParsingBugsTests('testNoDecodeResponse'));
1380 $suite->addTest(new ParsingBugsTests('testAutoCoDec'));
1381 $suite->addTest(new ParsingBugsTests('testUTF8Response'));
1382 $suite->addTest(new ParsingBugsTests('testUTF8Request'));
1383 $suite->addTest(new ParsingBugsTests('testUTF8IntString'));
1384 $suite->addTest(new ParsingBugsTests('testStringInt'));
1385 $suite->addTest(new ParsingBugsTests('testStructMemExists'));
1386 $suite->addTest(new ParsingBugsTests('testDoubleDataInArrayTag'));
1387 $suite->addTest(new ParsingBugsTests('testDoubleStuffInValueTag'));
1388 $suite->addTest(new ParsingBugsTests('testNilValue'));
1389 $suite->addTest(new ParsingBugsTests('testLocale'));
1391 $title = 'XML-RPC Unit Tests';
1395 $suite = new PHPUnit_TestSuite($only);
1398 if(isset($_SERVER['REQUEST_METHOD']))
1400 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n<head>\n<title>$title</title>\n</head>\n<body>\n<h1>$title</h1>\n";
1407 if(isset($_SERVER['REQUEST_METHOD']))
1409 echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
1410 echo '<h3>Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.htmlspecialchars($LOCALSERVER.$URI).' and https://'.htmlspecialchars($HTTPSSERVER.$HTTPSURI)."\n ...</h3>\n";
1415 echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
1416 echo 'Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.$LOCALSERVER.$URI.' and https://'.$HTTPSSERVER.$HTTPSURI."\n\n";
1419 // do some basic timing measurement
1420 list($micro, $sec) = explode(' ', microtime());
1421 $start_time = $sec + $micro;
1423 $PHPUnit = new PHPUnit;
1424 $result = $PHPUnit->run($suite, ($DEBUG == 0 ? '.' : '<hr/>'));
1426 list($micro, $sec) = explode(' ', microtime());
1427 $end_time = $sec + $micro;
1429 if(!isset($_SERVER['REQUEST_METHOD']))
1431 echo $result->toString()."\n";
1434 if(isset($_SERVER['REQUEST_METHOD']))
1436 echo '<h3>'.$result->failureCount()." test failures</h3>\n";
1437 printf("Time spent: %.2f secs<br/>\n", $end_time - $start_time);
1441 echo $result->failureCount()." test failures\n";
1442 printf("Time spent: %.2f secs\n", $end_time - $start_time);
1445 if($result->failureCount() && !$DEBUG)
1447 $target = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=1' : $_SERVER['PHP_SELF'].'?DEBUG=1';
1448 $t2 = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=2' : $_SERVER['PHP_SELF'].'?DEBUG=2';
1449 if(isset($_SERVER['REQUEST_METHOD']))
1451 echo '<p>Run testsuite with <a href="'.$target.'">DEBUG=1</a> to have more detail about tests results. Or with <a href="'.$t2.'">DEBUG=2</a> for even more.</p>'."\n";
1455 echo "Run testsuite with DEBUG=1 (or 2) to have more detail about tests results\n";
1459 if(isset($_SERVER['REQUEST_METHOD']))
1462 <a href="#" onclick="if (document.getElementById('opts').style.display == 'block') document.getElementById('opts').style.display = 'none'; else document.getElementById('opts').style.display = 'block';">More options...</a>
1463 <div id="opts" style="display: none;">
1464 <form method="GET" style="border: 1px solid silver; margin: 5px; padding: 5px; font-family: monospace;">
1465 HTTP Server: <input name="LOCALSERVER" size="30" value="<?php echo htmlspecialchars($LOCALSERVER); ?>"/> Path: <input name="URI" size="30" value="<?php echo htmlspecialchars($URI); ?>"/><br/>
1466 HTTPS Server: <input name="HTTPSSERVER" size="30" value="<?php echo htmlspecialchars($HTTPSSERVER); ?>"/> Path: <input name="HTTPSURI" size="30" value="<?php echo htmlspecialchars($HTTPSURI); ?>"/><br/>
1467 Proxy Server: <input name="PROXY" size="30" value="<?php echo isset($PROXY) ? htmlspecialchars($PROXY) : ''; ?>"/> <input type="submit" value="Run Testsuite"/>
1471 echo $result->toHTML()."\n</body>\n</html>\n";