blob: cfb897170d30a4943aa9ef95ac7ae19de56c43a0 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyevee7e6132013-01-03 20:03:14 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyevee7e6132013-01-03 20:03:14 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyevee7e6132013-01-03 20:03:14 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyevee7e6132013-01-03 20:03:14 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyevee7e6132013-01-03 20:03:14 -080019 */
20
21#include <boost/test/unit_test.hpp>
22#include <boost/lexical_cast.hpp>
23
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "logging.hpp"
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080025#include <unistd.h>
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "action-log.hpp"
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080027#include <iostream>
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070028#include <ndnx-name.h>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029#include <boost/filesystem.hpp>
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080030
31using namespace std;
32using namespace boost;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070033using namespace Ndnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080034namespace fs = boost::filesystem;
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080035
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080036BOOST_AUTO_TEST_SUITE(TestSyncLog)
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080037
38
39BOOST_AUTO_TEST_CASE (BasicDatabaseTest)
40{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080041 INIT_LOGGERS ();
42
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080043 fs::path tmpdir = fs::unique_path (fs::temp_directory_path () / "%%%%-%%%%-%%%%-%%%%");
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080044 SyncLog db (tmpdir, Name ("/alex"));
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080045
46 HashPtr hash = db.RememberStateInStateLog ();
47 // should be empty
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080048 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080049
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080050 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080051 hash = db.RememberStateInStateLog ();
52
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080053 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080054
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080055 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080056 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080057 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080058
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080059 db.UpdateDeviceSeqNo (Name ("/alex"), 2);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080060 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080061 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080062
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080063 db.UpdateDeviceSeqNo (Name ("/alex"), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080064 hash = db.RememberStateInStateLog ();
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080065 BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080066
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080067 db.UpdateLocator(Name ("/alex"), Name("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080068
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080069 BOOST_CHECK_EQUAL(db.LookupLocator(Name ("/alex")), Name ("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080070
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080071 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 Afanasyev8e2104a2013-01-22 10:56:18 -080078
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080079 msg = db.FindStateDifferences ("2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c", "00");
80 BOOST_CHECK_EQUAL (msg->state_size(), 1);
81 BOOST_CHECK_EQUAL (msg->state (0).type (), SyncState::DELETE);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080082
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080083 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 Afanasyevee7e6132013-01-03 20:03:14 -080088
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080089 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 Afanasyev8e2104a2013-01-22 10:56:18 -080094
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080095 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 Afanasyevee7e6132013-01-03 20:03:14 -0800106
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800107 remove_all (tmpdir);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800108}
109
110BOOST_AUTO_TEST_SUITE_END()