blob: 5fdd5fa8203ad6dda0acd85c9865bb0e316086bf [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"
Zhenkai Zhue29616f2013-01-14 15:40:57 -080024
Yukai Tuccdcdba2016-10-24 13:48:01 -070025namespace ndn {
26namespace chronoshare {
27namespace tests {
Zhenkai Zhue29616f2013-01-14 15:40:57 -080028
Yukai Tuccdcdba2016-10-24 13:48:01 -070029namespace fs = boost::filesystem;
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080030
Yukai Tuccdcdba2016-10-24 13:48:01 -070031INIT_LOGGER("Test.SyncCore")
32
33class TestSyncCoreFixture : public IdentityManagementTimeFixture
34{
35public:
36 void
37 advanceClocks()
38 {
39 for (int i = 0; i < 100; ++i) {
40 usleep(10000);
41 IdentityManagementTimeFixture::advanceClocks(time::milliseconds(10), 1);
42 }
43 }
44};
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
Yukai Tuccdcdba2016-10-24 13:48:01 -070080 shared_ptr<Face> c1 = make_shared<Face>(this->m_io);
81 auto log1 = make_shared<SyncLog>(dir1, user1);
82 auto core1 = make_shared<SyncCore>(*c1, log1, user1, loc1, syncPrefix, bind(&callback, _1));
Alexander Afanasyevfc720362013-01-24 21:49:48 -080083
Yukai Tuccdcdba2016-10-24 13:48:01 -070084 shared_ptr<Face> c2 = make_shared<Face>(this->m_io);
85 auto log2 = make_shared<SyncLog>(dir2, user2);
86 auto core2 = make_shared<SyncCore>(*c2, log2, user2, loc2, syncPrefix, bind(&callback, _1));
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -080087
Yukai Tuccdcdba2016-10-24 13:48:01 -070088 // @TODO: Implement test using the dummy forwarder and disable dependency on real time
89
90 this->advanceClocks();
91
92 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
93
94 // _LOG_TRACE("\n\n\n\n\n\n----------\n");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -080095
Zhenkai Zhue29616f2013-01-14 15:40:57 -080096 core1->updateLocalState(1);
Yukai Tuccdcdba2016-10-24 13:48:01 -070097 this->advanceClocks();
98 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080099 BOOST_CHECK_EQUAL(core2->seq(user1), 1);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800101
102 core1->updateLocalState(5);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700103 this->advanceClocks();
104 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800105 BOOST_CHECK_EQUAL(core2->seq(user1), 5);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800106 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800107
108 core2->updateLocalState(10);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700109 this->advanceClocks();
110 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800111 BOOST_CHECK_EQUAL(core1->seq(user2), 10);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800113
114 // simple simultaneous data generation
Yukai Tuccdcdba2016-10-24 13:48:01 -0700115 _LOG_TRACE("\n\n\n\n\n\n----------Simultaneous\n");
Alexander Afanasyevc507ac22013-01-21 16:01:58 -0800116
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800117 core1->updateLocalState(11);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700118 this->advanceClocks();
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800119 core2->updateLocalState(15);
Yukai Tuccdcdba2016-10-24 13:48:01 -0700120 this->advanceClocks();
121 BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root()));
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -0800122 BOOST_CHECK_EQUAL(core1->seq(user2), 15);
123 BOOST_CHECK_EQUAL(core2->seq(user1), 11);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800124
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 BOOST_CHECK_EQUAL(log1->LookupLocator(user1), loc1);
126 BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2);
127 BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1);
128 BOOST_CHECK_EQUAL(log2->LookupLocator(user2), loc2);
Zhenkai Zhue29616f2013-01-14 15:40:57 -0800129}
130
131BOOST_AUTO_TEST_SUITE_END()
Yukai Tuccdcdba2016-10-24 13:48:01 -0700132
133} // namespace tests
134} // namespace chronoshare
135} // namespace ndn