fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / test / test_web_seed.cpp
blob243a73cd5f990311ba59d9386fe1d3968b82832a
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 "libtorrent/file_pool.hpp"
36 #include "libtorrent/storage.hpp"
37 #include "libtorrent/bencode.hpp"
38 #include "libtorrent/create_torrent.hpp"
39 #include <boost/thread.hpp>
40 #include <boost/tuple/tuple.hpp>
41 #include <boost/filesystem/operations.hpp>
42 #include <fstream>
44 #include "test.hpp"
45 #include "setup_transfer.hpp"
47 using namespace boost::filesystem;
48 using namespace libtorrent;
50 // proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
51 void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
53 using namespace libtorrent;
55 session ses;
56 session_settings settings;
57 settings.ignore_limits_on_local_network = false;
58 ses.set_settings(settings);
59 ses.set_severity_level(alert::debug);
60 ses.listen_on(std::make_pair(51000, 52000));
61 ses.set_download_rate_limit(torrent_file->total_size() / 10);
62 remove_all("./tmp1");
64 char const* test_name[] = {"no", "SOCKS4", "SOCKS5", "SOCKS5 password", "HTTP", "HTTP password"};
66 std::cerr << " ==== TESTING " << test_name[proxy] << " proxy ====" << std::endl;
68 if (proxy)
70 start_proxy(8002, proxy);
71 proxy_settings ps;
72 ps.hostname = "127.0.0.1";
73 ps.port = 8002;
74 ps.username = "testuser";
75 ps.password = "testpass";
76 ps.type = (proxy_settings::proxy_type)proxy;
77 ses.set_web_seed_proxy(ps);
80 torrent_handle th = ses.add_torrent(*torrent_file, "./tmp1");
82 std::vector<announce_entry> empty;
83 th.replace_trackers(empty);
85 const size_type total_size = torrent_file->total_size();
87 float rate_sum = 0.f;
88 float ses_rate_sum = 0.f;
90 for (int i = 0; i < 30; ++i)
92 torrent_status s = th.status();
93 session_status ss = ses.status();
94 std::cerr << (s.progress * 100.f) << " %"
95 << " torrent rate: " << (s.download_rate / 1000.f) << " kB/s"
96 << " session rate: " << (ss.download_rate / 1000.f) << " kB/s"
97 << " session total: " << ss.total_payload_download
98 << " torrent total: " << s.total_payload_download
99 << std::endl;
100 rate_sum += s.download_payload_rate;
101 ses_rate_sum += ss.payload_download_rate;
103 print_alerts(ses, "ses");
105 if (th.is_seed() && ss.download_rate == 0.f)
107 TEST_CHECK(ses.status().total_payload_download == total_size);
108 TEST_CHECK(th.status().total_payload_download == total_size);
109 break;
111 test_sleep(1000);
114 std::cerr << "total_size: " << total_size
115 << " rate_sum: " << rate_sum
116 << " session_rate_sum: " << ses_rate_sum
117 << std::endl;
119 // the rates for each second should sum up to the total, with a 10% error margin
120 TEST_CHECK(fabs(rate_sum - total_size) < total_size * .1f);
121 TEST_CHECK(fabs(ses_rate_sum - total_size) < total_size * .1f);
123 TEST_CHECK(th.is_seed());
125 if (proxy) stop_proxy(8002);
127 remove_all("./tmp1");
130 int test_main()
132 using namespace libtorrent;
133 using namespace boost::filesystem;
135 create_directory("test_torrent");
136 char random_data[300000];
137 std::srand(std::time(0));
138 std::generate(random_data, random_data + sizeof(random_data), &std::rand);
139 std::ofstream("./test_torrent/test1").write(random_data, 35);
140 std::ofstream("./test_torrent/test2").write(random_data, 16536 - 35);
141 std::ofstream("./test_torrent/test3").write(random_data, 16536);
142 std::ofstream("./test_torrent/test4").write(random_data, 17);
143 std::ofstream("./test_torrent/test5").write(random_data, 16536);
144 std::ofstream("./test_torrent/test6").write(random_data, 300000);
145 std::ofstream("./test_torrent/test7").write(random_data, 300000);
147 file_storage fs;
148 add_files(fs, path("test_torrent"));
150 libtorrent::create_torrent t(fs, 16 * 1024);
151 t.add_url_seed("http://127.0.0.1:8000/");
153 start_web_server(8000);
155 // calculate the hash for all pieces
156 int num = t.num_pieces();
157 std::vector<char> buf(t.piece_length());
159 file_pool fp;
160 boost::scoped_ptr<storage_interface> s(default_storage_constructor(
161 fs, ".", fp));
163 for (int i = 0; i < num; ++i)
165 s->read(&buf[0], i, 0, fs.piece_size(i));
166 hasher h(&buf[0], fs.piece_size(i));
167 t.set_hash(i, h.final());
170 boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
172 for (int i = 0; i < 6; ++i)
173 test_transfer(torrent_file, i);
175 stop_web_server(8000);
176 remove_all("./test_torrent");
177 return 0;