blob: d0c47f5fc7f700c0e4f89d2a423ccec5e6bd928a [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yukai Tuccdcdba2016-10-24 13:48:01 -07003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08004 *
5 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
6 *
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.
10 *
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.
14 *
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.
19 */
Yukai Tuccdcdba2016-10-24 13:48:01 -070020
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080021#include "sync-core.hpp"
Alexander Afanasyevabe952a2013-01-17 17:06:32 -080022
Yukai Tuccdcdba2016-10-24 13:48:01 -070023#include "test-common.hpp"
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080024#include "dummy-forwarder.hpp"
Zhenkai Zhue29616f2013-01-14 15:40:57 -080025
Yukai Tuccdcdba2016-10-24 13:48:01 -070026namespace ndn {
27namespace chronoshare {
28namespace tests {
Zhenkai Zhue29616f2013-01-14 15:40:57 -080029
Yukai Tuccdcdba2016-10-24 13:48:01 -070030namespace fs = boost::filesystem;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080031
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080032_LOG_INIT(Test.SyncCore);
Yukai Tuccdcdba2016-10-24 13:48:01 -070033
34class TestSyncCoreFixture : public IdentityManagementTimeFixture
35{
36public:
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080037 TestSyncCoreFixture()
38 : forwarder(m_io, m_keyChain)
Yukai Tuccdcdba2016-10-24 13:48:01 -070039 {
Yukai Tuccdcdba2016-10-24 13:48:01 -070040 }
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080041
42public:
43 DummyForwarder forwarder;
Yukai Tuccdcdba2016-10-24 13:48:01 -070044};
45
46BOOST_FIXTURE_TEST_SUITE(TestSyncCore, TestSyncCoreFixture)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080047
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048void
49callback(const SyncStateMsgPtr& msg)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080050{
Yukai Tuccdcdba2016-10-24 13:48:01 -070051 _LOG_DEBUG("Callback I'm called!!!!");
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080052 BOOST_CHECK(msg->state_size() > 0);
Zhenkai Zhu085aae72013-01-17 21:09:01 -080053 int size = msg->state_size();
54 int index = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080055 while (index < size) {
Zhenkai Zhu085aae72013-01-17 21:09:01 -080056 SyncState state = msg->state(index);
57 BOOST_CHECK(state.has_old_seq());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 if (state.seq() != 0) {
Alexander Afanasyevdac84922013-01-20 23:32:17 -080059 BOOST_CHECK(state.old_seq() != state.seq());
Zhenkai Zhu085aae72013-01-17 21:09:01 -080060 }
61 index++;
62 }
Zhenkai Zhue29616f2013-01-14 15:40:57 -080063}
64
Yukai Tuccdcdba2016-10-24 13:48:01 -070065BOOST_AUTO_TEST_CASE(TwoNodes)
Zhenkai Zhue29616f2013-01-14 15:40:57 -080066{
Yukai Tuccdcdba2016-10-24 13:48:01 -070067 fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "SyncCoreTest";
68 if (exists(tmpdir)) {
69 remove_all(tmpdir);
Zhenkai Zhu4e1c1d92013-01-17 14:12:46 -080070 }
71
Yukai Tuccdcdba2016-10-24 13:48:01 -070072 std::string dir1 = (tmpdir / "1").string();
73 std::string dir2 = (tmpdir / "2").string();
74 Name user1("/shuai");
75 Name loc1("/locator1");
76 Name user2("/loli");
77 Name loc2("/locator2");
78 Name syncPrefix("/broadcast/arslan");
Zhenkai Zhub330aed2013-01-17 13:29:37 -080079
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080080 Face& c1 = forwarder.addFace();
Yukai Tuccdcdba2016-10-24 13:48:01 -070081 auto log1 = make_shared<SyncLog>(dir1, user1);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080082 auto core1 = make_shared<SyncCore>(c1, log1, user1, loc1, syncPrefix, bind(&callback, _1));
Alexander Afanasyevfc720362013-01-24 21:49:48 -080083
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080084 Face& c2 = forwarder.addFace();
Yukai Tuccdcdba2016-10-24 13:48:01 -070085 auto log2 = make_shared<SyncLog>(dir2, user2);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080086 auto core2 = make_shared<SyncCore>(c2, log2, user2, loc2, syncPrefix, bind(&callback, _1));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080087
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080088 advanceClocks(time::milliseconds(10), 100);
Yukai Tuccdcdba2016-10-24 13:48:01 -070089
90 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
91
Zhenkai Zhue29616f2013-01-14 15:40:57 -080092 core1->updateLocalState(1);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080093 advanceClocks(time::milliseconds(10), 10);
Yukai Tuccdcdba2016-10-24 13:48:01 -070094 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080095 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080097
98 core1->updateLocalState(5);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080099 advanceClocks(time::milliseconds(10), 10);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700100 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800101 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800102 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800103
104 core2->updateLocalState(10);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -0800105 advanceClocks(time::milliseconds(10), 10);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700106 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800107 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800108 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800109
110 // simple simultaneous data generation
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800111
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800112 core1->updateLocalState(11);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -0800113 advanceClocks(time::milliseconds(10), 10);
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800114 core2->updateLocalState(15);
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -0800115 advanceClocks(time::milliseconds(10), 10);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700116 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800117 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
118 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800119
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 BOOST_CHECK_EQUAL(log1->LookupLocator(user1), loc1);
121 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
122 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
123 BOOST_CHECK_EQUAL(log2->LookupLocator(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800124}
125
126BOOST_AUTO_TEST_SUITE_END()
Yukai Tuccdcdba2016-10-24 13:48:01 -0700127
128} // namespace tests
129} // namespace chronoshare
130} // namespace ndn