blob: 90b9b6dccd1366e76227e1cfac5da8e5eecc8cdb [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, 0x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, 0x81, 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
149 KeyChain keyChain;
150 keyChain.sign(file);
151
152 Block wire = file.wireEncode();
153
154 TorrentFile file2;
155 file2.wireDecode(wire);
156
157 BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
158 BOOST_CHECK_EQUAL(*(file2.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
159 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
160 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
161 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
162 BOOST_CHECK_EQUAL(file2.getCommonPrefix(), "/NTORRENT/linux15.01");
163}
164
165BOOST_AUTO_TEST_CASE(CheckEncodeDecodeNoTorrentFilePtr)
166{
167 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
168 "/NTORRENT/linux15.01",
169 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
170 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
171
172 KeyChain keyChain;
173 keyChain.sign(file);
174
175 Block wire = file.wireEncode();
176
177 TorrentFile file2;
178 file2.wireDecode(wire);
179
180 BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
181 BOOST_CHECK(!file2.getTorrentFilePtr());
182 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
183 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
184 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
185}
186
187BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyTorrentFile)
188{
189 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
190 "",
191 {});
192
193 KeyChain keyChain;
194 keyChain.sign(file);
195
196 Block wire = file.wireEncode();
197
198 TorrentFile file2;
199
200 BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
201}
202
203
204BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyCatalog)
205{
206
207 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
208 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
209 "/NTORRENT/linux15.01",
210 {});
211
212 KeyChain keyChain;
213 keyChain.sign(file);
214
215 Block wire = file.wireEncode();
216
217 TorrentFile file2;
218
219 BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
220}
221
222BOOST_AUTO_TEST_CASE(DecodeFromWire)
223{
224
225 Block TorrentFileBlock(TorrentFileTest, sizeof(TorrentFileTest));
226 TorrentFile file(TorrentFileBlock);
227
228 BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/.torrent-file");
229 BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
230 BOOST_CHECK_EQUAL(file.getCatalog().size(), 2);
231 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
232 BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
233 BOOST_CHECK_EQUAL(file.getCommonPrefix(), "/NTORRENT/linux15.01");
234}
235
236BOOST_AUTO_TEST_CASE(TestInsertErase)
237{
238 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
239 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
240 "/NTORRENT/linux15.01",
241 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
242 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
243
244 file.erase("/NTORRENT/linux15.01/file0/1A2B3C4D");
245 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file1/2A3B4C5E");
246
247 file.erase("/NTORRENT/linux15.01/file1/2A3B4C5E");
248 BOOST_CHECK_EQUAL(file.getCatalog().size(), 0);
249
250 file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
251 BOOST_CHECK_EQUAL(file.getCatalog().size(), 1);
252 BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file3/AB34C5KA");
253}
254
255BOOST_AUTO_TEST_CASE(TestInsertAndEncodeTwice)
256{
257 TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
258 "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
259 "/NTORRENT/linux15.01",
260 {"/NTORRENT/linux15.01/file0/1A2B3C4D",
261 "/NTORRENT/linux15.01/file1/2A3B4C5E"});
262
263 KeyChain keyChain;
264 keyChain.sign(file);
265 Block wire = file.wireEncode();
266
267 TorrentFile file2;
268 file2.wireDecode(wire);
269 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
270
271 file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
272 file.insert("/NTORRENT/linux15.01/file4/CB24C3GB");
273 Block wire2 = file.wireEncode();
274
275 file2.wireDecode(wire2);
276 BOOST_CHECK_EQUAL(file2.getCatalog().size(), 4);
277
278 BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
279 BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
280 BOOST_CHECK_EQUAL(file2.getCatalog()[2], "/NTORRENT/linux15.01/file3/AB34C5KA");
281 BOOST_CHECK_EQUAL(file2.getCatalog()[3], "/NTORRENT/linux15.01/file4/CB24C3GB");
282}
283
284BOOST_AUTO_TEST_SUITE_END()
285
286} // namespace tests
287
288} // namespace ntorrent
289
290} // namespace ndn