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 | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 21 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "action-log.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include "logging.hpp" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 27 | #include <boost/filesystem.hpp> |
| 28 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <unistd.h> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 34 | using namespace Ndnx; |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(TestActionLog) |
| 38 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(ActionLogTest) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | Name localName("/alex"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%"); |
| 44 | SyncLogPtr syncLog = make_shared<SyncLog>(tmpdir, localName); |
| 45 | CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | ActionLogPtr actionLog = |
| 48 | make_shared<ActionLog>(ccnx, tmpdir, syncLog, "top-secret", "test-chronoshare", |
| 49 | ActionLog::OnFileAddedOrChangedCallback(), |
| 50 | ActionLog::OnFileRemovedCallback()); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 52 | // const std::string &filename, |
| 53 | // const Hash &hash, |
| 54 | // time_t wtime, |
| 55 | // int mode, |
| 56 | // int seg_num |
| 57 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 60 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | actionLog->AddLocalActionUpdate("file.txt", |
| 63 | *Hash::FromString( |
| 64 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 65 | time(NULL), 0755, 10); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1); |
| 68 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 69 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 1); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | HashPtr hash = syncLog->RememberStateInStateLog(); |
| 72 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
| 73 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 74 | "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | PcoPtr pco = actionLog->LookupActionPco(localName, 0); |
| 77 | BOOST_CHECK_EQUAL((bool)pco, false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | pco = actionLog->LookupActionPco(localName, 1); |
| 80 | BOOST_CHECK_EQUAL((bool)pco, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(pco->name(), "/alex/test-chronoshare/action/top-secret/%00%01"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 84 | ActionItemPtr action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(0)); |
| 85 | BOOST_CHECK_EQUAL((bool)action, false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(1)); |
| 88 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 90 | if (action) { |
| 91 | BOOST_CHECK_EQUAL(action->version(), 0); |
| 92 | BOOST_CHECK_EQUAL(action->action(), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(action->filename(), "file.txt"); |
| 95 | BOOST_CHECK_EQUAL(action->seg_num(), 10); |
| 96 | BOOST_CHECK_EQUAL(action->file_hash().size(), 32); |
| 97 | BOOST_CHECK_EQUAL(action->mode(), 0755); |
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 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), false); |
| 100 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false); |
| 101 | } |
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 | actionLog->AddLocalActionUpdate("file.txt", |
| 104 | *Hash::FromString( |
| 105 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 106 | time(NULL), 0755, 10); |
| 107 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2); |
| 108 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
| 109 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 111 | action = actionLog->LookupAction(Name("/alex"), 2); |
| 112 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | if (action) { |
| 115 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), true); |
| 116 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(action->parent_seq_no(), 1); |
| 119 | BOOST_CHECK_EQUAL(action->version(), 1); |
| 120 | } |
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 | BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true); |
| 123 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 124 | |
| 125 | // create a real remote action |
| 126 | ActionItem item; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 127 | item.set_action(ActionItem::UPDATE); |
| 128 | item.set_filename("file.txt"); |
| 129 | item.set_version(2); |
| 130 | item.set_timestamp(time(NULL)); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 132 | BytesPtr item_msg = serializeMsg(item); |
| 133 | Name actionName = Name("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1); |
| 134 | Bytes actionData = ccnx->createContentObject(actionName, head(*item_msg), item_msg->size()); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 136 | pco = make_shared<ParsedContentObject>(actionData); |
| 137 | BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true); |
| 138 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 3); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 140 | remove_all(tmpdir); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_SUITE_END() |
| 144 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 145 | // catch (boost::exception &err) |
| 146 | // { |
| 147 | // cout << *boost::get_error_info<errmsg_info_str> (err) << endl; |
| 148 | // } |