3 include(getcwd().'/parse_args.php');
5 require_once('xmlrpc.inc');
6 require_once('xmlrpcs.inc');
7 require_once('xmlrpc_wrappers.inc');
9 require_once 'phpunit.php';
10 //require_once 'PHPUnit/TestDecorator.php';
12 // let testuite run for the needed time
13 if ((int)ini_get('max_execution_time') < 180)
14 ini_set('max_execution_time', 180);
16 $suite = new PHPUnit_TestSuite();
18 // array with list of failed tests
19 $failed_tests = array();
21 class LocalhostTests extends PHPUnit_TestCase
26 var $request_compression = null;
27 var $accepted_compression = '';
29 function fail($message = '')
31 PHPUnit_TestCase::fail($message);
32 // save in global var that this particular test has failed
33 // (but only if not called from subclass objects / multitests)
34 if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests')
37 $trace = debug_backtrace();
38 for ($i = 0; $i < count($trace); $i++)
40 if (strpos($trace[$i]['function'], 'test') === 0)
42 $failed_tests[$trace[$i]['function']] = true;
51 global $DEBUG, $LOCALSERVER, $URI;
52 $server = explode(':', $LOCALSERVER);
53 if(count($server) > 1)
55 $this->client=new xmlrpc_client($URI, $server[0], $server[1]);
59 $this->client=new xmlrpc_client($URI, $LOCALSERVER);
63 $this->client->setDebug($DEBUG);
65 $this->client->request_compression = $this->request_compression;
66 $this->client->accepted_compression = $this->accepted_compression;
69 function send($msg, $errrorcode=0, $return_response=false)
71 $r = $this->client->send($msg, $this->timeout, $this->method);
72 // for multicall, return directly array of responses
77 $this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
93 $sendstring="here are 3 \"entities\": < > & " .
94 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
95 " - isn't that great? \\\"hackery\\\" at it's best " .
96 " also don't want to miss out on \$item[0]. ".
97 "The real weird stuff follows: CRLF here".chr(13).chr(10).
98 "a simple CR here".chr(13).
99 "a simple LF here".chr(10).
100 "and then LFCR".chr(10).chr(13).
101 "last but not least weird names: G".chr(252)."nter, El".chr(232)."ne, and an xml comment closing tag: -->";
102 $f=new xmlrpcmsg('examples.stringecho', array(
103 new xmlrpcval($sendstring, 'string')
108 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
109 // so we relax our tests...
110 $l1 = strlen($sendstring);
111 $l2 = strlen($v->scalarval());
113 $this->assertEquals($sendstring, $v->scalarval());
115 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
119 function testAddingDoubles()
121 // note that rounding errors mean we
122 // keep precision to sensible levels here ;-)
124 $f=new xmlrpcmsg('examples.addtwodouble',array(
125 new xmlrpcval($a, 'double'),
126 new xmlrpcval($b, 'double')
131 $this->assertEquals($a+$b,$v->scalarval());
135 function testAdding()
137 $f=new xmlrpcmsg('examples.addtwo',array(
138 new xmlrpcval(12, 'int'),
139 new xmlrpcval(-23, 'int')
144 $this->assertEquals(12-23, $v->scalarval());
148 function testInvalidNumber()
150 $f=new xmlrpcmsg('examples.addtwo',array(
151 new xmlrpcval('fred', 'int'),
152 new xmlrpcval("\"; exec('ls')", 'int')
155 /// @todo a fault condition should be generated here
156 /// by the server, which we pick up on
159 $this->assertEquals(0, $v->scalarval());
163 function testBoolean()
165 $f=new xmlrpcmsg('examples.invertBooleans', array(
167 new xmlrpcval(true, 'boolean'),
168 new xmlrpcval(false, 'boolean'),
169 new xmlrpcval(1, 'boolean'),
170 new xmlrpcval(0, 'boolean'),
171 //new xmlrpcval('true', 'boolean'),
172 //new xmlrpcval('false', 'boolean')
182 for($i=0; $i<$sz; $i++)
194 $this->assertEquals($answer, $got);
198 function testBase64()
200 $sendstring='Mary had a little lamb,
201 Whose fleece was white as snow,
202 And everywhere that Mary went
203 the lamb was sure to go.
205 Mary had a little lamb
206 She tied it to a pylon
207 Ten thousand volts went down its back
208 And turned it into nylon';
209 $f=new xmlrpcmsg('examples.decode64',array(
210 new xmlrpcval($sendstring, 'base64')
215 if (strlen($sendstring) == strlen($v->scalarval()))
216 $this->assertEquals($sendstring, $v->scalarval());
218 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
222 function testDateTime()
225 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
226 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
227 $this->assertEquals($t1->serialize(), $t2->serialize());
228 if (class_exists('DateTime'))
230 $datetime = new DateTime();
231 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
232 $this->assertEquals($t1->serialize(), $t3->serialize());
236 function testCountEntities()
238 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
239 $f = new xmlrpcmsg('validator1.countTheEntities',array(
240 new xmlrpcval($sendstring, 'string')
242 $v = $this->send($f);
247 $expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
248 while(list(,$val) = each($expect_array))
250 $b = $v->structmem($val);
251 $got .= $b->me['int'];
253 $this->assertEquals($expected, $got);
257 function _multicall_msg($method, $params)
259 $struct['methodName'] = new xmlrpcval($method, 'string');
260 $struct['params'] = new xmlrpcval($params, 'array');
261 return new xmlrpcval($struct, 'struct');
264 function testServerMulticall()
266 // We manually construct a system.multicall() call to ensure
267 // that the server supports it.
269 // NB: This test will NOT pass if server does not support system.multicall.
271 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
272 $good1 = $this->_multicall_msg(
274 array(php_xmlrpc_encode('system.listMethods')));
275 $bad = $this->_multicall_msg(
277 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
278 $recursive = $this->_multicall_msg(
280 array(new xmlrpcval(array(), 'array')));
281 $good2 = $this->_multicall_msg(
282 'system.methodSignature',
283 array(php_xmlrpc_encode('system.listMethods')));
284 $arg = new xmlrpcval(
285 array($good1, $bad, $recursive, $good2),
289 $f = new xmlrpcmsg('system.multicall', array($arg));
290 $v = $this->send($f);
293 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
294 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
296 $r1 = $v->arraymem(0);
298 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
299 "did not get array of size 1 from good1"
302 $r2 = $v->arraymem(1);
304 $r2->kindOf() == 'struct',
308 $r3 = $v->arraymem(2);
310 $r3->kindOf() == 'struct',
311 "recursive system.multicall did not fail"
314 $r4 = $v->arraymem(3);
316 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
317 "did not get array of size 1 from good2"
322 function testClientMulticall1()
324 // NB: This test will NOT pass if server does not support system.multicall.
326 $this->client->no_multicall = false;
328 $good1 = new xmlrpcmsg('system.methodHelp',
329 array(php_xmlrpc_encode('system.listMethods')));
330 $bad = new xmlrpcmsg('test.nosuch',
331 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
332 $recursive = new xmlrpcmsg('system.multicall',
333 array(new xmlrpcval(array(), 'array')));
334 $good2 = new xmlrpcmsg('system.methodSignature',
335 array(php_xmlrpc_encode('system.listMethods'))
338 $r = $this->send(array($good1, $bad, $recursive, $good2));
341 $this->assertTrue(count($r) == 4, "wrong number of return values");
344 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
345 if(!$r[0]->faultCode())
347 $val = $r[0]->value();
349 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
350 "good1 did not return string"
353 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
354 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
355 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
356 if(!$r[3]->faultCode())
358 $val = $r[3]->value();
359 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
361 // This is the only assert in this test which should fail
362 // if the test server does not support system.multicall.
363 $this->assertTrue($this->client->no_multicall == false,
364 "server does not support system.multicall"
368 function testClientMulticall2()
370 // NB: This test will NOT pass if server does not support system.multicall.
372 $this->client->no_multicall = true;
374 $good1 = new xmlrpcmsg('system.methodHelp',
375 array(php_xmlrpc_encode('system.listMethods')));
376 $bad = new xmlrpcmsg('test.nosuch',
377 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
378 $recursive = new xmlrpcmsg('system.multicall',
379 array(new xmlrpcval(array(), 'array')));
380 $good2 = new xmlrpcmsg('system.methodSignature',
381 array(php_xmlrpc_encode('system.listMethods'))
384 $r = $this->send(array($good1, $bad, $recursive, $good2));
387 $this->assertTrue(count($r) == 4, "wrong number of return values");
390 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
391 if(!$r[0]->faultCode())
393 $val = $r[0]->value();
395 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
396 "good1 did not return string");
398 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
399 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
400 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
401 if(!$r[3]->faultCode())
403 $val = $r[3]->value();
404 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
408 function testClientMulticall3()
410 // NB: This test will NOT pass if server does not support system.multicall.
412 $this->client->return_type = 'phpvals';
413 $this->client->no_multicall = false;
415 $good1 = new xmlrpcmsg('system.methodHelp',
416 array(php_xmlrpc_encode('system.listMethods')));
417 $bad = new xmlrpcmsg('test.nosuch',
418 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
419 $recursive = new xmlrpcmsg('system.multicall',
420 array(new xmlrpcval(array(), 'array')));
421 $good2 = new xmlrpcmsg('system.methodSignature',
422 array(php_xmlrpc_encode('system.listMethods'))
425 $r = $this->send(array($good1, $bad, $recursive, $good2));
428 $this->assertTrue(count($r) == 4, "wrong number of return values");
430 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
431 if(!$r[0]->faultCode())
433 $val = $r[0]->value();
435 is_string($val) , "good1 did not return string");
437 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
438 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
439 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
440 if(!$r[3]->faultCode())
442 $val = $r[3]->value();
443 $this->assertTrue(is_array($val), "good2 did not return array");
445 $this->client->return_type = 'xmlrpcvals';
448 function testCatchWarnings()
450 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
451 new xmlrpcval('whatever', 'string')
453 $v = $this->send($f);
456 $this->assertEquals($v->scalarval(), true);
460 function testCatchExceptions()
463 $f = new xmlrpcmsg('examples.raiseException', array(
464 new xmlrpcval('whatever', 'string')
466 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
467 $this->client->path = $URI.'?EXCEPTION_HANDLING=1';
468 $v = $this->send($f, 1);
469 $this->client->path = $URI.'?EXCEPTION_HANDLING=2';
470 $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
473 function testZeroParams()
475 $f = new xmlrpcmsg('system.listMethods');
476 $v = $this->send($f);
479 function testCodeInjectionServerSide()
481 $f = new xmlrpcmsg('system.MethodHelp');
482 $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>";
483 $v = $this->send($f);
484 //$v = $r->faultCode();
487 $this->assertEquals(0, $v->structsize());
491 function testAutoRegisteredFunction()
493 $f=new xmlrpcmsg('examples.php.getStateName',array(
494 new xmlrpcval(23, 'int')
499 $this->assertEquals('Michigan', $v->scalarval());
503 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
507 function testAutoRegisteredClass()
509 $f=new xmlrpcmsg('examples.php2.getStateName',array(
510 new xmlrpcval(23, 'int')
515 $this->assertEquals('Michigan', $v->scalarval());
516 $f=new xmlrpcmsg('examples.php3.getStateName',array(
517 new xmlrpcval(23, 'int')
522 $this->assertEquals('Michigan', $v->scalarval());
527 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
531 function testAutoRegisteredMethod()
533 $func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
536 $this->fail('Registration of examples.getStateName failed');
541 $this->assertEquals('Michigan', $v);
545 function testGetCookies()
547 // let server set to us some cookies we tell it
550 'c2' => array('value' => 'c2'),
551 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
552 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
553 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
555 $cookiesval = php_xmlrpc_encode($cookies);
556 $f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
557 $r=$this->send($f, 0, true);
561 $this->assertEquals(1, $v->scalarval());
562 // now check if we decoded the cookies as we had set them
563 $rcookies = $r->cookies();
564 foreach($cookies as $c => $v)
565 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
566 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
567 if(isset($v['expires']))
569 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
571 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
575 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
578 $this->assertEquals($cookies, $rcookies);
582 function testSetCookies()
584 // let server set to us some cookies we tell it
589 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
591 $f=new xmlrpcmsg('examples.getcookies',array());
592 foreach ($cookies as $cookie => $val)
594 $this->client->setCookie($cookie, $val);
595 $cookies[$cookie] = (string) $cookies[$cookie];
597 $r = $this->client->send($f, $this->timeout, $this->method);
598 $this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
602 $v = php_xmlrpc_decode($v);
603 // on IIS and Apache getallheaders returns something slightly different...
604 $this->assertEquals($v, $cookies);
608 function testSendTwiceSameMsg()
610 $f=new xmlrpcmsg('examples.stringecho', array(
611 new xmlrpcval('hello world', 'string')
613 $v1 = $this->send($f);
614 $v2 = $this->send($f);
615 //$v = $r->faultCode();
618 $this->assertEquals($v2, $v1);
623 class LocalHostMultiTests extends LocalhostTests
627 global $failed_tests;
628 foreach(get_class_methods('LocalhostTests') as $meth)
630 if(strpos($meth, 'test') === 0 && $meth != 'testHttps' && $meth != 'testCatchExceptions')
632 if (!isset($failed_tests[$meth]))
642 function testDeflate()
644 if(!function_exists('gzdeflate'))
646 $this->fail('Zlib missing: cannot test deflate functionality');
649 $this->client->accepted_compression = array('deflate');
650 $this->client->request_compression = 'deflate';
656 if(!function_exists('gzdeflate'))
658 $this->fail('Zlib missing: cannot test gzip functionality');
661 $this->client->accepted_compression = array('gzip');
662 $this->client->request_compression = 'gzip';
666 function testKeepAlives()
668 if(!function_exists('curl_init'))
670 $this->fail('CURL missing: cannot test http 1.1');
673 $this->method = 'http11';
674 $this->client->keepalive = true;
680 global $PROXYSERVER, $PROXYPORT;
683 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
687 $this->fail('PROXY definition missing: cannot test proxy');
690 function testHttp11()
692 if(!function_exists('curl_init'))
694 $this->fail('CURL missing: cannot test http 1.1');
697 $this->method = 'http11'; // not an error the double assignment!
698 $this->client->method = 'http11';
699 //$this->client->verifyhost = 0;
700 //$this->client->verifypeer = 0;
701 $this->client->keepalive = false;
705 function testHttp11Gzip()
707 if(!function_exists('curl_init'))
709 $this->fail('CURL missing: cannot test http 1.1');
712 $this->method = 'http11'; // not an error the double assignment!
713 $this->client->method = 'http11';
714 $this->client->keepalive = false;
715 $this->client->accepted_compression = array('gzip');
716 $this->client->request_compression = 'gzip';
720 function testHttp11Deflate()
722 if(!function_exists('curl_init'))
724 $this->fail('CURL missing: cannot test http 1.1');
727 $this->method = 'http11'; // not an error the double assignment!
728 $this->client->method = 'http11';
729 $this->client->keepalive = false;
730 $this->client->accepted_compression = array('deflate');
731 $this->client->request_compression = 'deflate';
735 function testHttp11Proxy()
737 global $PROXYSERVER, $PROXYPORT;
738 if(!function_exists('curl_init'))
740 $this->fail('CURL missing: cannot test http 1.1 w. proxy');
743 else if ($PROXYSERVER == '')
745 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
748 $this->method = 'http11'; // not an error the double assignment!
749 $this->client->method = 'http11';
750 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
751 //$this->client->verifyhost = 0;
752 //$this->client->verifypeer = 0;
753 $this->client->keepalive = false;
759 global $HTTPSSERVER, $HTTPSURI, $HTTPSIGNOREPEER;
760 if(!function_exists('curl_init'))
762 $this->fail('CURL missing: cannot test https functionality');
765 $this->client->server = $HTTPSSERVER;
766 $this->method = 'https';
767 $this->client->method = 'https';
768 $this->client->path = $HTTPSURI;
769 $this->client->setSSLVerifyPeer( !$HTTPSIGNOREPEER );
773 function testHttpsProxy()
775 global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
776 if(!function_exists('curl_init'))
778 $this->fail('CURL missing: cannot test https functionality');
781 else if ($PROXYSERVER == '')
783 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
786 $this->client->server = $HTTPSSERVER;
787 $this->method = 'https';
788 $this->client->method = 'https';
789 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
790 $this->client->path = $HTTPSURI;
794 function testUTF8Responses()
797 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
798 $this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
802 function testUTF8Requests()
804 $this->client->request_charset_encoding = 'UTF-8';
808 function testISOResponses()
811 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
812 $this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
816 function testISORequests()
818 $this->client->request_charset_encoding = 'ISO-8859-1';
823 class ParsingBugsTests extends PHPUnit_TestCase
825 function testMinusOneString()
827 $v=new xmlrpcval('-1');
828 $u=new xmlrpcval('-1', 'string');
829 $this->assertEquals($u->scalarval(), $v->scalarval());
832 function testUnicodeInMemberName(){
833 $str = "G".chr(252)."nter, El".chr(232)."ne";
834 $v = array($str => new xmlrpcval(1));
835 $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
836 $r = $r->serialize();
837 $m = new xmlrpcmsg('dummy');
838 $r = $m->parseResponse($r);
840 $this->assertEquals($v->structmemexists($str), true);
843 function testUnicodeInErrorString()
845 $response = utf8_encode(
846 '<?xml version="1.0"?>
848 <!-- found by G. giunta, covers what happens when lib receives
849 UTF8 chars in response text and comments -->
850 <!-- ���àüè -->
856 <name>faultCode</name>
857 <value><int>888</int></value>
860 <name>faultString</name>
861 <value><string>���àüè</string></value>
867 $m=new xmlrpcmsg('dummy');
868 $r=$m->parseResponse($response);
869 $v=$r->faultString();
870 $this->assertEquals('���à üè', $v);
873 function testValidNumbers()
875 $m=new xmlrpcmsg('dummy');
877 '<?xml version="1.0"?>
884 <name>integer1</name>
885 <value><int>01</int></value>
889 <value><double>01.10</double></value>
892 <name>integer2</name>
893 <value><int>+1</int></value>
897 <value><double>+1.10</double></value>
901 <value><double>-1.10e2</double></value>
908 $r=$m->parseResponse($fp);
910 $s=$v->structmem('integer1');
911 $t=$v->structmem('float1');
912 $u=$v->structmem('integer2');
913 $w=$v->structmem('float2');
914 $x=$v->structmem('float3');
915 $this->assertEquals(1, $s->scalarval());
916 $this->assertEquals(1.1, $t->scalarval());
917 $this->assertEquals(1, $u->scalarval());
918 $this->assertEquals(1.1, $w->scalarval());
919 $this->assertEquals(-110.0, $x->scalarval());
922 function testAddScalarToStruct()
924 $v=new xmlrpcval(array('a' => 'b'), 'struct');
925 // use @ operator in case error_log gets on screen
926 $r= @$v->addscalar('c');
927 $this->assertEquals(0, $r);
930 function testAddStructToStruct()
932 $v=new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
933 $r=$v->addstruct(array('b' => new xmlrpcval('c')));
934 $this->assertEquals(2, $v->structsize());
935 $this->assertEquals(1, $r);
936 $r=$v->addstruct(array('b' => new xmlrpcval('b')));
937 $this->assertEquals(2, $v->structsize());
940 function testAddArrayToArray()
942 $v=new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
943 $r=$v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
944 $this->assertEquals(4, $v->arraysize());
945 $this->assertEquals(1, $r);
948 function testEncodeArray()
951 $v = php_xmlrpc_encode($r);
952 $this->assertEquals('array', $v->kindof());
955 function testEncodeRecursive()
957 $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
958 $this->assertEquals('scalar', $v->kindof());
961 function testBrokenRequests()
963 $s = new xmlrpc_server();
964 // omitting the 'params' tag: not tolerated by the lib anymore
965 $f = '<?xml version="1.0"?>
967 <methodName>system.methodHelp</methodName>
969 <value><string>system.methodHelp</string></value>
972 $r = $s->parserequest($f);
973 $this->assertEquals(15, $r->faultCode());
974 // omitting a 'param' tag
975 $f = '<?xml version="1.0"?>
977 <methodName>system.methodHelp</methodName>
979 <value><string>system.methodHelp</string></value>
982 $r = $s->parserequest($f);
983 $this->assertEquals(15, $r->faultCode());
984 // omitting a 'value' tag
985 $f = '<?xml version="1.0"?>
987 <methodName>system.methodHelp</methodName>
989 <param><string>system.methodHelp</string></param>
992 $r = $s->parserequest($f);
993 $this->assertEquals(15, $r->faultCode());
996 function testBrokenResponses()
998 $m=new xmlrpcmsg('dummy');
1000 // omitting the 'params' tag: no more tolerated by the lib...
1001 $f = '<?xml version="1.0"?>
1004 <value><string>system.methodHelp</string></value>
1007 $r = $m->parseResponse($f);
1008 $this->assertEquals(2, $r->faultCode());
1009 // omitting the 'param' tag: no more tolerated by the lib...
1010 $f = '<?xml version="1.0"?>
1013 <value><string>system.methodHelp</string></value>
1016 $r = $m->parseResponse($f);
1017 $this->assertEquals(2, $r->faultCode());
1018 // omitting a 'value' tag: KO
1019 $f = '<?xml version="1.0"?>
1022 <param><string>system.methodHelp</string></param>
1025 $r = $m->parseResponse($f);
1026 $this->assertEquals(2, $r->faultCode());
1029 function testBuggyHttp()
1031 $s = new xmlrpcmsg('dummy');
1032 $f = 'HTTP/1.1 100 Welcome to the jungle
1035 X-Content-Marx-Brothers: Harpo
1037 Content-Length: who knows?
1041 <?xml version="1.0"?>
1042 <!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
1043 <methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1044 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1047 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1048 <script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
1050 $r = $s->parseResponse($f);
1052 $s = $v->structmem('content');
1053 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1056 function testStringBug()
1058 $s = new xmlrpcmsg('dummy');
1059 $f = '<?xml version="1.0"?>
1061 <!-- found by 2z69xks7bpy001@sneakemail.com, amongst others
1062 covers what happens when there\'s character data after </string>
1063 and before </value> -->
1070 <name>success</name>
1072 <boolean>1</boolean>
1076 <name>sessionID</name>
1078 <string>S300510007I</string>
1085 </methodResponse> ';
1086 $r = $s->parseResponse($f);
1088 $s = $v->structmem('sessionID');
1089 $this->assertEquals('S300510007I', $s->scalarval());
1092 function testWhiteSpace()
1094 $s = new xmlrpcmsg('dummy');
1095 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1096 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1099 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1101 $r = $s->parseResponse($f);
1103 $s = $v->structmem('content');
1104 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1107 function testDoubleDataInArrayTag()
1109 $s = new xmlrpcmsg('dummy');
1110 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1113 </array></value></param></params></methodResponse>
1115 $r = $s->parseResponse($f);
1116 $v = $r->faultCode();
1117 $this->assertEquals(2, $v);
1118 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1119 <data><value>Hello world</value></data>
1121 </array></value></param></params></methodResponse>
1123 $r = $s->parseResponse($f);
1124 $v = $r->faultCode();
1125 $this->assertEquals(2, $v);
1128 function testDoubleStuffInValueTag()
1130 $s = new xmlrpcmsg('dummy');
1131 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1132 <string>hello world</string>
1133 <array><data></data></array>
1134 </value></param></params></methodResponse>
1136 $r = $s->parseResponse($f);
1137 $v = $r->faultCode();
1138 $this->assertEquals(2, $v);
1139 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1140 <string>hello</string>
1141 <string>world</string>
1142 </value></param></params></methodResponse>
1144 $r = $s->parseResponse($f);
1145 $v = $r->faultCode();
1146 $this->assertEquals(2, $v);
1147 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1148 <string>hello</string>
1149 <struct><member><name>hello><value>world</value></member></struct>
1150 </value></param></params></methodResponse>
1152 $r = $s->parseResponse($f);
1153 $v = $r->faultCode();
1154 $this->assertEquals(2, $v);
1157 function testAutodecodeResponse()
1159 $s = new xmlrpcmsg('dummy');
1160 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1161 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1164 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1166 $r = $s->parseResponse($f, true, 'phpvals');
1169 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s);
1172 function testNoDecodeResponse()
1174 $s = new xmlrpcmsg('dummy');
1175 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1176 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1179 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
1180 $r = $s->parseResponse($f, true, 'xml');
1182 $this->assertEquals($f, $v);
1185 function testAutoCoDec()
1187 $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
1188 $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
1189 $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
1190 //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
1191 $v1 = php_xmlrpc_encode($data, array('auto_dates'));
1192 $v2 = php_xmlrpc_decode_xml($v1->serialize());
1193 $this->assertEquals($v1, $v2);
1194 $r1 = new xmlrpcresp($v1);
1195 $r2 = php_xmlrpc_decode_xml($r1->serialize());
1196 $r2->serialize(); // needed to set internal member payload
1197 $this->assertEquals($r1, $r2);
1198 $m1 = new xmlrpcmsg('hello dolly', array($v1));
1199 $m2 = php_xmlrpc_decode_xml($m1->serialize());
1200 $m2->serialize(); // needed to set internal member payload
1201 $this->assertEquals($m1, $m2);
1204 function testUTF8Request()
1206 $sendstring='κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
1207 $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
1208 $f = new xmlrpcval($sendstring, 'string');
1210 $this->assertEquals("<value><string>κόσμε</string></value>\n", $v);
1211 $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
1214 function testUTF8Response()
1216 $s = new xmlrpcmsg('dummy');
1217 $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>
1218 <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>
1220 $r = $s->parseResponse($f, false, 'phpvals');
1223 $this->assertEquals("������", $v);
1224 $f = '<?xml version="1.0" encoding="utf-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1225 <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>
1227 $r = $s->parseResponse($f, false, 'phpvals');
1230 $this->assertEquals("������", $v);
1233 function testUTF8IntString()
1235 $v=new xmlrpcval(100, 'int');
1236 $s=$v->serialize('UTF-8');
1237 $this->assertequals("<value><int>100</int></value>\n", $s);
1240 function testStringInt()
1242 $v=new xmlrpcval('hello world', 'int');
1244 $this->assertequals("<value><int>0</int></value>\n", $s);
1247 function testStructMemExists()
1249 $v=php_xmlrpc_encode(array('hello' => 'world'));
1250 $b=$v->structmemexists('hello');
1251 $this->assertequals(true, $b);
1252 $b=$v->structmemexists('world');
1253 $this->assertequals(false, $b);
1256 function testNilvalue()
1258 // default case: we do not accept nil values received
1259 $v = new xmlrpcval('hello', 'null');
1260 $r = new xmlrpcresp($v);
1261 $s = $r->serialize();
1262 $m = new xmlrpcmsg('dummy');
1263 $r = $m->parseresponse($s);
1264 $this->assertequals(2, $r->faultCode());
1265 // enable reception of nil values
1266 $GLOBALS['xmlrpc_null_extension'] = true;
1267 $r = $m->parseresponse($s);
1269 $this->assertequals('null', $v->scalartyp());
1270 // test with the apache version: EX:NIL
1271 $GLOBALS['xmlrpc_null_apache_encoding'] = true;
1273 $v = new xmlrpcval('hello', 'null');
1274 $s = $v->serialize();
1275 $this->assertequals(1, preg_match( '#<value><ex:nil/></value>#', $s ));
1277 $r = new xmlrpcresp($v);
1278 $s = $r->serialize();
1279 $r = $m->parseresponse($s);
1281 $this->assertequals('null', $v->scalartyp());
1282 $GLOBALS['xmlrpc_null_extension'] = false;
1283 $r = $m->parseresponse($s);
1284 $this->assertequals(2, $r->faultCode());
1287 function TestLocale()
1289 $locale = setlocale(LC_NUMERIC, 0);
1290 /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
1291 if (setlocale(LC_NUMERIC,'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false)
1293 $v = new xmlrpcval(1.1, 'double');
1294 if (strpos($v->scalarval(), ',') == 1)
1296 $r = $v->serialize();
1297 $this->assertequals(false, strpos($r, ','));
1299 setlocale(LC_NUMERIC, $locale);
1304 class InvalidHostTests extends PHPUnit_TestCase
1310 global $DEBUG,$LOCALSERVER;
1311 $this->client=new xmlrpc_client('/NOTEXIST.php', $LOCALSERVER, 80);
1314 $this->client->setDebug($DEBUG);
1320 $f = new xmlrpcmsg('examples.echo',array(
1321 new xmlrpcval('hello', 'string')
1323 $r = $this->client->send($f, 5);
1324 $this->assertEquals(5, $r->faultCode());
1327 function testSrvNotFound()
1329 $f = new xmlrpcmsg('examples.echo',array(
1330 new xmlrpcval('hello', 'string')
1332 $this->client->server .= 'XXX';
1333 $r = $this->client->send($f, 5);
1334 $this->assertEquals(5, $r->faultCode());
1337 function testCurlKAErr()
1339 global $LOCALSERVER, $URI;
1340 if(!function_exists('curl_init'))
1342 $this->fail('CURL missing: cannot test curl keepalive errors');
1345 $f = new xmlrpcmsg('examples.stringecho',array(
1346 new xmlrpcval('hello', 'string')
1348 // test 2 calls w. keepalive: 1st time connection ko, second time ok
1349 $this->client->server .= 'XXX';
1350 $this->client->keepalive = true;
1351 $r = $this->client->send($f, 5, 'http11');
1352 // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
1353 $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
1355 // now test a successful connection
1356 $server = explode(':', $LOCALSERVER);
1357 if(count($server) > 1)
1359 $this->client->port = $server[1];
1361 $this->client->server = $server[0];
1362 $this->client->path = $URI;
1364 $r = $this->client->send($f, 5, 'http11');
1365 $this->assertEquals(0, $r->faultCode());
1367 is_object( $ro ) && $this->assertEquals('hello', $ro->scalarVal());
1372 $suite->addTest(new LocalhostTests('testString'));
1373 $suite->addTest(new LocalhostTests('testAdding'));
1374 $suite->addTest(new LocalhostTests('testAddingDoubles'));
1375 $suite->addTest(new LocalhostTests('testInvalidNumber'));
1376 $suite->addTest(new LocalhostTests('testBoolean'));
1377 $suite->addTest(new LocalhostTests('testCountEntities'));
1378 $suite->addTest(new LocalhostTests('testBase64'));
1379 $suite->addTest(new LocalhostTests('testDateTime'));
1380 $suite->addTest(new LocalhostTests('testServerMulticall'));
1381 $suite->addTest(new LocalhostTests('testClientMulticall1'));
1382 $suite->addTest(new LocalhostTests('testClientMulticall2'));
1383 $suite->addTest(new LocalhostTests('testClientMulticall3'));
1384 $suite->addTest(new LocalhostTests('testCatchWarnings'));
1385 $suite->addTest(new LocalhostTests('testCatchExceptions'));
1386 $suite->addTest(new LocalhostTests('testZeroParams'));
1387 $suite->addTest(new LocalhostTests('testCodeInjectionServerSide'));
1388 $suite->addTest(new LocalhostTests('testAutoRegisteredFunction'));
1389 $suite->addTest(new LocalhostTests('testAutoRegisteredMethod'));
1390 $suite->addTest(new LocalhostTests('testSetCookies'));
1391 $suite->addTest(new LocalhostTests('testGetCookies'));
1392 $suite->addTest(new LocalhostTests('testSendTwiceSameMsg'));
1394 $suite->addTest(new LocalhostMultiTests('testUTF8Requests'));
1395 $suite->addTest(new LocalhostMultiTests('testUTF8Responses'));
1396 $suite->addTest(new LocalhostMultiTests('testISORequests'));
1397 $suite->addTest(new LocalhostMultiTests('testISOResponses'));
1398 $suite->addTest(new LocalhostMultiTests('testGzip'));
1399 $suite->addTest(new LocalhostMultiTests('testDeflate'));
1400 $suite->addTest(new LocalhostMultiTests('testProxy'));
1401 $suite->addTest(new LocalhostMultiTests('testHttp11'));
1402 $suite->addTest(new LocalhostMultiTests('testHttp11Gzip'));
1403 $suite->addTest(new LocalhostMultiTests('testHttp11Deflate'));
1404 $suite->addTest(new LocalhostMultiTests('testKeepAlives'));
1405 $suite->addTest(new LocalhostMultiTests('testHttp11Proxy'));
1406 $suite->addTest(new LocalhostMultiTests('testHttps'));
1407 $suite->addTest(new LocalhostMultiTests('testHttpsProxy'));
1409 $suite->addTest(new InvalidHostTests('test404'));
1410 //$suite->addTest(new InvalidHostTests('testSrvNotFound'));
1411 $suite->addTest(new InvalidHostTests('testCurlKAErr'));
1413 $suite->addTest(new ParsingBugsTests('testMinusOneString'));
1414 $suite->addTest(new ParsingBugsTests('testUnicodeInMemberName'));
1415 $suite->addTest(new ParsingBugsTests('testUnicodeInErrorString'));
1416 $suite->addTest(new ParsingBugsTests('testValidNumbers'));
1417 $suite->addTest(new ParsingBugsTests('testAddScalarToStruct'));
1418 $suite->addTest(new ParsingBugsTests('testAddStructToStruct'));
1419 $suite->addTest(new ParsingBugsTests('testAddArrayToArray'));
1420 $suite->addTest(new ParsingBugsTests('testEncodeArray'));
1421 $suite->addTest(new ParsingBugsTests('testEncodeRecursive'));
1422 $suite->addTest(new ParsingBugsTests('testBrokenrequests'));
1423 $suite->addTest(new ParsingBugsTests('testBrokenresponses'));
1424 $suite->addTest(new ParsingBugsTests('testBuggyHttp'));
1425 $suite->addTest(new ParsingBugsTests('testStringBug'));
1426 $suite->addTest(new ParsingBugsTests('testWhiteSpace'));
1427 $suite->addTest(new ParsingBugsTests('testAutodecodeResponse'));
1428 $suite->addTest(new ParsingBugsTests('testNoDecodeResponse'));
1429 $suite->addTest(new ParsingBugsTests('testAutoCoDec'));
1430 $suite->addTest(new ParsingBugsTests('testUTF8Response'));
1431 $suite->addTest(new ParsingBugsTests('testUTF8Request'));
1432 $suite->addTest(new ParsingBugsTests('testUTF8IntString'));
1433 $suite->addTest(new ParsingBugsTests('testStringInt'));
1434 $suite->addTest(new ParsingBugsTests('testStructMemExists'));
1435 $suite->addTest(new ParsingBugsTests('testDoubleDataInArrayTag'));
1436 $suite->addTest(new ParsingBugsTests('testDoubleStuffInValueTag'));
1437 $suite->addTest(new ParsingBugsTests('testNilValue'));
1438 $suite->addTest(new ParsingBugsTests('testLocale'));
1440 $title = 'XML-RPC Unit Tests';
1444 $suite = new PHPUnit_TestSuite($only);
1447 if(isset($_SERVER['REQUEST_METHOD']))
1449 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";
1456 if(isset($_SERVER['REQUEST_METHOD']))
1458 echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
1459 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";
1465 echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
1466 echo 'Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.$LOCALSERVER.$URI.' and https://'.$HTTPSSERVER.$HTTPSURI."\n\n";
1469 // do some basic timing measurement
1470 list($micro, $sec) = explode(' ', microtime());
1471 $start_time = $sec + $micro;
1473 $PHPUnit = new PHPUnit;
1474 $result = $PHPUnit->run($suite, ($DEBUG == 0 ? '.' : '<hr/>'));
1476 list($micro, $sec) = explode(' ', microtime());
1477 $end_time = $sec + $micro;
1479 if(!isset($_SERVER['REQUEST_METHOD']))
1481 echo $result->toString()."\n";
1484 if(isset($_SERVER['REQUEST_METHOD']))
1486 echo '<h3>'.$result->failureCount()." test failures</h3>\n";
1487 printf("Time spent: %.2f secs<br/>\n", $end_time - $start_time);
1491 echo $result->failureCount()." test failures\n";
1492 printf("Time spent: %.2f secs\n", $end_time - $start_time);
1495 if($result->failureCount() && !$DEBUG)
1497 $target = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=1' : $_SERVER['PHP_SELF'].'?DEBUG=1';
1498 $t2 = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=2' : $_SERVER['PHP_SELF'].'?DEBUG=2';
1499 if(isset($_SERVER['REQUEST_METHOD']))
1501 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";
1505 echo "Run testsuite with DEBUG=1 (or 2) to have more detail about tests results\n";
1509 if(isset($_SERVER['REQUEST_METHOD']))
1512 <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>
1513 <div id="opts" style="display: none;">
1514 <form method="GET" style="border: 1px solid silver; margin: 5px; padding: 5px; font-family: monospace;">
1515 HTTP Server: <input name="LOCALSERVER" size="30" value="<?php echo htmlspecialchars($LOCALSERVER); ?>"/> Path: <input name="URI" size="30" value="<?php echo htmlspecialchars($URI); ?>"/><br/>
1516 HTTPS Server: <input name="HTTPSSERVER" size="30" value="<?php echo htmlspecialchars($HTTPSSERVER); ?>"/> Path: <input name="HTTPSURI" size="30" value="<?php echo htmlspecialchars($HTTPSURI); ?>"/> Do not verify cert: <input name="HTTPSIGNOREPEER" value="true" type="checkbox" <?php if ($HTTPSIGNOREPEER) echo 'checked="checked"'; ?>/><br/>
1518 Proxy Server: <input name="PROXY" size="30" value="<?php echo isset($PROXY) ? htmlspecialchars($PROXY) : ''; ?>"/> <input type="submit" value="Run Testsuite"/>
1522 echo $result->toHTML()."\n</body>\n</html>\n";