xmlrpc.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Perform basic XML-RPC tests that do not require addition callbacks.
  4. */
  5. class XMLRPCBasicTestCase extends DrupalWebTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'XML-RPC basic',
  9. 'description' => 'Perform basic XML-RPC tests that do not require additional callbacks.',
  10. 'group' => 'XML-RPC',
  11. );
  12. }
  13. /**
  14. * Ensure that a basic XML-RPC call with no parameters works.
  15. */
  16. protected function testListMethods() {
  17. // Minimum list of methods that should be included.
  18. $minimum = array(
  19. 'system.multicall',
  20. 'system.methodSignature',
  21. 'system.getCapabilities',
  22. 'system.listMethods',
  23. 'system.methodHelp',
  24. );
  25. // Invoke XML-RPC call to get list of methods.
  26. $url = url(NULL, array('absolute' => TRUE)) . 'xmlrpc.php';
  27. $methods = xmlrpc($url, array('system.listMethods' => array()));
  28. // Ensure that the minimum methods were found.
  29. $count = 0;
  30. foreach ($methods as $method) {
  31. if (in_array($method, $minimum)) {
  32. $count++;
  33. }
  34. }
  35. $this->assertEqual($count, count($minimum), 'system.listMethods returned at least the minimum listing');
  36. }
  37. /**
  38. * Ensure that system.methodSignature returns an array of signatures.
  39. */
  40. protected function testMethodSignature() {
  41. $url = url(NULL, array('absolute' => TRUE)) . 'xmlrpc.php';
  42. $signature = xmlrpc($url, array('system.methodSignature' => array('system.listMethods')));
  43. $this->assert(is_array($signature) && !empty($signature) && is_array($signature[0]),
  44. 'system.methodSignature returns an array of signature arrays.');
  45. }
  46. /**
  47. * Ensure that XML-RPC correctly handles invalid messages when parsing.
  48. */
  49. protected function testInvalidMessageParsing() {
  50. $invalid_messages = array(
  51. array(
  52. 'message' => xmlrpc_message(''),
  53. 'assertion' => 'Empty message correctly rejected during parsing.',
  54. ),
  55. array(
  56. 'message' => xmlrpc_message('<?xml version="1.0" encoding="ISO-8859-1"?>'),
  57. 'assertion' => 'Empty message with XML declaration correctly rejected during parsing.',
  58. ),
  59. array(
  60. 'message' => xmlrpc_message('<?xml version="1.0"?><params><param><value><string>value</string></value></param></params>'),
  61. 'assertion' => 'Non-empty message without a valid message type is rejected during parsing.',
  62. ),
  63. array(
  64. 'message' => xmlrpc_message('<methodResponse><params><param><value><string>value</string></value></param></methodResponse>'),
  65. 'assertion' => 'Non-empty malformed message is rejected during parsing.',
  66. ),
  67. );
  68. foreach ($invalid_messages as $assertion) {
  69. $this->assertFalse(xmlrpc_message_parse($assertion['message']), $assertion['assertion']);
  70. }
  71. }
  72. }
  73. class XMLRPCValidator1IncTestCase extends DrupalWebTestCase {
  74. public static function getInfo() {
  75. return array(
  76. 'name' => 'XML-RPC validator',
  77. 'description' => 'See <a href="http://www.xmlrpc.com/validator1Docs">the xmlrpc validator1 specification</a>.',
  78. 'group' => 'XML-RPC',
  79. );
  80. }
  81. function setUp() {
  82. parent::setUp('xmlrpc_test');
  83. }
  84. /**
  85. * Run validator1 tests.
  86. */
  87. function testValidator1() {
  88. $xml_url = url(NULL, array('absolute' => TRUE)) . 'xmlrpc.php';
  89. srand();
  90. mt_srand();
  91. $array_1 = array(array('curly' => mt_rand(-100, 100)),
  92. array('curly' => mt_rand(-100, 100)),
  93. array('larry' => mt_rand(-100, 100)),
  94. array('larry' => mt_rand(-100, 100)),
  95. array('moe' => mt_rand(-100, 100)),
  96. array('moe' => mt_rand(-100, 100)),
  97. array('larry' => mt_rand(-100, 100)));
  98. shuffle($array_1);
  99. $l_res_1 = xmlrpc_test_arrayOfStructsTest($array_1);
  100. $r_res_1 = xmlrpc($xml_url, array('validator1.arrayOfStructsTest' => array($array_1)));
  101. $this->assertIdentical($l_res_1, $r_res_1);
  102. $string_2 = 't\'&>>zf"md>yr>xlcev<h<"k&j<og"w&&>">>uai"np&s>>q\'&b<>"&&&';
  103. $l_res_2 = xmlrpc_test_countTheEntities($string_2);
  104. $r_res_2 = xmlrpc($xml_url, array('validator1.countTheEntities' => array($string_2)));
  105. $this->assertIdentical($l_res_2, $r_res_2);
  106. $struct_3 = array('moe' => mt_rand(-100, 100), 'larry' => mt_rand(-100, 100), 'curly' => mt_rand(-100, 100), 'homer' => mt_rand(-100, 100));
  107. $l_res_3 = xmlrpc_test_easyStructTest($struct_3);
  108. $r_res_3 = xmlrpc($xml_url, array('validator1.easyStructTest' => array($struct_3)));
  109. $this->assertIdentical($l_res_3, $r_res_3);
  110. $struct_4 = array('sub1' => array('bar' => 13),
  111. 'sub2' => 14,
  112. 'sub3' => array('foo' => 1, 'baz' => 2),
  113. 'sub4' => array('ss' => array('sss' => array('ssss' => 'sssss'))));
  114. $l_res_4 = xmlrpc_test_echoStructTest($struct_4);
  115. $r_res_4 = xmlrpc($xml_url, array('validator1.echoStructTest' => array($struct_4)));
  116. $this->assertIdentical($l_res_4, $r_res_4);
  117. $int_5 = mt_rand(-100, 100);
  118. $bool_5 = (($int_5 % 2) == 0);
  119. $string_5 = $this->randomName();
  120. $double_5 = (double)(mt_rand(-1000, 1000) / 100);
  121. $time_5 = REQUEST_TIME;
  122. $base64_5 = $this->randomName(100);
  123. $l_res_5 = xmlrpc_test_manyTypesTest($int_5, $bool_5, $string_5, $double_5, xmlrpc_date($time_5), $base64_5);
  124. // See http://drupal.org/node/37766 why this currently fails
  125. $l_res_5[5] = $l_res_5[5]->data;
  126. $r_res_5 = xmlrpc($xml_url, array('validator1.manyTypesTest' => array($int_5, $bool_5, $string_5, $double_5, xmlrpc_date($time_5), xmlrpc_base64($base64_5))));
  127. // @todo Contains objects, objects are not equal.
  128. $this->assertEqual($l_res_5, $r_res_5);
  129. $size = mt_rand(100, 200);
  130. $array_6 = array();
  131. for ($i = 0; $i < $size; $i++) {
  132. $array_6[] = $this->randomName(mt_rand(8, 12));
  133. }
  134. $l_res_6 = xmlrpc_test_moderateSizeArrayCheck($array_6);
  135. $r_res_6 = xmlrpc($xml_url, array('validator1.moderateSizeArrayCheck' => array($array_6)));
  136. $this->assertIdentical($l_res_6, $r_res_6);
  137. $struct_7 = array();
  138. for ($y = 2000; $y < 2002; $y++) {
  139. for ($m = 3; $m < 5; $m++) {
  140. for ($d = 1; $d < 6; $d++) {
  141. $ys = (string) $y;
  142. $ms = sprintf('%02d', $m);
  143. $ds = sprintf('%02d', $d);
  144. $struct_7[$ys][$ms][$ds]['moe'] = mt_rand(-100, 100);
  145. $struct_7[$ys][$ms][$ds]['larry'] = mt_rand(-100, 100);
  146. $struct_7[$ys][$ms][$ds]['curly'] = mt_rand(-100, 100);
  147. }
  148. }
  149. }
  150. $l_res_7 = xmlrpc_test_nestedStructTest($struct_7);
  151. $r_res_7 = xmlrpc($xml_url, array('validator1.nestedStructTest' => array($struct_7)));
  152. $this->assertIdentical($l_res_7, $r_res_7);
  153. $int_8 = mt_rand(-100, 100);
  154. $l_res_8 = xmlrpc_test_simpleStructReturnTest($int_8);
  155. $r_res_8 = xmlrpc($xml_url, array('validator1.simpleStructReturnTest' => array($int_8)));
  156. $this->assertIdentical($l_res_8, $r_res_8);
  157. /* Now test multicall */
  158. $x = array();
  159. $x['validator1.arrayOfStructsTest'] = array($array_1);
  160. $x['validator1.countTheEntities'] = array($string_2);
  161. $x['validator1.easyStructTest'] = array($struct_3);
  162. $x['validator1.echoStructTest'] = array($struct_4);
  163. $x['validator1.manyTypesTest'] = array($int_5, $bool_5, $string_5, $double_5, xmlrpc_date($time_5), xmlrpc_base64($base64_5));
  164. $x['validator1.moderateSizeArrayCheck'] = array($array_6);
  165. $x['validator1.nestedStructTest'] = array($struct_7);
  166. $x['validator1.simpleStructReturnTest'] = array($int_8);
  167. $a_l_res = array($l_res_1, $l_res_2, $l_res_3, $l_res_4, $l_res_5, $l_res_6, $l_res_7, $l_res_8);
  168. $a_r_res = xmlrpc($xml_url, $x);
  169. $this->assertEqual($a_l_res, $a_r_res);
  170. }
  171. }
  172. class XMLRPCMessagesTestCase extends DrupalWebTestCase {
  173. public static function getInfo() {
  174. return array(
  175. 'name' => 'XML-RPC message and alteration',
  176. 'description' => 'Test large messages and method alterations.',
  177. 'group' => 'XML-RPC',
  178. );
  179. }
  180. function setUp() {
  181. parent::setUp('xmlrpc_test');
  182. }
  183. /**
  184. * Make sure that XML-RPC can transfer large messages.
  185. */
  186. function testSizedMessages() {
  187. // These tests can produce up to 128 x 160 words in the XML-RPC message
  188. // (see xmlrpc_test_message_sized_in_kb()) with 4 tags used to represent
  189. // each. Set a large enough tag limit to allow this to be tested.
  190. variable_set('xmlrpc_message_maximum_tag_count', 100000);
  191. $xml_url = url(NULL, array('absolute' => TRUE)) . 'xmlrpc.php';
  192. $sizes = array(8, 80, 160);
  193. foreach ($sizes as $size) {
  194. $xml_message_l = xmlrpc_test_message_sized_in_kb($size);
  195. $xml_message_r = xmlrpc($xml_url, array('messages.messageSizedInKB' => array($size)));
  196. $this->assertEqual($xml_message_l, $xml_message_r, format_string('XML-RPC messages.messageSizedInKB of %s Kb size received', array('%s' => $size)));
  197. }
  198. }
  199. /**
  200. * Ensure that hook_xmlrpc_alter() can hide even builtin methods.
  201. */
  202. protected function testAlterListMethods() {
  203. // Ensure xmlrpc_test_xmlrpc_alter() is disabled and retrieve regular list of methods.
  204. variable_set('xmlrpc_test_xmlrpc_alter', FALSE);
  205. $url = url(NULL, array('absolute' => TRUE)) . 'xmlrpc.php';
  206. $methods1 = xmlrpc($url, array('system.listMethods' => array()));
  207. // Enable the alter hook and retrieve the list of methods again.
  208. variable_set('xmlrpc_test_xmlrpc_alter', TRUE);
  209. $methods2 = xmlrpc($url, array('system.listMethods' => array()));
  210. $diff = array_diff($methods1, $methods2);
  211. $this->assertTrue(is_array($diff) && !empty($diff), 'Method list is altered by hook_xmlrpc_alter');
  212. $removed = reset($diff);
  213. $this->assertEqual($removed, 'system.methodSignature', 'Hiding builting system.methodSignature with hook_xmlrpc_alter works');
  214. }
  215. }