Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate_torrent() #7865

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/setup_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ see LICENSE file.
#include "libtorrent/aux_/merkle.hpp"
#include "libtorrent/disk_interface.hpp" // for default_block_size
#include "libtorrent/aux_/ip_helpers.hpp"
#include "libtorrent/load_torrent.hpp"

#include "test.hpp"
#include "test_utils.hpp"
Expand All @@ -56,7 +57,7 @@ using namespace lt;
#define SEPARATOR "/"
#endif

std::shared_ptr<torrent_info> generate_torrent(bool const with_files, bool const with_hashes)
lt::add_torrent_params generate_torrent(bool const with_files, bool const with_hashes)
{
if (with_files)
{
Expand Down Expand Up @@ -106,7 +107,7 @@ std::shared_ptr<torrent_info> generate_torrent(bool const with_files, bool const
}

std::vector<char> const buf = bencode(t.generate());
return std::make_shared<torrent_info>(buf, from_span);
return load_torrent_buffer(buf);
}

namespace {
Expand Down
3 changes: 2 additions & 1 deletion test/setup_transfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ see LICENSE file.
#include "libtorrent/units.hpp"
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/fwd.hpp"
#include "libtorrent/add_torrent_params.hpp"

EXPORT std::shared_ptr<lt::torrent_info> generate_torrent(bool with_files = false, bool with_hashes = false);
EXPORT lt::add_torrent_params generate_torrent(bool with_files = false, bool with_hashes = false);

EXPORT int load_file(std::string const& filename, std::vector<char>& v
, lt::error_code& ec, int limit = 8000000);
Expand Down
21 changes: 8 additions & 13 deletions test/test_priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,18 @@ TORRENT_TEST(file_priority_multiple_calls)
settings_pack pack = settings();
lt::session ses(pack);

auto t = ::generate_torrent(true);
add_torrent_params addp = ::generate_torrent(true);

add_torrent_params addp;
addp.flags &= ~torrent_flags::paused;
addp.flags &= ~torrent_flags::auto_managed;
addp.save_path = ".";
addp.ti = t;
torrent_handle h = ses.add_torrent(addp);

for (file_index_t const i : t->files().file_range())
for (file_index_t const i : addp.ti->files().file_range())
h.file_priority(i, lt::low_priority);

std::vector<download_priority_t> const expected(
std::size_t(t->files().num_files()), lt::low_priority);
std::size_t(addp.ti->files().num_files()), lt::low_priority);
for (int i = 0; i < 10; ++i)
{
auto const p = h.get_file_priorities();
Expand Down Expand Up @@ -564,13 +562,12 @@ TORRENT_TEST(test_piece_priority_after_resume)
{
auto const new_prio = lt::low_priority;

add_torrent_params p;
auto ti = generate_torrent();
add_torrent_params p = generate_torrent();
auto ti = p.ti;
{
auto const prio = top_priority;

p.save_path = ".";
p.ti = ti;
p.file_priorities.resize(1, prio);

lt::session ses(settings());
Expand Down Expand Up @@ -619,15 +616,13 @@ lt::download_priority_t rand_prio(Engine& rng)

TORRENT_TEST(file_priority_stress_test)
{
add_torrent_params atp;
auto ti = generate_torrent();
int const num_files = ti->num_files();
add_torrent_params atp = generate_torrent();
int const num_files = atp.ti->num_files();

lt::aux::vector<lt::download_priority_t, lt::file_index_t>
local_prios(static_cast<std::size_t>(num_files), lt::default_priority);

atp.save_path = ".";
atp.ti = ti;
atp.file_priorities = local_prios;
atp.flags &= ~torrent_flags::need_save_resume;

Expand Down Expand Up @@ -688,7 +683,7 @@ TORRENT_TEST(file_priority_stress_test)
TEST_CHECK(st.need_save_resume_data & torrent_handle::if_config_changed);

auto const pp = h.get_piece_priorities();
auto const& fs = ti->files();
auto const& fs = atp.ti->files();
lt::piece_index_t i(0);

std::cout << "piece prios:\n";
Expand Down
26 changes: 13 additions & 13 deletions test/test_read_resume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TORRENT_TEST(read_resume_missing_file_format)
}

namespace {
std::shared_ptr<torrent_info> generate_torrent()
add_torrent_params generate_torrent()
{
std::vector<lt::create_file_entry> fs;
fs.emplace_back("test_resume/tmp1", 128 * 1024 * 8);
Expand All @@ -156,39 +156,39 @@ std::shared_ptr<torrent_info> generate_torrent()
t.set_hash(i, ph);
}

return load_torrent_buffer(bencode(t.generate())).ti;
return load_torrent_buffer(bencode(t.generate()));
}
} // anonymous namespace

TORRENT_TEST(read_resume_torrent)
{
std::shared_ptr<torrent_info> ti = generate_torrent();
add_torrent_params p = generate_torrent();

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

// 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(bencode(rd));
TEST_CHECK(atp.ti);

TEST_EQUAL(atp.ti->info_hashes(), ti->info_hashes());
TEST_EQUAL(atp.ti->name(), ti->name());
TEST_EQUAL(atp.ti->info_hashes(), p.ti->info_hashes());
TEST_EQUAL(atp.ti->name(), p.ti->name());
}

TORRENT_TEST(mismatching_v1_hash)
{
std::shared_ptr<torrent_info> ti = generate_torrent();
add_torrent_params p = 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());
rd["info-hash2"] = p.ti->info_hashes().v2;
rd["info"] = bdecode(p.ti->info_section());

std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), rd);
Expand All @@ -202,14 +202,14 @@ TORRENT_TEST(mismatching_v1_hash)

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

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

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