fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / test / test_pex.cpp
blobf9a7c51b1c79c0ef622e41b6f8842a80d5663ba8
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/session_settings.hpp"
35 #include "libtorrent/hasher.hpp"
36 #include "libtorrent/extensions/ut_pex.hpp"
37 #include <boost/thread.hpp>
38 #include <boost/tuple/tuple.hpp>
39 #include <boost/filesystem/operations.hpp>
41 #include "test.hpp"
42 #include "setup_transfer.hpp"
44 using boost::filesystem::remove_all;
46 void test_pex()
48 using namespace libtorrent;
50 session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48200, 49000));
51 session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49200, 50000));
52 session ses3(fingerprint("LT", 0, 1, 0, 0), std::make_pair(50200, 51000));
54 // this is to avoid everything finish from a single peer
55 // immediately. To make the swarm actually connect all
56 // three peers before finishing.
57 float rate_limit = 500000;
58 ses1.set_upload_rate_limit(int(rate_limit));
59 ses2.set_download_rate_limit(int(rate_limit));
60 ses3.set_download_rate_limit(int(rate_limit));
61 // make the peer connecting the two worthless to transfer
62 // data, to force peer 3 to connect directly to peer 1 through pex
63 ses2.set_upload_rate_limit(200);
64 ses3.set_upload_rate_limit(int(rate_limit / 2));
66 ses1.add_extension(&create_ut_pex_plugin);
67 ses2.add_extension(&create_ut_pex_plugin);
69 session_settings settings;
70 settings.allow_multiple_connections_per_ip = true;
71 settings.ignore_limits_on_local_network = false;
72 ses1.set_settings(settings);
73 ses2.set_settings(settings);
74 ses3.set_settings(settings);
76 #ifndef TORRENT_DISABLE_ENCRYPTION
77 pe_settings pes;
78 pes.out_enc_policy = pe_settings::forced;
79 pes.in_enc_policy = pe_settings::forced;
80 ses1.set_pe_settings(pes);
81 ses2.set_pe_settings(pes);
82 ses3.set_pe_settings(pes);
83 #endif
85 torrent_handle tor1;
86 torrent_handle tor2;
87 torrent_handle tor3;
89 boost::tie(tor1, tor2, tor3) = setup_transfer(&ses1, &ses2, &ses3, true, false, false, "_pex");
91 test_sleep(1000);
93 tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1"), ses1.listen_port()));
94 tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1"), ses3.listen_port()));
96 for (int i = 0; i < 40; ++i)
98 print_alerts(ses1, "ses1");
99 print_alerts(ses2, "ses2");
100 print_alerts(ses3, "ses3");
102 torrent_status st1 = tor1.status();
103 torrent_status st2 = tor2.status();
104 torrent_status st3 = tor3.status();
106 std::cerr
107 << "\033[33m" << int(st1.upload_payload_rate / 1000.f) << "kB/s "
108 << st1.num_peers << ": "
109 << "\033[32m" << int(st2.download_payload_rate / 1000.f) << "kB/s "
110 << "\033[31m" << int(st2.upload_payload_rate / 1000.f) << "kB/s "
111 << "\033[0m" << int(st2.progress * 100) << "% "
112 << st2.num_peers << " - "
113 << "\033[32m" << int(st3.download_payload_rate / 1000.f) << "kB/s "
114 << "\033[31m" << int(st3.upload_payload_rate / 1000.f) << "kB/s "
115 << "\033[0m" << int(st3.progress * 100) << "% "
116 << st3.num_peers
117 << std::endl;
119 if (st3.state == torrent_status::seeding) break;
120 test_sleep(1000);
123 TEST_CHECK(tor3.is_seed());
125 if (!tor2.is_seed() && tor3.is_seed()) std::cerr << "done\n";
128 int test_main()
130 using namespace libtorrent;
131 using namespace boost::filesystem;
133 // in case the previous run was terminated
134 try { remove_all("./tmp1_pex"); } catch (std::exception&) {}
135 try { remove_all("./tmp2_pex"); } catch (std::exception&) {}
136 try { remove_all("./tmp3_pex"); } catch (std::exception&) {}
138 test_pex();
140 remove_all("./tmp1_pex");
141 remove_all("./tmp2_pex");
142 remove_all("./tmp3_pex");
144 return 0;