blob: 6378cb2fa3307930b2c22d68a849be1c3c1784a0 [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"
23#include "dispatcher.h"
24#include <boost/test/unit_test.hpp>
25#include <boost/make_shared.hpp>
26#include <boost/filesystem.hpp>
27#include <fstream>
28#include <cassert>
29
30using namespace Ccnx;
31using namespace std;
32using namespace boost;
33namespace fs = boost::filesystem;
34
35BOOST_AUTO_TEST_SUITE(DispatcherTest)
36
37
38void cleanDir(fs::path dir)
39{
40 if (fs::exists(dir))
41 {
42 fs::remove_all(dir);
43 }
44}
45
46void checkRoots(const HashPtr &root1, const HashPtr &root2)
47{
48 BOOST_CHECK_EQUAL(*root1, *root2);
49}
50
51BOOST_AUTO_TEST_CASE(TestDispatcher)
52{
53 fs::path dir1("test-white-house");
54 fs::path dir2("test-black-house");
55
56 string user1 = "/obama";
57 string user2 = "/romney";
58
59 string folder = "who-is-president";
60
61 CcnxWrapperPtr ccnx1 = make_shared<CcnxWrapper>();
62 usleep(1000);
63 CcnxWrapperPtr ccnx2 = make_shared<CcnxWrapper>();
64 usleep(1000);
65
66 cleanDir(dir1);
67 cleanDir(dir2);
68
69 fs::create_directory(dir1);
70 fs::create_directory(dir2);
71
72 Dispatcher d1(user1, folder, dir1, ccnx1, 2, false);
73 usleep(1000);
74 Dispatcher d2(user2, folder, dir2, ccnx2, 2, false);
75
76 sleep(1);
77
78 checkRoots(d1.SyncRoot(), d2.SyncRoot());
79
80 fs::path filename("a_letter_to_romney.txt");
81 string words = "I'm the new socialist President. You are not.";
82
83 fs::path abf = dir1 / filename;
84
85 ofstream ofs;
86 ofs.open(abf.string().c_str());
87 ofs << words;
88 ofs.close();
89
90 d1.Did_LocalFile_AddOrModify(filename);
91
92 sleep(2);
93
94 fs::path ef = dir2 / filename;
95 BOOST_REQUIRE_MESSAGE(fs::exists(ef), user1 << " failed to notify " << user2 << " about " << filename.string());
96 BOOST_CHECK_EQUAL(fs::file_size(abf), fs::file_size(ef));
97 HashPtr fileHash1 = Hash::FromFileContent(abf);
98 HashPtr fileHash2 = Hash::FromFileContent(ef);
99 BOOST_CHECK_EQUAL(*fileHash1, *fileHash2);
100
101 cleanDir(dir1);
102 cleanDir(dir2);
103}
104
105BOOST_AUTO_TEST_SUITE_END()