1 /*
2
3 Copyright (c) 2014, Arvid Norberg
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
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.
18
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.
30
31 */
32
33 #include "libtorrent/session.hpp"
34 #include "libtorrent/alert_types.hpp"
35 #include "libtorrent/create_torrent.hpp"
36 #include "libtorrent/torrent_info.hpp"
37 #include "libtorrent/aux_/path.hpp"
38 #include "setup_transfer.hpp"
39 #include "settings.hpp"
40 #include "test.hpp"
41 #include "settings.hpp"
42
43 #include <iostream>
44 #include <fstream>
45 #include <iostream>
46 #include <tuple>
47
48 using namespace lt;
49 using std::ignore;
50
51 namespace {
52
all_of(std::vector<bool> const & v)53 bool all_of(std::vector<bool> const& v)
54 {
55 return std::all_of(v.begin(), v.end(), [](bool val){ return val; });
56 }
57
test_remap_files(storage_mode_t storage_mode=storage_mode_sparse)58 void test_remap_files(storage_mode_t storage_mode = storage_mode_sparse)
59 {
60 using namespace lt;
61
62 // create a torrent with 2 files, remap them into 3 files and make sure
63 // the file priorities don't break things
64 static std::array<const int, 2> const file_sizes{{100000, 100000}};
65 int const piece_size = 0x8000;
66 auto t = make_torrent(file_sizes, piece_size);
67
68 static std::array<const int, 3> const remap_file_sizes
69 {{10000, 10000, int(t->total_size() - 20000)}};
70
71 file_storage fs = make_file_storage(remap_file_sizes, piece_size, "multifile-");
72
73 t->remap_files(fs);
74
75 auto const alert_mask = alert_category::all
76 & ~alert_category::stats;
77
78 session_proxy p1;
79
80 settings_pack sett = settings();
81 sett.set_int(settings_pack::alert_mask, alert_mask);
82 lt::session ses(sett);
83
84 add_torrent_params params;
85 params.save_path = ".";
86 params.storage_mode = storage_mode;
87 params.flags &= ~torrent_flags::paused;
88 params.flags &= ~torrent_flags::auto_managed;
89 params.ti = t;
90
91 torrent_handle tor1 = ses.add_torrent(params);
92
93 // prevent race conditions of adding pieces while checking
94 lt::torrent_status st = tor1.status();
95 for (int i = 0; i < 40; ++i)
96 {
97 st = tor1.status();
98 if (st.state != torrent_status::checking_files
99 && st.state != torrent_status::checking_resume_data)
100 break;
101 std::this_thread::sleep_for(lt::milliseconds(100));
102 }
103 TEST_CHECK(st.state != torrent_status::checking_files
104 && st.state != torrent_status::checking_files);
105 TEST_CHECK(st.num_pieces == 0);
106
107 // write pieces
108 for (auto const i : fs.piece_range())
109 {
110 std::vector<char> const piece = generate_piece(i, fs.piece_size(i));
111 tor1.add_piece(i, piece.data());
112 }
113
114 // read pieces
115 for (auto const i : fs.piece_range())
116 {
117 tor1.read_piece(i);
118 }
119
120 // wait for all alerts to come back and verify the data against the expected
121 // piece data
122 aux::vector<bool, piece_index_t> pieces(std::size_t(fs.num_pieces()), false);
123 aux::vector<bool, piece_index_t> passed(std::size_t(fs.num_pieces()), false);
124 aux::vector<bool, file_index_t> files(std::size_t(fs.num_files()), false);
125
126 while (!all_of(pieces) || !all_of(passed) || !all_of(files))
127 {
128 alert* a = ses.wait_for_alert(lt::seconds(5));
129 if (a == nullptr) break;
130
131 std::vector<alert*> alerts;
132 ses.pop_alerts(&alerts);
133
134 for (alert* i : alerts)
135 {
136 printf("%s\n", i->message().c_str());
137
138 read_piece_alert* rp = alert_cast<read_piece_alert>(i);
139 if (rp)
140 {
141 auto const idx = rp->piece;
142 TEST_EQUAL(t->piece_size(idx), rp->size);
143
144 std::vector<char> const piece = generate_piece(idx, t->piece_size(idx));
145 TEST_CHECK(std::memcmp(rp->buffer.get(), piece.data(), std::size_t(rp->size)) == 0);
146 TEST_CHECK(pieces[idx] == false);
147 pieces[idx] = true;
148 }
149
150 file_completed_alert* fc = alert_cast<file_completed_alert>(i);
151 if (fc)
152 {
153 auto const idx = fc->index;
154 TEST_CHECK(files[idx] == false);
155 files[idx] = true;
156 }
157
158 piece_finished_alert* pf = alert_cast<piece_finished_alert>(i);
159 if (pf)
160 {
161 auto const idx = pf->piece_index;
162 TEST_CHECK(passed[idx] == false);
163 passed[idx] = true;
164 }
165 }
166 }
167
168 TEST_CHECK(all_of(pieces));
169 TEST_CHECK(all_of(files));
170 TEST_CHECK(all_of(passed));
171
172 // just because we can read them back throught libtorrent, doesn't mean the
173 // files have hit disk yet (because of the cache). Retry a few times to try
174 // to pick up the files
175 for (file_index_t i(0); i < file_index_t(int(remap_file_sizes.size())); ++i)
176 {
177 std::string name = fs.file_path(i);
178 for (int j = 0; j < 10 && !exists(name); ++j)
179 {
180 std::this_thread::sleep_for(lt::milliseconds(500));
181 print_alerts(ses, "ses");
182 }
183
184 std::printf("%s\n", name.c_str());
185 TEST_CHECK(exists(name));
186 }
187
188 print_alerts(ses, "ses");
189
190 st = tor1.status();
191 TEST_EQUAL(st.is_seeding, true);
192
193 std::printf("\ntesting force recheck\n\n");
194
195 // test force rechecking a seeding torrent with remapped files
196 tor1.force_recheck();
197
198 for (int i = 0; i < 50; ++i)
199 {
200 torrent_status st1 = tor1.status();
201 if (st1.is_seeding) break;
202 std::this_thread::sleep_for(lt::milliseconds(100));
203 print_alerts(ses, "ses");
204 }
205
206 print_alerts(ses, "ses");
207 st = tor1.status();
208 TEST_CHECK(st.is_seeding);
209 }
210
211 } // anonymous namespace
212
TORRENT_TEST(remap_files)213 TORRENT_TEST(remap_files)
214 {
215 test_remap_files();
216 }
217