RpcHelperTest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "rpc_helper.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "RpcRequest.h"
  4. #include "RecoverableException.h"
  5. #ifdef ENABLE_XML_RPC
  6. #include "XmlRpcRequestParserStateMachine.h"
  7. #endif // ENABLE_XML_RPC
  8. namespace aria2 {
  9. namespace rpc {
  10. class RpcHelperTest : public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(RpcHelperTest);
  12. #ifdef ENABLE_XML_RPC
  13. CPPUNIT_TEST(testParseMemory);
  14. CPPUNIT_TEST(testParseMemory_shouldFail);
  15. CPPUNIT_TEST(testParseMemory_withoutStringTag);
  16. #endif // ENABLE_XML_RPC
  17. CPPUNIT_TEST_SUITE_END();
  18. public:
  19. void setUp() {}
  20. void tearDown() {}
  21. #ifdef ENABLE_XML_RPC
  22. void testParseMemory();
  23. void testParseMemory_shouldFail();
  24. void testParseMemory_withoutParams();
  25. void testParseMemory_withoutStringTag();
  26. #endif // ENABLE_XML_RPC
  27. };
  28. CPPUNIT_TEST_SUITE_REGISTRATION(RpcHelperTest);
  29. #ifdef ENABLE_XML_RPC
  30. void RpcHelperTest::testParseMemory()
  31. {
  32. std::string s =
  33. "<?xml version=\"1.0\"?>"
  34. "<methodCall>"
  35. " <methodName>aria2.addURI</methodName>"
  36. " <params>"
  37. " <param>"
  38. " <value><i4>100</i4></value>"
  39. " </param>"
  40. " <param>"
  41. " <value>"
  42. " <struct>"
  43. " <member>"
  44. " <name>max-count</name>"
  45. " <value><i4>65535</i4></value>"
  46. " </member>"
  47. " <member>"
  48. " <name>seed-ratio</name>"
  49. " <value><double>0.99</double></value>"
  50. " </member>"
  51. " </struct>"
  52. " </value>"
  53. " </param>"
  54. " <param>"
  55. " <value>"
  56. " <array>"
  57. " <data>"
  58. " <value><string>pudding</string></value>"
  59. " <value><base64>aGVsbG8gd29ybGQ=</base64></value>"
  60. " </data>"
  61. " </array>"
  62. " </value>"
  63. " </param>"
  64. " </params>"
  65. "</methodCall>";
  66. RpcRequest req = xmlParseMemory(s.c_str(), s.size());
  67. CPPUNIT_ASSERT_EQUAL(std::string("aria2.addURI"), req.methodName);
  68. CPPUNIT_ASSERT_EQUAL((size_t)3, req.params->size());
  69. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)100,
  70. downcast<Integer>(req.params->get(0))->i());
  71. const Dict* dict = downcast<Dict>(req.params->get(1));
  72. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)65535,
  73. downcast<Integer>(dict->get("max-count"))->i());
  74. // Current implementation handles double as string.
  75. CPPUNIT_ASSERT_EQUAL(std::string("0.99"),
  76. downcast<String>(dict->get("seed-ratio"))->s());
  77. const List* list = downcast<List>(req.params->get(2));
  78. CPPUNIT_ASSERT_EQUAL(std::string("pudding"),
  79. downcast<String>(list->get(0))->s());
  80. CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
  81. downcast<String>(list->get(1))->s());
  82. }
  83. void RpcHelperTest::testParseMemory_shouldFail()
  84. {
  85. try {
  86. std::string s = "<methodCall>"
  87. " <methodName>aria2.addURI</methodName>"
  88. " <params>"
  89. " <param>"
  90. " <value><i4>100</i4></value>"
  91. " </param>";
  92. xmlParseMemory(s.c_str(), s.size());
  93. CPPUNIT_FAIL("exception must be thrown.");
  94. }
  95. catch (RecoverableException& e) {
  96. // success
  97. }
  98. }
  99. void RpcHelperTest::testParseMemory_withoutParams()
  100. {
  101. {
  102. std::string s = "<methodCall>"
  103. " <methodName>aria2.addURI</methodName>"
  104. " <params>"
  105. " </params>"
  106. "</methodCall>";
  107. RpcRequest req = xmlParseMemory(s.c_str(), s.size());
  108. CPPUNIT_ASSERT(req.params);
  109. }
  110. {
  111. std::string s = "<methodCall>"
  112. " <methodName>aria2.addURI</methodName>"
  113. "</methodCall>";
  114. RpcRequest req = xmlParseMemory(s.c_str(), s.size());
  115. CPPUNIT_ASSERT(req.params->size());
  116. }
  117. }
  118. void RpcHelperTest::testParseMemory_withoutStringTag()
  119. {
  120. std::string s = "<?xml version=\"1.0\"?>"
  121. "<methodCall>"
  122. " <methodName>aria2.addUri</methodName>"
  123. " <params>"
  124. " <param>"
  125. " <value>http://aria2.sourceforge.net</value>"
  126. " </param>"
  127. " <param>"
  128. " <value>http://aria2.<foo/>sourceforge.net</value>"
  129. " </param>"
  130. " <param>"
  131. " <value>"
  132. " <struct>"
  133. " <member>"
  134. " <name>hello</name>"
  135. " <value>world</value>"
  136. " </member>"
  137. " </struct>"
  138. " </value>"
  139. " </param>"
  140. " <param>"
  141. " <value>"
  142. " <array>"
  143. " <data>"
  144. " <value>apple</value>"
  145. " <value>banana</value>"
  146. " <value><string>lemon</string>peanuts</value>"
  147. " </data>"
  148. " </array>"
  149. " </value>"
  150. " </param>"
  151. " </params>"
  152. "</methodCall>";
  153. RpcRequest req = xmlParseMemory(s.c_str(), s.size());
  154. CPPUNIT_ASSERT_EQUAL((size_t)4, req.params->size());
  155. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"),
  156. downcast<String>(req.params->get(0))->s());
  157. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"),
  158. downcast<String>(req.params->get(1))->s());
  159. const Dict* dict = downcast<Dict>(req.params->get(2));
  160. CPPUNIT_ASSERT_EQUAL(std::string("world"),
  161. downcast<String>(dict->get("hello"))->s());
  162. const List* list = downcast<List>(req.params->get(3));
  163. CPPUNIT_ASSERT_EQUAL(std::string("apple"),
  164. downcast<String>(list->get(0))->s());
  165. CPPUNIT_ASSERT_EQUAL(std::string("banana"),
  166. downcast<String>(list->get(1))->s());
  167. CPPUNIT_ASSERT_EQUAL(std::string("lemon"),
  168. downcast<String>(list->get(2))->s());
  169. }
  170. #endif // ENABLE_XML_RPC
  171. } // namespace rpc
  172. } // namespace aria2