blob: 20843af6b23170f887d0a432d9617efee62295f3 [file] [log] [blame]
Zhenkai Zhufaee2d42013-01-24 17:47:13 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "ccnx-wrapper.h"
Alexander Afanasyevfc720362013-01-24 21:49:48 -080023#include "logging.h"
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080024#include "dispatcher.h"
25#include <boost/test/unit_test.hpp>
26#include <boost/make_shared.hpp>
27#include <boost/filesystem.hpp>
28#include <fstream>
29#include <cassert>
30
31using namespace Ccnx;
32using namespace std;
33using namespace boost;
34namespace fs = boost::filesystem;
35
Alexander Afanasyevfc720362013-01-24 21:49:48 -080036INIT_LOGGER ("Test.Dispatcher");
37
Alexander Afanasyev816251e2013-01-28 16:16:49 -080038BOOST_AUTO_TEST_SUITE(TestDispatcher)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080039
40
41void cleanDir(fs::path dir)
42{
43 if (fs::exists(dir))
44 {
45 fs::remove_all(dir);
46 }
47}
48
49void checkRoots(const HashPtr &root1, const HashPtr &root2)
50{
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 Afanasyevfc720362013-01-24 21:49:48 -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
66 CcnxWrapperPtr ccnx1 = make_shared<CcnxWrapper>();
Alexander Afanasyevfc720362013-01-24 21:49:48 -080067 usleep(100);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080068 CcnxWrapperPtr ccnx2 = make_shared<CcnxWrapper>();
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
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080074 Dispatcher d1(user1, folder, dir1, ccnx1, false);
Alexander Afanasyevfc720362013-01-24 21:49:48 -080075 usleep(100);
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080076 Dispatcher d2(user2, folder, dir2, ccnx2, 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 Afanasyevfc720362013-01-24 21:49:48 -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());
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080090 for (int i = 0; i < 5000; i ++)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080091 {
92 ofs << words;
93 }
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080094 ofs.close();
95
96 d1.Did_LocalFile_AddOrModify(filename);
97
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080098 sleep(5);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080099
100 fs::path ef = dir2 / filename;
101 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about " << filename.string());
102 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
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800107 // cleanDir(dir1);
108 // cleanDir(dir2);
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800109}
110
111BOOST_AUTO_TEST_SUITE_END()