blob: 8dca18f31d584178eed3ef52b2b20166428cb048 [file] [log] [blame]
Yukai Tu931546f2016-10-24 13:48:01 -07001/* -*- 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
28namespace ndn {
29namespace chronoshare {
30namespace tests {
31
32namespace fs = boost::filesystem;
33
34_LOG_INIT(Test.ContentServer);
35
36class TestContentServerFixture : public IdentityManagementTimeFixture
37{
38public:
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,
53 "top-secret", "test-chronoshare",
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
64 server = make_unique<ContentServer>(face, actionLog, root,
65 deviceName, shareFolderName,
66 "test-chronoshare", m_keyChain, 5);
67
68 Name localPrefix("/local");
69 Name broadcastPrefix("/multicast");
70
71 server->registerPrefix(localPrefix);
72 server->registerPrefix(broadcastPrefix);
73
74 advanceClocks(time::milliseconds(10), 1000);
75 }
76
77 ~TestContentServerFixture()
78 {
79 cleanDir(root);
80 }
81
82 void
83 onActionData(const Interest& interest, Data& data)
84 {
85 _LOG_DEBUG("on action data, interest Name: " << interest);
86 }
87
88 void
89 onTimeout(const Interest& interest)
90 {
91 _LOG_DEBUG("on timeout, interest Name: " << interest);
92 BOOST_CHECK(false);
93 }
94
95 void cleanDir(fs::path dir) {
96 if (exists(dir)) {
97 remove_all(dir);
98 }
99 }
100
101public:
102 DummyForwarder forwarder;
103 Face& face;
104 shared_ptr<SyncLog> syncLog;
105 shared_ptr<ActionLog> actionLog;
106 unique_ptr<ContentServer> server;
107 Name appName;
108 Name deviceName;
109
110 std::string shareFolderName;
111
112 fs::path root;
113};
114
115BOOST_FIXTURE_TEST_SUITE(TestContentServer, TestContentServerFixture)
116
117BOOST_AUTO_TEST_CASE(TestContentServerServe)
118{
119 Interest interest(
120 Name("/local").append(deviceName).append(appName).append("action")
121 .append(shareFolderName).appendNumber(1));
122
123 _LOG_DEBUG("interest Name: " << interest);
124
125 dynamic_cast<util::DummyClientFace*>(&face)->receive(interest);
126
127 advanceClocks(time::milliseconds(10), 1000);
128
129 BOOST_CHECK_EQUAL(dynamic_cast<util::DummyClientFace*>(&face)->sentData.size(),1);
130
131 Data data = dynamic_cast<util::DummyClientFace*>(&face)->sentData.at(0);
132
133 BOOST_CHECK_EQUAL(data.getName(), "/local/device/test-chronoshare/action/sharefolder/%01");
134
135 shared_ptr<ActionItem> action = deserializeMsg<ActionItem>(
136 Buffer(data.getContent().value(), data.getContent().value_size()));
137
138 BOOST_CHECK_EQUAL(action->action(), 0);
139 BOOST_CHECK_EQUAL(action->filename(), "file.txt");
140 BOOST_CHECK_EQUAL(action->file_hash().size(), 32);
141 BOOST_CHECK_EQUAL(action->version(), 0);
142 BOOST_CHECK_EQUAL(action->has_parent_device_name(), false);
143 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false);
144}
145
146BOOST_AUTO_TEST_SUITE_END()
147
148} // namespace tests
149} // namespace chronoshare
150} // namespace ndn