PageRenderTime 77ms CodeModel.GetById 35ms RepoModel.GetById 5ms app.codeStats 0ms

/test/testsuite.php

https://github.com/mehrdadmhd/phpxmlrpc
PHP | 1524 lines | 1490 code | 23 blank | 11 comment | 25 complexity | 0ef259381b81a57776a4ae5a96920acd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. include(getcwd().'/parse_args.php');
  3. require_once('xmlrpc.inc');
  4. require_once('xmlrpcs.inc');
  5. require_once('xmlrpc_wrappers.inc');
  6. require_once 'phpunit.php';
  7. //require_once 'PHPUnit/TestDecorator.php';
  8. // let testuite run for the needed time
  9. if ((int)ini_get('max_execution_time') < 180)
  10. ini_set('max_execution_time', 180);
  11. $suite = new PHPUnit_TestSuite();
  12. // array with list of failed tests
  13. $failed_tests = array();
  14. class LocalhostTests extends PHPUnit_TestCase
  15. {
  16. var $client = null;
  17. var $method = 'http';
  18. var $timeout = 10;
  19. var $request_compression = null;
  20. var $accepted_compression = '';
  21. function fail($message = '')
  22. {
  23. PHPUnit_TestCase::fail($message);
  24. // save in global var that this particular test has failed
  25. // (but only if not called from subclass objects / multitests)
  26. if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests')
  27. {
  28. global $failed_tests;
  29. $trace = debug_backtrace();
  30. for ($i = 0; $i < count($trace); $i++)
  31. {
  32. if (strpos($trace[$i]['function'], 'test') === 0)
  33. {
  34. $failed_tests[$trace[$i]['function']] = true;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. function setUp()
  41. {
  42. global $DEBUG, $LOCALSERVER, $URI;
  43. $server = explode(':', $LOCALSERVER);
  44. if(count($server) > 1)
  45. {
  46. $this->client=new xmlrpc_client($URI, $server[0], $server[1]);
  47. }
  48. else
  49. {
  50. $this->client=new xmlrpc_client($URI, $LOCALSERVER);
  51. }
  52. if($DEBUG)
  53. {
  54. $this->client->setDebug($DEBUG);
  55. }
  56. $this->client->request_compression = $this->request_compression;
  57. $this->client->accepted_compression = $this->accepted_compression;
  58. }
  59. function send($msg, $errrorcode=0, $return_response=false)
  60. {
  61. $r = $this->client->send($msg, $this->timeout, $this->method);
  62. // for multicall, return directly array of responses
  63. if(is_array($r))
  64. {
  65. return $r;
  66. }
  67. $this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
  68. if(!$r->faultCode())
  69. {
  70. if($return_response)
  71. return $r;
  72. else
  73. return $r->value();
  74. }
  75. else
  76. {
  77. return null;
  78. }
  79. }
  80. function testString()
  81. {
  82. $sendstring="here are 3 \"entities\": < > & " .
  83. "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
  84. " - isn't that great? \\\"hackery\\\" at it's best " .
  85. " also don't want to miss out on \$item[0]. ".
  86. "The real weird stuff follows: CRLF here".chr(13).chr(10).
  87. "a simple CR here".chr(13).
  88. "a simple LF here".chr(10).
  89. "and then LFCR".chr(10).chr(13).
  90. "last but not least weird names: G".chr(252)."nter, El".chr(232)."ne, and an xml comment closing tag: -->";
  91. $f=new xmlrpcmsg('examples.stringecho', array(
  92. new xmlrpcval($sendstring, 'string')
  93. ));
  94. $v=$this->send($f);
  95. if($v)
  96. {
  97. // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
  98. // so we relax our tests...
  99. $l1 = strlen($sendstring);
  100. $l2 = strlen($v->scalarval());
  101. if ($l1 == $l2)
  102. $this->assertEquals($sendstring, $v->scalarval());
  103. else
  104. $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
  105. }
  106. }
  107. function testAddingDoubles()
  108. {
  109. // note that rounding errors mean we
  110. // keep precision to sensible levels here ;-)
  111. $a=12.13; $b=-23.98;
  112. $f=new xmlrpcmsg('examples.addtwodouble',array(
  113. new xmlrpcval($a, 'double'),
  114. new xmlrpcval($b, 'double')
  115. ));
  116. $v=$this->send($f);
  117. if($v)
  118. {
  119. $this->assertEquals($a+$b,$v->scalarval());
  120. }
  121. }
  122. function testAdding()
  123. {
  124. $f=new xmlrpcmsg('examples.addtwo',array(
  125. new xmlrpcval(12, 'int'),
  126. new xmlrpcval(-23, 'int')
  127. ));
  128. $v=$this->send($f);
  129. if($v)
  130. {
  131. $this->assertEquals(12-23, $v->scalarval());
  132. }
  133. }
  134. function testInvalidNumber()
  135. {
  136. $f=new xmlrpcmsg('examples.addtwo',array(
  137. new xmlrpcval('fred', 'int'),
  138. new xmlrpcval("\"; exec('ls')", 'int')
  139. ));
  140. $v=$this->send($f);
  141. /// @todo a fault condition should be generated here
  142. /// by the server, which we pick up on
  143. if($v)
  144. {
  145. $this->assertEquals(0, $v->scalarval());
  146. }
  147. }
  148. function testBoolean()
  149. {
  150. $f=new xmlrpcmsg('examples.invertBooleans', array(
  151. new xmlrpcval(array(
  152. new xmlrpcval(true, 'boolean'),
  153. new xmlrpcval(false, 'boolean'),
  154. new xmlrpcval(1, 'boolean'),
  155. new xmlrpcval(0, 'boolean'),
  156. //new xmlrpcval('true', 'boolean'),
  157. //new xmlrpcval('false', 'boolean')
  158. ),
  159. 'array'
  160. )));
  161. $answer='0101';
  162. $v=$this->send($f);
  163. if($v)
  164. {
  165. $sz=$v->arraysize();
  166. $got='';
  167. for($i=0; $i<$sz; $i++)
  168. {
  169. $b=$v->arraymem($i);
  170. if($b->scalarval())
  171. {
  172. $got.='1';
  173. }
  174. else
  175. {
  176. $got.='0';
  177. }
  178. }
  179. $this->assertEquals($answer, $got);
  180. }
  181. }
  182. function testBase64()
  183. {
  184. $sendstring='Mary had a little lamb,
  185. Whose fleece was white as snow,
  186. And everywhere that Mary went
  187. the lamb was sure to go.
  188. Mary had a little lamb
  189. She tied it to a pylon
  190. Ten thousand volts went down its back
  191. And turned it into nylon';
  192. $f=new xmlrpcmsg('examples.decode64',array(
  193. new xmlrpcval($sendstring, 'base64')
  194. ));
  195. $v=$this->send($f);
  196. if($v)
  197. {
  198. if (strlen($sendstring) == strlen($v->scalarval()))
  199. $this->assertEquals($sendstring, $v->scalarval());
  200. else
  201. $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
  202. }
  203. }
  204. function testDateTime()
  205. {
  206. $time = time();
  207. $t1 = new xmlrpcval($time, 'dateTime.iso8601');
  208. $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
  209. $this->assertEquals($t1->serialize(), $t2->serialize());
  210. if (class_exists('DateTime'))
  211. {
  212. $datetime = new DateTime();
  213. $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
  214. $this->assertEquals($t1->serialize(), $t3->serialize());
  215. }
  216. }
  217. function testCountEntities()
  218. {
  219. $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
  220. $f = new xmlrpcmsg('validator1.countTheEntities',array(
  221. new xmlrpcval($sendstring, 'string')
  222. ));
  223. $v = $this->send($f);
  224. if($v)
  225. {
  226. $got = '';
  227. $expected = '37210';
  228. $expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
  229. while(list(,$val) = each($expect_array))
  230. {
  231. $b = $v->structmem($val);
  232. $got .= $b->me['int'];
  233. }
  234. $this->assertEquals($expected, $got);
  235. }
  236. }
  237. function _multicall_msg($method, $params)
  238. {
  239. $struct['methodName'] = new xmlrpcval($method, 'string');
  240. $struct['params'] = new xmlrpcval($params, 'array');
  241. return new xmlrpcval($struct, 'struct');
  242. }
  243. function testServerMulticall()
  244. {
  245. // We manually construct a system.multicall() call to ensure
  246. // that the server supports it.
  247. // NB: This test will NOT pass if server does not support system.multicall.
  248. // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
  249. $good1 = $this->_multicall_msg(
  250. 'system.methodHelp',
  251. array(php_xmlrpc_encode('system.listMethods')));
  252. $bad = $this->_multicall_msg(
  253. 'test.nosuch',
  254. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
  255. $recursive = $this->_multicall_msg(
  256. 'system.multicall',
  257. array(new xmlrpcval(array(), 'array')));
  258. $good2 = $this->_multicall_msg(
  259. 'system.methodSignature',
  260. array(php_xmlrpc_encode('system.listMethods')));
  261. $arg = new xmlrpcval(
  262. array($good1, $bad, $recursive, $good2),
  263. 'array'
  264. );
  265. $f = new xmlrpcmsg('system.multicall', array($arg));
  266. $v = $this->send($f);
  267. if($v)
  268. {
  269. //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
  270. $this->assertTrue($v->arraysize() == 4, "bad number of return values");
  271. $r1 = $v->arraymem(0);
  272. $this->assertTrue(
  273. $r1->kindOf() == 'array' && $r1->arraysize() == 1,
  274. "did not get array of size 1 from good1"
  275. );
  276. $r2 = $v->arraymem(1);
  277. $this->assertTrue(
  278. $r2->kindOf() == 'struct',
  279. "no fault from bad"
  280. );
  281. $r3 = $v->arraymem(2);
  282. $this->assertTrue(
  283. $r3->kindOf() == 'struct',
  284. "recursive system.multicall did not fail"
  285. );
  286. $r4 = $v->arraymem(3);
  287. $this->assertTrue(
  288. $r4->kindOf() == 'array' && $r4->arraysize() == 1,
  289. "did not get array of size 1 from good2"
  290. );
  291. }
  292. }
  293. function testClientMulticall1()
  294. {
  295. // NB: This test will NOT pass if server does not support system.multicall.
  296. $this->client->no_multicall = false;
  297. $good1 = new xmlrpcmsg('system.methodHelp',
  298. array(php_xmlrpc_encode('system.listMethods')));
  299. $bad = new xmlrpcmsg('test.nosuch',
  300. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
  301. $recursive = new xmlrpcmsg('system.multicall',
  302. array(new xmlrpcval(array(), 'array')));
  303. $good2 = new xmlrpcmsg('system.methodSignature',
  304. array(php_xmlrpc_encode('system.listMethods'))
  305. );
  306. $r = $this->send(array($good1, $bad, $recursive, $good2));
  307. if($r)
  308. {
  309. $this->assertTrue(count($r) == 4, "wrong number of return values");
  310. }
  311. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
  312. if(!$r[0]->faultCode())
  313. {
  314. $val = $r[0]->value();
  315. $this->assertTrue(
  316. $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
  317. "good1 did not return string"
  318. );
  319. }
  320. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
  321. $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
  322. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
  323. if(!$r[3]->faultCode())
  324. {
  325. $val = $r[3]->value();
  326. $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
  327. }
  328. // This is the only assert in this test which should fail
  329. // if the test server does not support system.multicall.
  330. $this->assertTrue($this->client->no_multicall == false,
  331. "server does not support system.multicall"
  332. );
  333. }
  334. function testClientMulticall2()
  335. {
  336. // NB: This test will NOT pass if server does not support system.multicall.
  337. $this->client->no_multicall = true;
  338. $good1 = new xmlrpcmsg('system.methodHelp',
  339. array(php_xmlrpc_encode('system.listMethods')));
  340. $bad = new xmlrpcmsg('test.nosuch',
  341. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
  342. $recursive = new xmlrpcmsg('system.multicall',
  343. array(new xmlrpcval(array(), 'array')));
  344. $good2 = new xmlrpcmsg('system.methodSignature',
  345. array(php_xmlrpc_encode('system.listMethods'))
  346. );
  347. $r = $this->send(array($good1, $bad, $recursive, $good2));
  348. if($r)
  349. {
  350. $this->assertTrue(count($r) == 4, "wrong number of return values");
  351. }
  352. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
  353. if(!$r[0]->faultCode())
  354. {
  355. $val = $r[0]->value();
  356. $this->assertTrue(
  357. $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
  358. "good1 did not return string");
  359. }
  360. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
  361. $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
  362. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
  363. if(!$r[3]->faultCode())
  364. {
  365. $val = $r[3]->value();
  366. $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
  367. }
  368. }
  369. function testClientMulticall3()
  370. {
  371. // NB: This test will NOT pass if server does not support system.multicall.
  372. $this->client->return_type = 'phpvals';
  373. $this->client->no_multicall = false;
  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'))
  382. );
  383. $r = $this->send(array($good1, $bad, $recursive, $good2));
  384. if($r)
  385. {
  386. $this->assertTrue(count($r) == 4, "wrong number of return values");
  387. }
  388. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
  389. if(!$r[0]->faultCode())
  390. {
  391. $val = $r[0]->value();
  392. $this->assertTrue(
  393. is_string($val) , "good1 did not return string");
  394. }
  395. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
  396. $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
  397. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
  398. if(!$r[3]->faultCode())
  399. {
  400. $val = $r[3]->value();
  401. $this->assertTrue(is_array($val), "good2 did not return array");
  402. }
  403. $this->client->return_type = 'xmlrpcvals';
  404. }
  405. function testCatchWarnings()
  406. {
  407. $f = new xmlrpcmsg('examples.generatePHPWarning', array(
  408. new xmlrpcval('whatever', 'string')
  409. ));
  410. $v = $this->send($f);
  411. if($v)
  412. {
  413. $this->assertEquals($v->scalarval(), true);
  414. }
  415. }
  416. function testCatchExceptions()
  417. {
  418. global $URI;
  419. $f = new xmlrpcmsg('examples.raiseException', array(
  420. new xmlrpcval('whatever', 'string')
  421. ));
  422. $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
  423. $this->client->path = $URI.'?EXCEPTION_HANDLING=1';
  424. $v = $this->send($f, 1);
  425. $this->client->path = $URI.'?EXCEPTION_HANDLING=2';
  426. $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
  427. }
  428. function testZeroParams()
  429. {
  430. $f = new xmlrpcmsg('system.listMethods');
  431. $v = $this->send($f);
  432. }
  433. function testCodeInjectionServerSide()
  434. {
  435. $f = new xmlrpcmsg('system.MethodHelp');
  436. $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>";
  437. $v = $this->send($f);
  438. //$v = $r->faultCode();
  439. if ($v)
  440. {
  441. $this->assertEquals(0, $v->structsize());
  442. }
  443. }
  444. function testAutoRegisteredFunction()
  445. {
  446. $f=new xmlrpcmsg('examples.php.getStateName',array(
  447. new xmlrpcval(23, 'int')
  448. ));
  449. $v=$this->send($f);
  450. if($v)
  451. {
  452. $this->assertEquals('Michigan', $v->scalarval());
  453. }
  454. else
  455. {
  456. $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
  457. }
  458. }
  459. function testAutoRegisteredClass()
  460. {
  461. $f=new xmlrpcmsg('examples.php2.getStateName',array(
  462. new xmlrpcval(23, 'int')
  463. ));
  464. $v=$this->send($f);
  465. if($v)
  466. {
  467. $this->assertEquals('Michigan', $v->scalarval());
  468. $f=new xmlrpcmsg('examples.php3.getStateName',array(
  469. new xmlrpcval(23, 'int')
  470. ));
  471. $v=$this->send($f);
  472. if($v)
  473. {
  474. $this->assertEquals('Michigan', $v->scalarval());
  475. }
  476. }
  477. else
  478. {
  479. $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
  480. }
  481. }
  482. function testAutoRegisteredMethod()
  483. {
  484. $func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
  485. if($func == '')
  486. {
  487. $this->fail('Registration of examples.getStateName failed');
  488. }
  489. else
  490. {
  491. $v=$func(23);
  492. $this->assertEquals('Michigan', $v);
  493. }
  494. }
  495. function testGetCookies()
  496. {
  497. // let server set to us some cookies we tell it
  498. $cookies = array(
  499. //'c1' => array(),
  500. 'c2' => array('value' => 'c2'),
  501. 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
  502. 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
  503. 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
  504. );
  505. $cookiesval = php_xmlrpc_encode($cookies);
  506. $f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
  507. $r=$this->send($f, 0, true);
  508. if($r)
  509. {
  510. $v = $r->value();
  511. $this->assertEquals(1, $v->scalarval());
  512. // now check if we decoded the cookies as we had set them
  513. $rcookies = $r->cookies();
  514. foreach($cookies as $c => $v)
  515. // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
  516. // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
  517. if(isset($v['expires']))
  518. {
  519. if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
  520. {
  521. $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
  522. }
  523. else
  524. {
  525. $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
  526. }
  527. }
  528. $this->assertEquals($cookies, $rcookies);
  529. }
  530. }
  531. function testSetCookies()
  532. {
  533. // let server set to us some cookies we tell it
  534. $cookies = array(
  535. 'c0' => null,
  536. 'c1' => 1,
  537. 'c2' => '2 3',
  538. 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
  539. );
  540. $f=new xmlrpcmsg('examples.getcookies',array());
  541. foreach ($cookies as $cookie => $val)
  542. {
  543. $this->client->setCookie($cookie, $val);
  544. $cookies[$cookie] = (string) $cookies[$cookie];
  545. }
  546. $r = $this->client->send($f, $this->timeout, $this->method);
  547. $this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
  548. if(!$r->faultCode())
  549. {
  550. $v = $r->value();
  551. $v = php_xmlrpc_decode($v);
  552. // on IIS and Apache getallheaders returns something slightly different...
  553. $this->assertEquals($v, $cookies);
  554. }
  555. }
  556. function testSendTwiceSameMsg()
  557. {
  558. $f=new xmlrpcmsg('examples.stringecho', array(
  559. new xmlrpcval('hello world', 'string')
  560. ));
  561. $v1 = $this->send($f);
  562. $v2 = $this->send($f);
  563. //$v = $r->faultCode();
  564. if ($v1 && $v2)
  565. {
  566. $this->assertEquals($v2, $v1);
  567. }
  568. }
  569. }
  570. class LocalHostMultiTests extends LocalhostTests
  571. {
  572. function _runtests()
  573. {
  574. global $failed_tests;
  575. foreach(get_class_methods('LocalhostTests') as $meth)
  576. {
  577. if(strpos($meth, 'test') === 0 && $meth != 'testHttps' && $meth != 'testCatchExceptions')
  578. {
  579. if (!isset($failed_tests[$meth]))
  580. $this->$meth();
  581. }
  582. if ($this->_failed)
  583. {
  584. break;
  585. }
  586. }
  587. }
  588. function testDeflate()
  589. {
  590. if(!function_exists('gzdeflate'))
  591. {
  592. $this->fail('Zlib missing: cannot test deflate functionality');
  593. return;
  594. }
  595. $this->client->accepted_compression = array('deflate');
  596. $this->client->request_compression = 'deflate';
  597. $this->_runtests();
  598. }
  599. function testGzip()
  600. {
  601. if(!function_exists('gzdeflate'))
  602. {
  603. $this->fail('Zlib missing: cannot test gzip functionality');
  604. return;
  605. }
  606. $this->client->accepted_compression = array('gzip');
  607. $this->client->request_compression = 'gzip';
  608. $this->_runtests();
  609. }
  610. function testKeepAlives()
  611. {
  612. if(!function_exists('curl_init'))
  613. {
  614. $this->fail('CURL missing: cannot test http 1.1');
  615. return;
  616. }
  617. $this->method = 'http11';
  618. $this->client->keepalive = true;
  619. $this->_runtests();
  620. }
  621. function testProxy()
  622. {
  623. global $PROXYSERVER, $PROXYPORT;
  624. if ($PROXYSERVER)
  625. {
  626. $this->client->setProxy($PROXYSERVER, $PROXYPORT);
  627. $this->_runtests();
  628. }
  629. else
  630. $this->fail('PROXY definition missing: cannot test proxy');
  631. }
  632. function testHttp11()
  633. {
  634. if(!function_exists('curl_init'))
  635. {
  636. $this->fail('CURL missing: cannot test http 1.1');
  637. return;
  638. }
  639. $this->method = 'http11'; // not an error the double assignment!
  640. $this->client->method = 'http11';
  641. //$this->client->verifyhost = 0;
  642. //$this->client->verifypeer = 0;
  643. $this->client->keepalive = false;
  644. $this->_runtests();
  645. }
  646. function testHttp11Gzip()
  647. {
  648. if(!function_exists('curl_init'))
  649. {
  650. $this->fail('CURL missing: cannot test http 1.1');
  651. return;
  652. }
  653. $this->method = 'http11'; // not an error the double assignment!
  654. $this->client->method = 'http11';
  655. $this->client->keepalive = false;
  656. $this->client->accepted_compression = array('gzip');
  657. $this->client->request_compression = 'gzip';
  658. $this->_runtests();
  659. }
  660. function testHttp11Deflate()
  661. {
  662. if(!function_exists('curl_init'))
  663. {
  664. $this->fail('CURL missing: cannot test http 1.1');
  665. return;
  666. }
  667. $this->method = 'http11'; // not an error the double assignment!
  668. $this->client->method = 'http11';
  669. $this->client->keepalive = false;
  670. $this->client->accepted_compression = array('deflate');
  671. $this->client->request_compression = 'deflate';
  672. $this->_runtests();
  673. }
  674. function testHttp11Proxy()
  675. {
  676. global $PROXYSERVER, $PROXYPORT;
  677. if(!function_exists('curl_init'))
  678. {
  679. $this->fail('CURL missing: cannot test http 1.1 w. proxy');
  680. return;
  681. }
  682. else if ($PROXYSERVER == '')
  683. {
  684. $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
  685. return;
  686. }
  687. $this->method = 'http11'; // not an error the double assignment!
  688. $this->client->method = 'http11';
  689. $this->client->setProxy($PROXYSERVER, $PROXYPORT);
  690. //$this->client->verifyhost = 0;
  691. //$this->client->verifypeer = 0;
  692. $this->client->keepalive = false;
  693. $this->_runtests();
  694. }
  695. function testHttps()
  696. {
  697. global $HTTPSSERVER, $HTTPSURI, $HTTPSIGNOREPEER;
  698. if(!function_exists('curl_init'))
  699. {
  700. $this->fail('CURL missing: cannot test https functionality');
  701. return;
  702. }
  703. $this->client->server = $HTTPSSERVER;
  704. $this->method = 'https';
  705. $this->client->method = 'https';
  706. $this->client->path = $HTTPSURI;
  707. $this->client->setSSLVerifyPeer( !$HTTPSIGNOREPEER );
  708. $this->_runtests();
  709. }
  710. function testHttpsProxy()
  711. {
  712. global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
  713. if(!function_exists('curl_init'))
  714. {
  715. $this->fail('CURL missing: cannot test https functionality');
  716. return;
  717. }
  718. else if ($PROXYSERVER == '')
  719. {
  720. $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
  721. return;
  722. }
  723. $this->client->server = $HTTPSSERVER;
  724. $this->method = 'https';
  725. $this->client->method = 'https';
  726. $this->client->setProxy($PROXYSERVER, $PROXYPORT);
  727. $this->client->path = $HTTPSURI;
  728. $this->_runtests();
  729. }
  730. function testUTF8Responses()
  731. {
  732. global $URI;
  733. //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
  734. $this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
  735. $this->_runtests();
  736. }
  737. function testUTF8Requests()
  738. {
  739. $this->client->request_charset_encoding = 'UTF-8';
  740. $this->_runtests();
  741. }
  742. function testISOResponses()
  743. {
  744. global $URI;
  745. //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
  746. $this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
  747. $this->_runtests();
  748. }
  749. function testISORequests()
  750. {
  751. $this->client->request_charset_encoding = 'ISO-8859-1';
  752. $this->_runtests();
  753. }
  754. }
  755. class ParsingBugsTests extends PHPUnit_TestCase
  756. {
  757. function testMinusOneString()
  758. {
  759. $v=new xmlrpcval('-1');
  760. $u=new xmlrpcval('-1', 'string');
  761. $this->assertEquals($u->scalarval(), $v->scalarval());
  762. }
  763. function testUnicodeInMemberName(){
  764. $str = "G".chr(252)."nter, El".chr(232)."ne";
  765. $v = array($str => new xmlrpcval(1));
  766. $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
  767. $r = $r->serialize();
  768. $m = new xmlrpcmsg('dummy');
  769. $r = $m->parseResponse($r);
  770. $v = $r->value();
  771. $this->assertEquals($v->structmemexists($str), true);
  772. }
  773. function testUnicodeInErrorString()
  774. {
  775. $response = utf8_encode(
  776. '<?xml version="1.0"?>
  777. <!-- $Id -->
  778. <!-- found by G. giunta, covers what happens when lib receives
  779. UTF8 chars in response text and comments -->
  780. <!-- ���&#224;&#252;&#232; -->
  781. <methodResponse>
  782. <fault>
  783. <value>
  784. <struct>
  785. <member>
  786. <name>faultCode</name>
  787. <value><int>888</int></value>
  788. </member>
  789. <member>
  790. <name>faultString</name>
  791. <value><string>���&#224;&#252;&#232;</string></value>
  792. </member>
  793. </struct>
  794. </value>
  795. </fault>
  796. </methodResponse>');
  797. $m=new xmlrpcmsg('dummy');
  798. $r=$m->parseResponse($response);
  799. $v=$r->faultString();
  800. $this->assertEquals('���ŕüč', $v);
  801. }
  802. function testValidNumbers()
  803. {
  804. $m=new xmlrpcmsg('dummy');
  805. $fp=
  806. '<?xml version="1.0"?>
  807. <methodResponse>
  808. <params>
  809. <param>
  810. <value>
  811. <struct>
  812. <member>
  813. <name>integer1</name>
  814. <value><int>01</int></value>
  815. </member>
  816. <member>
  817. <name>float1</name>
  818. <value><double>01.10</double></value>
  819. </member>
  820. <member>
  821. <name>integer2</name>
  822. <value><int>+1</int></value>
  823. </member>
  824. <member>
  825. <name>float2</name>
  826. <value><double>+1.10</double></value>
  827. </member>
  828. <member>
  829. <name>float3</name>
  830. <value><double>-1.10e2</double></value>
  831. </member>
  832. </struct>
  833. </value>
  834. </param>
  835. </params>
  836. </methodResponse>';
  837. $r=$m->parseResponse($fp);
  838. $v=$r->value();
  839. $s=$v->structmem('integer1');
  840. $t=$v->structmem('float1');
  841. $u=$v->structmem('integer2');
  842. $w=$v->structmem('float2');
  843. $x=$v->structmem('float3');
  844. $this->assertEquals(1, $s->scalarval());
  845. $this->assertEquals(1.1, $t->scalarval());
  846. $this->assertEquals(1, $u->scalarval());
  847. $this->assertEquals(1.1, $w->scalarval());
  848. $this->assertEquals(-110.0, $x->scalarval());
  849. }
  850. function testAddScalarToStruct()
  851. {
  852. $v=new xmlrpcval(array('a' => 'b'), 'struct');
  853. // use @ operator in case error_log gets on screen
  854. $r= @$v->addscalar('c');
  855. $this->assertEquals(0, $r);
  856. }
  857. function testAddStructToStruct()
  858. {
  859. $v=new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
  860. $r=$v->addstruct(array('b' => new xmlrpcval('c')));
  861. $this->assertEquals(2, $v->structsize());
  862. $this->assertEquals(1, $r);
  863. $r=$v->addstruct(array('b' => new xmlrpcval('b')));
  864. $this->assertEquals(2, $v->structsize());
  865. }
  866. function testAddArrayToArray()
  867. {
  868. $v=new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
  869. $r=$v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
  870. $this->assertEquals(4, $v->arraysize());
  871. $this->assertEquals(1, $r);
  872. }
  873. function testEncodeArray()
  874. {
  875. $r=range(1, 100);
  876. $v = php_xmlrpc_encode($r);
  877. $this->assertEquals('array', $v->kindof());
  878. }
  879. function testEncodeRecursive()
  880. {
  881. $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
  882. $this->assertEquals('scalar', $v->kindof());
  883. }
  884. function testBrokenRequests()
  885. {
  886. $s = new xmlrpc_server();
  887. // omitting the 'params' tag: not tolerated by the lib anymore
  888. $f = '<?xml version="1.0"?>
  889. <methodCall>
  890. <methodName>system.methodHelp</methodName>
  891. <param>
  892. <value><string>system.methodHelp</string></value>
  893. </param>
  894. </methodCall>';
  895. $r = $s->parserequest($f);
  896. $this->assertEquals(15, $r->faultCode());
  897. // omitting a 'param' tag
  898. $f = '<?xml version="1.0"?>
  899. <methodCall>
  900. <methodName>system.methodHelp</methodName>
  901. <params>
  902. <value><string>system.methodHelp</string></value>
  903. </params>
  904. </methodCall>';
  905. $r = $s->parserequest($f);
  906. $this->assertEquals(15, $r->faultCode());
  907. // omitting a 'value' tag
  908. $f = '<?xml version="1.0"?>
  909. <methodCall>
  910. <methodName>system.methodHelp</methodName>
  911. <params>
  912. <param><string>system.methodHelp</string></param>
  913. </params>
  914. </methodCall>';
  915. $r = $s->parserequest($f);
  916. $this->assertEquals(15, $r->faultCode());
  917. }
  918. function testBrokenResponses()
  919. {
  920. $m=new xmlrpcmsg('dummy');
  921. //$m->debug = 1;
  922. // omitting the 'params' tag: no more tolerated by the lib...
  923. $f = '<?xml version="1.0"?>
  924. <methodResponse>
  925. <param>
  926. <value><string>system.methodHelp</string></value>
  927. </param>
  928. </methodResponse>';
  929. $r = $m->parseResponse($f);
  930. $this->assertEquals(2, $r->faultCode());
  931. // omitting the 'param' tag: no more tolerated by the lib...
  932. $f = '<?xml version="1.0"?>
  933. <methodResponse>
  934. <params>
  935. <value><string>system.methodHelp</string></value>
  936. </params>
  937. </methodResponse>';
  938. $r = $m->parseResponse($f);
  939. $this->assertEquals(2, $r->faultCode());
  940. // omitting a 'value' tag: KO
  941. $f = '<?xml version="1.0"?>
  942. <methodResponse>
  943. <params>
  944. <param><string>system.methodHelp</string></param>
  945. </params>
  946. </methodResponse>';
  947. $r = $m->parseResponse($f);
  948. $this->assertEquals(2, $r->faultCode());
  949. }
  950. function testBuggyHttp()
  951. {
  952. $s = new xmlrpcmsg('dummy');
  953. $f = 'HTTP/1.1 100 Welcome to the jungle
  954. HTTP/1.0 200 OK
  955. X-Content-Marx-Brothers: Harpo
  956. Chico and Groucho
  957. Content-Length: who knows?
  958. <?xml version="1.0"?>
  959. <!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
  960. <methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
  961. <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
  962. and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
  963. <script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
  964. ';
  965. $r = $s->parseResponse($f);
  966. $v = $r->value();
  967. $s = $v->structmem('content');
  968. $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
  969. }
  970. function testStringBug()
  971. {
  972. $s = new xmlrpcmsg('dummy');
  973. $f = '<?xml version="1.0"?>
  974. <!-- $Id -->
  975. <!-- found by 2z69xks7bpy001@sneakemail.com, amongst others
  976. covers what happens when there\'s character data after </string>
  977. and before </value> -->
  978. <methodResponse>
  979. <params>
  980. <param>
  981. <value>
  982. <struct>
  983. <member>
  984. <name>success</name>
  985. <value>
  986. <boolean>1</boolean>
  987. </value>
  988. </member>
  989. <member>
  990. <name>sessionID</name>
  991. <value>
  992. <string>S300510007I</string>
  993. </value>
  994. </member>
  995. </struct>
  996. </value>
  997. </param>
  998. </params>
  999. </methodResponse> ';
  1000. $r = $s->parseResponse($f);
  1001. $v = $r->value();
  1002. $s = $v->structmem('sessionID');
  1003. $this->assertEquals('S300510007I', $s->scalarval());
  1004. }
  1005. function testWhiteSpace()
  1006. {
  1007. $s = new xmlrpcmsg('dummy');
  1008. $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
  1009. <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
  1010. and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
  1011. ';
  1012. $r = $s->parseResponse($f);
  1013. $v = $r->value();
  1014. $s = $v->structmem('content');
  1015. $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
  1016. }
  1017. function testDoubleDataInArrayTag()
  1018. {
  1019. $s = new xmlrpcmsg('dummy');
  1020. $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
  1021. <data></data>
  1022. <data></data>
  1023. </array></value></param></params></methodResponse>
  1024. ';
  1025. $r = $s->parseResponse($f);
  1026. $v = $r->faultCode();
  1027. $this->assertEquals(2, $v);
  1028. $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
  1029. <data><value>Hello world</value></data>
  1030. <data></data>
  1031. </array></value></param></params></methodResponse>
  1032. ';
  1033. $r = $s->parseResponse($f);
  1034. $v = $r->faultCode();
  1035. $this->assertEquals(2, $v);
  1036. }
  1037. function testDoubleStuffInValueTag()
  1038. {
  1039. $s = new xmlrpcmsg('dummy');
  1040. $f = '<?xml version="1.0"?><methodResponse><params><param><value>
  1041. <string>hello world</string>
  1042. <array><data></data></array>
  1043. </value></param></params></methodResponse>
  1044. ';
  1045. $r = $s->parseResponse($f);
  1046. $v = $r->faultCode();
  1047. $this->assertEquals(2, $v);
  1048. $f = '<?xml version="1.0"?><methodResponse><params><param><value>
  1049. <string>hello</string>
  1050. <string>world</string>
  1051. </value></param></params></methodResponse>
  1052. ';
  1053. $r = $s->parseResponse($f);
  1054. $v = $r->faultCode();
  1055. $this->assertEquals(2, $v);
  1056. $f = '<?xml version="1.0"?><methodResponse><params><param><value>
  1057. <string>hello</string>
  1058. <struct><member><name>hello><value>world</value></member></struct>
  1059. </value></param></params></methodResponse>
  1060. ';
  1061. $r = $s->parseResponse($f);
  1062. $v = $r->faultCode();
  1063. $this->assertEquals(2, $v);
  1064. }
  1065. function testAutodecodeResponse()
  1066. {
  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
  1070. and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
  1071. ';
  1072. $r = $s->parseResponse($f, true, 'phpvals');
  1073. $v = $r->value();
  1074. $s = $v['content'];
  1075. $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s);
  1076. }
  1077. function testNoDecodeResponse()
  1078. {
  1079. $s = new xmlrpcmsg('dummy');
  1080. $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
  1081. <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
  1082. and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
  1083. $r = $s->parseResponse($f, true, 'xml');
  1084. $v = $r->value();
  1085. $this->assertEquals($f, $v);
  1086. }
  1087. function testAutoCoDec()
  1088. {
  1089. $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
  1090. $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
  1091. $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
  1092. //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
  1093. $v1 = php_xmlrpc_encode($data, array('auto_dates'));
  1094. $v2 = php_xmlrpc_decode_xml($v1->serialize());
  1095. $this->assertEquals($v1, $v2);
  1096. $r1 = new xmlrpcresp($v1);
  1097. $r2 = php_xmlrpc_decode_xml($r1->serialize());
  1098. $r2->serialize(); // needed to set internal member payload
  1099. $this->assertEquals($r1, $r2);
  1100. $m1 = new xmlrpcmsg('hello dolly', array($v1));
  1101. $m2 = php_xmlrpc_decode_xml($m1->serialize());
  1102. $m2->serialize(); // needed to set internal member payload
  1103. $this->assertEquals($m1, $m2);
  1104. }
  1105. function testUTF8Request()
  1106. {
  1107. $sendstring='κ὚σΟξ'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
  1108. $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
  1109. $f = new xmlrpcval($sendstring, 'string');
  1110. $v=$f->serialize();
  1111. $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
  1112. $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
  1113. }
  1114. function testUTF8Response()
  1115. {
  1116. $s = new xmlrpcmsg('dummy');
  1117. $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>
  1118. <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>
  1119. ';
  1120. $r = $s->parseResponse($f, false, 'phpvals');
  1121. $v = $r->value();
  1122. $v = $v['content'];
  1123. $this->assertEquals("������", $v);
  1124. $f = '<?xml version="1.0" encoding="utf-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
  1125. <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>
  1126. ';
  1127. $r = $s->parseResponse($f, false, 'phpvals');
  1128. $v = $r->value();
  1129. $v = $v['content'];
  1130. $this->assertEquals("������", $v);
  1131. }
  1132. function testUTF8IntString()
  1133. {
  1134. $v=new xmlrpcval(100, 'int');
  1135. $s=$v->serialize('UTF-8');
  1136. $this->assertequals("<value><int>100</int></value>\n", $s);
  1137. }
  1138. function testStringInt()
  1139. {
  1140. $v=new xmlrpcval('hello world', 'int');
  1141. $s=$v->serialize();
  1142. $this->assertequals("<value><int>0</int></value>\n", $s);
  1143. }
  1144. function testStructMemExists()
  1145. {
  1146. $v=php_xmlrpc_encode(array('hello' => 'world'));
  1147. $b=$v->structmemexists('hello');
  1148. $this->assertequals(true, $b);
  1149. $b=$v->structmemexists('world');
  1150. $this->assertequals(false, $b);
  1151. }
  1152. function testNilvalue()
  1153. {
  1154. // default case: we do not accept nil values received
  1155. $v = new xmlrpcval('hello', 'null');
  1156. $r = new xmlrpcresp($v);
  1157. $s = $r->serialize();
  1158. $m = new xmlrpcmsg('dummy');
  1159. $r = $m->parseresponse($s);
  1160. $this->assertequals(2, $r->faultCode());
  1161. // enable reception of nil values
  1162. $GLOBALS['xmlrpc_null_extension'] = true;
  1163. $r = $m->parseresponse($s);
  1164. $v = $r->value();
  1165. $this->assertequals('null', $v->scalartyp());
  1166. // test with the apache version: EX:NIL
  1167. $GLOBALS['xmlrpc_null_apache_encoding'] = true;
  1168. // serialization
  1169. $v = new xmlrpcval('hello', 'null');
  1170. $s = $v->serialize();
  1171. $this->assertequals(1, preg_match( '#<value><ex:nil/></value>#', $s ));
  1172. // deserialization
  1173. $r = new xmlrpcresp($v);
  1174. $s = $r->serialize();
  1175. $r = $m->parseresponse($s);
  1176. $v = $r->value();
  1177. $this->assertequals('null', $v->scalartyp());
  1178. $GLOBALS['xmlrpc_null_extension'] = false;
  1179. $r = $m->parseresponse($s);
  1180. $this->assertequals(2, $r->faultCode());
  1181. }
  1182. function TestLocale()
  1183. {
  1184. $locale = setlocale(LC_NUMERIC, 0);
  1185. /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
  1186. if (setlocale(LC_NUMERIC,'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false)
  1187. {
  1188. $v = new xmlrpcval(1.1, 'double');
  1189. if (strpos($v->scalarval(), ',') == 1)
  1190. {
  1191. $r = $v->serialize();
  1192. $this->assertequals(false, strpos($r, ','));
  1193. }
  1194. setlocale(LC_NUMERIC, $locale);
  1195. }
  1196. }
  1197. }
  1198. class InvalidHostTests extends PHPUnit_TestCase
  1199. {
  1200. var $client = null;
  1201. function setUp()
  1202. {
  1203. global $DEBUG,$LOCALSERVER;
  1204. $this->client=new xmlrpc_client('/NOTEXIST.php', $LOCALSERVER, 80);
  1205. if($DEBUG)
  1206. {
  1207. $this->client->setDebug($DEBUG);
  1208. }
  1209. }
  1210. function test404()
  1211. {
  1212. $f = new xmlrpcmsg('examples.echo',array(
  1213. new xmlrpcval('hello', 'string')
  1214. ));
  1215. $r = $this->client->send($f, 5);
  1216. $this->assertEquals(5, $r->faultCode());
  1217. }
  1218. function testSrvNotFound()
  1219. {
  1220. $f = new xmlrpcmsg('examples.echo',array(
  1221. new xmlrpcval('hello', 'string')
  1222. ));
  1223. $this->client->server .= 'XXX';
  1224. $r = $this->client->send($f, 5);
  1225. $this->assertEquals(5, $r->faultCode());
  1226. }
  1227. function testCurlKAErr()
  1228. {
  1229. global $LOCALSERVER, $URI;
  1230. if(!function_exists('curl_init'))
  1231. {
  1232. $this->fail('CURL missing: cannot test curl keepalive errors');
  1233. return;
  1234. }
  1235. $f = new xmlrpcmsg('examples.stringecho',array(
  1236. new xmlrpcval('hello', 'string')
  1237. ));
  1238. // test 2 calls w. keepalive: 1st time connection ko, second time ok
  1239. $this->client->server .= 'XXX';
  1240. $this->client->keepalive = true;
  1241. $r = $this->client->send($f, 5, 'http11');
  1242. // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
  1243. $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
  1244. // now test a successful connection
  1245. $server = explode(':', $LOCALSERVER);
  1246. if(count($server) > 1)
  1247. {
  1248. $this->client->port = $server[1];
  1249. }
  1250. $this->client->server = $server[0];
  1251. $this->client->path = $URI;
  1252. $r = $this->client->send($f, 5, 'http11');
  1253. $this->assertEquals(0, $r->faultCode());
  1254. $ro = $r->value();
  1255. is_object( $ro ) && $this->assertEquals('hello', $ro->scalarVal());
  1256. }
  1257. }
  1258. $suite->addTest(new LocalhostTests('testString'));
  1259. $suite->addTest(new LocalhostTests('testAdding'));
  1260. $suite->addTest(new LocalhostTests('testAddingDoubles'));
  1261. $suite->addTest(new LocalhostTests('testInvalidNumber'));
  1262. $suite->addTest(new LocalhostTests('testBoolean'));
  1263. $suite->addTest(new LocalhostTests('testCountEntities'));
  1264. $suite->addTest(new LocalhostTests('testBase64'));
  1265. $suite->addTest(new LocalhostTests('testDateTime'));
  1266. $suite->addTest(new LocalhostTests('testServerMulticall'));
  1267. $suite->addTest(new LocalhostTests('testClientMulticall1'));
  1268. $suite->addTest(new LocalhostTests('testClientMulticall2'));
  1269. $suite->addTest(new LocalhostTests('testClientMulticall3'));
  1270. $suite->addTest(new LocalhostTests('testCatchWarnings'));
  1271. $suite->addTest(new LocalhostTests('testCatchExceptions'));
  1272. $suite->addTest(new LocalhostTests('testZeroParams'));
  1273. $suite->addTest(new LocalhostTests('testCodeInjectionServerSide'));
  1274. $suite->addTest(new LocalhostTests('testAutoRegisteredFunction'));
  1275. $suite->addTest(new LocalhostTests('testAutoRegisteredMethod'));
  1276. $suite->addTest(new LocalhostTests('testSetCookies'));
  1277. $suite->addTest(new LocalhostTests('testGetCookies'));
  1278. $suite->addTest(new LocalhostTests('testSendTwiceSameMsg'));
  1279. $suite->addTest(new LocalhostMultiTests('testUTF8Requests'));
  1280. $suite->addTest(new LocalhostMultiTests('testUTF8Responses'));
  1281. $suite->addTest(new LocalhostMultiTests('testISORequests'));
  1282. $suite->addTest(new LocalhostMultiTests('testISOResponses'));
  1283. $suite->addTest(new LocalhostMultiTests('testGzip'));
  1284. $suite->addTest(new LocalhostMultiTests('testDeflate'));
  1285. $suite->addTest(new LocalhostMultiTests('testProxy'));
  1286. $suite->addTest(new LocalhostMultiTests('testHttp11'));
  1287. $suite->addTest(new LocalhostMultiTests('testHttp11Gzip'));
  1288. $suite->addTest(new LocalhostMultiTests('testHttp11Deflate'));
  1289. $suite->addTest(new LocalhostMultiTests('testKeepAlives'));
  1290. $suite->addTest(new LocalhostMultiTests('testHttp11Proxy'));
  1291. $suite->addTest(new LocalhostMultiTests('testHttps'));
  1292. $suite->addTest(new LocalhostMultiTests('testHttpsProxy'));
  1293. $suite->addTest(new InvalidHostTests('test404'));
  1294. //$suite->addTest(new InvalidHostTests('testSrvNotFound'));
  1295. $suite->addTest(new InvalidHostTests('testCurlKAErr'));
  1296. $suite->addTest(new ParsingBugsTests('testMinusOneString'));
  1297. $suite->addTest(new ParsingBugsTests('testUnicodeInMemberName'));
  1298. $suite->addTest(new ParsingBugsTests('testUnicodeInErrorString'));
  1299. $suite->addTest(new ParsingBugsTests('testValidNumbers'));
  1300. $suite->addTest(new ParsingBugsTests('testAddScalarToStruct'));
  1301. $suite->addTest(new ParsingBugsTests('testAddStructToStruct'));
  1302. $suite->addTest(new ParsingBugsTests('testAddArrayToArray'));
  1303. $suite->addTest(new ParsingBugsTests('testEncodeArray'));
  1304. $suite->addTest(new ParsingBugsTests('testEncodeRecursive'));
  1305. $suite->addTest(new ParsingBugsTests('testBrokenrequests'));
  1306. $suite->addTest(new ParsingBugsTests('testBrokenresponses'));
  1307. $suite->addTest(new ParsingBugsTests('testBuggyHttp'));
  1308. $suite->addTest(new ParsingBugsTests('testStringBug'));
  1309. $suite->addTest(new ParsingBugsTests('testWhiteSpace'));
  1310. $suite->addTest(new ParsingBugsTests('testAutodecodeResponse'));
  1311. $suite->addTest(new ParsingBugsTests('testNoDecodeResponse'));
  1312. $suite->addTest(new ParsingBugsTests('testAutoCoDec'));
  1313. $suite->addTest(new ParsingBugsTests('testUTF8Response'));
  1314. $suite->addTest(new ParsingBugsTests('testUTF8Request'));
  1315. $suite->addTest(new ParsingBugsTests('testUTF8IntString'));
  1316. $suite->addTest(new ParsingBugsTests('testStringInt'));
  1317. $suite->addTest(new ParsingBugsTests('testStructMemExists'));
  1318. $suite->addTest(new ParsingBugsTests('testDoubleDataInArrayTag'));
  1319. $suite->addTest(new ParsingBugsTests('testDoubleStuffInValueTag'));
  1320. $suite->addTest(new ParsingBugsTests('testNilValue'));
  1321. $suite->addTest(new ParsingBugsTests('testLocale'));
  1322. $title = 'XML-RPC Unit Tests';
  1323. if(isset($only))
  1324. {
  1325. $suite = new PHPUnit_TestSuite($only);
  1326. }
  1327. if(isset($_SERVER['REQUEST_METHOD']))
  1328. {
  1329. 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";
  1330. }
  1331. else
  1332. {
  1333. echo "$title\n\n";
  1334. }
  1335. if(isset($_SERVER['REQUEST_METHOD']))
  1336. {
  1337. echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
  1338. 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";
  1339. flush();
  1340. @ob_flush();
  1341. }
  1342. else
  1343. {
  1344. echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
  1345. echo 'Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.$LOCALSERVER.$URI.' and https://'.$HTTPSSERVER.$HTTPSURI."\n\n";
  1346. }
  1347. // do some basic timing measurement
  1348. list($micro, $sec) = explode(' ', microtime());
  1349. $start_time = $sec + $micro;
  1350. $PHPUnit = new PHPUnit;
  1351. $result = $PHPUnit->run($suite, ($DEBUG == 0 ? '.' : '<hr/>'));
  1352. list($micro, $sec) = explode(' ', microtime());
  1353. $end_time = $sec + $micro;
  1354. if(!isset($_SERVER['REQUEST_METHOD']))
  1355. {
  1356. echo $result->toString()."\n";
  1357. }
  1358. if(isset($_SERVER['REQUEST_METHOD']))
  1359. {
  1360. echo '<h3>'.$result->failureCount()." test failures</h3>\n";
  1361. printf("Time spent: %.2f secs<br/>\n", $end_time - $start_time);
  1362. }
  1363. else
  1364. {
  1365. echo $result->failureCount()." test failures\n";
  1366. printf("Time spent: %.2f secs\n", $end_time - $start_time);
  1367. }
  1368. if($result->failureCount() && !$DEBUG)
  1369. {
  1370. $target = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&amp;DEBUG=1' : $_SERVER['PHP_SELF'].'?DEBUG=1';
  1371. $t2 = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&amp;DEBUG=2' : $_SERVER['PHP_SELF'].'?DEBUG=2';
  1372. if(isset($_SERVER['REQUEST_METHOD']))
  1373. {
  1374. 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";
  1375. }
  1376. else
  1377. {
  1378. echo "Run testsuite with DEBUG=1 (or 2) to have more detail about tests results\n";
  1379. }
  1380. }
  1381. if(isset($_SERVER['REQUEST_METHOD']))
  1382. {
  1383. ?>
  1384. <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>
  1385. <div id="opts" style="display: none;">
  1386. <form method="GET" style="border: 1px solid silver; margin: 5px; padding: 5px; font-family: monospace;">
  1387. HTTP Server:&nbsp;&nbsp;<input name="LOCALSERVER" size="30" value="<?php echo htmlspecialchars($LOCALSERVER); ?>"/> Path: <input name="URI" size="30" value="<?php echo htmlspecialchars($URI); ?>"/><br/>
  1388. 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/>
  1389. Proxy Server: <input name="PROXY" size="30" value="<?php echo isset($PROXY) ? htmlspecialchars($PROXY) : ''; ?>"/> <input type="submit" value="Run Testsuite"/>
  1390. </form>
  1391. </div>
  1392. <?php
  1393. echo $result->toHTML()."\n</body>\n</html>\n";
  1394. }
  1395. ?>