blob: def78769285682ecd3cbf6be009cb798fe8cb27c [file] [log] [blame]
Alexander Afanasyev47cf2ef2013-01-28 15:13:47 -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
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080022#include "ccnx-wrapper.h"
23#include "ccnx-closure.h"
24#include "ccnx-name.h"
Zhenkai Zhua6472e02013-01-06 14:33:37 -080025#include "ccnx-selectors.h"
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080026#include "ccnx-pco.h"
27#include <unistd.h>
28
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -080029#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080030#include <boost/test/unit_test.hpp>
Alexander Afanasyev47cf2ef2013-01-28 15:13:47 -080031#include <boost/make_shared.hpp>
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080032
33using namespace Ccnx;
34using namespace std;
35using namespace boost;
36
Alexander Afanasyev816251e2013-01-28 16:16:49 -080037BOOST_AUTO_TEST_SUITE(TestCcnxWrapper)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080038
Alexander Afanasyev47cf2ef2013-01-28 15:13:47 -080039CcnxWrapperPtr c1;
40CcnxWrapperPtr c2;
Zhenkai Zhu3722d432013-01-04 16:23:36 -080041int g_timeout_counter = 0;
42int g_dataCallback_counter = 0;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080043
44void publish1(const Name &name)
45{
46 string content = name.toString();
47 c1->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
48}
49
50void publish2(const Name &name)
51{
52 string content = name.toString();
53 c2->publishData(name, (const unsigned char*)content.c_str(), content.size(), 5);
54}
55
Alexander Afanasyevf278db32013-01-21 14:41:01 -080056void dataCallback(const Name &name, Ccnx::PcoPtr pco)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080057{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080058 cout << " in data callback" << endl;
Alexander Afanasyevf278db32013-01-21 14:41:01 -080059 BytesPtr content = pco->contentPtr ();
60 string msg(reinterpret_cast<const char *> (head (*content)), content->size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080061 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080062 BOOST_CHECK_EQUAL(name, msg);
63}
64
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080065void
66timeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080067{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080068 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080069}
70
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080071void
72setup()
73{
74 if (!c1)
75 {
76 c1 = make_shared<CcnxWrapper> ();
77 }
78 if (!c2)
79 {
80 c2 = make_shared<CcnxWrapper> ();
81 }
82}
83
84void
85teardown()
86{
87 if (c1)
88 {
89 c1.reset();
90 }
91 if (c2)
92 {
93 c2.reset();
94 }
95}
96
97
Alexander Afanasyev816251e2013-01-28 16:16:49 -080098BOOST_AUTO_TEST_CASE (BlaCcnxWrapperTest)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080099{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800100 setup();
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800101 Name prefix1("/c1");
102 Name prefix2("/c2");
103
104 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -0800105 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800106 c2->setInterestFilter(prefix2, bind(publish2, _1));
107
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800108 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800109
110 c1->sendInterest(Name("/c2/hi"), closure);
111 usleep(100000);
112 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -0800113 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800114 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
115 // reset
116 g_dataCallback_counter = 0;
117 g_timeout_counter = 0;
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800118
119 teardown();
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800120}
121
122BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
123{
124
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800125 setup();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800126 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800127
128 Selectors selectors;
129 selectors.interestLifetime(1);
130
131 string n1 = "/random/01";
132 c1->sendInterest(Name(n1), closure, selectors);
133 sleep(2);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800134 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800135 usleep(100000);
136 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
137 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
138
139 string n2 = "/random/02";
140 selectors.interestLifetime(2);
141 c1->sendInterest(Name(n2), closure, selectors);
142 sleep(1);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800143 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800144 usleep(100000);
145 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
146 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
147
148 // reset
149 g_dataCallback_counter = 0;
150 g_timeout_counter = 0;
Alexander Afanasyev7065b2d2013-01-28 22:33:38 -0800151
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800152 teardown();
Alexander Afanasyev7065b2d2013-01-28 22:33:38 -0800153
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800154}
155
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800156void
157reexpress(const Name &name, const Closure &closure, Selectors selectors)
158{
159 g_timeout_counter ++;
160 c1->sendInterest(name, closure, selectors);
161}
162
163BOOST_AUTO_TEST_CASE (TestTimeout)
164{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800165 setup();
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800166 g_dataCallback_counter = 0;
167 g_timeout_counter = 0;
168 Closure closure (bind(dataCallback, _1, _2), bind(reexpress, _1, _2, _3));
169
170 Selectors selectors;
171 selectors.interestLifetime(1);
172
173 string n1 = "/random/04";
174 c1->sendInterest(Name(n1), closure, selectors);
175 usleep(3500000);
176 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
177 usleep(1000);
178 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
179 BOOST_CHECK_EQUAL(g_timeout_counter, 3);
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800180 teardown();
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800181}
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800182
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800183BOOST_AUTO_TEST_CASE (TestUnsigned)
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800184{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800185 setup();
186 string n1 = "/xxxxxx/unsigned/01";
187 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
188
189 g_dataCallback_counter = 0;
190 c1->sendInterest(Name(n1), closure);
191 usleep(1000);
192 c2->publishUnsignedData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
193 usleep(1000);
194 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
195 teardown();
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800196}
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800197
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800198 BOOST_AUTO_TEST_CASE (CcnxWrapperUnsigningTest)
199 {
200 setup();
201 Bytes data;
202 data.resize(1024);
203 for (int i = 0; i < 1024; i++)
204 {
205 data[i] = 'm';
206 }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800207
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800208 Name name("/unsigningtest");
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800209
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800210 posix_time::ptime start = posix_time::second_clock::local_time();
211 for (uint64_t i = 0; i < 100000; i++)
212 {
213 Name n = name;
214 n.appendComp(i);
215 c1->publishUnsignedData(n, data, 10);
216 }
217 posix_time::ptime end = posix_time::second_clock::local_time();
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800218
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800219 posix_time::time_duration duration = end - start;
220
221 cout << "Publishing 100000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl;
222 cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 100000.0 << " milliseconds" << endl;
223 teardown();
224 }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800225
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800226BOOST_AUTO_TEST_SUITE_END()