Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 23 | #include "logging.h" |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 24 | #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 | |
| 31 | using namespace Ccnx; |
| 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | namespace fs = boost::filesystem; |
| 35 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 36 | INIT_LOGGER ("Test.Dispatcher"); |
| 37 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestDispatcher) |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | void cleanDir(fs::path dir) |
| 42 | { |
| 43 | if (fs::exists(dir)) |
| 44 | { |
| 45 | fs::remove_all(dir); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void checkRoots(const HashPtr &root1, const HashPtr &root2) |
| 50 | { |
| 51 | BOOST_CHECK_EQUAL(*root1, *root2); |
| 52 | } |
| 53 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(DispatcherTest) |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 55 | { |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 56 | INIT_LOGGERS (); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 58 | fs::path dir1("./TestDispatcher/test-white-house"); |
| 59 | fs::path dir2("./TestDispatcher/test-black-house"); |
| 60 | |
| 61 | string user1 = "/obamaa"; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 62 | string user2 = "/romney"; |
| 63 | |
| 64 | string folder = "who-is-president"; |
| 65 | |
| 66 | CcnxWrapperPtr ccnx1 = make_shared<CcnxWrapper>(); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 67 | usleep(100); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 68 | CcnxWrapperPtr ccnx2 = make_shared<CcnxWrapper>(); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 69 | usleep(100); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 70 | |
| 71 | cleanDir(dir1); |
| 72 | cleanDir(dir2); |
| 73 | |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 74 | Dispatcher d1(user1, folder, dir1, ccnx1, false); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 75 | usleep(100); |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 76 | Dispatcher d2(user2, folder, dir2, ccnx2, false); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 77 | |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 78 | usleep(14900000); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 80 | _LOG_DEBUG ("checking obama vs romney"); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 81 | checkRoots(d1.SyncRoot(), d2.SyncRoot()); |
| 82 | |
| 83 | fs::path filename("a_letter_to_romney.txt"); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 84 | string words = "I'm the new socialist President. You are not!"; |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 85 | |
| 86 | fs::path abf = dir1 / filename; |
| 87 | |
| 88 | ofstream ofs; |
| 89 | ofs.open(abf.string().c_str()); |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 90 | for (int i = 0; i < 5000; i ++) |
Zhenkai Zhu | a7ed62a | 2013-01-25 13:14:37 -0800 | [diff] [blame] | 91 | { |
| 92 | ofs << words; |
| 93 | } |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 94 | ofs.close(); |
| 95 | |
| 96 | d1.Did_LocalFile_AddOrModify(filename); |
| 97 | |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 98 | sleep(5); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 99 | |
| 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 Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 107 | // cleanDir(dir1); |
| 108 | // cleanDir(dir2); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_SUITE_END() |