blob: 4df377741582ae6764c4a52ea72245a4ef8ef73c [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,
Lijing Wang8e56d082016-12-25 14:45:23 -080053 "top-secret", name::Component("test-chronoshare"),
Yukai Tu931546f2016-10-24 13:48:01 -070054 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 Wang8e56d082016-12-25 14:45:23 -080064 server = make_unique<ContentServer>(face, actionLog, root, deviceName, shareFolderName,
65 name::Component("test-chronoshare"), m_keyChain, time::seconds(5));
Yukai Tu931546f2016-10-24 13:48:01 -070066
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
100public:
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
114BOOST_FIXTURE_TEST_SUITE(TestContentServer, TestContentServerFixture)
115
116BOOST_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
145BOOST_AUTO_TEST_SUITE_END()
146
147} // namespace tests
148} // namespace chronoshare
149} // namespace ndn