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