blob: 572db3173562f7eeb70a62d3455fda5268d232d2 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080035INIT_LOGGER("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
39
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080040void
41cleanDir(fs::path dir)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080042{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 if (fs::exists(dir)) {
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080044 fs::remove_all(dir);
45 }
46}
47
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048void
49checkRoots(const HashPtr& root1, const HashPtr& root2)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080050{
51 BOOST_CHECK_EQUAL(*root1, *root2);
52}
53
Alexander Afanasyev816251e2013-01-28 16:16:49 -080054BOOST_AUTO_TEST_CASE(DispatcherTest)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080055{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 INIT_LOGGERS();
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080057
Alexander Afanasyevfc720362013-01-24 21:49:48 -080058 fs::path dir1("./TestDispatcher/test-white-house");
59 fs::path dir2("./TestDispatcher/test-black-house");
60
61 string user1 = "/obamaa";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080062 string user2 = "/romney";
63
64 string folder = "who-is-president";
65
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070066 NdnxWrapperPtr ndnx1 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080067 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070068 NdnxWrapperPtr ndnx2 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080069 usleep(100);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080070
71 cleanDir(dir1);
72 cleanDir(dir2);
73
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070074 Dispatcher d1(user1, folder, dir1, ndnx1, false);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080075 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 Dispatcher d2(user2, folder, dir2, ndnx2, false);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080077
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080078 usleep(14900000);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080079
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080 _LOG_DEBUG("checking obama vs romney");
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080081 checkRoots(d1.SyncRoot(), d2.SyncRoot());
82
83 fs::path filename("a_letter_to_romney.txt");
Alexander Afanasyevfc720362013-01-24 21:49:48 -080084 string words = "I'm the new socialist President. You are not!";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080085
86 fs::path abf = dir1 / filename;
87
88 ofstream ofs;
89 ofs.open(abf.string().c_str());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080090 for (int i = 0; i < 5000; i++) {
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080091 ofs << words;
92 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080093 ofs.close();
94
95 d1.Did_LocalFile_AddOrModify(filename);
96
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080097 sleep(5);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080098
99 fs::path ef = dir2 / filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about "
101 << filename.string());
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800102 BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef));
103 HashPtr fileHash1 = Hash::FromFileContent(abf);
104 HashPtr fileHash2 = Hash::FromFileContent(ef);
105 BOOST_CHECK_EQUAL(*fileHash1, *fileHash2);
106
Zhenkai Zhu3d209592013-01-30 10:16:37 -0800107 cleanDir(dir1);
108 cleanDir(dir2);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800109}
110
111BOOST_AUTO_TEST_SUITE_END()