blob: abd224af66f435017835c2711f22fb39412d1bf0 [file] [log] [blame]
spirosmastorakisa6057f52016-01-28 13:34:41 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent is free software: you can redistribute it and/or modify it under the
8* terms of the GNU Lesser General Public License as published by the Free Software
9* Foundation, either version 3 of the License, or (at your option) any later version.
10*
11* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
12* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14*
15* You should have received copies of the GNU General Public License and GNU Lesser
16* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#include "boost-test.hpp"
23
24#include "torrent-file.hpp"
25
26#include <ndn-cxx/data.hpp>
27#include <ndn-cxx/signature.hpp>
28#include <ndn-cxx/security/key-chain.hpp>
29
30namespace ndn {
31
32namespace ntorrent {
33
34namespace tests {
35
36
37const static uint8_t TorrentFileTest[] = {
38 0x6, 0xfd,
39 0x1, 0xe4,
40 0x7, 0x25,
41 0x8, 0x8,
42 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
43 0x8, 0xa,
44 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
45 0x8, 0xd,
46 0x2e, 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65,
47 0x14, 0x0,
48 0x15, 0x77,
49 0x7, 0x37,
50 0x8, 0x8,
51 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
52 0x8, 0xa,
53 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
54 0x8, 0xd,
55 0x2e, 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65,
56 0x8, 0x8,
57 0x73, 0x65, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x32,
58 0x8, 0x6,
59 0x41, 0x45, 0x33, 0x32, 0x31, 0x43,
60 0x7, 0x16,
61 0x8, 0x8,
62 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
63 0x8, 0xa,
64 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
65 0x7, 0x11,
66 0x8, 0x5,
67 0x66, 0x69, 0x6c, 0x65, 0x30,
68 0x8, 0x8,
69 0x31, 0x41, 0x32, 0x42, 0x33, 0x43, 0x34, 0x44,
70 0x7, 0x11,
71 0x8, 0x5,
72 0x66, 0x69, 0x6c, 0x65, 0x31,
73 0x8, 0x8,
74 0x32, 0x41, 0x33, 0x42, 0x34, 0x43, 0x35, 0x45,
75 0x16, 0x3c,
76 0x1b, 0x1,
77 0x1,
78 0x1c, 0x37,
79 0x7, 0x35,
80 0x8, 0xc,
81 0x74, 0x6d, 0x70, 0x2d, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79,
82 0x8, 0x4,
83 0x3d, 0xa8, 0x2c, 0x19,
84 0x8, 0x3,
85 0x4b, 0x45, 0x59,
86 0x8, 0x11,
87 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x35, 0x34, 0x39, 0x37, 0x33, 0x38, 0x37, 0x36, 0x31, 0x37, 0x31,
88 0x8, 0x7,
89 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52, 0x54,
90 0x17, 0xfd,
91 0x1, 0x0, 0x37, 0x61, 0xc9, 0x3b, 0x43, 0xe8, 0xa3, 0xcd, 0xf0, 0xb1,
92 0x14, 0x78, 0x50, 0x6, 0x3c, 0x8a, 0x7, 0x4d, 0xe4, 0xa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0x2c, 0xf0,
108 0x2f, 0xce, 0x7d, 0x1d, 0xbf, 0x16, 0xc, 0x74, 0x35, 0x83, 0x8a, 0x22,
109 0xc4, 0x2, 0xeb, 0x42, 0xd3, 0x76, 0x36, 0x8a, 0x71, 0x7b, 0x6b, 0xb8,
110 0x3f, 0x7a, 0xb8, 0xc6, 0xf6, 0xca, 0xed, 0x74, 0xdb, 0xa4, 0x42, 0x65,
111 0xa5, 0x96, 0xde, 0xd9, 0xfa, 0x45, 0x25, 0x9a, 0xc1, 0xde, 0xd1, 0xf6,
112 0x7a, 0xaf, 0xa3, 0xfd, 0x4e, 0xa9
113};
114
115BOOST_AUTO_TEST_SUITE(TestTorrentFile)
116
117BOOST_AUTO_TEST_CASE(CheckGettersSetters)
118{
119
120 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
121 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
122 "/NTORRENT/linux15.01",
123 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
124 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
125
126 BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/.torrent-file");
127 BOOST_CHECK_EQUAL(file.getCatalog().size(), 2);
128 BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
129 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
130 BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
131
132 TorrentFile file2("/NTORRENT/linux15.01/.torrent-file",
133 "/NTORRENT/linux15.01",
134 {"/NTORRENT/linux15.01/file0/1A2B3C4D"});
135
136 BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
137 BOOST_CHECK(!file2.getTorrentFilePtr());
138 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 1);
139}
140
141BOOST_AUTO_TEST_CASE(CheckEncodeDecode)
142{
143 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
144 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
145 "/NTORRENT/linux15.01",
146 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
147 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
148
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800149 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800150 KeyChain keyChain;
151 keyChain.sign(file);
152
153 Block wire = file.wireEncode();
154
155 TorrentFile file2;
156 file2.wireDecode(wire);
157
158 BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
159 BOOST_CHECK_EQUAL(*(file2.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
160 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
161 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
162 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
163 BOOST_CHECK_EQUAL(file2.getCommonPrefix(), "/NTORRENT/linux15.01");
164}
165
166BOOST_AUTO_TEST_CASE(CheckEncodeDecodeNoTorrentFilePtr)
167{
168 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
169 "/NTORRENT/linux15.01",
170 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
171 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
172
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800173 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800174 KeyChain keyChain;
175 keyChain.sign(file);
176
177 Block wire = file.wireEncode();
178
179 TorrentFile file2;
180 file2.wireDecode(wire);
181
182 BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
183 BOOST_CHECK(!file2.getTorrentFilePtr());
184 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
185 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
186 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
187}
188
189BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyTorrentFile)
190{
191 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
192 "",
193 {});
194
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800195 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800196 KeyChain keyChain;
197 keyChain.sign(file);
198
199 Block wire = file.wireEncode();
200
201 TorrentFile file2;
202
203 BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
204}
205
206
207BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyCatalog)
208{
209
210 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
211 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
212 "/NTORRENT/linux15.01",
213 {});
214
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800215 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800216 KeyChain keyChain;
217 keyChain.sign(file);
218
219 Block wire = file.wireEncode();
220
221 TorrentFile file2;
222
223 BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
224}
225
226BOOST_AUTO_TEST_CASE(DecodeFromWire)
227{
228
229 Block TorrentFileBlock(TorrentFileTest, sizeof(TorrentFileTest));
230 TorrentFile file(TorrentFileBlock);
231
232 BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/.torrent-file");
233 BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
234 BOOST_CHECK_EQUAL(file.getCatalog().size(), 2);
235 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
236 BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
237 BOOST_CHECK_EQUAL(file.getCommonPrefix(), "/NTORRENT/linux15.01");
238}
239
240BOOST_AUTO_TEST_CASE(TestInsertErase)
241{
242 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
243 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
244 "/NTORRENT/linux15.01",
245 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
246 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
247
248 file.erase("/NTORRENT/linux15.01/file0/1A2B3C4D");
249 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file1/2A3B4C5E");
250
251 file.erase("/NTORRENT/linux15.01/file1/2A3B4C5E");
252 BOOST_CHECK_EQUAL(file.getCatalog().size(), 0);
253
254 file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
255 BOOST_CHECK_EQUAL(file.getCatalog().size(), 1);
256 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file3/AB34C5KA");
257}
258
259BOOST_AUTO_TEST_CASE(TestInsertAndEncodeTwice)
260{
261 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
262 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
263 "/NTORRENT/linux15.01",
264 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
265 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
266
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800267 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800268 KeyChain keyChain;
269 keyChain.sign(file);
270 Block wire = file.wireEncode();
271
272 TorrentFile file2;
273 file2.wireDecode(wire);
274 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
275
276 file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
277 file.insert("/NTORRENT/linux15.01/file4/CB24C3GB");
spirosmastorakis6d4300f2016-02-29 20:18:43 -0800278 file.finalize();
spirosmastorakisa6057f52016-01-28 13:34:41 -0800279 Block wire2 = file.wireEncode();
280
281 file2.wireDecode(wire2);
282 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 4);
283
284 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
285 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
286 BOOST_CHECK_EQUAL(file2.getCatalog()[2], "/NTORRENT/linux15.01/file3/AB34C5KA");
287 BOOST_CHECK_EQUAL(file2.getCatalog()[3], "/NTORRENT/linux15.01/file4/CB24C3GB");
288}
289
290BOOST_AUTO_TEST_SUITE_END()
291
292} // namespace tests
293
294} // namespace ntorrent
295
296} // namespace ndn