RpcHelperTest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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"), downcast<String>(list->get(0))->s());
  79. CPPUNIT_ASSERT_EQUAL(std::string("hello world"), downcast<String>(list->get(1))->s());
  80. }
  81. void RpcHelperTest::testParseMemory_shouldFail()
  82. {
  83. try {
  84. std::string s =
  85. "<methodCall>"
  86. " <methodName>aria2.addURI</methodName>"
  87. " <params>"
  88. " <param>"
  89. " <value><i4>100</i4></value>"
  90. " </param>";
  91. xmlParseMemory(s.c_str(), s.size());
  92. CPPUNIT_FAIL("exception must be thrown.");
  93. } catch(RecoverableException& e) {
  94. // success
  95. }
  96. }
  97. void RpcHelperTest::testParseMemory_withoutParams()
  98. {
  99. {
  100. std::string s =
  101. "<methodCall>"
  102. " <methodName>aria2.addURI</methodName>"
  103. " <params>"
  104. " </params>"
  105. "</methodCall>";
  106. RpcRequest req = xmlParseMemory(s.c_str(), s.size());
  107. CPPUNIT_ASSERT(req.params);
  108. }
  109. {
  110. std::string s =
  111. "<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 =
  121. "<?xml version=\"1.0\"?>"
  122. "<methodCall>"
  123. " <methodName>aria2.addUri</methodName>"
  124. " <params>"
  125. " <param>"
  126. " <value>http://aria2.sourceforge.net</value>"
  127. " </param>"
  128. " <param>"
  129. " <value>http://aria2.<foo/>sourceforge.net</value>"
  130. " </param>"
  131. " <param>"
  132. " <value>"
  133. " <struct>"
  134. " <member>"
  135. " <name>hello</name>"
  136. " <value>world</value>"
  137. " </member>"
  138. " </struct>"
  139. " </value>"
  140. " </param>"
  141. " <param>"
  142. " <value>"
  143. " <array>"
  144. " <data>"
  145. " <value>apple</value>"
  146. " <value>banana</value>"
  147. " <value><string>lemon</string>peanuts</value>"
  148. " </data>"
  149. " </array>"
  150. " </value>"
  151. " </param>"
  152. " </params>"
  153. "</methodCall>";
  154. RpcRequest req =
  155. xmlParseMemory(s.c_str(), s.size());
  156. CPPUNIT_ASSERT_EQUAL((size_t)4, req.params->size());
  157. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"),
  158. downcast<String>(req.params->get(0))->s());
  159. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"),
  160. downcast<String>(req.params->get(1))->s());
  161. const Dict* dict = downcast<Dict>(req.params->get(2));
  162. CPPUNIT_ASSERT_EQUAL(std::string("world"),
  163. downcast<String>(dict->get("hello"))->s());
  164. const List* list = downcast<List>(req.params->get(3));
  165. CPPUNIT_ASSERT_EQUAL(std::string("apple"), downcast<String>(list->get(0))->s());
  166. CPPUNIT_ASSERT_EQUAL(std::string("banana"), downcast<String>(list->get(1))->s());
  167. CPPUNIT_ASSERT_EQUAL(std::string("lemon"), downcast<String>(list->get(2))->s());
  168. }
  169. #endif // ENABLE_XML_RPC
  170. } // namespace rpc
  171. } // namespace aria2