Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
| 6 | * |
| 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ChronoShare 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "content-server.hpp" |
| 22 | #include "action-log.hpp" |
| 23 | #include "sync-core.hpp" |
| 24 | |
| 25 | #include "test-common.hpp" |
| 26 | #include "dummy-forwarder.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace chronoshare { |
| 30 | namespace tests { |
| 31 | |
| 32 | namespace fs = boost::filesystem; |
| 33 | |
| 34 | _LOG_INIT(Test.ContentServer); |
| 35 | |
| 36 | class TestContentServerFixture : public IdentityManagementTimeFixture |
| 37 | { |
| 38 | public: |
| 39 | TestContentServerFixture() |
| 40 | : forwarder(m_io, m_keyChain) |
| 41 | , face(forwarder.addFace()) |
| 42 | , appName("test-chronoshare") |
| 43 | , deviceName("/device") |
| 44 | , shareFolderName("sharefolder") |
| 45 | , root("test-server-and-fetch") |
| 46 | { |
| 47 | cleanDir(root); |
| 48 | |
| 49 | create_directory(root); |
| 50 | |
| 51 | syncLog = make_shared<SyncLog>(root, deviceName); |
| 52 | actionLog = std::make_shared<ActionLog>(face, root, syncLog, |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 53 | "top-secret", name::Component("test-chronoshare"), |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 54 | ActionLog::OnFileAddedOrChangedCallback(), |
| 55 | ActionLog::OnFileRemovedCallback()); |
| 56 | |
| 57 | actionLog->AddLocalActionUpdate("file.txt", |
| 58 | *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 59 | std::time(nullptr), 0755, 10); |
| 60 | BOOST_CHECK_EQUAL(syncLog->SeqNo(deviceName), 1); |
| 61 | |
| 62 | ndn::ConstBufferPtr hash = syncLog->RememberStateInStateLog(); |
| 63 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 64 | server = make_unique<ContentServer>(face, actionLog, root, deviceName, shareFolderName, |
| 65 | name::Component("test-chronoshare"), m_keyChain, time::seconds(5)); |
Yukai Tu | 931546f | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 66 | |
| 67 | Name localPrefix("/local"); |
| 68 | Name broadcastPrefix("/multicast"); |
| 69 | |
| 70 | server->registerPrefix(localPrefix); |
| 71 | server->registerPrefix(broadcastPrefix); |
| 72 | |
| 73 | advanceClocks(time::milliseconds(10), 1000); |
| 74 | } |
| 75 | |
| 76 | ~TestContentServerFixture() |
| 77 | { |
| 78 | cleanDir(root); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | onActionData(const Interest& interest, Data& data) |
| 83 | { |
| 84 | _LOG_DEBUG("on action data, interest Name: " << interest); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | onTimeout(const Interest& interest) |
| 89 | { |
| 90 | _LOG_DEBUG("on timeout, interest Name: " << interest); |
| 91 | BOOST_CHECK(false); |
| 92 | } |
| 93 | |
| 94 | void cleanDir(fs::path dir) { |
| 95 | if (exists(dir)) { |
| 96 | remove_all(dir); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public: |
| 101 | DummyForwarder forwarder; |
| 102 | Face& face; |
| 103 | shared_ptr<SyncLog> syncLog; |
| 104 | shared_ptr<ActionLog> actionLog; |
| 105 | unique_ptr<ContentServer> server; |
| 106 | Name appName; |
| 107 | Name deviceName; |
| 108 | |
| 109 | std::string shareFolderName; |
| 110 | |
| 111 | fs::path root; |
| 112 | }; |
| 113 | |
| 114 | BOOST_FIXTURE_TEST_SUITE(TestContentServer, TestContentServerFixture) |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(TestContentServerServe) |
| 117 | { |
| 118 | Interest interest( |
| 119 | Name("/local").append(deviceName).append(appName).append("action") |
| 120 | .append(shareFolderName).appendNumber(1)); |
| 121 | |
| 122 | _LOG_DEBUG("interest Name: " << interest); |
| 123 | |
| 124 | dynamic_cast<util::DummyClientFace*>(&face)->receive(interest); |
| 125 | |
| 126 | advanceClocks(time::milliseconds(10), 1000); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(dynamic_cast<util::DummyClientFace*>(&face)->sentData.size(),1); |
| 129 | |
| 130 | Data data = dynamic_cast<util::DummyClientFace*>(&face)->sentData.at(0); |
| 131 | |
| 132 | BOOST_CHECK_EQUAL(data.getName(), "/local/device/test-chronoshare/action/sharefolder/%01"); |
| 133 | |
| 134 | shared_ptr<ActionItem> action = deserializeMsg<ActionItem>( |
| 135 | Buffer(data.getContent().value(), data.getContent().value_size())); |
| 136 | |
| 137 | BOOST_CHECK_EQUAL(action->action(), 0); |
| 138 | BOOST_CHECK_EQUAL(action->filename(), "file.txt"); |
| 139 | BOOST_CHECK_EQUAL(action->file_hash().size(), 32); |
| 140 | BOOST_CHECK_EQUAL(action->version(), 0); |
| 141 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), false); |
| 142 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() |
| 146 | |
| 147 | } // namespace tests |
| 148 | } // namespace chronoshare |
| 149 | } // namespace ndn |