blob: 60f05052ed42be0ac70ee66aad6c24b1226a7c95 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Zhenkai Zhufaee2d42013-01-24 17:47:13 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Zhenkai Zhufaee2d42013-01-24 17:47:13 -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.
Zhenkai Zhufaee2d42013-01-24 17:47:13 -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.
Zhenkai Zhufaee2d42013-01-24 17:47:13 -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.
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080019 */
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080020#include "dispatcher.hpp"
Yukai Tu47f5c692016-10-24 13:48:01 -070021#include "dummy-forwarder.hpp"
22#include "test-common.hpp"
23
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080024#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080025#include <boost/make_shared.hpp>
26#include <boost/test/unit_test.hpp>
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080027#include <cassert>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include <fstream>
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080029
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080030namespace fs = boost::filesystem;
31
Yukai Tu47f5c692016-10-24 13:48:01 -070032namespace ndn {
33namespace chronoshare {
34namespace tests {
35
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080036_LOG_INIT(Test.Dispatcher);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080037
Yukai Tu47f5c692016-10-24 13:48:01 -070038class TestDispatcherFixture : public IdentityManagementTimeFixture
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080039{
Yukai Tu47f5c692016-10-24 13:48:01 -070040public:
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 Zhufaee2d42013-01-24 17:47:13 -080051 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080052
Yukai Tu47f5c692016-10-24 13:48:01 -070053 ~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
65public:
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
76BOOST_FIXTURE_TEST_SUITE(TestDispatcher, TestDispatcherFixture)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080077
Alexander Afanasyev816251e2013-01-28 16:16:49 -080078BOOST_AUTO_TEST_CASE(DispatcherTest)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080079{
Yukai Tu47f5c692016-10-24 13:48:01 -070080 Dispatcher d1(user1, folder, dir1, face1, false);
81 Dispatcher d2(user2, folder, dir2, face2, false);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080082
Yukai Tu47f5c692016-10-24 13:48:01 -070083 advanceClocks(time::milliseconds(10), 1000);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080084
Yukai Tu47f5c692016-10-24 13:48:01 -070085 _LOG_DEBUG("checking obama vs trump");
86 BOOST_CHECK(*(d1.SyncRoot()) == *(d2.SyncRoot()));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080087
Yukai Tu47f5c692016-10-24 13:48:01 -070088 fs::path filename("a_letter_to_obama.txt");
89 std::string words = "I'm the new socialist President. You are not!";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080090
91 fs::path abf = dir1 / filename;
92
Yukai Tu47f5c692016-10-24 13:48:01 -070093 std::ofstream ofs;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080094 ofs.open(abf.string().c_str());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 for (int i = 0; i < 5000; i++) {
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080096 ofs << words;
97 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080098 ofs.close();
99
100 d1.Did_LocalFile_AddOrModify(filename);
101
Yukai Tu47f5c692016-10-24 13:48:01 -0700102 advanceClocks(time::milliseconds(10), 1000);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800103
104 fs::path ef = dir2 / filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about "
106 << filename.string());
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800107 BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800108
Yukai Tu47f5c692016-10-24 13:48:01 -0700109 ConstBufferPtr fileHash1 = digestFromFile(abf);
110 ConstBufferPtr fileHash2 = digestFromFile(ef);
111
112 BOOST_CHECK(*fileHash1 == *fileHash2);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800113}
114
115BOOST_AUTO_TEST_SUITE_END()
Yukai Tu47f5c692016-10-24 13:48:01 -0700116
117} // namespace tests
118} // namespace chronoshare
119} // namespace ndn