From d0dbf889ae85a1abba844f08286ad0e745c1b69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 14 May 2017 18:36:24 +0200 Subject: [PATCH] Use libtorrent sha1 hash in tests --- toREST/include/torrents.hpp | 21 ++++++++++++++++ toREST/tests/include/ServerContext.hpp | 1 + toREST/tests/include/SessionContext.hpp | 6 +++-- toREST/tests/include/TorrentContext.hpp | 32 +++++------------------- toREST/tests/stubs/ServerContext.cpp | 7 ++++++ toREST/tests/stubs/SessionContext.cpp | 24 +++++++++++++++++- toREST/tests/stubs/TorrentContext.cpp | 26 ++++++++++++++++--- toREST/tests/torrents_test.cpp | 33 ++++++++++++++++++++++++- 8 files changed, 116 insertions(+), 34 deletions(-) diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 3f2e2bc..24dca93 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -67,6 +67,27 @@ void del(torrent_session &session, response resp, request req) { if (request_object.find("info_hash") == request_object.end()) { return respond(http::bad_request); } + + auto obj = request_object.at("info_hash"); + + if (!obj.is_string()) { + return respond(http::bad_request); + } + + std::string sha1_hash_in = obj; + libtorrent::sha1_hash sha1_hash; + std::stringstream ss; + ss << sha1_hash_in; + ss >> sha1_hash; + auto handle = session.find_torrent(sha1_hash); + + if (handle.is_valid()) { + session.remove_torrent(handle); + } else { + return respond(http::bad_request); + } + + respond(http::no_content); } template diff --git a/toREST/tests/include/ServerContext.hpp b/toREST/tests/include/ServerContext.hpp index 8121cb6..ea83740 100644 --- a/toREST/tests/include/ServerContext.hpp +++ b/toREST/tests/include/ServerContext.hpp @@ -27,6 +27,7 @@ public: static void ok(std::shared_ptr response, const nlohmann::json &data); static void bad_request(std::shared_ptr response); static void accepted(std::shared_ptr response); + static void no_content(std::shared_ptr response); static void created(std::shared_ptr response, std::shared_ptr request, const std::string &location); }; diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index 5335635..088654d 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -2,9 +2,9 @@ #define _TR_TEST_SESSION_CONTEXT_HPP_ #include +#include #include #include -#include class TestSessionSettings { public: @@ -38,8 +38,10 @@ public: int listen_port(); bool is_dht_running() const; std::vector &get_torrents(); + TestTorrent find_torrent(const libtorrent::sha1_hash &hash); + void remove_torrent(const TestTorrent &torrent); void apply_settings(TestSessionSettings settings); void async_add_torrent(const libtorrent::add_torrent_params ¶ms); }; -#endif +#endif \ No newline at end of file diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 5427bb5..e62c2f5 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -4,15 +4,6 @@ #include -class InfoHash { -private: - std::string hash; - -public: - InfoHash(const std::string &h) : hash(h) {} - std::string to_string() { return hash; } -}; - class TorrentStatus { public: bool paused = false; @@ -26,30 +17,19 @@ public: class TestTorrent { public: - TestTorrent(const libtorrent::add_torrent_params ¶ms) : hash(params.info_hash.to_string()) { - auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused; - status_.paused = paused; - status_.save_path = params.save_path; - status_.name = params.name; - upload_limit_ = params.upload_limit; - download_limit_ = params.download_limit; - } - TestTorrent() : hash("12a") {} + TestTorrent(const libtorrent::add_torrent_params ¶ms); + TestTorrent() {} bool is_valid(); - InfoHash info_hash(); + libtorrent::sha1_hash info_hash() const; TorrentStatus status(int type = 0); int query_name = 1; int query_save_path = 2; bool valid = true; int upload_limit_ = 0; - int upload_limit(){ - return upload_limit_; - } + int upload_limit(); int download_limit_ = 0; - int download_limit(){ - return download_limit_; - } - InfoHash hash; + int download_limit(); + libtorrent::sha1_hash hash_; TorrentStatus status_; }; diff --git a/toREST/tests/stubs/ServerContext.cpp b/toREST/tests/stubs/ServerContext.cpp index c248268..948beba 100644 --- a/toREST/tests/stubs/ServerContext.cpp +++ b/toREST/tests/stubs/ServerContext.cpp @@ -83,6 +83,13 @@ void CommonResponse::ok(std::shared_ptr response, const nlohmann:: REQUIRE(response->content() == data_dump); is_json_request(response); }; + +void CommonResponse::no_content(std::shared_ptr response) { + REQUIRE(response->code() == "204"); + REQUIRE(response->message() == "No Content"); + REQUIRE(response->content() == ""); +}; + void CommonResponse::bad_request(std::shared_ptr response) { REQUIRE(response->code() == "400"); REQUIRE(response->message() == "Bad Request"); diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index bffe453..01c6110 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -64,6 +64,29 @@ bool TestSession::is_dht_running() const { std::vector &TestSession::get_torrents() { return torrents_; } + +TestTorrent TestSession::find_torrent(const libtorrent::sha1_hash &hash) { + for (auto &torrent : torrents_) { + if (torrent.info_hash() == hash) { + return torrent; + } + } + auto res = TestTorrent(); + res.valid = false; + return res; +} + +void TestSession::remove_torrent(const TestTorrent &torrent) { + auto itr = torrents_.end(); + for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { + if (itr->info_hash() == torrent.info_hash()) { + break; + } + } + if (itr != torrents_.end()) + torrents_.erase(itr); +} + void TestSession::apply_settings(TestSessionSettings settings) { settings_ = settings; }; @@ -71,4 +94,3 @@ void TestSession::apply_settings(TestSessionSettings settings) { void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) { torrents_.emplace_back(settings); }; - diff --git a/toREST/tests/stubs/TorrentContext.cpp b/toREST/tests/stubs/TorrentContext.cpp index 410ba26..95df1fb 100644 --- a/toREST/tests/stubs/TorrentContext.cpp +++ b/toREST/tests/stubs/TorrentContext.cpp @@ -1,13 +1,31 @@ #include -InfoHash TestTorrent::info_hash() { - return hash; -} - TorrentStatus TestTorrent::status(int type) { return status_; } bool TestTorrent::is_valid() { return valid; +} + +int TestTorrent::upload_limit() { + return upload_limit_; +} + +int TestTorrent::download_limit() { + return download_limit_; +} + +TestTorrent::TestTorrent(const libtorrent::add_torrent_params ¶ms) { + auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused; + status_.paused = paused; + status_.save_path = params.save_path; + status_.name = params.name; + upload_limit_ = params.upload_limit; + download_limit_ = params.download_limit; + hash_ = params.info_hash; +} + +libtorrent::sha1_hash TestTorrent::info_hash() const { + return hash_; } \ No newline at end of file diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 44d4fb0..38d7afe 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -7,7 +7,7 @@ using namespace std; const nlohmann::json torrent = { - {"hash", "12a"}, + {"hash", "12a12a12a12a12a12a12"}, {"paused", false}, {"seeding", false}, {"state", 0}, @@ -37,6 +37,9 @@ SCENARIO("We are running a GET /session/torrents resource") { } GIVEN("we have at least one torrent") { auto t_torrent = TestTorrent(); + libtorrent::sha1_hash sha; + sha.assign("12a12a12a12a12a12a12"); + t_torrent.hash_ = sha; torrent_session.get_torrents().emplace_back(t_torrent); THEN("the server should reply with an array of torrents") { tr::session::torrents::get(torrent_session, response, request); @@ -182,6 +185,34 @@ SCENARIO("We are running a DELETE /session/torrents resource") { CommonResponse::bad_request(response); } } + GIVEN("required field is wrong type") { + request->content << nlohmann::json({{"info_hash", 1}}); + THEN("the server should reply with bad request") { + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::bad_request(response); + } + } + GIVEN("fields are correct") { + request->content << nlohmann::json({{"info_hash", torrent_id}}); + GIVEN("the torrent exists") { + torrent_session.torrents_.emplace_back(); + libtorrent::sha1_hash sha1_hash; + std::stringstream ss; + ss << torrent_id; + ss >> sha1_hash; + torrent_session.torrents_[0].hash_ = sha1_hash; + THEN("the server should reply no content") { + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::no_content(response); + } + } + GIVEN("the torrent doesn't exist") { + THEN("the server should reply with bad request") { + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::bad_request(response); + } + } + } } } }