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 | ee7e613 | 2013-01-03 20:03:14 -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 | ee7e613 | 2013-01-03 20:03:14 -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 | ee7e613 | 2013-01-03 20:03:14 -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 | ee7e613 | 2013-01-03 20:03:14 -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 | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -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 | ee7e613 | 2013-01-03 20:03:14 -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 | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 26 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <ccnx-name.hpp> |
| 28 | #include <iostream> |
| 29 | #include <unistd.h> |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | using namespace boost; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 33 | using namespace Ndnx; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 34 | namespace fs = boost::filesystem; |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(TestSyncLog) |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 37 | |
| 38 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(BasicDatabaseTest) |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | INIT_LOGGERS(); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -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 | SyncLog db(tmpdir, Name("/alex")); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | HashPtr hash = db.RememberStateInStateLog(); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 47 | // should be empty |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 49 | "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 51 | db.UpdateDeviceSeqNo(Name("/alex"), 1); |
| 52 | hash = db.RememberStateInStateLog(); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 55 | "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | db.UpdateDeviceSeqNo(Name("/alex"), 2); |
| 58 | hash = db.RememberStateInStateLog(); |
| 59 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 60 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | db.UpdateDeviceSeqNo(Name("/alex"), 2); |
| 63 | hash = db.RememberStateInStateLog(); |
| 64 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 65 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | db.UpdateDeviceSeqNo(Name("/alex"), 1); |
| 68 | hash = db.RememberStateInStateLog(); |
| 69 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 70 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | db.UpdateLocator(Name("/alex"), Name("/hawaii")); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(db.LookupLocator(Name("/alex")), Name("/hawaii")); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | SyncStateMsgPtr msg = |
| 77 | db.FindStateDifferences("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833"); |
| 78 | BOOST_CHECK_EQUAL(msg->state_size(), 0); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | msg = db.FindStateDifferences("00", |
| 81 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
| 82 | BOOST_CHECK_EQUAL(msg->state_size(), 1); |
| 83 | BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE); |
| 84 | BOOST_CHECK_EQUAL(msg->state(0).seq(), 2); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 85 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 86 | msg = db.FindStateDifferences("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", |
| 87 | "00"); |
| 88 | BOOST_CHECK_EQUAL(msg->state_size(), 1); |
| 89 | BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::DELETE); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 91 | msg = db.FindStateDifferences("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405", |
| 92 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
| 93 | BOOST_CHECK_EQUAL(msg->state_size(), 1); |
| 94 | BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE); |
| 95 | BOOST_CHECK_EQUAL(msg->state(0).seq(), 2); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 97 | msg = db.FindStateDifferences("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", |
| 98 | "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405"); |
| 99 | BOOST_CHECK_EQUAL(msg->state_size(), 1); |
| 100 | BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE); |
| 101 | BOOST_CHECK_EQUAL(msg->state(0).seq(), 0); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | db.UpdateDeviceSeqNo(Name("/bob"), 1); |
| 104 | hash = db.RememberStateInStateLog(); |
| 105 | BOOST_CHECK_EQUAL(lexical_cast<string>(*hash), |
| 106 | "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519"); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 108 | msg = db.FindStateDifferences("00", |
| 109 | "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519"); |
| 110 | BOOST_CHECK_EQUAL(msg->state_size(), 2); |
| 111 | BOOST_CHECK_EQUAL(msg->state(0).type(), SyncState::UPDATE); |
| 112 | BOOST_CHECK_EQUAL(msg->state(0).seq(), 2); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(msg->state(1).type(), SyncState::UPDATE); |
| 115 | BOOST_CHECK_EQUAL(msg->state(1).seq(), 1); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 117 | remove_all(tmpdir); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_SUITE_END() |