Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 4 | * |
| 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 Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 21 | #include "sync-core.hpp" |
Alexander Afanasyev | abe952a | 2013-01-17 17:06:32 -0800 | [diff] [blame] | 22 | |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 23 | #include "test-common.hpp" |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 24 | #include "dummy-forwarder.hpp" |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 25 | |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 26 | namespace ndn { |
| 27 | namespace chronoshare { |
| 28 | namespace tests { |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 29 | |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 30 | namespace fs = boost::filesystem; |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 32 | _LOG_INIT(Test.SyncCore); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 33 | |
| 34 | class TestSyncCoreFixture : public IdentityManagementTimeFixture |
| 35 | { |
| 36 | public: |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 37 | TestSyncCoreFixture() |
| 38 | : forwarder(m_io, m_keyChain) |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 39 | { |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 40 | } |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 41 | |
| 42 | public: |
| 43 | DummyForwarder forwarder; |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | BOOST_FIXTURE_TEST_SUITE(TestSyncCore, TestSyncCoreFixture) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | void |
| 49 | callback(const SyncStateMsgPtr& msg) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 50 | { |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 51 | _LOG_DEBUG("Callback I'm called!!!!"); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 52 | BOOST_CHECK(msg->state_size() > 0); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 53 | int size = msg->state_size(); |
| 54 | int index = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 55 | while (index < size) { |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 56 | SyncState state = msg->state(index); |
| 57 | BOOST_CHECK(state.has_old_seq()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | if (state.seq() != 0) { |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 59 | BOOST_CHECK(state.old_seq() != state.seq()); |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 60 | } |
| 61 | index++; |
| 62 | } |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(TwoNodes) |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 66 | { |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 67 | fs::path tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "SyncCoreTest"; |
| 68 | if (exists(tmpdir)) { |
| 69 | remove_all(tmpdir); |
Zhenkai Zhu | 4e1c1d9 | 2013-01-17 14:12:46 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 72 | 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 Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 80 | Face& c1 = forwarder.addFace(); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 81 | auto log1 = make_shared<SyncLog>(dir1, user1); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 82 | auto core1 = make_shared<SyncCore>(c1, log1, user1, loc1, syncPrefix, bind(&callback, _1)); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 84 | Face& c2 = forwarder.addFace(); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 85 | auto log2 = make_shared<SyncLog>(dir2, user2); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 86 | auto core2 = make_shared<SyncCore>(c2, log2, user2, loc2, syncPrefix, bind(&callback, _1)); |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 88 | advanceClocks(time::milliseconds(10), 100); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 89 | |
| 90 | BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root())); |
| 91 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 92 | core1->updateLocalState(1); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 93 | advanceClocks(time::milliseconds(10), 10); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root())); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(core2->seq(user1), 1); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 97 | |
| 98 | core1->updateLocalState(5); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 99 | advanceClocks(time::milliseconds(10), 10); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root())); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(core2->seq(user1), 5); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(log2->LookupLocator(user1), loc1); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 103 | |
| 104 | core2->updateLocalState(10); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 105 | advanceClocks(time::milliseconds(10), 10); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root())); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(core1->seq(user2), 10); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(log1->LookupLocator(user2), loc2); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 109 | |
| 110 | // simple simultaneous data generation |
Alexander Afanasyev | c507ac2 | 2013-01-21 16:01:58 -0800 | [diff] [blame] | 111 | |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 112 | core1->updateLocalState(11); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 113 | advanceClocks(time::milliseconds(10), 10); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 114 | core2->updateLocalState(15); |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 115 | advanceClocks(time::milliseconds(10), 10); |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(toHex(*core1->root()), toHex(*core2->root())); |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(core1->seq(user2), 15); |
| 118 | BOOST_CHECK_EQUAL(core2->seq(user1), 11); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | 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 Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_SUITE_END() |
Yukai Tu | ccdcdba | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 127 | |
| 128 | } // namespace tests |
| 129 | } // namespace chronoshare |
| 130 | } // namespace ndn |