Browse Source

Use libtorrent sha1 hash in tests

master
Jørgen Lien Sellæg 9 years ago
parent
commit
d0dbf889ae
  1. 21
      toREST/include/torrents.hpp
  2. 1
      toREST/tests/include/ServerContext.hpp
  3. 4
      toREST/tests/include/SessionContext.hpp
  4. 32
      toREST/tests/include/TorrentContext.hpp
  5. 7
      toREST/tests/stubs/ServerContext.cpp
  6. 24
      toREST/tests/stubs/SessionContext.cpp
  7. 26
      toREST/tests/stubs/TorrentContext.cpp
  8. 33
      toREST/tests/torrents_test.cpp

21
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 <class settings, class torrent_session, class request, class response>

1
toREST/tests/include/ServerContext.hpp

@ -27,6 +27,7 @@ public:
static void ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data);
static void bad_request(std::shared_ptr<TestResponse> response);
static void accepted(std::shared_ptr<TestResponse> response);
static void no_content(std::shared_ptr<TestResponse> response);
static void created(std::shared_ptr<TestResponse> response, std::shared_ptr<TestRequest> request, const std::string &location);
};

4
toREST/tests/include/SessionContext.hpp

@ -2,9 +2,9 @@
#define _TR_TEST_SESSION_CONTEXT_HPP_
#include <TorrentContext.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <sstream>
#include <vector>
#include <libtorrent/magnet_uri.hpp>
class TestSessionSettings {
public:
@ -38,6 +38,8 @@ public:
int listen_port();
bool is_dht_running() const;
std::vector<TestTorrent> &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 &params);
};

32
toREST/tests/include/TorrentContext.hpp

@ -4,15 +4,6 @@
#include <libtorrent/magnet_uri.hpp>
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 &params) : 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 &params);
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_;
};

7
toREST/tests/stubs/ServerContext.cpp

@ -83,6 +83,13 @@ void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::
REQUIRE(response->content() == data_dump);
is_json_request(response);
};
void CommonResponse::no_content(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "204");
REQUIRE(response->message() == "No Content");
REQUIRE(response->content() == "");
};
void CommonResponse::bad_request(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "400");
REQUIRE(response->message() == "Bad Request");

24
toREST/tests/stubs/SessionContext.cpp

@ -64,6 +64,29 @@ bool TestSession::is_dht_running() const {
std::vector<TestTorrent> &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);
};

26
toREST/tests/stubs/TorrentContext.cpp

@ -1,9 +1,5 @@
#include <TorrentContext.hpp>
InfoHash TestTorrent::info_hash() {
return hash;
}
TorrentStatus TestTorrent::status(int type) {
return status_;
}
@ -11,3 +7,25 @@ TorrentStatus TestTorrent::status(int type) {
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 &params) {
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_;
}

33
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);
}
}
}
}
}
}

Loading…
Cancel
Save