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 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 = explode(':', $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 testDateTime()
227 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
228 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
229 $this->assertEquals($t1->serialize(), $t2->serialize());
230 if (class_exists('DateTime'))
232 $datetime = new DateTime();
233 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
234 $this->assertEquals($t1->serialize(), $t3->serialize());
238 function testCountEntities()
240 $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
241 $f = new xmlrpcmsg('validator1.countTheEntities',array(
242 new xmlrpcval($sendstring, 'string')
244 $v = $this->send($f);
249 $expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
250 while(list(,$val) = each($expect_array))
252 $b = $v->structmem($val);
253 $got .= $b->me['int'];
255 $this->assertEquals($expected, $got);
259 function _multicall_msg($method, $params)
261 $struct['methodName'] = new xmlrpcval($method, 'string');
262 $struct['params'] = new xmlrpcval($params, 'array');
263 return new xmlrpcval($struct, 'struct');
266 function testServerMulticall()
268 // We manually construct a system.multicall() call to ensure
269 // that the server supports it.
271 // NB: This test will NOT pass if server does not support system.multicall.
273 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
274 $good1 = $this->_multicall_msg(
276 array(php_xmlrpc_encode('system.listMethods')));
277 $bad = $this->_multicall_msg(
279 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
280 $recursive = $this->_multicall_msg(
282 array(new xmlrpcval(array(), 'array')));
283 $good2 = $this->_multicall_msg(
284 'system.methodSignature',
285 array(php_xmlrpc_encode('system.listMethods')));
286 $arg = new xmlrpcval(
287 array($good1, $bad, $recursive, $good2),
291 $f = new xmlrpcmsg('system.multicall', array($arg));
292 $v = $this->send($f);
295 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
296 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
298 $r1 = $v->arraymem(0);
300 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
301 "did not get array of size 1 from good1"
304 $r2 = $v->arraymem(1);
306 $r2->kindOf() == 'struct',
310 $r3 = $v->arraymem(2);
312 $r3->kindOf() == 'struct',
313 "recursive system.multicall did not fail"
316 $r4 = $v->arraymem(3);
318 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
319 "did not get array of size 1 from good2"
324 function testClientMulticall1()
326 // NB: This test will NOT pass if server does not support system.multicall.
328 $this->client->no_multicall = false;
330 $good1 = new xmlrpcmsg('system.methodHelp',
331 array(php_xmlrpc_encode('system.listMethods')));
332 $bad = new xmlrpcmsg('test.nosuch',
333 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
334 $recursive = new xmlrpcmsg('system.multicall',
335 array(new xmlrpcval(array(), 'array')));
336 $good2 = new xmlrpcmsg('system.methodSignature',
337 array(php_xmlrpc_encode('system.listMethods'))
340 $r = $this->send(array($good1, $bad, $recursive, $good2));
343 $this->assertTrue(count($r) == 4, "wrong number of return values");
346 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
347 if(!$r[0]->faultCode())
349 $val = $r[0]->value();
351 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
352 "good1 did not return string"
355 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
356 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
357 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
358 if(!$r[3]->faultCode())
360 $val = $r[3]->value();
361 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
363 // This is the only assert in this test which should fail
364 // if the test server does not support system.multicall.
365 $this->assertTrue($this->client->no_multicall == false,
366 "server does not support system.multicall"
370 function testClientMulticall2()
372 // NB: This test will NOT pass if server does not support system.multicall.
374 $this->client->no_multicall = true;
376 $good1 = new xmlrpcmsg('system.methodHelp',
377 array(php_xmlrpc_encode('system.listMethods')));
378 $bad = new xmlrpcmsg('test.nosuch',
379 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
380 $recursive = new xmlrpcmsg('system.multicall',
381 array(new xmlrpcval(array(), 'array')));
382 $good2 = new xmlrpcmsg('system.methodSignature',
383 array(php_xmlrpc_encode('system.listMethods'))
386 $r = $this->send(array($good1, $bad, $recursive, $good2));
389 $this->assertTrue(count($r) == 4, "wrong number of return values");
392 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
393 if(!$r[0]->faultCode())
395 $val = $r[0]->value();
397 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
398 "good1 did not return string");
400 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
401 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
402 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
403 if(!$r[3]->faultCode())
405 $val = $r[3]->value();
406 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
410 function testClientMulticall3()
412 // NB: This test will NOT pass if server does not support system.multicall.
414 $this->client->return_type = 'phpvals';
415 $this->client->no_multicall = false;
417 $good1 = new xmlrpcmsg('system.methodHelp',
418 array(php_xmlrpc_encode('system.listMethods')));
419 $bad = new xmlrpcmsg('test.nosuch',
420 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
421 $recursive = new xmlrpcmsg('system.multicall',
422 array(new xmlrpcval(array(), 'array')));
423 $good2 = new xmlrpcmsg('system.methodSignature',
424 array(php_xmlrpc_encode('system.listMethods'))
427 $r = $this->send(array($good1, $bad, $recursive, $good2));
430 $this->assertTrue(count($r) == 4, "wrong number of return values");
432 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
433 if(!$r[0]->faultCode())
435 $val = $r[0]->value();
437 is_string($val) , "good1 did not return string");
439 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
440 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
441 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
442 if(!$r[3]->faultCode())
444 $val = $r[3]->value();
445 $this->assertTrue(is_array($val), "good2 did not return array");
447 $this->client->return_type = 'xmlrpcvals';
450 function testCatchWarnings()
452 $f = new xmlrpcmsg('examples.generatePHPWarning', array(
453 new xmlrpcval('whatever', 'string')
455 $v = $this->send($f);
458 $this->assertEquals($v->scalarval(), true);
462 function testCatchExceptions()
465 $f = new xmlrpcmsg('examples.raiseException', array(
466 new xmlrpcval('whatever', 'string')
468 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
469 $this->client->path = $URI.'?EXCEPTION_HANDLING=1';
470 $v = $this->send($f, 1);
471 $this->client->path = $URI.'?EXCEPTION_HANDLING=2';
472 $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
475 function testZeroParams()
477 $f = new xmlrpcmsg('system.listMethods');
478 $v = $this->send($f);
481 function testCodeInjectionServerSide()
483 $f = new xmlrpcmsg('system.MethodHelp');
484 $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>";
485 $v = $this->send($f);
486 //$v = $r->faultCode();
489 $this->assertEquals(0, $v->structsize());
493 function testAutoRegisteredFunction()
495 $f=new xmlrpcmsg('examples.php.getStateName',array(
496 new xmlrpcval(23, 'int')
501 $this->assertEquals('Michigan', $v->scalarval());
505 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
509 function testAutoRegisteredClass()
511 $f=new xmlrpcmsg('examples.php2.getStateName',array(
512 new xmlrpcval(23, 'int')
517 $this->assertEquals('Michigan', $v->scalarval());
518 $f=new xmlrpcmsg('examples.php3.getStateName',array(
519 new xmlrpcval(23, 'int')
524 $this->assertEquals('Michigan', $v->scalarval());
529 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
533 function testAutoRegisteredMethod()
535 $func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
538 $this->fail('Registration of examples.getStateName failed');
543 $this->assertEquals('Michigan', $v);
547 function testGetCookies()
549 // let server set to us some cookies we tell it
552 'c2' => array('value' => 'c2'),
553 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
554 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
555 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
557 $cookiesval = php_xmlrpc_encode($cookies);
558 $f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
559 $r=$this->send($f, 0, true);
563 $this->assertEquals(1, $v->scalarval());
564 // now check if we decoded the cookies as we had set them
565 $rcookies = $r->cookies();
566 foreach($cookies as $c => $v)
567 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
568 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
569 if(isset($v['expires']))
571 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
573 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
577 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
580 $this->assertEquals($cookies, $rcookies);
584 function testSetCookies()
586 // let server set to us some cookies we tell it
591 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
593 $f=new xmlrpcmsg('examples.getcookies',array());
594 foreach ($cookies as $cookie => $val)
596 $this->client->setCookie($cookie, $val);
597 $cookies[$cookie] = (string) $cookies[$cookie];
599 $r = $this->client->send($f, $this->timeout, $this->method);
600 $this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
604 $v = php_xmlrpc_decode($v);
605 // on IIS and Apache getallheaders returns something slightly different...
606 $this->assertEquals($v, $cookies);
610 function testSendTwiceSameMsg()
612 $f=new xmlrpcmsg('examples.stringecho', array(
613 new xmlrpcval('hello world', 'string')
615 $v1 = $this->send($f);
616 $v2 = $this->send($f);
617 //$v = $r->faultCode();
620 $this->assertEquals($v2, $v1);
625 class LocalHostMultiTests extends LocalhostTests
629 global $failed_tests;
630 foreach(get_class_methods('LocalhostTests') as $meth)
632 if(strpos($meth, 'test') === 0 && $meth != 'testHttps' && $meth != 'testCatchExceptions')
634 if (!isset($failed_tests[$meth]))
644 function testDeflate()
646 if(!function_exists('gzdeflate'))
648 $this->fail('Zlib missing: cannot test deflate functionality');
651 $this->client->accepted_compression = array('deflate');
652 $this->client->request_compression = 'deflate';
658 if(!function_exists('gzdeflate'))
660 $this->fail('Zlib missing: cannot test gzip functionality');
663 $this->client->accepted_compression = array('gzip');
664 $this->client->request_compression = 'gzip';
668 function testKeepAlives()
670 if(!function_exists('curl_init'))
672 $this->fail('CURL missing: cannot test http 1.1');
675 $this->method = 'http11';
676 $this->client->keepalive = true;
682 global $PROXYSERVER, $PROXYPORT;
685 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
689 $this->fail('PROXY definition missing: cannot test proxy');
692 function testHttp11()
694 if(!function_exists('curl_init'))
696 $this->fail('CURL missing: cannot test http 1.1');
699 $this->method = 'http11'; // not an error the double assignment!
700 $this->client->method = 'http11';
701 //$this->client->verifyhost = 0;
702 //$this->client->verifypeer = 0;
703 $this->client->keepalive = false;
707 function testHttp11Gzip()
709 if(!function_exists('curl_init'))
711 $this->fail('CURL missing: cannot test http 1.1');
714 $this->method = 'http11'; // not an error the double assignment!
715 $this->client->method = 'http11';
716 $this->client->keepalive = false;
717 $this->client->accepted_compression = array('gzip');
718 $this->client->request_compression = 'gzip';
722 function testHttp11Deflate()
724 if(!function_exists('curl_init'))
726 $this->fail('CURL missing: cannot test http 1.1');
729 $this->method = 'http11'; // not an error the double assignment!
730 $this->client->method = 'http11';
731 $this->client->keepalive = false;
732 $this->client->accepted_compression = array('deflate');
733 $this->client->request_compression = 'deflate';
737 function testHttp11Proxy()
739 global $PROXYSERVER, $PROXYPORT;
740 if(!function_exists('curl_init'))
742 $this->fail('CURL missing: cannot test http 1.1 w. proxy');
745 else if ($PROXYSERVER == '')
747 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
750 $this->method = 'http11'; // not an error the double assignment!
751 $this->client->method = 'http11';
752 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
753 //$this->client->verifyhost = 0;
754 //$this->client->verifypeer = 0;
755 $this->client->keepalive = false;
761 global $HTTPSSERVER, $HTTPSURI;
762 if(!function_exists('curl_init'))
764 $this->fail('CURL missing: cannot test https functionality');
767 $this->client->server = $HTTPSSERVER;
768 $this->method = 'https';
769 $this->client->method = 'https';
770 $this->client->path = $HTTPSURI;
774 function testHttpsProxy()
776 global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
777 if(!function_exists('curl_init'))
779 $this->fail('CURL missing: cannot test https functionality');
782 else if ($PROXYSERVER == '')
784 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
787 $this->client->server = $HTTPSSERVER;
788 $this->method = 'https';
789 $this->client->method = 'https';
790 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
791 $this->client->path = $HTTPSURI;
795 function testUTF8Responses()
798 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
799 $this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
803 function testUTF8Requests()
805 $this->client->request_charset_encoding = 'UTF-8';
809 function testISOResponses()
812 //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
813 $this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
817 function testISORequests()
819 $this->client->request_charset_encoding = 'ISO-8859-1';
824 class ParsingBugsTests extends PHPUnit_TestCase
826 function testMinusOneString()
828 $v=new xmlrpcval('-1');
829 $u=new xmlrpcval('-1', 'string');
830 $this->assertEquals($u->scalarval(), $v->scalarval());
833 function testUnicodeInMemberName(){
834 $v = array('G�nter, El�ne' => 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('G�nter, El�ne'), 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 reponse 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 if ( setlocale(LC_NUMERIC,'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false )
1292 $v = new xmlrpcval(1.1,'double');
1293 $r = $v->serialize();
1294 $this->assertequals(false, strpos($r,','));
1295 $this->assertequals(1, strpos($v->scalarval(),','));
1296 setlocale(LC_NUMERIC,$locale);
1301 class InvalidHostTests extends PHPUnit_TestCase
1307 global $DEBUG,$LOCALSERVER;
1308 $this->client=new xmlrpc_client('/NOTEXIST.php', $LOCALSERVER, 80);
1311 $this->client->setDebug($DEBUG);
1317 $f = new xmlrpcmsg('examples.echo',array(
1318 new xmlrpcval('hello', 'string')
1320 $r = $this->client->send($f, 5);
1321 $this->assertEquals(5, $r->faultCode());
1324 function testSrvNotFound()
1326 $f = new xmlrpcmsg('examples.echo',array(
1327 new xmlrpcval('hello', 'string')
1329 $this->client->server .= 'XXX';
1330 $r = $this->client->send($f, 5);
1331 $this->assertEquals(5, $r->faultCode());
1334 function testCurlKAErr()
1336 global $LOCALSERVER, $URI;
1337 if(!function_exists('curl_init'))
1339 $this->fail('CURL missing: cannot test curl keepalive errors');
1342 $f = new xmlrpcmsg('examples.stringecho',array(
1343 new xmlrpcval('hello', 'string')
1345 // test 2 calls w. keepalive: 1st time connection ko, second time ok
1346 $this->client->server .= 'XXX';
1347 $this->client->keepalive = true;
1348 $r = $this->client->send($f, 5, 'http11');
1349 $this->assertEquals(8, $r->faultCode());
1351 // now test a successful connection
1352 $server = explode(':', $LOCALSERVER);
1353 if(count($server) > 1)
1355 $this->client->port = $server[1];
1357 $this->client->server = $server[0];
1358 $this->client->path = $URI;
1360 $r = $this->client->send($f, 5, 'http11');
1362 $this->assertEquals('hello', $ro->scalarVal());
1367 $suite->addTest(new LocalhostTests('testString'));
1368 $suite->addTest(new LocalhostTests('testAdding'));
1369 $suite->addTest(new LocalhostTests('testAddingDoubles'));
1370 $suite->addTest(new LocalhostTests('testInvalidNumber'));
1371 $suite->addTest(new LocalhostTests('testBoolean'));
1372 $suite->addTest(new LocalhostTests('testCountEntities'));
1373 $suite->addTest(new LocalhostTests('testBase64'));
1374 $suite->addTest(new LocalhostTests('testDateTime'));
1375 $suite->addTest(new LocalhostTests('testServerMulticall'));
1376 $suite->addTest(new LocalhostTests('testClientMulticall1'));
1377 $suite->addTest(new LocalhostTests('testClientMulticall2'));
1378 $suite->addTest(new LocalhostTests('testClientMulticall3'));
1379 $suite->addTest(new LocalhostTests('testCatchWarnings'));
1380 $suite->addTest(new LocalhostTests('testCatchExceptions'));
1381 $suite->addTest(new LocalhostTests('testZeroParams'));
1382 $suite->addTest(new LocalhostTests('testCodeInjectionServerSide'));
1383 $suite->addTest(new LocalhostTests('testAutoRegisteredFunction'));
1384 $suite->addTest(new LocalhostTests('testAutoRegisteredMethod'));
1385 $suite->addTest(new LocalhostTests('testSetCookies'));
1386 $suite->addTest(new LocalhostTests('testGetCookies'));
1387 $suite->addTest(new LocalhostTests('testSendTwiceSameMsg'));
1389 $suite->addTest(new LocalhostMultiTests('testUTF8Requests'));
1390 $suite->addTest(new LocalhostMultiTests('testUTF8Responses'));
1391 $suite->addTest(new LocalhostMultiTests('testISORequests'));
1392 $suite->addTest(new LocalhostMultiTests('testISOResponses'));
1393 $suite->addTest(new LocalhostMultiTests('testGzip'));
1394 $suite->addTest(new LocalhostMultiTests('testDeflate'));
1395 $suite->addTest(new LocalhostMultiTests('testProxy'));
1396 $suite->addTest(new LocalhostMultiTests('testHttp11'));
1397 $suite->addTest(new LocalhostMultiTests('testHttp11Gzip'));
1398 $suite->addTest(new LocalhostMultiTests('testHttp11Deflate'));
1399 $suite->addTest(new LocalhostMultiTests('testKeepAlives'));
1400 $suite->addTest(new LocalhostMultiTests('testHttp11Proxy'));
1401 $suite->addTest(new LocalhostMultiTests('testHttps'));
1402 $suite->addTest(new LocalhostMultiTests('testHttpsProxy'));
1404 $suite->addTest(new InvalidHostTests('test404'));
1405 //$suite->addTest(new InvalidHostTests('testSrvNotFound'));
1406 $suite->addTest(new InvalidHostTests('testCurlKAErr'));
1408 $suite->addTest(new ParsingBugsTests('testMinusOneString'));
1409 $suite->addTest(new ParsingBugsTests('testUnicodeInMemberName'));
1410 $suite->addTest(new ParsingBugsTests('testUnicodeInErrorString'));
1411 $suite->addTest(new ParsingBugsTests('testValidNumbers'));
1412 $suite->addTest(new ParsingBugsTests('testAddScalarToStruct'));
1413 $suite->addTest(new ParsingBugsTests('testAddStructToStruct'));
1414 $suite->addTest(new ParsingBugsTests('testAddArrayToArray'));
1415 $suite->addTest(new ParsingBugsTests('testEncodeArray'));
1416 $suite->addTest(new ParsingBugsTests('testEncodeRecursive'));
1417 $suite->addTest(new ParsingBugsTests('testBrokenrequests'));
1418 $suite->addTest(new ParsingBugsTests('testBrokenresponses'));
1419 $suite->addTest(new ParsingBugsTests('testBuggyHttp'));
1420 $suite->addTest(new ParsingBugsTests('testStringBug'));
1421 $suite->addTest(new ParsingBugsTests('testWhiteSpace'));
1422 $suite->addTest(new ParsingBugsTests('testAutodecodeResponse'));
1423 $suite->addTest(new ParsingBugsTests('testNoDecodeResponse'));
1424 $suite->addTest(new ParsingBugsTests('testAutoCoDec'));
1425 $suite->addTest(new ParsingBugsTests('testUTF8Response'));
1426 $suite->addTest(new ParsingBugsTests('testUTF8Request'));
1427 $suite->addTest(new ParsingBugsTests('testUTF8IntString'));
1428 $suite->addTest(new ParsingBugsTests('testStringInt'));
1429 $suite->addTest(new ParsingBugsTests('testStructMemExists'));
1430 $suite->addTest(new ParsingBugsTests('testDoubleDataInArrayTag'));
1431 $suite->addTest(new ParsingBugsTests('testDoubleStuffInValueTag'));
1432 $suite->addTest(new ParsingBugsTests('testNilValue'));
1433 $suite->addTest(new ParsingBugsTests('testLocale'));
1435 $title = 'XML-RPC Unit Tests';
1439 $suite = new PHPUnit_TestSuite($only);
1442 if(isset($_SERVER['REQUEST_METHOD']))
1444 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";
1451 if(isset($_SERVER['REQUEST_METHOD']))
1453 echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
1454 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";
1460 echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
1461 echo 'Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.$LOCALSERVER.$URI.' and https://'.$HTTPSSERVER.$HTTPSURI."\n\n";
1464 // do some basic timing measurement
1465 list($micro, $sec) = explode(' ', microtime());
1466 $start_time = $sec + $micro;
1468 $PHPUnit = new PHPUnit;
1469 $result = $PHPUnit->run($suite, ($DEBUG == 0 ? '.' : '<hr/>'));
1471 list($micro, $sec) = explode(' ', microtime());
1472 $end_time = $sec + $micro;
1474 if(!isset($_SERVER['REQUEST_METHOD']))
1476 echo $result->toString()."\n";
1479 if(isset($_SERVER['REQUEST_METHOD']))
1481 echo '<h3>'.$result->failureCount()." test failures</h3>\n";
1482 printf("Time spent: %.2f secs<br/>\n", $end_time - $start_time);
1486 echo $result->failureCount()." test failures\n";
1487 printf("Time spent: %.2f secs\n", $end_time - $start_time);
1490 if($result->failureCount() && !$DEBUG)
1492 $target = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=1' : $_SERVER['PHP_SELF'].'?DEBUG=1';
1493 $t2 = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&DEBUG=2' : $_SERVER['PHP_SELF'].'?DEBUG=2';
1494 if(isset($_SERVER['REQUEST_METHOD']))
1496 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";
1500 echo "Run testsuite with DEBUG=1 (or 2) to have more detail about tests results\n";
1504 if(isset($_SERVER['REQUEST_METHOD']))
1507 <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>
1508 <div id="opts" style="display: none;">
1509 <form method="GET" style="border: 1px solid silver; margin: 5px; padding: 5px; font-family: monospace;">
1510 HTTP Server: <input name="LOCALSERVER" size="30" value="<?php echo htmlspecialchars($LOCALSERVER); ?>"/> Path: <input name="URI" size="30" value="<?php echo htmlspecialchars($URI); ?>"/><br/>
1511 HTTPS Server: <input name="HTTPSSERVER" size="30" value="<?php echo htmlspecialchars($HTTPSSERVER); ?>"/> Path: <input name="HTTPSURI" size="30" value="<?php echo htmlspecialchars($HTTPSURI); ?>"/><br/>
1512 Proxy Server: <input name="PROXY" size="30" value="<?php echo isset($PROXY) ? htmlspecialchars($PROXY) : ''; ?>"/> <input type="submit" value="Run Testsuite"/>
1516 echo $result->toHTML()."\n</body>\n</html>\n";