File: test_torrent_list.cpp

package info (click to toggle)
libtorrent-rasterbar 2.0.11-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (249 lines) | stat: -rw-r--r-- 7,230 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*

Copyright (c) 2018, Steven Siloti
Copyright (c) 2019, 2021, 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/aux_/torrent_list.hpp"
#include "libtorrent/sha1_hash.hpp"

using namespace lt;

namespace {

sha1_hash const sha1_1("abababababababababab");
sha1_hash const sha1_2("cbcbcbcbcbcbcbcbcbcb");
sha1_hash const sha1_3("cdcdcdcdcdcdcdcdcdcd");
sha1_hash const sha1_4("edededededededededed");
sha256_hash const sha2_1("xbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxb");
sha1_hash const sha2_1_truncated("xbxbxbxbxbxbxbxbxbxb");

info_hash_t const v1(sha1_1);
info_hash_t const v2(sha2_1);
info_hash_t const hybrid(sha1_1, sha2_1);

using ih = lt::info_hash_t;
}

TORRENT_TEST(torrent_list_empty)
{
	aux::torrent_list<int> l;
	TEST_CHECK(l.empty());
	TEST_CHECK(l.begin() == l.end());
	l.insert(v1, std::make_shared<int>(1337));
	TEST_CHECK(!l.empty());
	TEST_CHECK(l.begin() != l.end());
}

TORRENT_TEST(torrent_list_size)
{
	aux::torrent_list<int> l;
	TEST_EQUAL(l.size(), 0);
	l.insert(ih(sha1_1), std::make_shared<int>(1337));
	TEST_EQUAL(l.size(), 1);
	l.insert(ih(sha1_2), std::make_shared<int>(1338));
	TEST_EQUAL(l.size(), 2);
	l.insert(ih(sha1_3), std::make_shared<int>(1339));
	TEST_EQUAL(l.size(), 3);

	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_EQUAL(*l.find(sha1_2), 1338);
	TEST_EQUAL(*l.find(sha1_3), 1339);
}

TORRENT_TEST(torrent_list_duplicates)
{
	aux::torrent_list<int> l;
	TEST_EQUAL(l.size(), 0);
	TEST_CHECK(l.insert(v1, std::make_shared<int>(1337)));
	TEST_EQUAL(l.size(), 1);
	TEST_CHECK(!l.insert(v1, std::make_shared<int>(1338)));
	TEST_EQUAL(l.size(), 1);
	TEST_EQUAL(*l.find(sha1_1), 1337);
}

TORRENT_TEST(torrent_list_duplicates_v1)
{
	aux::torrent_list<int> l;
	TEST_EQUAL(l.size(), 0);
	TEST_CHECK(l.insert(hybrid, std::make_shared<int>(1337)));
	TEST_EQUAL(l.size(), 1);
	TEST_CHECK(!l.insert(v1, std::make_shared<int>(1338)));
	TEST_EQUAL(l.size(), 1);
	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_EQUAL(*l.find(sha2_1_truncated), 1337);
}

TORRENT_TEST(torrent_list_duplicates_v2)
{
	aux::torrent_list<int> l;
	TEST_EQUAL(l.size(), 0);
	TEST_CHECK(l.insert(hybrid, std::make_shared<int>(1337)));
	TEST_EQUAL(l.size(), 1);
	TEST_CHECK(!l.insert(v2, std::make_shared<int>(1338)));
	TEST_EQUAL(l.size(), 1);
	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_EQUAL(*l.find(sha2_1_truncated), 1337);
}

TORRENT_TEST(torrent_list_duplicates_self)
{
	aux::torrent_list<int> l;
	TEST_EQUAL(l.size(), 0);
	TEST_CHECK(l.insert(ih(sha2_1_truncated, sha2_1), std::make_shared<int>(1337)));
	TEST_EQUAL(l.size(), 1);
	TEST_CHECK(*l.find(sha2_1_truncated) == 1337);

	TEST_CHECK(l.erase(ih(sha2_1_truncated, sha2_1)));
	TEST_EQUAL(l.size(), 0);
	TEST_CHECK(l.find(sha2_1_truncated) == nullptr);
}

TORRENT_TEST(torrent_truncated_list_lookup)
{
	aux::torrent_list<int> l;
	l.insert(v2, std::make_shared<int>(1337));
	l.insert(v1, std::make_shared<int>(1338));

	TEST_EQUAL(*l.find(sha2_1_truncated), 1337);
	TEST_EQUAL(*l.find(sha1_1), 1338);
	TEST_CHECK(l.find(sha1_3) == nullptr);
}

TORRENT_TEST(torrent_list_lookup)
{
	aux::torrent_list<int> l;
	l.insert(ih(sha1_1), std::make_shared<int>(1337));
	l.insert(ih(sha1_2), std::make_shared<int>(1338));

	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_EQUAL(*l.find(sha1_2), 1338);
	TEST_CHECK(l.find(sha1_3) == nullptr);
}

TORRENT_TEST(torrent_list_order)
{
	aux::torrent_list<int> l;
	l.insert(ih(sha1_1), std::make_shared<int>(1));
	l.insert(ih(sha1_2), std::make_shared<int>(2));
	l.insert(ih(sha1_3), std::make_shared<int>(3));
	l.insert(ih(sha1_4), std::make_shared<int>(0));

	// iteration order is the same as insertion order, not sort order of
	// info-hashes
	std::vector<int> order;
	for (auto i : l)
	{
		order.push_back(*i);
	}

	TEST_CHECK((order == std::vector<int>{1, 2, 3, 0}));

	TEST_EQUAL(*l[0], 1);
	TEST_EQUAL(*l[1], 2);
	TEST_EQUAL(*l[2], 3);
	TEST_EQUAL(*l[3], 0);
}

TORRENT_TEST(torrent_list_erase)
{
	aux::torrent_list<int> l;
	l.insert(v1, std::make_shared<int>(1337));
	TEST_CHECK(!l.empty());

	// this doesn't exist, returns false
	TEST_CHECK(!l.erase(ih(sha1_2)));
	TEST_CHECK(!l.empty());

	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_CHECK(l.erase(ih(sha1_1)));
	TEST_CHECK(l.find(sha1_1) == nullptr);
	TEST_CHECK(l.empty());
}

TORRENT_TEST(torrent_list_erase2)
{
	aux::torrent_list<int> l;
	l.insert(ih(sha1_1), std::make_shared<int>(1337));
	l.insert(ih(sha1_2), std::make_shared<int>(1338));

	TEST_EQUAL(*l.find(sha1_1), 1337);
	TEST_EQUAL(l.size(), 2);
	TEST_CHECK(!l.empty());

	// delete an entry that isn't the last one
	TEST_CHECK(l.erase(ih(sha1_1)));
	TEST_CHECK(l.find(sha1_1) == nullptr);
	TEST_EQUAL(l.size(), 1);
	TEST_CHECK(!l.empty());
	TEST_EQUAL(*l.find(sha1_2), 1338);
}

TORRENT_TEST(torrent_list_clear)
{
	aux::torrent_list<int> l;
	l.insert(ih(sha1_1), std::make_shared<int>(1));
	l.insert(ih(sha1_2), std::make_shared<int>(2));
	l.insert(ih(sha1_3), std::make_shared<int>(3));
	l.insert(ih(sha1_4), std::make_shared<int>(0));

	TEST_CHECK(!l.empty());

	TEST_CHECK(*l.find(sha1_1) == 1);
	TEST_CHECK(*l.find(sha1_2) == 2);
	TEST_CHECK(*l.find(sha1_3) == 3);
	TEST_CHECK(*l.find(sha1_4) == 0);

	l.clear();
	TEST_CHECK(l.empty());

	TEST_CHECK(l.find(sha1_1) == nullptr);
	TEST_CHECK(l.find(sha1_2) == nullptr);
	TEST_CHECK(l.find(sha1_3) == nullptr);
	TEST_CHECK(l.find(sha1_4) == nullptr);
}

#if !defined TORRENT_DISABLE_ENCRYPTION
TORRENT_TEST(torrent_list_obfuscated_lookup)
{
	aux::torrent_list<int> l;
	l.insert(ih(sha1_1), std::make_shared<int>(1337));

	TEST_EQUAL(*l.find(sha1_1), 1337);
	static char const req2[4] = {'r', 'e', 'q', '2'};
	hasher h(req2);
	h.update(sha1_1);
	TEST_EQUAL(*l.find_obfuscated(h.final()), 1337);
	// this should not exist as an obfuscated hash
	TEST_CHECK(l.find_obfuscated(sha1_1) == nullptr);
}
#endif