Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | using boost::test_tools::output_test_stream; |
| 26 | |
| 27 | #include <boost/make_shared.hpp> |
| 28 | |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 29 | #include "sync-ccnx-wrapper.h" |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace Sync; |
| 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | |
| 35 | string echoStr = ""; |
| 36 | |
| 37 | void echo(string str) { |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 38 | echoStr = str; |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | struct TestStruct { |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 42 | string s_str1, s_str2; |
| 43 | void set(string str1, string str2) { |
| 44 | s_str1 = str1; |
| 45 | s_str2 = str2; |
| 46 | } |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | BOOST_AUTO_TEST_CASE (CcnxWrapperTest) |
| 50 | { |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 51 | CcnxWrapper ha; |
| 52 | CcnxWrapper hb; |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 54 | TestStruct foo; |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 56 | boost::function<void (string)> globalFunc = echo; |
| 57 | boost::function<void (string, string)> memberFunc = |
| 58 | bind(&TestStruct::set, &foo, _1, _2); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 60 | string prefix = "/ucla.edu"; |
| 61 | ha.setInterestFilter(prefix, globalFunc); |
| 62 | this_thread::sleep (posix_time::milliseconds (10)); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 64 | string interest = "/ucla.edu/0"; |
| 65 | hb.sendInterest(interest, memberFunc); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 67 | // give time for ccnd to react |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 68 | sleep(1); |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 69 | this_thread::sleep (posix_time::milliseconds (5)); |
| 70 | BOOST_CHECK_EQUAL(echoStr, interest); |
Zhenkai Zhu | 3a4758f | 2012-03-07 20:56:54 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 72 | string name = "/ucla.edu/0"; |
| 73 | string data = "random bits: !#$!@#$!"; |
| 74 | ha.publishData(name, data, 5); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 76 | hb.sendInterest(interest, memberFunc); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 78 | // give time for ccnd to react |
| 79 | this_thread::sleep (posix_time::milliseconds (5)); |
| 80 | BOOST_CHECK_EQUAL(foo.s_str1, name); |
| 81 | BOOST_CHECK_EQUAL(foo.s_str2, data); |
Zhenkai Zhu | 1bdc703 | 2012-03-07 20:47:52 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |