#include "rpc_helper.h" #include #include "RpcRequest.h" #include "RecoverableException.h" #ifdef ENABLE_XML_RPC # include "XmlRpcRequestParserStateMachine.h" #endif // ENABLE_XML_RPC namespace aria2 { namespace rpc { class RpcHelperTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(RpcHelperTest); #ifdef ENABLE_XML_RPC CPPUNIT_TEST(testParseMemory); CPPUNIT_TEST(testParseMemory_shouldFail); CPPUNIT_TEST(testParseMemory_withoutStringTag); #endif // ENABLE_XML_RPC CPPUNIT_TEST_SUITE_END(); public: void setUp() {} void tearDown() {} #ifdef ENABLE_XML_RPC void testParseMemory(); void testParseMemory_shouldFail(); void testParseMemory_withoutParams(); void testParseMemory_withoutStringTag(); #endif // ENABLE_XML_RPC }; CPPUNIT_TEST_SUITE_REGISTRATION(RpcHelperTest); #ifdef ENABLE_XML_RPC void RpcHelperTest::testParseMemory() { std::string s = "" "" " aria2.addURI" " " " " " 100" " " " " " " " " " " " max-count" " 65535" " " " " " seed-ratio" " 0.99" " " " " " " " " " " " " " " " " " pudding" " aGVsbG8gd29ybGQ=" " " " " " " " " " " ""; RpcRequest req = xmlParseMemory(s.c_str(), s.size()); CPPUNIT_ASSERT_EQUAL(std::string("aria2.addURI"), req.methodName); CPPUNIT_ASSERT_EQUAL((size_t)3, req.params->size()); CPPUNIT_ASSERT_EQUAL((Integer::ValueType)100, downcast(req.params->get(0))->i()); const Dict* dict = downcast(req.params->get(1)); CPPUNIT_ASSERT_EQUAL((Integer::ValueType)65535, downcast(dict->get("max-count"))->i()); // Current implementation handles double as string. CPPUNIT_ASSERT_EQUAL(std::string("0.99"), downcast(dict->get("seed-ratio"))->s()); const List* list = downcast(req.params->get(2)); CPPUNIT_ASSERT_EQUAL(std::string("pudding"), downcast(list->get(0))->s()); CPPUNIT_ASSERT_EQUAL(std::string("hello world"), downcast(list->get(1))->s()); } void RpcHelperTest::testParseMemory_shouldFail() { try { std::string s = "" " aria2.addURI" " " " " " 100" " "; xmlParseMemory(s.c_str(), s.size()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { // success } } void RpcHelperTest::testParseMemory_withoutParams() { { std::string s = "" " aria2.addURI" " " " " ""; RpcRequest req = xmlParseMemory(s.c_str(), s.size()); CPPUNIT_ASSERT(req.params); } { std::string s = "" " aria2.addURI" ""; RpcRequest req = xmlParseMemory(s.c_str(), s.size()); CPPUNIT_ASSERT(req.params->size()); } } void RpcHelperTest::testParseMemory_withoutStringTag() { std::string s = "" "" " aria2.addUri" " " " " " http://aria2.sourceforge.net" " " " " " http://aria2.sourceforge.net" " " " " " " " " " " " hello" " world" " " " " " " " " " " " " " " " " " apple" " banana" " lemonpeanuts" " " " " " " " " " " ""; RpcRequest req = xmlParseMemory(s.c_str(), s.size()); CPPUNIT_ASSERT_EQUAL((size_t)4, req.params->size()); CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"), downcast(req.params->get(0))->s()); CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net"), downcast(req.params->get(1))->s()); const Dict* dict = downcast(req.params->get(2)); CPPUNIT_ASSERT_EQUAL(std::string("world"), downcast(dict->get("hello"))->s()); const List* list = downcast(req.params->get(3)); CPPUNIT_ASSERT_EQUAL(std::string("apple"), downcast(list->get(0))->s()); CPPUNIT_ASSERT_EQUAL(std::string("banana"), downcast(list->get(1))->s()); CPPUNIT_ASSERT_EQUAL(std::string("lemon"), downcast(list->get(2))->s()); } #endif // ENABLE_XML_RPC } // namespace rpc } // namespace aria2