blob: 2089bc2d4a3888c4dc8002e2b3dbc7443df1ef11 [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 Zhu5957c0d2013-02-08 13:47:52 -080065void encapCallback(const Name &name, Ccnx::PcoPtr pco)
66{
67 cout << " in encap data callback" << endl;
68 PcoPtr npco = make_shared<ParsedContentObject> (*(pco->contentPtr()));
69 g_dataCallback_counter ++;
70 BOOST_CHECK(npco);
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070071 BOOST_CHECK(c1->checkPcoIntegrity(npco));
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -080072}
73
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080074void
75timeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080076{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080077 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080078}
79
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080080void
81setup()
82{
83 if (!c1)
84 {
85 c1 = make_shared<CcnxWrapper> ();
86 }
87 if (!c2)
88 {
89 c2 = make_shared<CcnxWrapper> ();
90 }
91}
92
93void
94teardown()
95{
96 if (c1)
97 {
98 c1.reset();
99 }
100 if (c2)
101 {
102 c2.reset();
103 }
104}
105
106
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800107BOOST_AUTO_TEST_CASE (BlaCcnxWrapperTest)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800108{
Alexander Afanasyevd012a0d2013-03-14 14:53:09 -0700109 INIT_LOGGERS ();
110
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800111 setup();
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800112 Name prefix1("/c1");
113 Name prefix2("/c2");
114
115 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -0800116 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800117 c2->setInterestFilter(prefix2, bind(publish2, _1));
118
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800119 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800120
121 c1->sendInterest(Name("/c2/hi"), closure);
122 usleep(100000);
123 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -0800124 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800125 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
126 // reset
127 g_dataCallback_counter = 0;
128 g_timeout_counter = 0;
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800129
130 teardown();
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800131}
132
133BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
134{
135
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800136 setup();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800137 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800138
139 Selectors selectors;
Alexander Afanasyevd012a0d2013-03-14 14:53:09 -0700140 selectors
141 .interestLifetime(1)
142 .childSelector(Selectors::RIGHT);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800143
144 string n1 = "/random/01";
145 c1->sendInterest(Name(n1), closure, selectors);
146 sleep(2);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800147 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800148 usleep(100000);
149 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
150 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
151
152 string n2 = "/random/02";
153 selectors.interestLifetime(2);
154 c1->sendInterest(Name(n2), closure, selectors);
155 sleep(1);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800156 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800157 usleep(100000);
158 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
159 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
160
161 // reset
162 g_dataCallback_counter = 0;
163 g_timeout_counter = 0;
Alexander Afanasyev7065b2d2013-01-28 22:33:38 -0800164
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800165 teardown();
Alexander Afanasyev7065b2d2013-01-28 22:33:38 -0800166
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800167}
168
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800169void
170reexpress(const Name &name, const Closure &closure, Selectors selectors)
171{
172 g_timeout_counter ++;
173 c1->sendInterest(name, closure, selectors);
174}
175
176BOOST_AUTO_TEST_CASE (TestTimeout)
177{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800178 setup();
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800179 g_dataCallback_counter = 0;
180 g_timeout_counter = 0;
181 Closure closure (bind(dataCallback, _1, _2), bind(reexpress, _1, _2, _3));
182
183 Selectors selectors;
184 selectors.interestLifetime(1);
185
186 string n1 = "/random/04";
187 c1->sendInterest(Name(n1), closure, selectors);
188 usleep(3500000);
189 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
190 usleep(1000);
191 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
192 BOOST_CHECK_EQUAL(g_timeout_counter, 3);
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800193 teardown();
Zhenkai Zhue8c8c8a2013-01-28 22:38:32 -0800194}
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800195
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800196BOOST_AUTO_TEST_CASE (TestUnsigned)
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800197{
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800198 setup();
199 string n1 = "/xxxxxx/unsigned/01";
200 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
201
202 g_dataCallback_counter = 0;
203 c1->sendInterest(Name(n1), closure);
204 usleep(1000);
205 c2->publishUnsignedData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
206 usleep(1000);
207 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -0800208
209 string n2 = "/xxxxxx/signed/01";
210 Bytes content = c1->createContentObject(Name(n1), (const unsigned char *)n2.c_str(), n2.size(), 1);
211 c1->publishUnsignedData(Name(n2), head(content), content.size(), 1);
212 Closure encapClosure(bind(encapCallback, _1, _2), bind(timeout, _1, _2, _3));
213 c2->sendInterest(Name(n2), encapClosure);
214 usleep(2000);
215 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800216 teardown();
Zhenkai Zhufbf08d32013-01-28 22:42:29 -0800217}
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800218
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800219 BOOST_AUTO_TEST_CASE (CcnxWrapperUnsigningTest)
220 {
221 setup();
222 Bytes data;
223 data.resize(1024);
224 for (int i = 0; i < 1024; i++)
225 {
226 data[i] = 'm';
227 }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800228
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800229 Name name("/unsigningtest");
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800230
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800231 posix_time::ptime start = posix_time::second_clock::local_time();
232 for (uint64_t i = 0; i < 100000; i++)
233 {
234 Name n = name;
235 n.appendComp(i);
236 c1->publishUnsignedData(n, data, 10);
237 }
238 posix_time::ptime end = posix_time::second_clock::local_time();
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800239
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800240 posix_time::time_duration duration = end - start;
241
242 cout << "Publishing 100000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl;
243 cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 100000.0 << " milliseconds" << endl;
244 teardown();
245 }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800246
Zhenkai Zhu5957c0d2013-02-08 13:47:52 -0800247
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800248BOOST_AUTO_TEST_SUITE_END()