blob: 4df81567776f9c29fe1a81789c89c927e2d69db8 [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080020 * 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
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070029#include "sync-ccnx-wrapper.h"
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080030
31using namespace Sync;
32using namespace std;
33using namespace boost;
34
35string echoStr = "";
36
37void echo(string str) {
Alexander Afanasyev2247b302012-03-14 14:11:54 -070038 echoStr = str;
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080039}
40
41struct TestStruct {
Alexander Afanasyev2247b302012-03-14 14:11:54 -070042 string s_str1, s_str2;
43 void set(string str1, string str2) {
44 s_str1 = str1;
45 s_str2 = str2;
46 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070047 int * num;
48 int n;
Zhenkai Zhua30e1782012-06-01 11:52:57 -070049
50 void rawSet(string str1, const char *buf, size_t len) {
51 std::cout << "In rawSet" << std::endl;
Zhenkai Zhua30e1782012-06-01 11:52:57 -070052 s_str1 = str1;
Zhenkai Zhudc70a292012-06-01 14:00:59 -070053
54 n = len / 4;
55 num = new int [n];
56 memcpy(num, buf, len);
Zhenkai Zhua30e1782012-06-01 11:52:57 -070057 }
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080058};
59
60BOOST_AUTO_TEST_CASE (CcnxWrapperTest)
61{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070062 CcnxWrapper ha;
63 CcnxWrapper hb;
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080064
Alexander Afanasyev2247b302012-03-14 14:11:54 -070065 TestStruct foo;
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080066
Alexander Afanasyev2247b302012-03-14 14:11:54 -070067 boost::function<void (string)> globalFunc = echo;
68 boost::function<void (string, string)> memberFunc =
69 bind(&TestStruct::set, &foo, _1, _2);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080070
Zhenkai Zhua30e1782012-06-01 11:52:57 -070071 boost::function<void (string, const char *, size_t)> rawFunc =
72 bind(&TestStruct::rawSet, &foo, _1, _2, _3);
73
Alexander Afanasyev2247b302012-03-14 14:11:54 -070074 string prefix = "/ucla.edu";
75 ha.setInterestFilter(prefix, globalFunc);
76 this_thread::sleep (posix_time::milliseconds (10));
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080077
Alexander Afanasyev2247b302012-03-14 14:11:54 -070078 string interest = "/ucla.edu/0";
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070079 hb.sendInterestForString(interest, memberFunc);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080080
Alexander Afanasyev2247b302012-03-14 14:11:54 -070081 // give time for ccnd to react
Chaoyi Bian98135192012-03-27 18:34:11 -070082 sleep(1);
Alexander Afanasyev2247b302012-03-14 14:11:54 -070083 this_thread::sleep (posix_time::milliseconds (5));
84 BOOST_CHECK_EQUAL(echoStr, interest);
Zhenkai Zhu3a4758f2012-03-07 20:56:54 -080085
Alexander Afanasyev2247b302012-03-14 14:11:54 -070086 string name = "/ucla.edu/0";
87 string data = "random bits: !#$!@#$!";
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070088 ha.publishStringData(name, data, 5);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080089
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070090 hb.sendInterestForString(interest, memberFunc);
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -080091
Alexander Afanasyev2247b302012-03-14 14:11:54 -070092 // give time for ccnd to react
93 this_thread::sleep (posix_time::milliseconds (5));
94 BOOST_CHECK_EQUAL(foo.s_str1, name);
95 BOOST_CHECK_EQUAL(foo.s_str2, data);
Zhenkai Zhua30e1782012-06-01 11:52:57 -070096
97 string rawDataName = "/ucla.edu/1";
98 int num[5] = {0, 1, 2, 3, 4};
99 ha.publishRawData(rawDataName, (const char *)num, sizeof(num), 30);
100 hb.sendInterest(rawDataName, rawFunc);
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700101
102 this_thread::sleep (posix_time::milliseconds (5));
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700103 BOOST_CHECK_EQUAL(foo.s_str1, rawDataName);
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700104 for (int i = 0; i < 5; i++) {
105 BOOST_CHECK(foo.num[i] == num[i]);
106 }
Zhenkai Zhu1bdc7032012-03-07 20:47:52 -0800107}
108
109