blob: 5270102c701ad6edb0559e590f8af9c2b6616805 [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 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "ccnx-wrapper.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "dispatcher.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080023#include "logging.hpp"
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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070030using namespace Ndnx;
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080031using namespace std;
32using namespace boost;
33namespace fs = boost::filesystem;
34
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080035_LOG_INIT(Test.Dispatcher);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080036
Alexander Afanasyev816251e2013-01-28 16:16:49 -080037BOOST_AUTO_TEST_SUITE(TestDispatcher)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080038
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039void
40cleanDir(fs::path dir)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080041{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042 if (fs::exists(dir)) {
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080043 fs::remove_all(dir);
44 }
45}
46
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047void
48checkRoots(const HashPtr& root1, const HashPtr& root2)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080049{
50 BOOST_CHECK_EQUAL(*root1, *root2);
51}
52
Alexander Afanasyev816251e2013-01-28 16:16:49 -080053BOOST_AUTO_TEST_CASE(DispatcherTest)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080054{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080055 fs::path dir1("./TestDispatcher/test-white-house");
56 fs::path dir2("./TestDispatcher/test-black-house");
57
58 string user1 = "/obamaa";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080059 string user2 = "/romney";
60
61 string folder = "who-is-president";
62
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070063 NdnxWrapperPtr ndnx1 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080064 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070065 NdnxWrapperPtr ndnx2 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080066 usleep(100);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080067
68 cleanDir(dir1);
69 cleanDir(dir2);
70
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070071 Dispatcher d1(user1, folder, dir1, ndnx1, false);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080072 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 Dispatcher d2(user2, folder, dir2, ndnx2, false);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080074
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080075 usleep(14900000);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080076
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077 _LOG_DEBUG("checking obama vs romney");
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080078 checkRoots(d1.SyncRoot(), d2.SyncRoot());
79
80 fs::path filename("a_letter_to_romney.txt");
Alexander Afanasyevfc720362013-01-24 21:49:48 -080081 string words = "I'm the new socialist President. You are not!";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080082
83 fs::path abf = dir1 / filename;
84
85 ofstream ofs;
86 ofs.open(abf.string().c_str());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 for (int i = 0; i < 5000; i++) {
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080088 ofs << words;
89 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080090 ofs.close();
91
92 d1.Did_LocalFile_AddOrModify(filename);
93
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080094 sleep(5);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080095
96 fs::path ef = dir2 / filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about "
98 << filename.string());
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080099 BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef));
100 HashPtr fileHash1 = Hash::FromFileContent(abf);
101 HashPtr fileHash2 = Hash::FromFileContent(ef);
102 BOOST_CHECK_EQUAL(*fileHash1, *fileHash2);
103
Zhenkai Zhu3d209592013-01-30 10:16:37 -0800104 cleanDir(dir1);
105 cleanDir(dir2);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800106}
107
108BOOST_AUTO_TEST_SUITE_END()