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