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