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