blob: ac8890d19a9eb208b6571de0ce8096f743935cb1 [file] [log] [blame]
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -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 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
27#include <boost/make_shared.hpp>
28
29#include "../model/sync-ccnx-wrapper.h"
30
31using namespace Sync;
32using namespace std;
33using namespace boost;
34
35string echoStr = "";
36
37void echo(string str) {
38 echoStr = str;
39}
40
41struct TestStruct {
Chaoyi Bian4194b742012-03-08 17:21:35 -080042 string s_str1, s_str2;
43 void set(string str1, string str2) {
44 s_str1 = str1;
45 s_str2 = str2;
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080046 }
47};
48
Zhenkai Zhu689a0df2012-03-09 18:07:35 -080049void sleeping(string str) {
50 for(int i = 0; i < 5; i++) {
51 cout << "Every 5 seconds"<<endl;
52 sleep(5);
53 }
54}
55
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080056BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
57{
58
59 CcnxWrapper ha;
60 CcnxWrapper hb;
61
62 TestStruct foo;
63
64 boost::function<void (string)> globalFunc = echo;
Chaoyi Bian4194b742012-03-08 17:21:35 -080065 boost::function<void (string, string)> memberFunc =
66 bind(&TestStruct::set, &foo, _1, _2);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080067
68 string prefix = "/ucla.edu";
69 ha.setInterestFilter(prefix, globalFunc);
70
71 string interest = "/ucla.edu/0";
72 hb.sendInterest(interest, memberFunc);
73
Zhenkai Zhu3a4758f2012-03-07 20:56:54 -080074 // give time for ccnd to react
Zhenkai Zhu7224ef02012-03-07 21:54:42 -080075 sleep(1);
Zhenkai Zhu3a4758f2012-03-07 20:56:54 -080076
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080077 BOOST_CHECK_EQUAL(echoStr, interest);
78
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080079
80 string name = "/ucla.edu/0";
81 string data = "random bits: !#$!@#$!";
Zhenkai Zhu7224ef02012-03-07 21:54:42 -080082 ha.publishData(name, data, 5);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080083
84 hb.sendInterest(interest, memberFunc);
85
Zhenkai Zhu3a4758f2012-03-07 20:56:54 -080086 // give time for ccnd to react
Zhenkai Zhu7224ef02012-03-07 21:54:42 -080087 sleep(1);
Chaoyi Bian4194b742012-03-08 17:21:35 -080088 BOOST_CHECK_EQUAL(foo.s_str1, name);
89 BOOST_CHECK_EQUAL(foo.s_str2, data);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080090
Zhenkai Zhu689a0df2012-03-09 18:07:35 -080091 CcnxWrapper hc;
92 boost::function<void (string)> func = sleeping;
93 hc.setInterestFilter("/test", func);
94
95 hb.sendInterest("/test/1", memberFunc);
96 sleep(2);
97 hb.sendInterest("/test/2", memberFunc);
98 sleep(60);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080099}
100
101