From 2ccf08e9b591eccfb8e1af75fc5985e278d34b28 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 18 Oct 2010 00:46:15 +0000 Subject: [PATCH] fixed bug in create torrent when adding sha1 file hashes to each file --- include/libtorrent/create_torrent.hpp | 7 ++++++- test/test_web_seed.cpp | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index d0720736d..575db15c2 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -281,11 +281,16 @@ namespace libtorrent if (t.should_add_file_hashes()) { int left_in_piece = t.piece_size(i); + int this_piece_size = left_in_piece; // the number of bytes from this file we just read while (left_in_piece > 0) { int to_hash_for_file = int((std::min)(size_type(left_in_piece), left_in_file)); - filehash.update(buf.bytes(), to_hash_for_file); + if (to_hash_for_file > 0) + { + int offset = this_piece_size - left_in_piece; + filehash.update(buf.bytes() + offset, to_hash_for_file); + } left_in_file -= to_hash_for_file; left_in_piece -= to_hash_for_file; if (left_in_file == 0) diff --git a/test/test_web_seed.cpp b/test/test_web_seed.cpp index e4a22f971..ab9d3f938 100644 --- a/test/test_web_seed.cpp +++ b/test/test_web_seed.cpp @@ -177,6 +177,15 @@ void save_file(char const* filename, char const* data, int size) } +sha1_hash file_hash(std::string const& name) +{ + std::vector buf; + load_file(name, buf); + if (buf.empty()) return sha1_hash(0); + hasher h(&buf[0], buf.size()); + return h.final(); +} + // test_url_seed determines whether to use url-seed or http-seed int run_suite(char const* protocol, bool test_url_seed) { @@ -186,6 +195,7 @@ int run_suite(char const* protocol, bool test_url_seed) create_directories("./tmp1_web_seed/test_torrent_dir", ec); file_storage fs; + std::srand(10); if (test_url_seed) { int file_sizes[] = @@ -193,7 +203,6 @@ int run_suite(char const* protocol, bool test_url_seed) ,1,1,1,1,1,1,13,65,34,75,2,3,4,5,23,9,43,4,43,6, 4}; char random_data[300000]; - std::srand(10); for (int i = 0; i != sizeof(file_sizes)/sizeof(file_sizes[0]); ++i) { std::generate(random_data, random_data + sizeof(random_data), &std::rand); @@ -207,7 +216,6 @@ int run_suite(char const* protocol, bool test_url_seed) else { char random_data[10000]; - std::srand(10); std::generate(random_data, random_data + sizeof(random_data), &std::rand); save_file("./tmp1_web_seed/seed", random_data, sizeof(random_data)); fs.add_file("seed", sizeof(random_data)); @@ -215,7 +223,7 @@ int run_suite(char const* protocol, bool test_url_seed) int port = start_web_server(strcmp(protocol, "https") == 0); - libtorrent::create_torrent t(fs, 16); + libtorrent::create_torrent t(fs, 16, 0, libtorrent::create_torrent::calculate_file_hashes); char tmp[512]; if (test_url_seed) { @@ -242,6 +250,17 @@ int run_suite(char const* protocol, bool test_url_seed) bencode(std::back_inserter(buf), t.generate()); boost::intrusive_ptr torrent_file(new torrent_info(&buf[0], buf.size(), ec)); + // verify that the file hashes are correct + for (int i = 0; i < torrent_file->num_files(); ++i) + { + TEST_CHECK(torrent_file->file_at(i).filehash); + sha1_hash h1 = *torrent_file->file_at(i).filehash; + sha1_hash h2 = file_hash(combine_path("./tmp1_web_seed", torrent_file->file_at(i).path)); + fprintf(stderr, "%s: %s == %s\n", torrent_file->file_at(i).path.c_str() + , to_hex(h1.to_string()).c_str(), to_hex(h2.to_string()).c_str()); + TEST_EQUAL(h1, h2); + } + for (int i = 0; i < 6; ++i) test_transfer(torrent_file, i, port, protocol, test_url_seed);