summaryrefslogtreecommitdiff
path: root/test/RpcMethodTest.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-07-11 21:09:51 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-07-11 21:09:51 +0900
commit7c06b903f3493db95fcf4bd42cccc59e3b4d0692 (patch)
treef10a85795bb086e0b257a0e6c6c5ffb05f28f344 /test/RpcMethodTest.cc
parent7a57ecc5f814dc305e5e4b9bd960d69b43870c0e (diff)
Use std::unique_ptr for ValueBase object hierarchy
Diffstat (limited to 'test/RpcMethodTest.cc')
-rw-r--r--test/RpcMethodTest.cc589
1 files changed, 317 insertions, 272 deletions
diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc
index 0e6e0f05..902a1c3a 100644
--- a/test/RpcMethodTest.cc
+++ b/test/RpcMethodTest.cc
@@ -152,15 +152,22 @@ std::string getString(const Dict* dict, const std::string& key)
}
} // namespace
+namespace {
+RpcRequest createReq(std::string methodName)
+{
+ return {std::move(methodName), List::g()};
+}
+} // namespace
+
void RpcMethodTest::testAddUri()
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam = List::g();
- urisParam->append("http://localhost/");
- req.params->append(urisParam);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
+ urisParam->append("http://localhost/");
+ req.params->append(std::move(urisParam));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const RequestGroupList& rgs =
e_->getRequestGroupMan()->getReservedGroups();
@@ -169,12 +176,16 @@ void RpcMethodTest::testAddUri()
(*rgs.begin())->getDownloadContext()->
getFirstFileEntry()->getRemainingUris().front());
}
- // with options
- std::shared_ptr<Dict> opt = Dict::g();
- opt->put(PREF_DIR->k, "/sink");
- req.params->append(opt);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
+ urisParam->append("http://localhost/");
+ req.params->append(std::move(urisParam));
+ // with options
+ auto opt = Dict::g();
+ opt->put(PREF_DIR->k, "/sink");
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
a2_gid_t gid;
CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
@@ -189,53 +200,52 @@ void RpcMethodTest::testAddUri()
void RpcMethodTest::testAddUri_withoutUri()
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq(AddUriRpcMethod::getMethodName()), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddUri_notUri()
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam = List::g();
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
urisParam->append("not uri");
- req.params->append(urisParam);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(urisParam));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddUri_withBadOption()
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam = List::g();
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
urisParam->append("http://localhost");
- req.params->append(urisParam);
- std::shared_ptr<Dict> opt = Dict::g();
+ req.params->append(std::move(urisParam));
+ auto opt = Dict::g();
opt->put(PREF_FILE_ALLOCATION->k, "badvalue");
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddUri_withPosition()
{
AddUriRpcMethod m;
- RpcRequest req1(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam1 = List::g();
+ auto req1 = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam1 = List::g();
urisParam1->append("http://uri1");
- req1.params->append(urisParam1);
- RpcResponse res1 = m.execute(req1, e_.get());
+ req1.params->append(std::move(urisParam1));
+ auto res1 = m.execute(std::move(req1), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res1.code);
- RpcRequest req2(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam2 = List::g();
+ auto req2 = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam2 = List::g();
urisParam2->append("http://uri2");
- req2.params->append(urisParam2);
+ req2.params->append(std::move(urisParam2));
req2.params->append(Dict::g());
req2.params->append(Integer::g(0));
- m.execute(req2, e_.get());
+ m.execute(std::move(req2), e_.get());
std::string uri =
getReservedGroup(e_->getRequestGroupMan().get(), 0)->
@@ -247,30 +257,37 @@ void RpcMethodTest::testAddUri_withPosition()
void RpcMethodTest::testAddUri_withBadPosition()
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam = List::g();
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
urisParam->append("http://localhost/");
- req.params->append(urisParam);
+ req.params->append(std::move(urisParam));
req.params->append(Dict::g());
req.params->append(Integer::g(-1));
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
#ifdef ENABLE_BITTORRENT
+namespace {
+RpcRequest createAddTorrentReq()
+{
+ auto req = createReq(AddTorrentRpcMethod::getMethodName());
+ req.params->append(readFile(A2_TEST_DIR"/single.torrent"));
+ auto uris = List::g();
+ uris->append("http://localhost/aria2-0.8.2.tar.bz2");
+ req.params->append(std::move(uris));
+ return req;
+}
+} // namespace
+
void RpcMethodTest::testAddTorrent()
{
File(e_->getOption()->get(PREF_DIR)+
"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
AddTorrentRpcMethod m;
- RpcRequest req(AddTorrentRpcMethod::getMethodName(), List::g());
- req.params->append(readFile(A2_TEST_DIR"/single.torrent"));
- std::shared_ptr<List> uris = List::g();
- uris->append("http://localhost/aria2-0.8.2.tar.bz2");
- req.params->append(uris);
{
// Saving upload metadata is disabled by option.
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createAddTorrentReq(), e_.get());
CPPUNIT_ASSERT
(!File(e_->getOption()->get(PREF_DIR)+
"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
@@ -280,7 +297,7 @@ void RpcMethodTest::testAddTorrent()
}
e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createAddTorrentReq(), e_.get());
CPPUNIT_ASSERT
(File(e_->getOption()->get(PREF_DIR)+
"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
@@ -300,15 +317,17 @@ void RpcMethodTest::testAddTorrent()
group->getDownloadContext()->getFirstFileEntry()->
getRemainingUris()[0]);
}
- // with options
- std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddTorrent";
- File(dir).mkdirs();
- std::shared_ptr<Dict> opt = Dict::g();
- opt->put(PREF_DIR->k, dir);
- File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
- req.params->append(opt);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createAddTorrentReq();
+ // with options
+ std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddTorrent";
+ File(dir).mkdirs();
+ auto opt = Dict::g();
+ opt->put(PREF_DIR->k, dir);
+ File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").remove();
+ req.params->append(std::move(opt));
+
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
a2_gid_t gid;
CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
@@ -325,36 +344,36 @@ void RpcMethodTest::testAddTorrent()
void RpcMethodTest::testAddTorrent_withoutTorrent()
{
AddTorrentRpcMethod m;
- RpcRequest req(AddTorrentRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq(AddTorrentRpcMethod::getMethodName()),
+ e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddTorrent_notBase64Torrent()
{
AddTorrentRpcMethod m;
- RpcRequest req(AddTorrentRpcMethod::getMethodName(), List::g());
+ auto req = createReq(AddTorrentRpcMethod::getMethodName());
req.params->append("not torrent");
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddTorrent_withPosition()
{
AddTorrentRpcMethod m;
- RpcRequest req1(AddTorrentRpcMethod::getMethodName(), List::g());
+ auto req1 = createReq(AddTorrentRpcMethod::getMethodName());
req1.params->append(readFile(A2_TEST_DIR"/test.torrent"));
req1.params->append(List::g());
req1.params->append(Dict::g());
- RpcResponse res1 = m.execute(req1, e_.get());
+ auto res1 = m.execute(std::move(req1), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res1.code);
- RpcRequest req2(AddTorrentRpcMethod::getMethodName(), List::g());
+ auto req2 = createReq(AddTorrentRpcMethod::getMethodName());
req2.params->append(readFile(A2_TEST_DIR"/single.torrent"));
req2.params->append(List::g());
req2.params->append(Dict::g());
req2.params->append(Integer::g(0));
- m.execute(req2, e_.get());
+ m.execute(std::move(req2), e_.get());
CPPUNIT_ASSERT_EQUAL((size_t)1,
getReservedGroup(e_->getRequestGroupMan().get(), 0)->
@@ -364,16 +383,23 @@ void RpcMethodTest::testAddTorrent_withPosition()
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
+namespace {
+RpcRequest createAddMetalinkReq()
+{
+ auto req = createReq(AddMetalinkRpcMethod::getMethodName());
+ req.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
+ return req;
+}
+} // namespace
+
void RpcMethodTest::testAddMetalink()
{
File(e_->getOption()->get(PREF_DIR)+
"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
AddMetalinkRpcMethod m;
- RpcRequest req(AddMetalinkRpcMethod::getMethodName(), List::g());
- req.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
{
// Saving upload metadata is disabled by option.
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createAddMetalinkReq(), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
@@ -390,7 +416,7 @@ void RpcMethodTest::testAddMetalink()
}
e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createAddMetalinkReq(), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
@@ -416,15 +442,17 @@ void RpcMethodTest::testAddMetalink()
CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.deb",
deb->getFirstFilePath());
}
- // with options
- std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddMetalink";
- File(dir).mkdirs();
- std::shared_ptr<Dict> opt = Dict::g();
- opt->put(PREF_DIR->k, dir);
- File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
- req.params->append(opt);
{
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createAddMetalinkReq();
+ // with options
+ std::string dir = A2_TEST_OUT_DIR"/aria2_RpcMethodTest_testAddMetalink";
+ File(dir).mkdirs();
+ auto opt = Dict::g();
+ opt->put(PREF_DIR->k, dir);
+ File(dir+"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").remove();
+ req.params->append(std::move(opt));
+
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
@@ -446,36 +474,36 @@ void RpcMethodTest::testAddMetalink()
void RpcMethodTest::testAddMetalink_withoutMetalink()
{
AddMetalinkRpcMethod m;
- RpcRequest req(AddMetalinkRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq(AddMetalinkRpcMethod::getMethodName()),
+ e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddMetalink_notBase64Metalink()
{
AddMetalinkRpcMethod m;
- RpcRequest req(AddMetalinkRpcMethod::getMethodName(), List::g());
+ auto req = createReq(AddMetalinkRpcMethod::getMethodName());
req.params->append("not metalink");
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testAddMetalink_withPosition()
{
AddUriRpcMethod m1;
- RpcRequest req1(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam1 = List::g();
+ auto req1 = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam1 = List::g();
urisParam1->append("http://uri");
- req1.params->append(urisParam1);
- RpcResponse res1 = m1.execute(req1, e_.get());
+ req1.params->append(std::move(urisParam1));
+ auto res1 = m1.execute(std::move(req1), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res1.code);
AddMetalinkRpcMethod m2;
- RpcRequest req2("ari2.addMetalink", List::g());
+ auto req2 = createReq(AddMetalinkRpcMethod::getMethodName());
req2.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
req2.params->append(Dict::g());
req2.params->append(Integer::g(0));
- RpcResponse res2 = m2.execute(req2, e_.get());
+ auto res2 = m2.execute(std::move(req2), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res2.code);
CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
@@ -487,48 +515,45 @@ void RpcMethodTest::testAddMetalink_withPosition()
void RpcMethodTest::testGetOption()
{
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
group->getOption()->put(PREF_DIR, "alpha");
e_->getRequestGroupMan()->addReservedGroup(group);
- std::shared_ptr<DownloadResult> dr = createDownloadResult(error_code::FINISHED,
- "http://host/fin");
+ auto dr = createDownloadResult(error_code::FINISHED, "http://host/fin");
dr->option->put(PREF_DIR, "bravo");
e_->getRequestGroupMan()->addDownloadResult(dr);
GetOptionRpcMethod m;
- RpcRequest req(GetOptionRpcMethod::getMethodName(), List::g());
+ auto req = createReq(GetOptionRpcMethod::getMethodName());
req.params->append(GroupId::toHex(group->getGID()));
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const Dict* resopt = downcast<Dict>(res.param);
CPPUNIT_ASSERT_EQUAL(std::string("alpha"),
downcast<String>(resopt->get(PREF_DIR->k))->s());
- req.params = List::g();
+ req = createReq(GetOptionRpcMethod::getMethodName());
req.params->append(dr->gid->toHex());
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resopt = downcast<Dict>(res.param);
CPPUNIT_ASSERT_EQUAL(std::string("bravo"),
downcast<String>(resopt->get(PREF_DIR->k))->s());
// Invalid GID
- req.params = List::g();
+ req = createReq(GetOptionRpcMethod::getMethodName());
req.params->append(GroupId::create()->toHex());
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testChangeOption()
{
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
e_->getRequestGroupMan()->addReservedGroup(group);
ChangeOptionRpcMethod m;
- RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
+ auto req = createReq(ChangeOptionRpcMethod::getMethodName());
req.params->append(GroupId::toHex(group->getGID()));
- std::shared_ptr<Dict> opt = Dict::g();
+ auto opt = Dict::g();
opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "100K");
#ifdef ENABLE_BITTORRENT
opt->put(PREF_BT_MAX_PEERS->k, "100");
@@ -541,10 +566,10 @@ void RpcMethodTest::testChangeOption()
e_->getBtRegistry()->put(group->getGID(), std::move(btObject));
}
#endif // ENABLE_BITTORRENT
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
- std::shared_ptr<Option> option = group->getOption();
+ auto option = group->getOption();
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL(100*1024,
@@ -569,33 +594,31 @@ void RpcMethodTest::testChangeOption()
void RpcMethodTest::testChangeOption_withBadOption()
{
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
e_->getRequestGroupMan()->addReservedGroup(group);
ChangeOptionRpcMethod m;
- RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
+ auto req = createReq(ChangeOptionRpcMethod::getMethodName());
req.params->append(GroupId::toHex(group->getGID()));
- std::shared_ptr<Dict> opt = Dict::g();
+ auto opt = Dict::g();
opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "badvalue");
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testChangeOption_withNotAllowedOption()
{
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
e_->getRequestGroupMan()->addReservedGroup(group);
ChangeOptionRpcMethod m;
- RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
+ auto req = createReq(ChangeOptionRpcMethod::getMethodName());
req.params->append(GroupId::toHex(group->getGID()));
- std::shared_ptr<Dict> opt = Dict::g();
+ auto opt = Dict::g();
opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
// The unacceptable options are just ignored.
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
@@ -603,23 +626,22 @@ void RpcMethodTest::testChangeOption_withNotAllowedOption()
void RpcMethodTest::testChangeOption_withoutGid()
{
ChangeOptionRpcMethod m;
- RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute
+ (createReq(ChangeOptionRpcMethod::getMethodName()), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testChangeGlobalOption()
{
ChangeGlobalOptionRpcMethod m;
- RpcRequest req
- (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
- std::shared_ptr<Dict> opt = Dict::g();
+ auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
+ auto opt = Dict::g();
opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
#ifdef ENABLE_BITTORRENT
opt->put(PREF_MAX_OVERALL_UPLOAD_LIMIT->k, "50K");
#endif // ENABLE_BITTORRENT
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL
@@ -639,24 +661,22 @@ void RpcMethodTest::testChangeGlobalOption()
void RpcMethodTest::testChangeGlobalOption_withBadOption()
{
ChangeGlobalOptionRpcMethod m;
- RpcRequest req
- (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
- std::shared_ptr<Dict> opt = Dict::g();
+ auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
+ auto opt = Dict::g();
opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "badvalue");
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
{
ChangeGlobalOptionRpcMethod m;
- RpcRequest req
- (ChangeGlobalOptionRpcMethod::getMethodName(), List::g());
- std::shared_ptr<Dict> opt = Dict::g();
+ auto req = createReq(ChangeGlobalOptionRpcMethod::getMethodName());
+ auto opt = Dict::g();
opt->put(PREF_ENABLE_RPC->k, "100K");
- req.params->append(opt);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(opt));
+ auto res = m.execute(std::move(req), e_.get());
// The unacceptable options are just ignored.
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
@@ -664,8 +684,7 @@ void RpcMethodTest::testChangeGlobalOption_withNotAllowedOption()
void RpcMethodTest::testNoSuchMethod()
{
NoSuchMethodRpcMethod m;
- RpcRequest req("make.hamburger", List::g());
- RpcResponse res = m.execute(req, 0);
+ auto res = m.execute(createReq("make.hamburger"), 0);
CPPUNIT_ASSERT_EQUAL(1, res.code);
CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
getString(downcast<Dict>(res.param), "faultString"));
@@ -674,8 +693,8 @@ void RpcMethodTest::testNoSuchMethod()
void RpcMethodTest::testTellStatus_withoutGid()
{
TellStatusRpcMethod m;
- RpcRequest req(TellStatusRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute
+ (createReq(TellStatusRpcMethod::getMethodName()), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
@@ -683,11 +702,11 @@ namespace {
void addUri(const std::string& uri, const std::shared_ptr<DownloadEngine>& e)
{
AddUriRpcMethod m;
- RpcRequest req(AddUriRpcMethod::getMethodName(), List::g());
- std::shared_ptr<List> urisParam = List::g();
+ auto req = createReq(AddUriRpcMethod::getMethodName());
+ auto urisParam = List::g();
urisParam->append(uri);
- req.params->append(urisParam);
- CPPUNIT_ASSERT_EQUAL(0, m.execute(req, e.get()).code);
+ req.params->append(std::move(urisParam));
+ CPPUNIT_ASSERT_EQUAL(0, m.execute(std::move(req), e.get()).code);
}
} // namespace
@@ -697,9 +716,9 @@ void addTorrent
(const std::string& torrentFile, const std::shared_ptr<DownloadEngine>& e)
{
AddTorrentRpcMethod m;
- RpcRequest req(AddTorrentRpcMethod::getMethodName(), List::g());
+ auto req = createReq(AddTorrentRpcMethod::getMethodName());
req.params->append(readFile(torrentFile));
- RpcResponse res = m.execute(req, e.get());
+ auto res = m.execute(std::move(req), e.get());
}
} // namespace
#endif // ENABLE_BITTORRENT
@@ -716,10 +735,10 @@ void RpcMethodTest::testTellWaiting()
#endif // !ENABLE_BITTORRENT
auto& rgman = e_->getRequestGroupMan();
TellWaitingRpcMethod m;
- RpcRequest req(TellWaitingRpcMethod::getMethodName(), List::g());
+ auto req = createReq(TellWaitingRpcMethod::getMethodName());
req.params->append(Integer::g(1));
req.params->append(Integer::g(2));
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
@@ -730,56 +749,60 @@ void RpcMethodTest::testTellWaiting()
->getGID()),
getString(downcast<Dict>(resParams->get(1)), "gid"));
// waiting.size() == offset+num
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
req.params->append(Integer::g(1));
req.params->append(Integer::g(3));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
// waiting.size() < offset+num
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
req.params->append(Integer::g(1));
req.params->append(Integer::g(4));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
// offset = INT32_MAX
- req.params->set(0, Integer::g(INT32_MAX));
- req.params->set(1, Integer::g(1));
- res = m.execute(req, e_.get());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
+ req.params->append(Integer::g(INT32_MAX));
+ req.params->append(Integer::g(1));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
// num = INT32_MAX
- req.params->set(0, Integer::g(1));
- req.params->set(1, Integer::g(INT32_MAX));
- res = m.execute(req, e_.get());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
+ req.params->append(Integer::g(1));
+ req.params->append(Integer::g(INT32_MAX));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)3, resParams->size());
// offset=INT32_MAX and num = INT32_MAX
- req.params->set(0, Integer::g(INT32_MAX));
- req.params->set(1, Integer::g(INT32_MAX));
- res = m.execute(req, e_.get());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
+ req.params->append(Integer::g(INT32_MAX));
+ req.params->append(Integer::g(INT32_MAX));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
// offset=INT32_MIN and num = INT32_MAX
- req.params->set(0, Integer::g(INT32_MIN));
- req.params->set(1, Integer::g(INT32_MAX));
- res = m.execute(req, e_.get());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
+ req.params->append(Integer::g(INT32_MIN));
+ req.params->append(Integer::g(INT32_MAX));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
// negative offset
- req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req = createReq(TellWaitingRpcMethod::getMethodName());
req.params->append(Integer::g(-1));
req.params->append(Integer::g(2));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
@@ -790,20 +813,26 @@ void RpcMethodTest::testTellWaiting()
2)->getGID()),
getString(downcast<Dict>(resParams->get(1)), "gid"));
// negative offset and size < num
- req.params->set(1, Integer::g(100));
- res = m.execute(req, e_.get());
+ req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req.params->append(Integer::g(-1));
+ req.params->append(Integer::g(100));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)4, resParams->size());
// nagative offset and normalized offset < 0
- req.params->set(0, Integer::g(-5));
- res = m.execute(req, e_.get());
+ req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req.params->append(Integer::g(-5));
+ req.params->append(Integer::g(100));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)0, resParams->size());
// nagative offset and normalized offset == 0
- req.params->set(0, Integer::g(-4));
- res = m.execute(req, e_.get());
+ req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
+ req.params->append(Integer::g(-4));
+ req.params->append(Integer::g(100));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)1, resParams->size());
@@ -812,24 +841,23 @@ void RpcMethodTest::testTellWaiting()
void RpcMethodTest::testTellWaiting_fail()
{
TellWaitingRpcMethod m;
- RpcRequest req(TellWaitingRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq(TellWaitingRpcMethod::getMethodName()),
+ e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
void RpcMethodTest::testGetVersion()
{
GetVersionRpcMethod m;
- RpcRequest req(GetVersionRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq(GetVersionRpcMethod::getMethodName()),
+ e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const Dict* resParams = downcast<Dict>(res.param);
CPPUNIT_ASSERT_EQUAL(std::string(PACKAGE_VERSION),
getString(resParams, "version"));
const List* featureList = downcast<List>(resParams->get("enabledFeatures"));
std::string features;
- for(List::ValueType::const_iterator i = featureList->begin();
- i != featureList->end(); ++i) {
+ for(auto i = featureList->begin(); i != featureList->end(); ++i) {
const String* s = downcast<String>(*i);
features += s->s();
features += ", ";
@@ -839,11 +867,11 @@ void RpcMethodTest::testGetVersion()
void RpcMethodTest::testGatherStoppedDownload()
{
- std::vector<std::shared_ptr<FileEntry> > fileEntries;
+ std::vector<std::shared_ptr<FileEntry>> fileEntries;
std::vector<a2_gid_t> followedBy;
followedBy.push_back(3);
followedBy.push_back(4);
- std::shared_ptr<DownloadResult> d(new DownloadResult());
+ auto d = std::make_shared<DownloadResult>();
d->gid = GroupId::create();
d->fileEntries = fileEntries;
d->inMemoryDownload = false;
@@ -852,9 +880,9 @@ void RpcMethodTest::testGatherStoppedDownload()
d->result = error_code::FINISHED;
d->followedBy = followedBy;
d->belongsTo = 2;
- std::shared_ptr<Dict> entry = Dict::g();
+ auto entry = Dict::g();
std::vector<std::string> keys;
- gatherStoppedDownload(entry, d, keys);
+ gatherStoppedDownload(entry.get(), d, keys);
const List* followedByRes = downcast<List>(entry->get("followedBy"));
CPPUNIT_ASSERT_EQUAL(GroupId::toHex(3),
@@ -867,33 +895,32 @@ void RpcMethodTest::testGatherStoppedDownload()
keys.push_back("gid");
entry = Dict::g();
- gatherStoppedDownload(entry, d, keys);
+ gatherStoppedDownload(entry.get(), d, keys);
CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
CPPUNIT_ASSERT(entry->containsKey("gid"));
}
void RpcMethodTest::testGatherProgressCommon()
{
- std::shared_ptr<DownloadContext> dctx(new DownloadContext(0, 0,"aria2.tar.bz2"));
+ auto dctx = std::make_shared<DownloadContext>(0, 0,"aria2.tar.bz2");
std::string uris[] = { "http://localhost/aria2.tar.bz2" };
dctx->getFirstFileEntry()->addUris(std::begin(uris), std::end(uris));
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- util::copy(option_)));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(),
+ util::copy(option_));
group->setDownloadContext(dctx);
- std::vector<std::shared_ptr<RequestGroup> > followedBy;
+ std::vector<std::shared_ptr<RequestGroup>> followedBy;
for(int i = 0; i < 2; ++i) {
- followedBy.push_back(std::shared_ptr<RequestGroup>
- (new RequestGroup(GroupId::create(),
- util::copy(option_))));
+ followedBy.push_back(std::make_shared<RequestGroup>(GroupId::create(),
+ util::copy(option_)));
}
group->followedBy(followedBy.begin(), followedBy.end());
- std::shared_ptr<GroupId> parent = GroupId::create();
+ auto parent = GroupId::create();
group->belongsTo(parent->getNumericId());
- std::shared_ptr<Dict> entry = Dict::g();
+ auto entry = Dict::g();
std::vector<std::string> keys;
- gatherProgressCommon(entry, group, keys);
+ gatherProgressCommon(entry.get(), group, keys);
const List* followedByRes = downcast<List>(entry->get("followedBy"));
CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[0]->getGID()),
@@ -918,7 +945,7 @@ void RpcMethodTest::testGatherProgressCommon()
keys.push_back("gid");
entry = Dict::g();
- gatherProgressCommon(entry, group, keys);
+ gatherProgressCommon(entry.get(), group, keys);
CPPUNIT_ASSERT_EQUAL((size_t)1, entry->size());
CPPUNIT_ASSERT(entry->containsKey("gid"));
@@ -928,12 +955,12 @@ void RpcMethodTest::testGatherProgressCommon()
#ifdef ENABLE_BITTORRENT
void RpcMethodTest::testGatherBitTorrentMetadata()
{
- std::shared_ptr<Option> option(new Option());
+ auto option = std::make_shared<Option>();
option->put(PREF_DIR, ".");
- std::shared_ptr<DownloadContext> dctx(new DownloadContext());
+ auto dctx = std::make_shared<DownloadContext>();
bittorrent::load(A2_TEST_DIR"/test.torrent", dctx, option);
- std::shared_ptr<Dict> btDict = Dict::g();
- gatherBitTorrentMetadata(btDict, bittorrent::getTorrentAttrs(dctx));
+ auto btDict = Dict::g();
+ gatherBitTorrentMetadata(btDict.get(), bittorrent::getTorrentAttrs(dctx));
CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
downcast<String>(btDict->get("comment"))->s());
CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
@@ -961,7 +988,7 @@ void RpcMethodTest::testGatherBitTorrentMetadata()
modBtAttrs->mode = BT_FILE_MODE_NONE;
modBtAttrs->metadata.clear();
btDict = Dict::g();
- gatherBitTorrentMetadata(btDict, modBtAttrs);
+ gatherBitTorrentMetadata(btDict.get(), modBtAttrs);
CPPUNIT_ASSERT(!btDict->containsKey("comment"));
CPPUNIT_ASSERT(!btDict->containsKey("creationDate"));
CPPUNIT_ASSERT(!btDict->containsKey("mode"));
@@ -973,19 +1000,17 @@ void RpcMethodTest::testGatherBitTorrentMetadata()
void RpcMethodTest::testChangePosition()
{
e_->getRequestGroupMan()->addReservedGroup
- (std::shared_ptr<RequestGroup>(new RequestGroup(GroupId::create(),
- util::copy(option_))));
+ (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
e_->getRequestGroupMan()->addReservedGroup
- (std::shared_ptr<RequestGroup>(new RequestGroup(GroupId::create(),
- util::copy(option_))));
+ (std::make_shared<RequestGroup>(GroupId::create(), util::copy(option_)));
a2_gid_t gid = getReservedGroup(e_->getRequestGroupMan().get(), 0)->getGID();
ChangePositionRpcMethod m;
- RpcRequest req(ChangePositionRpcMethod::getMethodName(), List::g());
+ auto req = createReq(ChangePositionRpcMethod::getMethodName());
req.params->append(GroupId::toHex(gid));
req.params->append(Integer::g(1));
req.params->append("POS_SET");
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
CPPUNIT_ASSERT_EQUAL
@@ -995,16 +1020,34 @@ void RpcMethodTest::testChangePosition()
void RpcMethodTest::testChangePosition_fail()
{
ChangePositionRpcMethod m;
- RpcRequest req(ChangePositionRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute
+ (createReq(ChangePositionRpcMethod::getMethodName()), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
+ auto req = createReq(ChangePositionRpcMethod::getMethodName());
req.params->append("1");
req.params->append(Integer::g(2));
req.params->append("bad keyword");
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
+namespace {
+RpcRequest createChangeUriReq(a2_gid_t gid, size_t fileIndex)
+{
+ auto req = createReq(ChangeUriRpcMethod::getMethodName());
+
+ req.params->append(GroupId::toHex(gid)); // GID
+ req.params->append(Integer::g(fileIndex)); // index of FileEntry
+ auto removeuris = List::g();
+ removeuris->append("http://example.org/mustremove1");
+ removeuris->append("http://example.org/mustremove2");
+ removeuris->append("http://example.org/notexist");
+ req.params->append(std::move(removeuris));
+ return req;
+}
+} // namespace
+
void RpcMethodTest::testChangeUri()
{
std::shared_ptr<FileEntry> files[3];
@@ -1014,29 +1057,21 @@ void RpcMethodTest::testChangeUri()
files[1]->addUri("http://example.org/aria2.tar.bz2");
files[1]->addUri("http://example.org/mustremove1");
files[1]->addUri("http://example.org/mustremove2");
- std::shared_ptr<DownloadContext> dctx(new DownloadContext());
+ auto dctx = std::make_shared<DownloadContext>();
dctx->setFileEntries(&files[0], &files[3]);
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
group->setDownloadContext(dctx);
e_->getRequestGroupMan()->addReservedGroup(group);
ChangeUriRpcMethod m;
- RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
- req.params->append(GroupId::toHex(group->getGID())); // GID
- req.params->append(Integer::g(2)); // index of FileEntry
- std::shared_ptr<List> removeuris = List::g();
- removeuris->append("http://example.org/mustremove1");
- removeuris->append("http://example.org/mustremove2");
- removeuris->append("http://example.org/notexist");
- req.params->append(removeuris);
- std::shared_ptr<List> adduris = List::g();
+ auto req = createChangeUriReq(group->getGID(), 2);
+ auto adduris = List::g();
adduris->append("http://example.org/added1");
adduris->append("http://example.org/added2");
adduris->append("baduri");
adduris->append("http://example.org/added3");
- req.params->append(adduris);
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(adduris));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
CPPUNIT_ASSERT_EQUAL((int64_t)3, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
@@ -1049,14 +1084,15 @@ void RpcMethodTest::testChangeUri()
CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added2"), uris[2]);
CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added3"), uris[3]);
+ req = createChangeUriReq(group->getGID(), 2);
// Change adduris
adduris = List::g();
adduris->append("http://example.org/added1-1");
adduris->append("http://example.org/added1-2");
- req.params->set(3, adduris);
+ req.params->append(std::move(adduris));
// Set position parameter
req.params->append(Integer::g(2));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
@@ -1066,10 +1102,14 @@ void RpcMethodTest::testChangeUri()
CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[3]);
// Change index of FileEntry
- req.params->set(1, Integer::g(1));
+ req = createChangeUriReq(group->getGID(), 1);
+ adduris = List::g();
+ adduris->append("http://example.org/added1-1");
+ adduris->append("http://example.org/added1-2");
+ req.params->append(std::move(adduris));
// Set position far beyond the size of uris in FileEntry.
- req.params->set(4, Integer::g(1000));
- res = m.execute(req, e_.get());
+ req.params->append(Integer::g(1000));
+ res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL((int64_t)0, downcast<Integer>(downcast<List>(res.param)->get(0))->i());
CPPUNIT_ASSERT_EQUAL((int64_t)2, downcast<Integer>(downcast<List>(res.param)->get(1))->i());
@@ -1079,61 +1119,67 @@ void RpcMethodTest::testChangeUri()
CPPUNIT_ASSERT_EQUAL(std::string("http://example.org/added1-2"), uris[1]);
}
+namespace {
+RpcRequest createChangeUriEmptyReq(a2_gid_t gid, size_t fileIndex)
+{
+ auto req = createReq(ChangeUriRpcMethod::getMethodName());
+
+ req.params->append(GroupId::toHex(gid)); // GID
+ req.params->append(Integer::g(fileIndex)); // index of FileEntry
+ req.params->append(List::g()); // remove uris
+ req.params->append(List::g()); // append uris
+ return req;
+}
+} // namespace
+
void RpcMethodTest::testChangeUri_fail()
{
std::shared_ptr<FileEntry> files[3];
for(int i = 0; i < 3; ++i) {
- files[i].reset(new FileEntry());
+ files[i] = std::make_shared<FileEntry>();
}
- std::shared_ptr<DownloadContext> dctx(new DownloadContext());
+ auto dctx = std::make_shared<DownloadContext>();
dctx->setFileEntries(&files[0], &files[3]);
- std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(),
- option_));
+ auto group = std::make_shared<RequestGroup>(GroupId::create(), option_);
group->setDownloadContext(dctx);
e_->getRequestGroupMan()->addReservedGroup(group);
ChangeUriRpcMethod m;
- RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
- req.params->append(GroupId::toHex(group->getGID())); // GID
- req.params->append(Integer::g(1)); // index of FileEntry
- std::shared_ptr<List> removeuris = List::g();
- req.params->append(removeuris);
- std::shared_ptr<List> adduris = List::g();
- req.params->append(adduris);
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createChangeUriEmptyReq(group->getGID(), 1);
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
- req.params->set(1, Integer::g(0));
- res = m.execute(req, e_.get());
+ req = createChangeUriEmptyReq(group->getGID(), 0);
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because 2nd argument is less than 1.
CPPUNIT_ASSERT_EQUAL(1, res.code);
- req.params->set(1, Integer::g(1));
- req.params->set(0, String::g(GroupId::create()->toHex()));
- res = m.execute(req, e_.get());
+ req = createChangeUriEmptyReq(GroupId::create()->getNumericId(), 1);
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because the given GID does not exist.
CPPUNIT_ASSERT_EQUAL(1, res.code);
- req.params->set(0, String::g(GroupId::toHex(group->getGID())));
- req.params->set(1, Integer::g(4));
- res = m.execute(req, e_.get());
+ req = createChangeUriEmptyReq(group->getGID(), 4);
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because FileEntry#3 does not exist.
CPPUNIT_ASSERT_EQUAL(1, res.code);
+ req = createChangeUriEmptyReq(group->getGID(), 1);
req.params->set(1, String::g("0"));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because index of FileEntry is string.
CPPUNIT_ASSERT_EQUAL(1, res.code);
- req.params->set(1, Integer::g(1));
+ req = createChangeUriEmptyReq(group->getGID(), 1);
req.params->set(2, String::g("http://url"));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because 3rd param is not list.
CPPUNIT_ASSERT_EQUAL(1, res.code);
+ req = createChangeUriEmptyReq(group->getGID(), 1);
req.params->set(2, List::g());
req.params->set(3, String::g("http://url"));
- res = m.execute(req, e_.get());
+ res = m.execute(std::move(req), e_.get());
// RPC request fails because 4th param is not list.
CPPUNIT_ASSERT_EQUAL(1, res.code);
}
@@ -1141,8 +1187,8 @@ void RpcMethodTest::testChangeUri_fail()
void RpcMethodTest::testGetSessionInfo()
{
GetSessionInfoRpcMethod m;
- RpcRequest req(GetSessionInfoRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute
+ (createReq(GetSessionInfoRpcMethod::getMethodName()), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL(util::toHex(e_->getSessionId()),
getString(downcast<Dict>(res.param), "sessionId"));
@@ -1156,30 +1202,30 @@ void RpcMethodTest::testPause()
"http://url3",
};
option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
- std::vector<std::shared_ptr<RequestGroup> > groups;
+ std::vector<std::shared_ptr<RequestGroup>> groups;
createRequestGroupForUri(groups, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)3, groups.size());
e_->getRequestGroupMan()->addReservedGroup(groups);
{
PauseRpcMethod m;
- RpcRequest req(PauseRpcMethod::getMethodName(), List::g());
+ auto req = createReq(PauseRpcMethod::getMethodName());
req.params->append(GroupId::toHex(groups[0]->getGID()));
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
CPPUNIT_ASSERT(groups[0]->isPauseRequested());
{
UnpauseRpcMethod m;
- RpcRequest req(UnpauseRpcMethod::getMethodName(), List::g());
+ auto req = createReq(UnpauseRpcMethod::getMethodName());
req.params->append(GroupId::toHex(groups[0]->getGID()));
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
CPPUNIT_ASSERT(!groups[0]->isPauseRequested());
{
PauseAllRpcMethod m;
- RpcRequest req(PauseAllRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createReq(PauseAllRpcMethod::getMethodName());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
for(size_t i = 0; i < groups.size(); ++i) {
@@ -1187,8 +1233,8 @@ void RpcMethodTest::testPause()
}
{
UnpauseAllRpcMethod m;
- RpcRequest req(UnpauseAllRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createReq(UnpauseAllRpcMethod::getMethodName());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
for(size_t i = 0; i < groups.size(); ++i) {
@@ -1196,8 +1242,8 @@ void RpcMethodTest::testPause()
}
{
ForcePauseAllRpcMethod m;
- RpcRequest req(ForcePauseAllRpcMethod::getMethodName(), List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto req = createReq(ForcePauseAllRpcMethod::getMethodName());
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
}
for(size_t i = 0; i < groups.size(); ++i) {
@@ -1208,47 +1254,47 @@ void RpcMethodTest::testPause()
void RpcMethodTest::testSystemMulticall()
{
SystemMulticallRpcMethod m;
- RpcRequest req("system.multicall", List::g());
- std::shared_ptr<List> reqparams = List::g();
- req.params->append(reqparams);
+ auto req = createReq("system.multicall");
+ auto reqparams = List::g();
for(int i = 0; i < 2; ++i) {
- std::shared_ptr<Dict> dict = Dict::g();
+ auto dict = Dict::g();
dict->put("methodName", AddUriRpcMethod::getMethodName());
- std::shared_ptr<List> params = List::g();
- std::shared_ptr<List> urisParam = List::g();
+ auto params = List::g();
+ auto urisParam = List::g();
urisParam->append("http://localhost/"+util::itos(i));
- params->append(urisParam);
- dict->put("params", params);
- reqparams->append(dict);
+ params->append(std::move(urisParam));
+ dict->put("params", std::move(params));
+ reqparams->append(std::move(dict));
}
{
- std::shared_ptr<Dict> dict = Dict::g();
+ auto dict = Dict::g();
dict->put("methodName", "not exists");
dict->put("params", List::g());
- reqparams->append(dict);
+ reqparams->append(std::move(dict));
}
{
reqparams->append("not struct");
}
{
- std::shared_ptr<Dict> dict = Dict::g();
+ auto dict = Dict::g();
dict->put("methodName", "system.multicall");
dict->put("params", List::g());
- reqparams->append(dict);
+ reqparams->append(std::move(dict));
}
{
// missing params
- std::shared_ptr<Dict> dict = Dict::g();
+ auto dict = Dict::g();
dict->put("methodName", GetVersionRpcMethod::getMethodName());
- reqparams->append(dict);
+ reqparams->append(std::move(dict));
}
{
- std::shared_ptr<Dict> dict = Dict::g();
+ auto dict = Dict::g();
dict->put("methodName", GetVersionRpcMethod::getMethodName());
dict->put("params", List::g());
- reqparams->append(dict);
+ reqparams->append(std::move(dict));
}
- RpcResponse res = m.execute(req, e_.get());
+ req.params->append(std::move(reqparams));
+ auto res = m.execute(std::move(req), e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
@@ -1278,8 +1324,7 @@ void RpcMethodTest::testSystemMulticall()
void RpcMethodTest::testSystemMulticall_fail()
{
SystemMulticallRpcMethod m;
- RpcRequest req("system.multicall", List::g());
- RpcResponse res = m.execute(req, e_.get());
+ auto res = m.execute(createReq("system.multicall"), e_.get());
CPPUNIT_ASSERT_EQUAL(1, res.code);
}