Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -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-manager.hpp" |
| 25 | #include "torrent-file.hpp" |
| 26 | |
| 27 | #include <boost/filesystem.hpp> |
| 28 | |
| 29 | #include <ndn-cxx/util/io.hpp> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace ntorrent { |
| 33 | namespace tests { |
| 34 | |
| 35 | using std::vector; |
| 36 | using ndn::Name; |
| 37 | |
| 38 | namespace fs = boost::filesystem; |
| 39 | |
| 40 | class TestTorrentManager : public TorrentManager { |
| 41 | public: |
| 42 | TestTorrentManager(const ndn::Name& torrentFileName, |
| 43 | const std::string& filePath) |
| 44 | : TorrentManager(torrentFileName, filePath) |
| 45 | {} |
| 46 | |
| 47 | std::vector<TorrentFile> torrentSegments() const { |
| 48 | return m_torrentSegments; |
| 49 | } |
| 50 | |
| 51 | std::vector<FileManifest> fileManifests() const { |
| 52 | return m_fileManifests; |
| 53 | } |
| 54 | |
| 55 | std::vector<bool> fileState(const ndn::Name& manifestName) { |
| 56 | return m_fileStates[manifestName].second; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | BOOST_AUTO_TEST_SUITE(TestTorrentManagerInitialize) |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE(CheckInitializeComplete) |
| 63 | { |
| 64 | vector<FileManifest> manifests; |
| 65 | vector<TorrentFile> torrentSegments; |
| 66 | std::string filePath = "tests/testdata/"; |
| 67 | // get torrent files and manifests |
| 68 | { |
| 69 | auto temp = TorrentFile::generate("tests/testdata/foo", |
| 70 | 1024, |
| 71 | 1024, |
| 72 | 1024, |
| 73 | false); |
| 74 | torrentSegments = temp.first; |
| 75 | auto temp1 = temp.second; |
| 76 | for (const auto& ms : temp1) { |
| 77 | for (const auto& m : ms.first) { |
| 78 | manifests.push_back(m); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | // write the torrent segments and manifests to disk |
| 83 | std::string dirPath = ".appdata/foo/"; |
| 84 | boost::filesystem::create_directories(dirPath); |
| 85 | std::string torrentPath = dirPath + "torrent_files/"; |
| 86 | boost::filesystem::create_directory(torrentPath); |
| 87 | auto fileNum = 0; |
| 88 | for (const auto& t : torrentSegments) { |
| 89 | fileNum++; |
| 90 | auto filename = torrentPath + to_string(fileNum); |
| 91 | io::save(t, filename); |
| 92 | } |
| 93 | fileNum = 0; |
| 94 | auto manifestPath = dirPath + "manifests/"; |
| 95 | boost::filesystem::create_directory(manifestPath); |
| 96 | for (const auto& m : manifests) { |
| 97 | fileNum++; |
| 98 | auto filename = manifestPath + to_string(fileNum); |
| 99 | io::save(m, filename); |
| 100 | } |
| 101 | // Initialize and verify |
| 102 | TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=02c737fd4c6e7de4b4825b089f39700c2dfa8fd2bb2b91f09201e357c4463253", |
| 103 | filePath); |
| 104 | manager.Initialize(); |
| 105 | |
| 106 | // Check that the torrent segments and file manifests match (content and order) |
| 107 | BOOST_CHECK(manager.torrentSegments() == torrentSegments); |
| 108 | BOOST_CHECK(manager.fileManifests() == manifests); |
| 109 | // next check the data packet state vectors |
| 110 | for (auto m : manager.fileManifests()) { |
| 111 | auto fileState = manager.fileState(m.getFullName()); |
| 112 | BOOST_CHECK(fileState.size() == m.catalog().size()); |
| 113 | for (auto s : fileState) { |
| 114 | BOOST_CHECK(s); |
| 115 | } |
| 116 | } |
| 117 | fs::remove_all(dirPath); |
| 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(CheckInitializeEmpty) |
| 121 | { |
| 122 | TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=02c737fd4c6e7de4b4825b089f39700c2dfa8fd2bb2b91f09201e357c4463253", |
| 123 | "tests/testdata/"); |
| 124 | manager.Initialize(); |
| 125 | BOOST_CHECK(manager.torrentSegments() == vector<TorrentFile>()); |
| 126 | BOOST_CHECK(manager.fileManifests() == vector<FileManifest>()); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(CheckInitializeNoManifests) |
| 130 | { |
| 131 | vector<TorrentFile> torrentSegments; |
| 132 | std::string filePath = "tests/testdata/"; |
| 133 | // get torrent files and manifests |
| 134 | { |
| 135 | auto temp = TorrentFile::generate("tests/testdata/foo", |
| 136 | 1024, |
| 137 | 1024, |
| 138 | 1024, |
| 139 | false); |
| 140 | torrentSegments = temp.first; |
| 141 | } |
| 142 | // write the torrent segments but no manifests to disk |
| 143 | std::string dirPath = ".appdata/foo/"; |
| 144 | boost::filesystem::create_directories(dirPath); |
| 145 | std::string torrentPath = dirPath + "torrent_files/"; |
| 146 | boost::filesystem::create_directory(torrentPath); |
| 147 | auto fileNum = 0; |
| 148 | for (const auto& t : torrentSegments) { |
| 149 | fileNum++; |
| 150 | auto filename = torrentPath + to_string(fileNum); |
| 151 | io::save(t, filename); |
| 152 | } |
| 153 | // Initialize and verify |
| 154 | TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=02c737fd4c6e7de4b4825b089f39700c2dfa8fd2bb2b91f09201e357c4463253", |
| 155 | filePath); |
| 156 | manager.Initialize(); |
| 157 | |
| 158 | // Check that the torrent segments and file manifests match (content and order) |
| 159 | BOOST_CHECK(manager.torrentSegments() == torrentSegments); |
| 160 | BOOST_CHECK(manager.fileManifests() == vector<FileManifest>()); |
| 161 | |
| 162 | fs::remove_all(dirPath); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(CheckInitializeMissingManifests) |
| 166 | { |
| 167 | vector<FileManifest> manifests; |
| 168 | vector<TorrentFile> torrentSegments; |
| 169 | std::string filePath = "tests/testdata/"; |
| 170 | // get torrent files and manifests |
| 171 | { |
| 172 | auto temp = TorrentFile::generate("tests/testdata/foo", |
| 173 | 1024, |
| 174 | 1024, |
| 175 | 1024, |
| 176 | false); |
| 177 | torrentSegments = temp.first; |
| 178 | auto temp1 = temp.second; |
| 179 | temp1.pop_back(); // remove the manifests for the last file |
| 180 | for (const auto& ms : temp1) { |
| 181 | for (const auto& m : ms.first) { |
| 182 | manifests.push_back(m); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | // write the torrent segments and manifests to disk |
| 187 | std::string dirPath = ".appdata/foo/"; |
| 188 | boost::filesystem::create_directories(dirPath); |
| 189 | std::string torrentPath = dirPath + "torrent_files/"; |
| 190 | boost::filesystem::create_directories(torrentPath); |
| 191 | auto fileNum = 0; |
| 192 | for (const auto& t : torrentSegments) { |
| 193 | fileNum++; |
| 194 | auto filename = torrentPath + to_string(fileNum); |
| 195 | io::save(t, filename); |
| 196 | |
| 197 | } |
| 198 | fileNum = 0; |
| 199 | auto manifestPath = dirPath + "manifests/"; |
| 200 | boost::filesystem::create_directory(manifestPath); |
| 201 | for (const auto& m : manifests) { |
| 202 | fileNum++; |
| 203 | auto filename = manifestPath + to_string(fileNum); |
| 204 | io::save(m, filename); |
| 205 | } |
| 206 | // Initialize and verify |
| 207 | TestTorrentManager manager("/NTORRENT/foo/torrent-file/sha256digest=02c737fd4c6e7de4b4825b089f39700c2dfa8fd2bb2b91f09201e357c4463253", |
| 208 | filePath); |
| 209 | manager.Initialize(); |
| 210 | |
| 211 | // Check that the torrent segments and file manifests match (content and order) |
| 212 | BOOST_CHECK(manager.torrentSegments() == torrentSegments); |
| 213 | BOOST_CHECK(manager.fileManifests() == manifests); |
| 214 | // next check the data packet state vectors |
| 215 | for (auto m : manager.fileManifests()) { |
| 216 | auto fileState = manager.fileState(m.getFullName()); |
| 217 | BOOST_CHECK(fileState.size() == m.catalog().size()); |
| 218 | for (auto s : fileState) { |
| 219 | BOOST_CHECK(s); |
| 220 | } |
| 221 | } |
| 222 | fs::remove_all(dirPath); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() |
| 226 | |
| 227 | } // namespace tests |
| 228 | } // namespace nTorrent |
| 229 | } // namespace ndn |