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