blob: fa3b80f6cc4499001de4680ed9343d69691c3247 [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{
Alexander Afanasyevf278db32013-01-21 14:41:01 -080058 BytesPtr content = pco->contentPtr ();
59 string msg(reinterpret_cast<const char *> (head (*content)), content->size());
Zhenkai Zhu3722d432013-01-04 16:23:36 -080060 g_dataCallback_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080061 BOOST_CHECK_EQUAL(name, msg);
62}
63
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080064void
65timeout(const Name &name, const Closure &closure, Selectors selectors)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080066{
Zhenkai Zhu3722d432013-01-04 16:23:36 -080067 g_timeout_counter ++;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080068}
69
Alexander Afanasyev816251e2013-01-28 16:16:49 -080070BOOST_AUTO_TEST_CASE (BlaCcnxWrapperTest)
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080071{
Alexander Afanasyev47cf2ef2013-01-28 15:13:47 -080072 c1 = make_shared<CcnxWrapper> ();
73 c2 = make_shared<CcnxWrapper> ();
74
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080075 Name prefix1("/c1");
76 Name prefix2("/c2");
77
78 c1->setInterestFilter(prefix1, bind(publish1, _1));
Zhenkai Zhu03a511d2013-01-04 17:13:28 -080079 usleep(100000);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080080 c2->setInterestFilter(prefix2, bind(publish2, _1));
81
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080082 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080083
84 c1->sendInterest(Name("/c2/hi"), closure);
85 usleep(100000);
86 c2->sendInterest(Name("/c1/hi"), closure);
Zhenkai Zhud34106a2013-01-04 15:51:55 -080087 sleep(1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -080088 BOOST_CHECK_EQUAL(g_dataCallback_counter, 2);
89 // reset
90 g_dataCallback_counter = 0;
91 g_timeout_counter = 0;
Zhenkai Zhu3722d432013-01-04 16:23:36 -080092}
93
94BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
95{
96
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080097 Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3));
Zhenkai Zhu3722d432013-01-04 16:23:36 -080098
99 Selectors selectors;
100 selectors.interestLifetime(1);
101
102 string n1 = "/random/01";
103 c1->sendInterest(Name(n1), closure, selectors);
104 sleep(2);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800105 c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800106 usleep(100000);
107 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
108 BOOST_CHECK_EQUAL(g_dataCallback_counter, 0);
109
110 string n2 = "/random/02";
111 selectors.interestLifetime(2);
112 c1->sendInterest(Name(n2), closure, selectors);
113 sleep(1);
Alexander Afanasyev46092452013-01-28 16:23:06 -0800114 c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 1);
Zhenkai Zhu3722d432013-01-04 16:23:36 -0800115 usleep(100000);
116 BOOST_CHECK_EQUAL(g_timeout_counter, 1);
117 BOOST_CHECK_EQUAL(g_dataCallback_counter, 1);
118
119 // reset
120 g_dataCallback_counter = 0;
121 g_timeout_counter = 0;
Alexander Afanasyev7065b2d2013-01-28 22:33:38 -0800122
123
124 c1.reset ();
125 c2.reset ();
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800126}
127
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800128// BOOST_AUTO_TEST_CASE (CcnxWrapperSigningTest)
129// {
130// Bytes data;
131// data.resize(1024);
132// for (int i = 0; i < 1024; i++)
133// {
134// data[i] = 'm';
135// }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800136
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800137// Name name("/signingtest");
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800138
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800139// posix_time::ptime start = posix_time::second_clock::local_time();
140// for (uint64_t i = 0; i < 10000; i++)
141// {
142// Name n = name;
143// n.appendComp(i);
144// c1->publishData(n, data, 10);
145// }
146// posix_time::ptime end = posix_time::second_clock::local_time();
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800147
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800148// posix_time::time_duration duration = end - start;
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800149
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800150// cout << "Publishing 10000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl;
151// cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 10000.0 << " milliseconds" << endl;
152// }
Zhenkai Zhu6fe6d882013-01-23 21:33:53 -0800153
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800154BOOST_AUTO_TEST_SUITE_END()