RpcHelperTest.cc 5.5 KB

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