Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 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. |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 19 | */ |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 20 | #include "dispatcher.hpp" |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 21 | #include "dummy-forwarder.hpp" |
| 22 | #include "test-common.hpp" |
| 23 | |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include <boost/make_shared.hpp> |
| 26 | #include <boost/test/unit_test.hpp> |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 27 | #include <cassert> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 28 | #include <fstream> |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 29 | |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 30 | namespace fs = boost::filesystem; |
| 31 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace chronoshare { |
| 34 | namespace tests { |
| 35 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 36 | _LOG_INIT(Test.Dispatcher); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 37 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 38 | class TestDispatcherFixture : public IdentityManagementTimeFixture |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 39 | { |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 40 | public: |
| 41 | TestDispatcherFixture() |
| 42 | : forwarder(m_io, m_keyChain) |
| 43 | , dir1(fs::path(fs::path(UNIT_TEST_CONFIG_PATH) / "TestDispatcher/test-white-house")) |
| 44 | , dir2(fs::path(fs::path(UNIT_TEST_CONFIG_PATH) / "TestDispatcher/test-black-house")) |
| 45 | , user1("/obamaa") |
| 46 | , user2("/trump") |
| 47 | , folder("who-is-president") |
| 48 | , face1(forwarder.addFace()) |
| 49 | , face2(forwarder.addFace()) |
| 50 | { |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 51 | } |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 52 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 53 | ~TestDispatcherFixture() |
| 54 | { |
| 55 | if (exists(fs::path(fs::path(UNIT_TEST_CONFIG_PATH) / "TestDispatcher"))) { |
| 56 | remove_all(fs::path(fs::path(UNIT_TEST_CONFIG_PATH) / "TestDispatcher")); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | checkRoots(ndn::ConstBufferPtr root1, ndn::ConstBufferPtr root2) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | public: |
| 66 | DummyForwarder forwarder; |
| 67 | fs::path dir1; |
| 68 | fs::path dir2; |
| 69 | std::string user1; |
| 70 | std::string user2; |
| 71 | std::string folder; |
| 72 | Face& face1; |
| 73 | Face& face2; |
| 74 | }; |
| 75 | |
| 76 | BOOST_FIXTURE_TEST_SUITE(TestDispatcher, TestDispatcherFixture) |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 78 | BOOST_AUTO_TEST_CASE(DispatcherTest) |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 79 | { |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 80 | Dispatcher d1(user1, folder, dir1, face1, false); |
| 81 | Dispatcher d2(user2, folder, dir2, face2, false); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 82 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 83 | advanceClocks(time::milliseconds(10), 1000); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 84 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 85 | _LOG_DEBUG("checking obama vs trump"); |
| 86 | BOOST_CHECK(*(d1.SyncRoot()) == *(d2.SyncRoot())); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 87 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 88 | fs::path filename("a_letter_to_obama.txt"); |
| 89 | std::string words = "I'm the new socialist President. You are not!"; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 90 | |
| 91 | fs::path abf = dir1 / filename; |
| 92 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 93 | std::ofstream ofs; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 94 | ofs.open(abf.string().c_str()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | for (int i = 0; i < 5000; i++) { |
Zhenkai Zhu | a7ed62a | 2013-01-25 13:14:37 -0800 | [diff] [blame] | 96 | ofs << words; |
| 97 | } |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 98 | ofs.close(); |
| 99 | |
| 100 | d1.Did_LocalFile_AddOrModify(filename); |
| 101 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 102 | advanceClocks(time::milliseconds(10), 1000); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 103 | |
| 104 | fs::path ef = dir2 / filename; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about " |
| 106 | << filename.string()); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef)); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 108 | |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 109 | ConstBufferPtr fileHash1 = digestFromFile(abf); |
| 110 | ConstBufferPtr fileHash2 = digestFromFile(ef); |
| 111 | |
| 112 | BOOST_CHECK(*fileHash1 == *fileHash2); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_SUITE_END() |
Yukai Tu | 47f5c69 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 116 | |
| 117 | } // namespace tests |
| 118 | } // namespace chronoshare |
| 119 | } // namespace ndn |