fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / test / test_metadata_extension.cpp
blob1c42ead7444f29ab3969e47a949b4e28023d47b5
1 /*
3 Copyright (c) 2008, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/session.hpp"
34 #include "libtorrent/hasher.hpp"
35 #include <boost/thread.hpp>
36 #include <boost/tuple/tuple.hpp>
37 #include <boost/filesystem/operations.hpp>
38 #include <boost/filesystem/convenience.hpp>
40 #include "test.hpp"
41 #include "setup_transfer.hpp"
42 #include "libtorrent/extensions/metadata_transfer.hpp"
43 #include "libtorrent/extensions/ut_metadata.hpp"
45 using boost::filesystem::remove_all;
46 using boost::tuples::ignore;
48 void test_transfer(bool clear_files, bool disconnect
49 , boost::shared_ptr<libtorrent::torrent_plugin> (*constructor)(libtorrent::torrent*, void*))
51 using namespace libtorrent;
53 session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48100, 49000));
54 session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49100, 50000));
55 ses1.add_extension(constructor);
56 ses2.add_extension(constructor);
57 torrent_handle tor1;
58 torrent_handle tor2;
59 #ifndef TORRENT_DISABLE_ENCRYPTION
60 pe_settings pes;
61 pes.out_enc_policy = pe_settings::forced;
62 pes.in_enc_policy = pe_settings::forced;
63 ses1.set_pe_settings(pes);
64 ses2.set_pe_settings(pes);
65 #endif
67 boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, clear_files, true, true, "_meta");
69 for (int i = 0; i < 50; ++i)
71 // make sure this function can be called on
72 // torrents without metadata
73 if (!disconnect) tor2.status();
74 print_alerts(ses1, "ses1", false, true);
75 print_alerts(ses2, "ses2", false, true);
77 if (disconnect && tor2.is_valid()) ses2.remove_torrent(tor2);
78 if (!disconnect && tor2.has_metadata()) break;
79 test_sleep(100);
82 if (disconnect) return;
84 TEST_CHECK(tor2.has_metadata());
85 std::cerr << "waiting for transfer to complete\n";
87 for (int i = 0; i < 30; ++i)
89 torrent_status st1 = tor1.status();
90 torrent_status st2 = tor2.status();
92 std::cerr
93 << "\033[33m" << int(st1.upload_payload_rate / 1000.f) << "kB/s "
94 << st1.num_peers << ": "
95 << "\033[32m" << int(st2.download_payload_rate / 1000.f) << "kB/s "
96 << "\033[31m" << int(st2.upload_payload_rate / 1000.f) << "kB/s "
97 << "\033[0m" << int(st2.progress * 100) << "% "
98 << st2.num_peers
99 << std::endl;
100 if (tor2.is_seed()) break;
101 test_sleep(1000);
104 TEST_CHECK(tor2.is_seed());
105 if (tor2.is_seed()) std::cerr << "done\n";
107 using boost::filesystem::remove_all;
108 remove_all("./tmp1_meta");
109 remove_all("./tmp2_meta");
110 remove_all("./tmp3_meta");
113 int test_main()
115 using namespace libtorrent;
116 using namespace boost::filesystem;
118 // test to disconnect one client prematurely
119 test_transfer(true, true, &create_metadata_plugin);
120 // test where one has data and one doesn't
121 test_transfer(true, false, &create_metadata_plugin);
122 // test where both have data (to trigger the file check)
123 test_transfer(false, false, &create_metadata_plugin);
125 // test to disconnect one client prematurely
126 test_transfer(true, true, &create_ut_metadata_plugin);
127 // test where one has data and one doesn't
128 test_transfer(true, false, &create_ut_metadata_plugin);
129 // test where both have data (to trigger the file check)
130 test_transfer(false, false, &create_ut_metadata_plugin);
132 remove_all("./tmp1");
133 remove_all("./tmp2");
135 return 0;