blob: 9e9ebb0c01c4bdb74bdab71cd6cb4290f1fcd78e [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
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080021#include <boost/lexical_cast.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include <boost/test/unit_test.hpp>
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080023
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "action-log.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include "logging.hpp"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080026#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include <ccnx-name.hpp>
28#include <iostream>
29#include <unistd.h>
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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039BOOST_AUTO_TEST_CASE(BasicDatabaseTest)
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080040{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041 INIT_LOGGERS();
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080042
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 fs::path tmpdir = fs::unique_path(fs::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%");
44 SyncLog db(tmpdir, Name("/alex"));
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080045
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080046 HashPtr hash = db.RememberStateInStateLog();
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080047 // should be empty
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
49 "7a6f2c1eefd539560d2dc3e5542868a79810d0867db15d9b87e41ec105899405");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080050
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080051 db.UpdateDeviceSeqNo(Name("/alex"), 1);
52 hash = db.RememberStateInStateLog();
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080053
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
55 "3410477233f98d6c3f9a6f8da24494bf5a65e1a7c9f4f66b228128bd4e020558");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080056
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 db.UpdateDeviceSeqNo(Name("/alex"), 2);
58 hash = db.RememberStateInStateLog();
59 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
60 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080061
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 db.UpdateDeviceSeqNo(Name("/alex"), 2);
63 hash = db.RememberStateInStateLog();
64 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
65 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080066
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 db.UpdateDeviceSeqNo(Name("/alex"), 1);
68 hash = db.RememberStateInStateLog();
69 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
70 "2ff304769cdb0125ac039e6fe7575f8576dceffc62618a431715aaf6eea2bf1c");
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080071
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080072 db.UpdateLocator(Name("/alex"), Name("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080073
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 BOOST_CHECK_EQUAL(db.LookupLocator(Name("/alex")), Name("/hawaii"));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080075
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 SyncStateMsgPtr msg =
77 db.FindStateDifferences("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
78 BOOST_CHECK_EQUAL(msg->state_size(), 0);
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -080079
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080 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 Afanasyev8e2104a2013-01-22 10:56:18 -080085
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 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 Afanasyevee7e6132013-01-03 20:03:14 -080090
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 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 Afanasyevee7e6132013-01-03 20:03:14 -080096
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 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 Afanasyev8e2104a2013-01-22 10:56:18 -0800102
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 db.UpdateDeviceSeqNo(Name("/bob"), 1);
104 hash = db.RememberStateInStateLog();
105 BOOST_CHECK_EQUAL(lexical_cast<string>(*hash),
106 "5df5affc07120335089525e82ec9fda60c6dccd7addb667106fb79de80610519");
Alexander Afanasyev5ec5a8f2013-01-17 17:30:33 -0800107
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800108 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 Afanasyev5ec5a8f2013-01-17 17:30:33 -0800113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 BOOST_CHECK_EQUAL(msg->state(1).type(), SyncState::UPDATE);
115 BOOST_CHECK_EQUAL(msg->state(1).seq(), 1);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800116
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800117 remove_all(tmpdir);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800118}
119
120BOOST_AUTO_TEST_SUITE_END()