LocalhostTests

The Contact Form 7 - Infusionsoft Add-on LocalhostTests class.

Defined (1)

The class is defined in the following location(s).

/src/xmlrpc-3.0/test/testsuite.php  
  1. class LocalhostTests extends PHPUnit_TestCase 
  2. var $client = null; 
  3. var $method = 'http'; 
  4. var $timeout = 10; 
  5. var $request_compression = null; 
  6. var $accepted_compression = ''; 
  7.  
  8. function fail($message = '') 
  9. PHPUnit_TestCase::fail($message); 
  10. // save in global var that this particular test has failed 
  11. // (but only if not called from subclass objects / multitests) 
  12. if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests') 
  13. global $failed_tests; 
  14. $trace = debug_backtrace(); 
  15. for ($i = 0; $i < count($trace); $i++) 
  16. if (strpos($trace[$i]['function'], 'test') === 0) 
  17. $failed_tests[$trace[$i]['function']] = true; 
  18. break; 
  19.  
  20. function setUp() 
  21. global $DEBUG, $LOCALSERVER, $URI; 
  22. $server = explode(':', $LOCALSERVER); 
  23. if(count($server) > 1) 
  24. $this->client=new xmlrpc_client($URI, $server[0], $server[1]); 
  25. else 
  26. $this->client=new xmlrpc_client($URI, $LOCALSERVER); 
  27. if($DEBUG) 
  28. $this->client->setDebug($DEBUG); 
  29. $this->client->request_compression = $this->request_compression; 
  30. $this->client->accepted_compression = $this->accepted_compression; 
  31.  
  32. function send($msg, $errrorcode=0, $return_response=false) 
  33. $r = $this->client->send($msg, $this->timeout, $this->method); 
  34. // for multicall, return directly array of responses 
  35. if(is_array($r)) 
  36. return $r; 
  37. $this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString()); 
  38. if(!$r->faultCode()) 
  39. if($return_response) 
  40. return $r; 
  41. else 
  42. return $r->value(); 
  43. else 
  44. return null; 
  45.  
  46. function testString() 
  47. $sendstring="here are 3 \"entities\": < > &" . 
  48. "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) . 
  49. " - isn't that great? \\\"hackery\\\" at it's best " . 
  50. " also don't want to miss out on \$item[0]. ". 
  51. "The real weird stuff follows: CRLF here".chr(13).chr(10). 
  52. "a simple CR here".chr(13). 
  53. "a simple LF here".chr(10). 
  54. "and then LFCR".chr(10).chr(13). 
  55. "last but not least weird names: Gnter, Elne, and an xml comment closing tag: -->"; 
  56. $f=new xmlrpcmsg('examples.stringecho', array( 
  57. new xmlrpcval($sendstring, 'string') 
  58. )); 
  59. $v=$this->send($f); 
  60. if($v) 
  61. // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized. 
  62. // so we relax our tests... 
  63. $l1 = strlen($sendstring); 
  64. $l2 = strlen($v->scalarval()); 
  65. if ($l1 == $l2) 
  66. $this->assertEquals($sendstring, $v->scalarval()); 
  67. else 
  68. $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval()); 
  69.  
  70. function testAddingDoubles() 
  71. // note that rounding errors mean i 
  72. // keep precision to sensible levels here ;-) 
  73. $a=12.13; $b=-23.98; 
  74. $f=new xmlrpcmsg('examples.addtwodouble', array( 
  75. new xmlrpcval($a, 'double'),  
  76. new xmlrpcval($b, 'double') 
  77. )); 
  78. $v=$this->send($f); 
  79. if($v) 
  80. $this->assertEquals($a+$b, $v->scalarval()); 
  81.  
  82. function testAdding() 
  83. $f=new xmlrpcmsg('examples.addtwo', array( 
  84. new xmlrpcval(12, 'int'),  
  85. new xmlrpcval(-23, 'int') 
  86. )); 
  87. $v=$this->send($f); 
  88. if($v) 
  89. $this->assertEquals(12-23, $v->scalarval()); 
  90.  
  91. function testInvalidNumber() 
  92. $f=new xmlrpcmsg('examples.addtwo', array( 
  93. new xmlrpcval('fred', 'int'),  
  94. new xmlrpcval("\"; exec('ls')", 'int') 
  95. )); 
  96. $v=$this->send($f); 
  97. /// @todo a fault condition should be generated here 
  98. /// by the server, which we pick up on 
  99. if($v) 
  100. $this->assertEquals(0, $v->scalarval()); 
  101.  
  102. function testBoolean() 
  103. $f=new xmlrpcmsg('examples.invertBooleans', array( 
  104. new xmlrpcval(array( 
  105. new xmlrpcval(true, 'boolean'),  
  106. new xmlrpcval(false, 'boolean'),  
  107. new xmlrpcval(1, 'boolean'),  
  108. new xmlrpcval(0, 'boolean'),  
  109. //new xmlrpcval('true', 'boolean'),  
  110. //new xmlrpcval('false', 'boolean') 
  111. ),  
  112. 'array' 
  113. ))); 
  114. $answer='0101'; 
  115. $v=$this->send($f); 
  116. if($v) 
  117. $sz=$v->arraysize(); 
  118. $got=''; 
  119. for($i=0; $i<$sz; $i++) 
  120. $b=$v->arraymem($i); 
  121. if($b->scalarval()) 
  122. $got.='1'; 
  123. else 
  124. $got.='0'; 
  125. $this->assertEquals($answer, $got); 
  126.  
  127. function testBase64() 
  128. $sendstring='Mary had a little lamb,  
  129. Whose fleece was white as snow,  
  130. And everywhere that Mary went 
  131. the lamb was sure to go. 
  132.  
  133. Mary had a little lamb 
  134. She tied it to a pylon 
  135. Ten thousand volts went down its back 
  136. And turned it into nylon'; 
  137. $f=new xmlrpcmsg('examples.decode64', array( 
  138. new xmlrpcval($sendstring, 'base64') 
  139. )); 
  140. $v=$this->send($f); 
  141. if($v) 
  142. if (strlen($sendstring) == strlen($v->scalarval())) 
  143. $this->assertEquals($sendstring, $v->scalarval()); 
  144. else 
  145. $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval()); 
  146.  
  147. function testDateTime() 
  148. $time = time(); 
  149. $t1 = new xmlrpcval($time, 'dateTime.iso8601'); 
  150. $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601'); 
  151. $this->assertEquals($t1->serialize(), $t2->serialize()); 
  152. if (class_exists('DateTime')) 
  153. $datetime = new DateTime(); 
  154. $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601'); 
  155. $this->assertEquals($t1->serialize(), $t3->serialize()); 
  156.  
  157. function testCountEntities() 
  158. $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<"; 
  159. $f = new xmlrpcmsg('validator1.countTheEntities', array( 
  160. new xmlrpcval($sendstring, 'string') 
  161. )); 
  162. $v = $this->send($f); 
  163. if($v) 
  164. $got = ''; 
  165. $expected = '37210'; 
  166. $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes'); 
  167. while(list(, $val) = each($expect_array)) 
  168. $b = $v->structmem($val); 
  169. $got .= $b->me['int']; 
  170. $this->assertEquals($expected, $got); 
  171.  
  172. function _multicall_msg($method, $params) 
  173. $struct['methodName'] = new xmlrpcval($method, 'string'); 
  174. $struct['params'] = new xmlrpcval($params, 'array'); 
  175. return new xmlrpcval($struct, 'struct'); 
  176.  
  177. function testServerMulticall() 
  178. // We manually construct a system.multicall() call to ensure 
  179. // that the server supports it. 
  180.  
  181. // NB: This test will NOT pass if server does not support system.multicall. 
  182.  
  183. // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py 
  184. $good1 = $this->_multicall_msg( 
  185. 'system.methodHelp',  
  186. array(php_xmlrpc_encode('system.listMethods'))); 
  187. $bad = $this->_multicall_msg( 
  188. 'test.nosuch',  
  189. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); 
  190. $recursive = $this->_multicall_msg( 
  191. 'system.multicall',  
  192. array(new xmlrpcval(array(), 'array'))); 
  193. $good2 = $this->_multicall_msg( 
  194. 'system.methodSignature',  
  195. array(php_xmlrpc_encode('system.listMethods'))); 
  196. $arg = new xmlrpcval( 
  197. array($good1, $bad, $recursive, $good2),  
  198. 'array' 
  199. ); 
  200.  
  201. $f = new xmlrpcmsg('system.multicall', array($arg)); 
  202. $v = $this->send($f); 
  203. if($v) 
  204. //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall"); 
  205. $this->assertTrue($v->arraysize() == 4, "bad number of return values"); 
  206.  
  207. $r1 = $v->arraymem(0); 
  208. $this->assertTrue( 
  209. $r1->kindOf() == 'array' && $r1->arraysize() == 1,  
  210. "did not get array of size 1 from good1" 
  211. ); 
  212.  
  213. $r2 = $v->arraymem(1); 
  214. $this->assertTrue( 
  215. $r2->kindOf() == 'struct',  
  216. "no fault from bad" 
  217. ); 
  218.  
  219. $r3 = $v->arraymem(2); 
  220. $this->assertTrue( 
  221. $r3->kindOf() == 'struct',  
  222. "recursive system.multicall did not fail" 
  223. ); 
  224.  
  225. $r4 = $v->arraymem(3); 
  226. $this->assertTrue( 
  227. $r4->kindOf() == 'array' && $r4->arraysize() == 1,  
  228. "did not get array of size 1 from good2" 
  229. ); 
  230.  
  231. function testClientMulticall1() 
  232. // NB: This test will NOT pass if server does not support system.multicall. 
  233.  
  234. $this->client->no_multicall = false; 
  235.  
  236. $good1 = new xmlrpcmsg('system.methodHelp',  
  237. array(php_xmlrpc_encode('system.listMethods'))); 
  238. $bad = new xmlrpcmsg('test.nosuch',  
  239. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); 
  240. $recursive = new xmlrpcmsg('system.multicall',  
  241. array(new xmlrpcval(array(), 'array'))); 
  242. $good2 = new xmlrpcmsg('system.methodSignature',  
  243. array(php_xmlrpc_encode('system.listMethods')) 
  244. ); 
  245.  
  246. $r = $this->send(array($good1, $bad, $recursive, $good2)); 
  247. if($r) 
  248. $this->assertTrue(count($r) == 4, "wrong number of return values"); 
  249.  
  250. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1"); 
  251. if(!$r[0]->faultCode()) 
  252. $val = $r[0]->value(); 
  253. $this->assertTrue( 
  254. $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',  
  255. "good1 did not return string" 
  256. ); 
  257. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad"); 
  258. $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall"); 
  259. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2"); 
  260. if(!$r[3]->faultCode()) 
  261. $val = $r[3]->value(); 
  262. $this->assertTrue($val->kindOf() == 'array', "good2 did not return array"); 
  263. // This is the only assert in this test which should fail 
  264. // if the test server does not support system.multicall. 
  265. $this->assertTrue($this->client->no_multicall == false,  
  266. "server does not support system.multicall" 
  267. ); 
  268.  
  269. function testClientMulticall2() 
  270. // NB: This test will NOT pass if server does not support system.multicall. 
  271.  
  272. $this->client->no_multicall = true; 
  273.  
  274. $good1 = new xmlrpcmsg('system.methodHelp',  
  275. array(php_xmlrpc_encode('system.listMethods'))); 
  276. $bad = new xmlrpcmsg('test.nosuch',  
  277. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); 
  278. $recursive = new xmlrpcmsg('system.multicall',  
  279. array(new xmlrpcval(array(), 'array'))); 
  280. $good2 = new xmlrpcmsg('system.methodSignature',  
  281. array(php_xmlrpc_encode('system.listMethods')) 
  282. ); 
  283.  
  284. $r = $this->send(array($good1, $bad, $recursive, $good2)); 
  285. if($r) 
  286. $this->assertTrue(count($r) == 4, "wrong number of return values"); 
  287.  
  288. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1"); 
  289. if(!$r[0]->faultCode()) 
  290. $val = $r[0]->value(); 
  291. $this->assertTrue( 
  292. $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',  
  293. "good1 did not return string"); 
  294. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad"); 
  295. $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall"); 
  296. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2"); 
  297. if(!$r[3]->faultCode()) 
  298. $val = $r[3]->value(); 
  299. $this->assertTrue($val->kindOf() == 'array', "good2 did not return array"); 
  300.  
  301. function testClientMulticall3() 
  302. // NB: This test will NOT pass if server does not support system.multicall. 
  303.  
  304. $this->client->return_type = 'phpvals'; 
  305. $this->client->no_multicall = false; 
  306.  
  307. $good1 = new xmlrpcmsg('system.methodHelp',  
  308. array(php_xmlrpc_encode('system.listMethods'))); 
  309. $bad = new xmlrpcmsg('test.nosuch',  
  310. array(php_xmlrpc_encode(1), php_xmlrpc_encode(2))); 
  311. $recursive = new xmlrpcmsg('system.multicall',  
  312. array(new xmlrpcval(array(), 'array'))); 
  313. $good2 = new xmlrpcmsg('system.methodSignature',  
  314. array(php_xmlrpc_encode('system.listMethods')) 
  315. ); 
  316.  
  317. $r = $this->send(array($good1, $bad, $recursive, $good2)); 
  318. if($r) 
  319. $this->assertTrue(count($r) == 4, "wrong number of return values"); 
  320. $this->assertTrue($r[0]->faultCode() == 0, "fault from good1"); 
  321. if(!$r[0]->faultCode()) 
  322. $val = $r[0]->value(); 
  323. $this->assertTrue( 
  324. is_string($val) , "good1 did not return string"); 
  325. $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad"); 
  326. $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall"); 
  327. $this->assertTrue($r[3]->faultCode() == 0, "fault from good2"); 
  328. if(!$r[3]->faultCode()) 
  329. $val = $r[3]->value(); 
  330. $this->assertTrue(is_array($val), "good2 did not return array"); 
  331. $this->client->return_type = 'xmlrpcvals'; 
  332.  
  333. function testCatchWarnings() 
  334. $f = new xmlrpcmsg('examples.generatePHPWarning', array( 
  335. new xmlrpcval('whatever', 'string') 
  336. )); 
  337. $v = $this->send($f); 
  338. if($v) 
  339. $this->assertEquals($v->scalarval(), true); 
  340.  
  341. function testCatchExceptions() 
  342. global $URI; 
  343. $f = new xmlrpcmsg('examples.raiseException', array( 
  344. new xmlrpcval('whatever', 'string') 
  345. )); 
  346. $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']); 
  347. $this->client->path = $URI.'?EXCEPTION_HANDLING=1'; 
  348. $v = $this->send($f, 1); 
  349. $this->client->path = $URI.'?EXCEPTION_HANDLING=2'; 
  350. $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']); 
  351.  
  352. function testZeroParams() 
  353. $f = new xmlrpcmsg('system.listMethods'); 
  354. $v = $this->send($f); 
  355.  
  356. function testCodeInjectionServerSide() 
  357. $f = new xmlrpcmsg('system.MethodHelp'); 
  358. $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>"; 
  359. $v = $this->send($f); 
  360. //$v = $r->faultCode(); 
  361. if ($v) 
  362. $this->assertEquals(0, $v->structsize()); 
  363.  
  364. function testAutoRegisteredFunction() 
  365. $f=new xmlrpcmsg('examples.php.getStateName', array( 
  366. new xmlrpcval(23, 'int') 
  367. )); 
  368. $v=$this->send($f); 
  369. if($v) 
  370. $this->assertEquals('Michigan', $v->scalarval()); 
  371. else 
  372. $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up'); 
  373.  
  374. function testAutoRegisteredClass() 
  375. $f=new xmlrpcmsg('examples.php2.getStateName', array( 
  376. new xmlrpcval(23, 'int') 
  377. )); 
  378. $v=$this->send($f); 
  379. if($v) 
  380. $this->assertEquals('Michigan', $v->scalarval()); 
  381. $f=new xmlrpcmsg('examples.php3.getStateName', array( 
  382. new xmlrpcval(23, 'int') 
  383. )); 
  384. $v=$this->send($f); 
  385. if($v) 
  386. $this->assertEquals('Michigan', $v->scalarval()); 
  387. else 
  388. $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up'); 
  389.  
  390. function testAutoRegisteredMethod() 
  391. $func=wrap_xmlrpc_method($this->client, 'examples.getStateName'); 
  392. if($func == '') 
  393. $this->fail('Registration of examples.getStateName failed'); 
  394. else 
  395. $v=$func(23); 
  396. $this->assertEquals('Michigan', $v); 
  397.  
  398. function testGetCookies() 
  399. // let server set to us some cookies we tell it 
  400. $cookies = array( 
  401. //'c1' => array(),  
  402. 'c2' => array('value' => 'c2'),  
  403. 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),  
  404. 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),  
  405. 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),  
  406. ); 
  407. $cookiesval = php_xmlrpc_encode($cookies); 
  408. $f=new xmlrpcmsg('examples.setcookies', array($cookiesval)); 
  409. $r=$this->send($f, 0, true); 
  410. if($r) 
  411. $v = $r->value(); 
  412. $this->assertEquals(1, $v->scalarval()); 
  413. // now check if we decoded the cookies as we had set them 
  414. $rcookies = $r->cookies(); 
  415. foreach($cookies as $c => $v) 
  416. // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT' 
  417. // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'... 
  418. if(isset($v['expires'])) 
  419. if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) 
  420. $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' , $cookies[$c]['expires']); 
  421. else 
  422. $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' , $cookies[$c]['expires']); 
  423. $this->assertEquals($cookies, $rcookies); 
  424.  
  425. function testSetCookies() 
  426. // let server set to us some cookies we tell it 
  427. $cookies = array( 
  428. 'c0' => null,  
  429. 'c1' => 1,  
  430. 'c2' => '2 3',  
  431. 'c3' => '!@#$%^&*()_+|}{":?><, ./\';[]\\=-' 
  432. ); 
  433. $f=new xmlrpcmsg('examples.getcookies', array()); 
  434. foreach ($cookies as $cookie => $val) 
  435. $this->client->setCookie($cookie, $val); 
  436. $cookies[$cookie] = (string) $cookies[$cookie]; 
  437. $r = $this->client->send($f, $this->timeout, $this->method); 
  438. $this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString()); 
  439. if(!$r->faultCode()) 
  440. $v = $r->value(); 
  441. $v = php_xmlrpc_decode($v); 
  442. // on IIS and Apache getallheaders returns something slightly different... 
  443. $this->assertEquals($v, $cookies); 
  444.  
  445. function testSendTwiceSameMsg() 
  446. $f=new xmlrpcmsg('examples.stringecho', array( 
  447. new xmlrpcval('hello world', 'string') 
  448. )); 
  449. $v1 = $this->send($f); 
  450. $v2 = $this->send($f); 
  451. //$v = $r->faultCode(); 
  452. if ($v1 && $v2) 
  453. $this->assertEquals($v2, $v1);