From 31ecd1b91452ea51b319d32c357bd3c58ccf471a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 18 Nov 2013 00:56:02 +0000 Subject: [PATCH] copy test_torrent_info unit test from libtorrent_aio --- test/Jamfile | 1 + test/test_torrent_info.cpp | 96 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 test/test_torrent_info.cpp diff --git a/test/Jamfile b/test/Jamfile index 8a63ed687..a9ff18bf8 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -99,6 +99,7 @@ feature launcher : none valgrind : composite ; feature.compose valgrind : "valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222" on ; test-suite libtorrent : + [ run test_torrent_info.cpp ] [ run test_time.cpp ] [ run test_file_storage.cpp ] [ run test_priority.cpp ] diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp new file mode 100644 index 000000000..2c819572a --- /dev/null +++ b/test/test_torrent_info.cpp @@ -0,0 +1,96 @@ +/* + +Copyright (c) 2012, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "test.hpp" +#include "libtorrent/file_storage.hpp" +#include "libtorrent/torrent_info.hpp" +#include "libtorrent/create_torrent.hpp" + +using namespace libtorrent; + +int test_main() +{ + + file_storage fs; + int total_size = 100 * 0x4000; + + fs.add_file("test/temporary.txt", 0x4000); + fs.add_file("test/A/tmp", 0x4000); + fs.add_file("test/Temporary.txt", 0x4000); + fs.add_file("test/TeMPorArY.txT", 0x4000); + fs.add_file("test/a", 0x4000); + fs.add_file("test/b.exe", 0x4000); + fs.add_file("test/B.ExE", 0x4000); + fs.add_file("test/B.exe", 0x4000); + fs.add_file("test/test/TEMPORARY.TXT", 0x4000); + fs.add_file("test/A", 0x4000); + + libtorrent::create_torrent t(fs, 0x4000); + + // calculate the hash for all pieces + int num = t.num_pieces(); + sha1_hash ph; + for (int i = 0; i < num; ++i) + t.set_hash(i, ph); + + std::vector tmp; + std::back_insert_iterator > out(tmp); + + entry tor = t.generate(); + bencode(out, tor); + + torrent_info ti(&tmp[0], tmp.size()); + + char const* filenames[10] = + { + "test/temporary.txt", + "test/A/tmp", + "test/Temporary.1.txt", // duplicate of temporary.txt + "test/TeMPorArY.2.txT", // duplicate of temporary.txt + "test/a.1", // a file may not have the same name as a directory + "test/b.exe", + "test/B.1.ExE", // duplicate of b.exe + "test/B.2.exe", // duplicate of b.exe + "test/test/TEMPORARY.TXT", // a file with the same name in a seprate directory is fine + "test/A.2", // duplicate of directory a + }; + + for (int i = 0; i < ti.num_files(); ++i) + { + std::string p = ti.file_at(i).path; + convert_path_to_posix(p); + fprintf(stderr, "%s == %s\n", p.c_str(), filenames[i]); + TEST_CHECK(p == filenames[i]); + } + + return 0; +}