Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "action-log.hpp" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 22 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 23 | #include "test-common.hpp" |
| 24 | #include "dummy-forwarder.hpp" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 25 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace chronoshare { |
| 28 | namespace tests { |
| 29 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 30 | namespace fs = boost::filesystem; |
| 31 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 32 | class TestActionLogFixture : public IdentityManagementTimeFixture |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 33 | { |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 34 | public: |
| 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 Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 43 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 44 | syncLog = make_shared<SyncLog>(tmpdir, localName); |
| 45 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 46 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 47 | public: |
| 48 | DummyForwarder forwarder; |
| 49 | Name localName; |
| 50 | fs::path tmpdir; |
| 51 | shared_ptr<SyncLog> syncLog; |
| 52 | }; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 53 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 54 | BOOST_FIXTURE_TEST_SUITE(TestActionLog, TestActionLogFixture) |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(UpdateAction) |
| 57 | { |
| 58 | auto actionLog = std::make_shared<ActionLog>(forwarder.addFace(), tmpdir, syncLog, |
| 59 | "top-secret", "test-chronoshare", |
| 60 | ActionLog::OnFileAddedOrChangedCallback(), |
| 61 | ActionLog::OnFileRemovedCallback()); |
| 62 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 66 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | actionLog->AddLocalActionUpdate("file.txt", |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 69 | *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 70 | std::time(nullptr), 0755, 10); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1); |
| 73 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 74 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 1); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 75 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 76 | ndn::ConstBufferPtr hash = syncLog->RememberStateInStateLog(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(toHex(*hash), |
| 79 | "91A849EEDE75ACD56AE1BCB99E92D8FB28757683BC387DBB0E59C3108FCF4F18"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 80 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 81 | ndn::shared_ptr<ndn::Data> data = actionLog->LookupActionData(localName, 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 82 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(bool(data), false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 84 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 85 | data = actionLog->LookupActionData(localName, 1); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 86 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 87 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL((bool)action, false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 94 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 95 | action = |
| 96 | actionLog->LookupAction(Name("/lijing/test-chronoshare/action/top-secret").appendNumber(1)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | if (action) { |
| 100 | BOOST_CHECK_EQUAL(action->version(), 0); |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(action->action(), ActionItem::UPDATE); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | 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 Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), false); |
| 109 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false); |
| 110 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | actionLog->AddLocalActionUpdate("file.txt", |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 113 | *fromHex("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 114 | std::time(nullptr), 0755, 10); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2); |
| 116 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
| 117 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 118 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 119 | action = actionLog->LookupAction(Name("/lijing"), 2); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 122 | if (action) { |
| 123 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), true); |
| 124 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(action->parent_seq_no(), 1); |
| 127 | BOOST_CHECK_EQUAL(action->version(), 1); |
| 128 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 129 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(data), true); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 132 | |
| 133 | // create a real remote action |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 134 | 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 Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 139 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 140 | std::string item_msg; |
| 141 | item->SerializeToString(&item_msg); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 142 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 143 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 152 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 3); |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 154 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(DeleteAction) |
| 156 | { |
| 157 | bool hasDeleteCallbackCalled = false; |
| 158 | auto actionLog = std::make_shared<ActionLog>(forwarder.addFace(), tmpdir, syncLog, |
| 159 | "top-secret", "test-chronoshare", |
| 160 | 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 Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | BOOST_AUTO_TEST_SUITE_END() |
| 243 | |
Yukai Tu | 09e02fd | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 244 | } // namespace tests |
| 245 | } // namespace chronoshare |
| 246 | } // namespace ndn |