Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | INIT_LOGGERS(); |
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 | Name localName("/alex"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 45 | fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%"); |
| 46 | SyncLogPtr syncLog = make_shared<SyncLog>(tmpdir, localName); |
| 47 | CcnxWrapperPtr ccnx = make_shared<CcnxWrapper>(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 49 | ActionLogPtr actionLog = |
| 50 | make_shared<ActionLog>(ccnx, tmpdir, syncLog, "top-secret", "test-chronoshare", |
| 51 | ActionLog::OnFileAddedOrChangedCallback(), |
| 52 | ActionLog::OnFileRemovedCallback()); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | // const std::string &filename, |
| 55 | // const Hash &hash, |
| 56 | // time_t wtime, |
| 57 | // int mode, |
| 58 | // int seg_num |
| 59 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 62 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | actionLog->AddLocalActionUpdate("file.txt", |
| 65 | *Hash::FromString( |
| 66 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 67 | time(NULL), 0755, 10); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 1); |
| 70 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 0); |
| 71 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 1); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | HashPtr hash = syncLog->RememberStateInStateLog(); |
| 74 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
| 75 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 76 | "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | PcoPtr pco = actionLog->LookupActionPco(localName, 0); |
| 79 | BOOST_CHECK_EQUAL((bool)pco, false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | pco = actionLog->LookupActionPco(localName, 1); |
| 82 | BOOST_CHECK_EQUAL((bool)pco, true); |
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 | 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] | 85 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 86 | ActionItemPtr action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(0)); |
| 87 | BOOST_CHECK_EQUAL((bool)action, false); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | action = actionLog->LookupAction(Name("/alex/test-chronoshare/action/top-secret")(1)); |
| 90 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 92 | if (action) { |
| 93 | BOOST_CHECK_EQUAL(action->version(), 0); |
| 94 | BOOST_CHECK_EQUAL(action->action(), 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(action->filename(), "file.txt"); |
| 97 | BOOST_CHECK_EQUAL(action->seg_num(), 10); |
| 98 | BOOST_CHECK_EQUAL(action->file_hash().size(), 32); |
| 99 | BOOST_CHECK_EQUAL(action->mode(), 0755); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), false); |
| 102 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), false); |
| 103 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | actionLog->AddLocalActionUpdate("file.txt", |
| 106 | *Hash::FromString( |
| 107 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"), |
| 108 | time(NULL), 0755, 10); |
| 109 | BOOST_CHECK_EQUAL(syncLog->SeqNo(localName), 2); |
| 110 | BOOST_CHECK_EQUAL(syncLog->LogSize(), 1); |
| 111 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 113 | action = actionLog->LookupAction(Name("/alex"), 2); |
| 114 | BOOST_CHECK_EQUAL((bool)action, true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 116 | if (action) { |
| 117 | BOOST_CHECK_EQUAL(action->has_parent_device_name(), true); |
| 118 | BOOST_CHECK_EQUAL(action->has_parent_seq_no(), true); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(action->parent_seq_no(), 1); |
| 121 | BOOST_CHECK_EQUAL(action->version(), 1); |
| 122 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true); |
| 125 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 2); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 126 | |
| 127 | // create a real remote action |
| 128 | ActionItem item; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 129 | item.set_action(ActionItem::UPDATE); |
| 130 | item.set_filename("file.txt"); |
| 131 | item.set_version(2); |
| 132 | item.set_timestamp(time(NULL)); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 134 | BytesPtr item_msg = serializeMsg(item); |
| 135 | Name actionName = Name("/")(Name("/zhenkai/test"))("test-chronoshare")("action")("top-secret")(1); |
| 136 | Bytes actionData = ccnx->createContentObject(actionName, head(*item_msg), item_msg->size()); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 138 | pco = make_shared<ParsedContentObject>(actionData); |
| 139 | BOOST_CHECK_EQUAL((bool)actionLog->AddRemoteAction(pco), true); |
| 140 | BOOST_CHECK_EQUAL(actionLog->LogSize(), 3); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 142 | remove_all(tmpdir); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() |
| 146 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 147 | // catch (boost::exception &err) |
| 148 | // { |
| 149 | // cout << *boost::get_error_info<errmsg_info_str> (err) << endl; |
| 150 | // } |