blob: deaff17a4fa41e325df8fbad6a1392ea3cfb3e8f [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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.
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "action-log.hpp"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080022
Yukai Tu09e02fd2016-10-24 13:48:01 -070023#include "test-common.hpp"
24#include "dummy-forwarder.hpp"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080025
Yukai Tu09e02fd2016-10-24 13:48:01 -070026namespace ndn {
27namespace chronoshare {
28namespace tests {
29
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080030namespace fs = boost::filesystem;
31
Yukai Tu09e02fd2016-10-24 13:48:01 -070032class TestActionLogFixture : public IdentityManagementTimeFixture
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080033{
Yukai Tu09e02fd2016-10-24 13:48:01 -070034public:
35 TestActionLogFixture()
36 : forwarder(m_io, m_keyChain)
37 , localName("/lijing")
38 , tmpdir(fs::unique_path(UNIT_TEST_CONFIG_PATH))
39 {
40 if (exists(tmpdir)) {
41 remove_all(tmpdir);
42 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080043
Yukai Tu09e02fd2016-10-24 13:48:01 -070044 syncLog = make_shared<SyncLog>(tmpdir, localName);
45 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080046
Yukai Tu09e02fd2016-10-24 13:48:01 -070047public:
48 DummyForwarder forwarder;
49 Name localName;
50 fs::path tmpdir;
51 shared_ptr<SyncLog> syncLog;
52};
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080053
Yukai Tu09e02fd2016-10-24 13:48:01 -070054BOOST_FIXTURE_TEST_SUITE(TestActionLog, TestActionLogFixture)
55
56BOOST_AUTO_TEST_CASE(UpdateAction)
57{
58 auto actionLog = std::make_shared<ActionLog>(forwarder.addFace(), tmpdir, syncLog,
Lijing Wang8e56d082016-12-25 14:45:23 -080059 "top-secret", name::Component("test-chronoshare"),
Yukai Tu09e02fd2016-10-24 13:48:01 -070060 ActionLog::OnFileAddedOrChangedCallback(),
61 ActionLog::OnFileRemovedCallback());
62
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080064
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
66 BOOST_CHECK_EQUAL(actionLog->LogSize(), 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080067
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 actionLog->AddLocalActionUpdate("file.txt",
Yukai Tu09e02fd2016-10-24 13:48:01 -070069 *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
70 std::time(nullptr), 0755, 10);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080071
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1);
73 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
74 BOOST_CHECK_EQUAL(actionLog->LogSize(), 1);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080075
Yukai Tu09e02fd2016-10-24 13:48:01 -070076 ndn::ConstBufferPtr hash = syncLog->RememberStateInStateLog();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
Yukai Tu09e02fd2016-10-24 13:48:01 -070078 BOOST_CHECK_EQUAL(toHex(*hash),
79 "91A849EEDE75ACD56AE1BCB99E92D8FB28757683BC387DBB0E59C3108FCF4F18");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080080
Yukai Tu09e02fd2016-10-24 13:48:01 -070081 ndn::shared_ptr<ndn::Data> data = actionLog->LookupActionData(localName, 0);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080082
Yukai Tu09e02fd2016-10-24 13:48:01 -070083 BOOST_CHECK_EQUAL(bool(data), false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080084
Yukai Tu09e02fd2016-10-24 13:48:01 -070085 data = actionLog->LookupActionData(localName, 1);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080086
Yukai Tu09e02fd2016-10-24 13:48:01 -070087 BOOST_CHECK_EQUAL(bool(data), true);
88
89 BOOST_CHECK_EQUAL(data->getName(), "/lijing/test-chronoshare/action/top-secret/%01");
90
91 ActionItemPtr action =
92 actionLog->LookupAction(Name("/lijing/test-chronoshare/action/top-secret").appendNumber(0));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 BOOST_CHECK_EQUAL((bool)action, false);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080094
Yukai Tu09e02fd2016-10-24 13:48:01 -070095 action =
96 actionLog->LookupAction(Name("/lijing/test-chronoshare/action/top-secret").appendNumber(1));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080098
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080099 if (action) {
100 BOOST_CHECK_EQUAL(action->version(), 0);
Yukai Tu09e02fd2016-10-24 13:48:01 -0700101 BOOST_CHECK_EQUAL(action->action(), ActionItem::UPDATE);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800102
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 BOOST_CHECK_EQUAL(action->filename(), "file.txt");
104 BOOST_CHECK_EQUAL(action->seg_num(), 10);
105 BOOST_CHECK_EQUAL(action->file_hash().size(), 32);
106 BOOST_CHECK_EQUAL(action->mode(), 0755);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800107
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800108 BOOST_CHECK_EQUAL(action->has_parent_device_name(), false);
109 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false);
110 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800111
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 actionLog->AddLocalActionUpdate("file.txt",
Yukai Tu09e02fd2016-10-24 13:48:01 -0700113 *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
114 std::time(nullptr), 0755, 10);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2);
116 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
117 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800118
Yukai Tu09e02fd2016-10-24 13:48:01 -0700119 action = actionLog->LookupAction(Name("/lijing"), 2);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 BOOST_CHECK_EQUAL((bool)action, true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800121
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 if (action) {
123 BOOST_CHECK_EQUAL(action->has_parent_device_name(), true);
124 BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800125
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 BOOST_CHECK_EQUAL(action->parent_seq_no(), 1);
127 BOOST_CHECK_EQUAL(action->version(), 1);
128 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800129
Yukai Tu09e02fd2016-10-24 13:48:01 -0700130 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(data), true);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800132
133 // create a real remote action
Yukai Tu09e02fd2016-10-24 13:48:01 -0700134 ActionItemPtr item = make_shared<ActionItem>();
135 item->set_action(ActionItem::UPDATE);
136 item->set_filename("file.txt");
137 item->set_version(2);
138 item->set_timestamp(std::time(nullptr));
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800139
Yukai Tu09e02fd2016-10-24 13:48:01 -0700140 std::string item_msg;
141 item->SerializeToString(&item_msg);
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800142
Yukai Tu09e02fd2016-10-24 13:48:01 -0700143 Name actionName = Name("/zhenkai/test/test-chronoshare/action/top-secret").appendNumber(1);
144
145 ndn::shared_ptr<Data> actionData = ndn::make_shared<Data>();
146 actionData->setName(actionName);
147 actionData->setContent(reinterpret_cast<const uint8_t*>(item_msg.c_str()), item_msg.size());
148 ndn::KeyChain m_keyChain;
149 m_keyChain.sign(*actionData);
150
151 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(actionData), true);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 BOOST_CHECK_EQUAL(actionLog->LogSize(), 3);
Yukai Tu09e02fd2016-10-24 13:48:01 -0700153}
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800154
Yukai Tu09e02fd2016-10-24 13:48:01 -0700155BOOST_AUTO_TEST_CASE(DeleteAction)
156{
157 bool hasDeleteCallbackCalled = false;
158 auto actionLog = std::make_shared<ActionLog>(forwarder.addFace(), tmpdir, syncLog,
Lijing Wang8e56d082016-12-25 14:45:23 -0800159 "top-secret", name::Component("test-chronoshare"),
Yukai Tu09e02fd2016-10-24 13:48:01 -0700160 ActionLog::OnFileAddedOrChangedCallback(),
161 bind([&] { hasDeleteCallbackCalled = true; }));
162
163 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0);
164
165 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
166 BOOST_CHECK_EQUAL(actionLog->LogSize(), 0);
167
168 actionLog->AddLocalActionUpdate("file.txt",
169 *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"),
170 std::time(nullptr), 0755, 10);
171
172 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1);
173 BOOST_CHECK_EQUAL(syncLog->LogSize(), 0);
174 BOOST_CHECK_EQUAL(actionLog->LogSize(), 1);
175
176 syncLog->RememberStateInStateLog();
177
178 actionLog->AddLocalActionDelete("file.txt");
179 BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2);
180 BOOST_CHECK_EQUAL(syncLog->LogSize(), 1);
181 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
182 BOOST_CHECK(hasDeleteCallbackCalled);
183
184 ndn::ConstBufferPtr hash = syncLog->RememberStateInStateLog();
185 BOOST_CHECK_EQUAL(syncLog->LogSize(), 2);
186 BOOST_CHECK_EQUAL(toHex(*hash),
187 "D2DFEDA56ED98C0E17D455A859BC8C3B9E31C85C138C280A8BADAB4FC551F282");
188
189 ndn::shared_ptr<ndn::Data> data = actionLog->LookupActionData(localName, 2);
190 BOOST_CHECK(data != nullptr);
191
192 BOOST_CHECK_EQUAL(data->getName(), "/lijing/test-chronoshare/action/top-secret/%02");
193
194 ActionItemPtr action = actionLog->LookupAction(Name("/lijing/test-chronoshare/action/top-secret").appendNumber(2));
195 BOOST_CHECK(action != nullptr);
196
197 if (action) {
198 BOOST_CHECK_EQUAL(action->version(), 1);
199 BOOST_CHECK_EQUAL(action->action(), ActionItem::DELETE);
200
201 BOOST_CHECK_EQUAL(action->filename(), "file.txt");
202
203 BOOST_CHECK_EQUAL(Name(Block(action->parent_device_name().data(), action->parent_device_name().size())),
204 Name("/lijing"));
205 BOOST_CHECK_EQUAL(action->parent_seq_no(), 1);
206 }
207
208 BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(data), true);
209 BOOST_CHECK_EQUAL(actionLog->LogSize(), 2);
210
211 // create a real remote action
212 ActionItemPtr item = make_shared<ActionItem>();
213 item->set_action(ActionItem::DELETE);
214 item->set_filename("file.txt");
215 item->set_version(2);
216 item->set_timestamp(std::time(0));
217
218 std::string filename = "file.txt";
219 BufferPtr parent_device_name = std::make_shared<Buffer>(filename.c_str(), filename.size());
220 item->set_parent_device_name(parent_device_name->buf(), parent_device_name->size());
221 item->set_parent_seq_no(0);
222
223 std::string item_msg;
224 item->SerializeToString(&item_msg);
225
226 Name actionName = Name("/yukai/test/test-chronoshare/action/top-secret").appendNumber(1);
227
228 ndn::shared_ptr<Data> actionData = ndn::make_shared<Data>();
229 actionData->setName(actionName);
230 actionData->setContent(reinterpret_cast<const uint8_t*>(item_msg.c_str()), item_msg.size());
231 ndn::KeyChain m_keyChain;
232 m_keyChain.sign(*actionData);
233
234 ActionItemPtr actionItem = actionLog->AddRemoteAction(actionData);
235 BOOST_CHECK_EQUAL((bool)actionItem, true);
236 BOOST_CHECK_EQUAL(actionLog->LogSize(), 3);
237 BOOST_CHECK_EQUAL(syncLog->LogSize(), 2);
238
239 BOOST_CHECK_EQUAL(action->action(), ActionItem::DELETE);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800240}
241
242BOOST_AUTO_TEST_SUITE_END()
243
Yukai Tu09e02fd2016-10-24 13:48:01 -0700244} // namespace tests
245} // namespace chronoshare
246} // namespace ndn