Skip to content

Commit

Permalink
add test coverage for read_resume_data() when v1 or v2 hashes do not …
Browse files Browse the repository at this point in the history
…match the torrent metadata
  • Loading branch information
arvidn committed Feb 17, 2024
1 parent 4128849 commit fb226a4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/read_resume_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ namespace {
{
ec = err;
}
else if (((ret.info_hashes.has_v1() && ret.info_hashes.v1 != ret.ti->info_hashes().v1)
|| (ret.info_hashes.has_v2() && ret.info_hashes.v2 != ret.ti->info_hashes().v2)))
{
ec = errors::mismatching_info_hash;
}
else
{
// time_t might be 32 bit if we're unlucky, but there isn't
Expand All @@ -150,6 +145,10 @@ namespace {
ret.ti->internal_set_comment(rd.dict_find_string_value("comment", ""));
}
}
else
{
ec = errors::mismatching_info_hash;
}
}

#if TORRENT_ABI_VERSION < 3
Expand Down
63 changes: 42 additions & 21 deletions test/test_read_resume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,6 @@ TORRENT_TEST(read_resume_missing_file_format)
TEST_EQUAL(ec, error_code(errors::invalid_file_tag));
}

TORRENT_TEST(read_resume_mismatching_torrent)
{
entry rd;

rd["file-format"] = "libtorrent resume file";
rd["file-version"] = 1;
rd["info-hash"] = "abcdefghijklmnopqrst";
entry& info = rd["info"];
info["piece length"] = 16384 * 16;
info["name"] = "test";


std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), rd);

// the info-hash field does not match the torrent in the "info" field, so it
// will be ignored
add_torrent_params atp = read_resume_data(resume_data);
TEST_CHECK(!atp.ti);
}

namespace {
std::shared_ptr<torrent_info> generate_torrent()
{
Expand Down Expand Up @@ -238,6 +217,48 @@ TORRENT_TEST(read_resume_torrent)
TEST_EQUAL(atp.ti->name(), ti->name());
}

TORRENT_TEST(mismatching_v1_hash)
{
std::shared_ptr<torrent_info> ti = generate_torrent();

entry rd;
rd["file-format"] = "libtorrent resume file";
rd["file-version"] = 1;
rd["info-hash"] = "abababababababababab";
rd["info-hash2"] = ti->info_hashes().v2;
rd["info"] = bdecode(ti->info_section());

std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), rd);

// the info-hash field does not match the torrent in the "info" field, so it
// will be ignored
error_code ec;
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(ec == errors::mismatching_info_hash);
}

TORRENT_TEST(mismatching_v2_hash)
{
std::shared_ptr<torrent_info> ti = generate_torrent();

entry rd;
rd["file-format"] = "libtorrent resume file";
rd["file-version"] = 1;
rd["info-hash"] = ti->info_hashes().v1;
rd["info-hash2"] = "abababababababababababababababab";
rd["info"] = bdecode(ti->info_section());

std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), rd);

// the info-hash field does not match the torrent in the "info" field, so it
// will be ignored
error_code ec;
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(ec == errors::mismatching_info_hash);
}

namespace {

void test_roundtrip(add_torrent_params input)
Expand Down

0 comments on commit fb226a4

Please sign in to comment.