Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include <boost/test/unit_test.hpp> |
| 23 | #include <boost/lexical_cast.hpp> |
| 24 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 25 | #include "logging.h" |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include "action-log.h" |
| 28 | #include <iostream> |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 29 | #include <ccnx-name.h> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 34 | using namespace Ccnx; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestSyncLog) |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE (BasicDatabaseTest) |
| 41 | { |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 42 | INIT_LOGGERS (); |
| 43 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 44 | fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%"); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 45 | SyncLog db (tmpdir, Name ("/alex")); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 46 | |
| 47 | HashPtr hash = db.RememberStateInStateLog (); |
| 48 | // should be empty |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 51 | db.UpdateDeviceSeqNo (Name ("/alex"), 1); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 52 | hash = db.RememberStateInStateLog (); |
| 53 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 56 | db.UpdateDeviceSeqNo (Name ("/alex"), 2); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 57 | hash = db.RememberStateInStateLog (); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 60 | db.UpdateDeviceSeqNo (Name ("/alex"), 2); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 61 | hash = db.RememberStateInStateLog (); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 64 | db.UpdateDeviceSeqNo (Name ("/alex"), 1); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 65 | hash = db.RememberStateInStateLog (); |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 68 | db.UpdateLocator(Name ("/alex"), Name("/hawaii")); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(db.LookupLocator(Name ("/alex")), Name ("/hawaii")); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 72 | SyncStateMsgPtr msg = db.FindStateDifferences ("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833"); |
| 73 | BOOST_CHECK_EQUAL (msg->state_size(), 0); |
| 74 | |
| 75 | msg = db.FindStateDifferences ("00", "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
| 76 | BOOST_CHECK_EQUAL (msg->state_size(), 1); |
| 77 | BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE); |
| 78 | BOOST_CHECK_EQUAL (msg->state (0).seq (), 2); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 80 | msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", "00"); |
| 81 | BOOST_CHECK_EQUAL (msg->state_size(), 1); |
| 82 | BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::DELETE); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 84 | msg = db.FindStateDifferences ("7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405", |
| 85 | "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c"); |
| 86 | BOOST_CHECK_EQUAL (msg->state_size(), 1); |
| 87 | BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE); |
| 88 | BOOST_CHECK_EQUAL (msg->state (0).seq (), 2); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 90 | msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", |
| 91 | "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405"); |
| 92 | BOOST_CHECK_EQUAL (msg->state_size(), 1); |
| 93 | BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE); |
| 94 | BOOST_CHECK_EQUAL (msg->state (0).seq (), 0); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 5ec5a8f | 2013-01-17 17:30:33 -0800 | [diff] [blame] | 96 | db.UpdateDeviceSeqNo (Name ("/bob"), 1); |
| 97 | hash = db.RememberStateInStateLog (); |
| 98 | BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519"); |
| 99 | |
| 100 | msg = db.FindStateDifferences ("00", "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519"); |
| 101 | BOOST_CHECK_EQUAL (msg->state_size(), 2); |
| 102 | BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::UPDATE); |
| 103 | BOOST_CHECK_EQUAL (msg->state (0).seq (), 2); |
| 104 | |
| 105 | BOOST_CHECK_EQUAL (msg->state (1).type (), SyncState::UPDATE); |
| 106 | BOOST_CHECK_EQUAL (msg->state (1).seq (), 1); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 108 | remove_all (tmpdir); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_SUITE_END() |