spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 1 | /* -*- 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" |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 25 | #include "file-manifest.hpp" |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 26 | |
| 27 | #include <ndn-cxx/data.hpp> |
| 28 | #include <ndn-cxx/signature.hpp> |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
| 30 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 31 | #include <boost/range/adaptors.hpp> |
| 32 | #include <boost/filesystem.hpp> |
| 33 | #include <boost/filesystem/fstream.hpp> |
| 34 | |
| 35 | namespace fs = boost::filesystem; |
| 36 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 37 | namespace ndn { |
| 38 | |
| 39 | namespace ntorrent { |
| 40 | |
| 41 | namespace tests { |
| 42 | |
| 43 | |
| 44 | const static uint8_t TorrentFileTest[] = { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 45 | 0x6, 0xfd, // data |
| 46 | 0x1, 0xea, |
| 47 | 0x7, 0x2c, // name |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 48 | 0x8, 0x8, |
| 49 | 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54, |
| 50 | 0x8, 0xa, |
| 51 | 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31, |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 52 | 0x8, 0xc, |
| 53 | 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65, |
| 54 | 0x8, 0x6, |
| 55 | 0x41, 0x42, 0x32, 0x43, 0x44, 0x41, |
| 56 | 0x14, 0x0, // meta-info |
| 57 | 0x15, 0x76, // content |
| 58 | 0x7, 0x36, |
| 59 | 0x8, 0x8, |
| 60 | 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54, |
| 61 | 0x8, 0xa, |
| 62 | 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31, |
| 63 | 0x8, 0xc, |
| 64 | 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65, |
| 65 | 0x8, 0x8, |
| 66 | 0x73, 0x65, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x32, |
| 67 | 0x8, 0x6, |
| 68 | 0x41, 0x45, 0x33, 0x32, 0x31, 0x43, |
| 69 | 0x7, 0x16, |
| 70 | 0x8, 0x8, |
| 71 | 0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54, |
| 72 | 0x8, 0xa, |
| 73 | 0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31, |
| 74 | 0x7, 0x11, |
| 75 | 0x8, 0x5, |
| 76 | 0x66, 0x69, 0x6c, 0x65, 0x30, |
| 77 | 0x8, 0x8, |
| 78 | 0x31, 0x41, 0x32, 0x42, 0x33, 0x43, 0x34, 0x44, |
| 79 | 0x7, 0x11, |
| 80 | 0x8, 0x5, |
| 81 | 0x66, 0x69, 0x6c, 0x65, 0x31, |
| 82 | 0x8, 0x8, |
| 83 | 0x32, 0x41, 0x33, 0x42, 0x34, 0x43, 0x35, 0x45, |
| 84 | 0x16, 0x3c, // SignatureInfo |
| 85 | 0x1b, 0x1, // SignatureType |
| 86 | 0x1, |
| 87 | 0x1c, 0x37, // KeyLocator |
| 88 | 0x7, 0x35, |
| 89 | 0x8, 0xc, |
| 90 | 0x74, 0x6d, 0x70, 0x2d, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, |
| 91 | 0x8, 0x4, |
| 92 | 0x3d, 0xa8, 0x2c, 0x19, |
| 93 | 0x8, 0x3, |
| 94 | 0x4b, 0x45, 0x59, |
| 95 | 0x8, 0x11, |
| 96 | 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x35, 0x34, 0x39, 0x37, 0x33, 0x38, |
| 97 | 0x37, 0x36, 0x31, 0x37, 0x31, |
| 98 | 0x8, 0x7, |
| 99 | 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52, 0x54, |
| 100 | 0x17, 0xfd, // SignatureValue |
| 101 | 0x1, 0x0, 0x3d, 0xb6, 0x32, 0x59, 0xbd, 0xe5, 0xad, 0x27, 0x1b, 0xc2, 0x68, |
| 102 | 0x3d, 0x7a, 0xd3, 0x4e, 0xf, 0x40, 0xa5, 0xf8, 0x80, 0x4b, 0xf2, 0xf9, 0xb1, |
| 103 | 0x7, 0xea, 0x56, 0x84, 0xd, 0x94, 0xd, 0xf9, 0x88, 0xd4, 0xe0, 0xb6, 0x63, 0xac, |
| 104 | 0x3a, 0xdc, 0x17, 0xc2, 0xff, 0xde, 0xc, 0xc3, 0xef, 0xb, 0x3b, 0x1a, 0xef, 0xb, |
| 105 | 0x7, 0x99, 0x9b, 0xb7, 0xe6, 0x4a, 0x68, 0xf8, 0xda, 0x62, 0x29, 0xfd, 0xb1, 0xb1, |
| 106 | 0xe9, 0xe3, 0x3a, 0x42, 0x1f, 0xbd, 0xad, 0x96, 0xc4, 0x5f, 0x34, 0x55, 0x9, 0xba, |
| 107 | 0x7a, 0x82, 0x2d, 0x9c, 0x7b, 0x52, 0x9d, 0xf, 0xd6, 0xea, 0xc3, 0x60, 0xbf, 0x75, |
| 108 | 0xfb, 0x0, 0x1b, 0xb8, 0x4, 0xa8, 0x6e, 0x95, 0x5a, 0xcf, 0xca, 0x90, 0x20, 0x21, |
| 109 | 0x58, 0x27, 0x2c, 0xef, 0xed, 0x23, 0x37, 0xfa, 0xd, 0xc6, 0x3, 0x7b, 0xd6, 0xef, |
| 110 | 0x76, 0x3, 0xd7, 0x22, 0xd8, 0x6f, 0xa2, 0x7d, 0xc2, 0x84, 0xee, 0x65, 0xbf, 0x63, |
| 111 | 0xe2, 0x27, 0xc4, 0xeb, 0xb7, 0xb2, 0xa6, 0xa0, 0x58, 0x68, 0x1a, 0x3a, 0x85, 0xd8, |
| 112 | 0x8d, 0xd8, 0xfb, 0x52, 0x9b, 0xba, 0x22, 0x2d, 0xb2, 0x4b, 0x9e, 0x3d, 0xf5, 0x61, |
| 113 | 0x83, 0x6f, 0x78, 0x67, 0x85, 0x3d, 0xf, 0x24, 0x47, 0xbf, 0x8c, 0xdf, 0xdf, 0x8a, |
| 114 | 0xc8, 0xa9, 0xed, 0xaf, 0x8b, 0x49, 0xdf, 0x2d, 0x2a, 0xfe, 0x2f, 0x4b, 0xc5, 0xe6, |
| 115 | 0x7e, 0xee, 0x35, 0xf1, 0x6, 0x88, 0x9c, 0xa0, 0x25, 0xf, 0x6, 0x56, 0xf4, 0x72, |
| 116 | 0x89, 0x2f, 0x95, 0x64, 0x39, 0x38, 0x21, 0xc3, 0x75, 0xef, 0x80, 0x5a, 0x73, 0x1a, |
| 117 | 0xec, 0xb9, 0x6d, 0x3, 0x47, 0xac, 0x64, 0x7f, 0x85, 0xbe, 0xb9, 0xab, 0x87, 0x9f, |
| 118 | 0xd2, 0x9c, 0xe7, 0x9c, 0x86, 0xd5, 0x59, 0x65, 0x73, 0x24, 0x15, 0x3e, 0xfc, 0x94, |
| 119 | 0xf8, 0x7, 0x26, 0x9b, 0x4f, 0x6e, 0x1b, 0x1c |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | BOOST_AUTO_TEST_SUITE(TestTorrentFile) |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(CheckGettersSetters) |
| 125 | { |
| 126 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 127 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
| 128 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 129 | "/NTORRENT/linux15.01", |
| 130 | {"/NTORRENT/linux15.01/file0/1A2B3C4D", |
| 131 | "/NTORRENT/linux15.01/file1/2A3B4C5E"}); |
| 132 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 133 | BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/torrent-file/AB2CDA"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(file.getCatalog().size(), 2); |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 135 | BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), |
| 136 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 138 | BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 139 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 140 | TorrentFile file2("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 141 | "/NTORRENT/linux15.01", |
| 142 | {"/NTORRENT/linux15.01/file0/1A2B3C4D"}); |
| 143 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 144 | BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/torrent-file/AB2CDA"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 145 | BOOST_CHECK(!file2.getTorrentFilePtr()); |
| 146 | BOOST_CHECK_EQUAL(file2.getCatalog().size(), 1); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(CheckEncodeDecode) |
| 150 | { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 151 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
| 152 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 153 | "/NTORRENT/linux15.01", |
| 154 | {"/NTORRENT/linux15.01/file0/1A2B3C4D", |
| 155 | "/NTORRENT/linux15.01/file1/2A3B4C5E"}); |
| 156 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 157 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 158 | KeyChain keyChain; |
| 159 | keyChain.sign(file); |
| 160 | |
| 161 | Block wire = file.wireEncode(); |
| 162 | |
| 163 | TorrentFile file2; |
| 164 | file2.wireDecode(wire); |
| 165 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 166 | BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/torrent-file/AB2CDA"); |
| 167 | BOOST_CHECK_EQUAL(*(file2.getTorrentFilePtr()), |
| 168 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2); |
| 170 | BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 171 | BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 172 | BOOST_CHECK_EQUAL(file2.getCommonPrefix(), "/NTORRENT/linux15.01"); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(CheckEncodeDecodeNoTorrentFilePtr) |
| 176 | { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 177 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 178 | "/NTORRENT/linux15.01", |
| 179 | {"/NTORRENT/linux15.01/file0/1A2B3C4D", |
| 180 | "/NTORRENT/linux15.01/file1/2A3B4C5E"}); |
| 181 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 182 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 183 | KeyChain keyChain; |
| 184 | keyChain.sign(file); |
| 185 | |
| 186 | Block wire = file.wireEncode(); |
| 187 | |
| 188 | TorrentFile file2; |
| 189 | file2.wireDecode(wire); |
| 190 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 191 | BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/torrent-file/AB2CDA"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 192 | BOOST_CHECK(!file2.getTorrentFilePtr()); |
| 193 | BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2); |
| 194 | BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 195 | BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyTorrentFile) |
| 199 | { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 200 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 201 | "", |
| 202 | {}); |
| 203 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 204 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 205 | KeyChain keyChain; |
| 206 | keyChain.sign(file); |
| 207 | |
| 208 | Block wire = file.wireEncode(); |
| 209 | |
| 210 | TorrentFile file2; |
| 211 | |
| 212 | BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyCatalog) |
| 217 | { |
| 218 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 219 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
| 220 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 221 | "/NTORRENT/linux15.01", |
| 222 | {}); |
| 223 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 224 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 225 | KeyChain keyChain; |
| 226 | keyChain.sign(file); |
| 227 | |
| 228 | Block wire = file.wireEncode(); |
| 229 | |
| 230 | TorrentFile file2; |
| 231 | |
| 232 | BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_CASE(DecodeFromWire) |
| 236 | { |
| 237 | |
| 238 | Block TorrentFileBlock(TorrentFileTest, sizeof(TorrentFileTest)); |
| 239 | TorrentFile file(TorrentFileBlock); |
| 240 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 241 | BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/torrent-file/AB2CDA"); |
| 242 | BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), |
| 243 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C"); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 244 | BOOST_CHECK_EQUAL(file.getCatalog().size(), 2); |
| 245 | BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 246 | BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 247 | BOOST_CHECK_EQUAL(file.getCommonPrefix(), "/NTORRENT/linux15.01"); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(TestInsertErase) |
| 251 | { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 252 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
| 253 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 254 | "/NTORRENT/linux15.01", |
| 255 | {"/NTORRENT/linux15.01/file0/1A2B3C4D", |
| 256 | "/NTORRENT/linux15.01/file1/2A3B4C5E"}); |
| 257 | |
| 258 | file.erase("/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 259 | BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 260 | |
| 261 | file.erase("/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 262 | BOOST_CHECK_EQUAL(file.getCatalog().size(), 0); |
| 263 | |
| 264 | file.insert("/NTORRENT/linux15.01/file3/AB34C5KA"); |
| 265 | BOOST_CHECK_EQUAL(file.getCatalog().size(), 1); |
| 266 | BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file3/AB34C5KA"); |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE(TestInsertAndEncodeTwice) |
| 270 | { |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 271 | TorrentFile file("/NTORRENT/linux15.01/torrent-file/AB2CDA", |
| 272 | "/NTORRENT/linux15.01/torrent-file/segment2/AE321C", |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 273 | "/NTORRENT/linux15.01", |
| 274 | {"/NTORRENT/linux15.01/file0/1A2B3C4D", |
| 275 | "/NTORRENT/linux15.01/file1/2A3B4C5E"}); |
| 276 | |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 277 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 278 | KeyChain keyChain; |
| 279 | keyChain.sign(file); |
| 280 | Block wire = file.wireEncode(); |
| 281 | |
| 282 | TorrentFile file2; |
| 283 | file2.wireDecode(wire); |
| 284 | BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2); |
| 285 | |
| 286 | file.insert("/NTORRENT/linux15.01/file3/AB34C5KA"); |
| 287 | file.insert("/NTORRENT/linux15.01/file4/CB24C3GB"); |
spirosmastorakis | 6d4300f | 2016-02-29 20:18:43 -0800 | [diff] [blame] | 288 | file.finalize(); |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 289 | Block wire2 = file.wireEncode(); |
| 290 | |
| 291 | file2.wireDecode(wire2); |
| 292 | BOOST_CHECK_EQUAL(file2.getCatalog().size(), 4); |
| 293 | |
| 294 | BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D"); |
| 295 | BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E"); |
| 296 | BOOST_CHECK_EQUAL(file2.getCatalog()[2], "/NTORRENT/linux15.01/file3/AB34C5KA"); |
| 297 | BOOST_CHECK_EQUAL(file2.getCatalog()[3], "/NTORRENT/linux15.01/file4/CB24C3GB"); |
| 298 | } |
| 299 | |
| 300 | BOOST_AUTO_TEST_SUITE_END() |
| 301 | |
spirosmastorakis | f5d1b6c | 2016-02-25 12:49:56 -0800 | [diff] [blame^] | 302 | BOOST_AUTO_TEST_CASE(TestTorrentFileGenerator) |
| 303 | { |
| 304 | const struct { |
| 305 | size_t d_dataPacketSize; |
| 306 | size_t d_subManifestSize; |
| 307 | const char *d_directoryPath; |
| 308 | size_t d_namesPerSegment; |
| 309 | bool d_shouldThrow; |
| 310 | } DATA [] = { |
| 311 | // Affirmative tests |
| 312 | {1024 , 100 , "tests/testdata/foo" , 1, false }, |
| 313 | {512 , 80 , "tests/testdata/foo" , 3, false }, |
| 314 | {256 , 50 , "tests/testdata/foo" , 2, false }, |
| 315 | {2048 , 20 , "tests/testdata/foo" , 4, false }, |
| 316 | {512 , 80 , "tests/testdata/foo" , 1, false }, |
| 317 | {1024 , 50 , "tests/testdata/foo" , 1, false }, |
| 318 | {2048 , 128 , "tests/testdata/foo", 2, false }, |
| 319 | // Negative tests |
| 320 | // non-existent directory |
| 321 | {128 , 128 , "tests/testdata/foo-fake", 2, true }, |
| 322 | }; |
| 323 | enum { NUM_DATA = sizeof DATA / sizeof *DATA }; |
| 324 | for (int i = 0; i < NUM_DATA; ++i) { |
| 325 | auto dataPacketSize = DATA[i].d_dataPacketSize; |
| 326 | auto subManifestSize = DATA[i].d_subManifestSize; |
| 327 | auto directoryPath = DATA[i].d_directoryPath; |
| 328 | auto namesPerSegment = DATA[i].d_namesPerSegment; |
| 329 | auto shouldThrow = DATA[i].d_shouldThrow; |
| 330 | |
| 331 | |
| 332 | std::pair<std::vector<TorrentFile>, |
| 333 | std::vector<std::pair<std::vector<FileManifest>, |
| 334 | std::vector<Data>>>> torrentFilePair1; |
| 335 | |
| 336 | std::pair<std::vector<TorrentFile>, |
| 337 | std::vector<std::pair<std::vector<FileManifest>, |
| 338 | std::vector<Data>>>> torrentFilePair2; |
| 339 | |
| 340 | if (shouldThrow) { |
| 341 | BOOST_REQUIRE_THROW(TorrentFile::generate(directoryPath, |
| 342 | namesPerSegment, |
| 343 | subManifestSize, |
| 344 | dataPacketSize), |
| 345 | TorrentFile::Error); |
| 346 | |
| 347 | BOOST_REQUIRE_THROW(TorrentFile::generate(directoryPath, |
| 348 | namesPerSegment, |
| 349 | subManifestSize, |
| 350 | dataPacketSize, |
| 351 | true), |
| 352 | TorrentFile::Error); |
| 353 | } |
| 354 | else { |
| 355 | torrentFilePair1 = TorrentFile::generate(directoryPath, |
| 356 | namesPerSegment, |
| 357 | subManifestSize, |
| 358 | dataPacketSize); |
| 359 | |
| 360 | torrentFilePair2 = TorrentFile::generate(directoryPath, |
| 361 | namesPerSegment, |
| 362 | subManifestSize, |
| 363 | dataPacketSize, |
| 364 | true); |
| 365 | |
| 366 | auto torrentFileSegments = torrentFilePair1.first; |
| 367 | auto manifestPairs1 = torrentFilePair1.second; |
| 368 | auto manifestPairs2 = torrentFilePair2.second; |
| 369 | |
| 370 | // Check that generate has not returned any data packets unless otherwise specified |
| 371 | for (auto i = manifestPairs1.begin(); i != manifestPairs1.end(); ++i) { |
| 372 | BOOST_CHECK_EQUAL((*i).second.size(), 0); |
| 373 | } |
| 374 | |
| 375 | Name directoryPathName(directoryPath); |
| 376 | fs::recursive_directory_iterator directoryPtr(fs::system_complete(directoryPath).string()); |
| 377 | int numberOfDirectoryFiles = 0; |
| 378 | for (fs::recursive_directory_iterator i = directoryPtr; |
| 379 | i != fs::recursive_directory_iterator(); ++i) { |
| 380 | numberOfDirectoryFiles++; |
| 381 | } |
| 382 | // Verify the basic attributes of the torrent-file |
| 383 | for (auto it = torrentFileSegments.begin(); it != torrentFileSegments.end(); ++it) { |
| 384 | // Verify that each file torrent-file is signed |
| 385 | BOOST_CHECK_NO_THROW(it->getFullName()); |
| 386 | BOOST_CHECK_EQUAL(it->getCommonPrefix(), |
| 387 | Name("/NTORRENT" + |
| 388 | directoryPathName.getSubName( |
| 389 | directoryPathName.size() - 1).toUri())); |
| 390 | if (it != torrentFileSegments.end() - 1) { |
| 391 | BOOST_CHECK_EQUAL(it->getCatalog().size(), namesPerSegment); |
| 392 | BOOST_CHECK_EQUAL(*(it->getTorrentFilePtr()), (it+1)->getFullName()); |
| 393 | } |
| 394 | else { |
| 395 | BOOST_CHECK_LE(it->getCatalog().size(), subManifestSize); |
| 396 | BOOST_CHECK(!(it->getTorrentFilePtr())); |
| 397 | } |
| 398 | } |
| 399 | int myDiv = (numberOfDirectoryFiles / namesPerSegment); |
| 400 | if (numberOfDirectoryFiles % namesPerSegment == 0) { |
| 401 | BOOST_CHECK_EQUAL(torrentFileSegments.size(), myDiv); |
| 402 | } |
| 403 | else { |
| 404 | BOOST_CHECK_EQUAL(torrentFileSegments.size(), (myDiv + 1)); |
| 405 | } |
| 406 | |
| 407 | std::vector<uint8_t> dataBytes; |
| 408 | for (auto i = manifestPairs2.begin() ; i != manifestPairs2.end(); ++i) { |
| 409 | for (auto d : (*i).second) { |
| 410 | auto content = d.getContent(); |
| 411 | dataBytes.insert(dataBytes.end(), content.value_begin(), content.value_end()); |
| 412 | } |
| 413 | } |
| 414 | // load the contents of the directory files from disk |
| 415 | std::vector<uint8_t> directoryFilesBytes; |
| 416 | fs::recursive_directory_iterator directoryPtr2(fs::system_complete(directoryPath).string()); |
| 417 | for (fs::recursive_directory_iterator i = directoryPtr2; |
| 418 | i != fs::recursive_directory_iterator(); |
| 419 | ++i) { |
| 420 | fs::ifstream is((*i), fs::ifstream::binary | fs::ifstream::in); |
| 421 | is >> std::noskipws; |
| 422 | std::istream_iterator<uint8_t> start(is), end; |
| 423 | std::vector<uint8_t> fileBytes(start, end); |
| 424 | directoryFilesBytes.insert(directoryFilesBytes.end(), fileBytes.begin(), fileBytes.end()); |
| 425 | } |
| 426 | // confirm that they are equal |
| 427 | BOOST_CHECK_EQUAL_COLLECTIONS(directoryFilesBytes.begin(), directoryFilesBytes.end(), |
| 428 | dataBytes.begin(), dataBytes.end()); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
spirosmastorakis | a6057f5 | 2016-01-28 13:34:41 -0800 | [diff] [blame] | 433 | } // namespace tests |
| 434 | |
| 435 | } // namespace ntorrent |
| 436 | |
| 437 | } // namespace ndn |