blob: ec24eab43f9b44e0812a978ff951679d05bcbe9c [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"
22#include "logging.hpp"
23#include "dispatcher.hpp"
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080024#include <boost/test/unit_test.hpp>
25#include <boost/make_shared.hpp>
26#include <boost/filesystem.hpp>
27#include <fstream>
28#include <cassert>
29
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 Afanasyevfc720362013-01-24 21:49:48 -080035INIT_LOGGER ("Test.Dispatcher");
36
Alexander Afanasyev816251e2013-01-28 16:16:49 -080037BOOST_AUTO_TEST_SUITE(TestDispatcher)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080038
39
40void cleanDir(fs::path dir)
41{
42 if (fs::exists(dir))
43 {
44 fs::remove_all(dir);
45 }
46}
47
48void checkRoots(const HashPtr &root1, const HashPtr &root2)
49{
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 INIT_LOGGERS ();
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080056
Alexander Afanasyevfc720362013-01-24 21:49:48 -080057 fs::path dir1("./TestDispatcher/test-white-house");
58 fs::path dir2("./TestDispatcher/test-black-house");
59
60 string user1 = "/obamaa";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080061 string user2 = "/romney";
62
63 string folder = "who-is-president";
64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070065 NdnxWrapperPtr ndnx1 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080066 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070067 NdnxWrapperPtr ndnx2 = make_shared<NdnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080068 usleep(100);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080069
70 cleanDir(dir1);
71 cleanDir(dir2);
72
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 Dispatcher d1(user1, folder, dir1, ndnx1, false);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080074 usleep(100);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070075 Dispatcher d2(user2, folder, dir2, ndnx2, false);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080076
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080077 usleep(14900000);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080078
Alexander Afanasyevfc720362013-01-24 21:49:48 -080079 _LOG_DEBUG ("checking obama vs romney");
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080080 checkRoots(d1.SyncRoot(), d2.SyncRoot());
81
82 fs::path filename("a_letter_to_romney.txt");
Alexander Afanasyevfc720362013-01-24 21:49:48 -080083 string words = "I'm the new socialist President. You are not!";
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080084
85 fs::path abf = dir1 / filename;
86
87 ofstream ofs;
88 ofs.open(abf.string().c_str());
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080089 for (int i = 0; i < 5000; i ++)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080090 {
91 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;
100 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about " << filename.string());
101 BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef));
102 HashPtr fileHash1 = Hash::FromFileContent(abf);
103 HashPtr fileHash2 = Hash::FromFileContent(ef);
104 BOOST_CHECK_EQUAL(*fileHash1, *fileHash2);
105
Zhenkai Zhu3d209592013-01-30 10:16:37 -0800106 cleanDir(dir1);
107 cleanDir(dir2);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800108}
109
110BOOST_AUTO_TEST_SUITE_END()