Alexander Afanasyev | 47cf2ef | 2013-01-28 15:13:47 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 22 | #include "ccnx-wrapper.h" |
| 23 | #include "ccnx-closure.h" |
| 24 | #include "ccnx-name.h" |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 25 | #include "ccnx-selectors.h" |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 26 | #include "ccnx-pco.h" |
| 27 | #include <unistd.h> |
| 28 | |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 29 | #include <boost/date_time/posix_time/posix_time.hpp> |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 30 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | 47cf2ef | 2013-01-28 15:13:47 -0800 | [diff] [blame] | 31 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace Ccnx; |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestCcnxWrapper) |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 47cf2ef | 2013-01-28 15:13:47 -0800 | [diff] [blame] | 39 | CcnxWrapperPtr c1; |
| 40 | CcnxWrapperPtr c2; |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 41 | int g_timeout_counter = 0; |
| 42 | int g_dataCallback_counter = 0; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 43 | |
| 44 | void 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 | |
| 50 | void 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 Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 56 | void dataCallback(const Name &name, Ccnx::PcoPtr pco) |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 57 | { |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 58 | cout << " in data callback" << endl; |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 59 | BytesPtr content = pco->contentPtr (); |
| 60 | string msg(reinterpret_cast<const char *> (head (*content)), content->size()); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 61 | g_dataCallback_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(name, msg); |
| 63 | } |
| 64 | |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 65 | void encapCallback(const Name &name, Ccnx::PcoPtr pco) |
| 66 | { |
| 67 | cout << " in encap data callback" << endl; |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame^] | 68 | BOOST_CHECK(!c1->verify(pco)); |
| 69 | cout << "++++++++++++++++++ Outer content couldn't be verified, which is expected." << endl; |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 70 | PcoPtr npco = make_shared<ParsedContentObject> (*(pco->contentPtr())); |
| 71 | g_dataCallback_counter ++; |
| 72 | BOOST_CHECK(npco); |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame^] | 73 | BOOST_CHECK(c1->verify(npco)); |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 76 | void |
| 77 | timeout(const Name &name, const Closure &closure, Selectors selectors) |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 78 | { |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 79 | g_timeout_counter ++; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 82 | void |
| 83 | setup() |
| 84 | { |
| 85 | if (!c1) |
| 86 | { |
| 87 | c1 = make_shared<CcnxWrapper> (); |
| 88 | } |
| 89 | if (!c2) |
| 90 | { |
| 91 | c2 = make_shared<CcnxWrapper> (); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | teardown() |
| 97 | { |
| 98 | if (c1) |
| 99 | { |
| 100 | c1.reset(); |
| 101 | } |
| 102 | if (c2) |
| 103 | { |
| 104 | c2.reset(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE (BlaCcnxWrapperTest) |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 110 | { |
Alexander Afanasyev | d012a0d | 2013-03-14 14:53:09 -0700 | [diff] [blame] | 111 | INIT_LOGGERS (); |
| 112 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 113 | setup(); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 114 | Name prefix1("/c1"); |
| 115 | Name prefix2("/c2"); |
| 116 | |
| 117 | c1->setInterestFilter(prefix1, bind(publish1, _1)); |
Zhenkai Zhu | 03a511d | 2013-01-04 17:13:28 -0800 | [diff] [blame] | 118 | usleep(100000); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 119 | c2->setInterestFilter(prefix2, bind(publish2, _1)); |
| 120 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 121 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 122 | |
| 123 | c1->sendInterest(Name("/c2/hi"), closure); |
| 124 | usleep(100000); |
| 125 | c2->sendInterest(Name("/c1/hi"), closure); |
Zhenkai Zhu | d34106a | 2013-01-04 15:51:55 -0800 | [diff] [blame] | 126 | sleep(1); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
| 128 | // reset |
| 129 | g_dataCallback_counter = 0; |
| 130 | g_timeout_counter = 0; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 131 | |
| 132 | teardown(); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE (CcnxWrapperSelector) |
| 136 | { |
| 137 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 138 | setup(); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 139 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 140 | |
| 141 | Selectors selectors; |
Alexander Afanasyev | d012a0d | 2013-03-14 14:53:09 -0700 | [diff] [blame] | 142 | selectors |
| 143 | .interestLifetime(1) |
| 144 | .childSelector(Selectors::RIGHT); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 145 | |
| 146 | string n1 = "/random/01"; |
| 147 | c1->sendInterest(Name(n1), closure, selectors); |
| 148 | sleep(2); |
Alexander Afanasyev | 4609245 | 2013-01-28 16:23:06 -0800 | [diff] [blame] | 149 | c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 150 | usleep(100000); |
| 151 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 152 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 153 | |
| 154 | string n2 = "/random/02"; |
| 155 | selectors.interestLifetime(2); |
| 156 | c1->sendInterest(Name(n2), closure, selectors); |
| 157 | sleep(1); |
Alexander Afanasyev | 4609245 | 2013-01-28 16:23:06 -0800 | [diff] [blame] | 158 | c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 1); |
Zhenkai Zhu | 3722d43 | 2013-01-04 16:23:36 -0800 | [diff] [blame] | 159 | usleep(100000); |
| 160 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 161 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 162 | |
| 163 | // reset |
| 164 | g_dataCallback_counter = 0; |
| 165 | g_timeout_counter = 0; |
Alexander Afanasyev | 7065b2d | 2013-01-28 22:33:38 -0800 | [diff] [blame] | 166 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 167 | teardown(); |
Alexander Afanasyev | 7065b2d | 2013-01-28 22:33:38 -0800 | [diff] [blame] | 168 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Zhenkai Zhu | e8c8c8a | 2013-01-28 22:38:32 -0800 | [diff] [blame] | 171 | void |
| 172 | reexpress(const Name &name, const Closure &closure, Selectors selectors) |
| 173 | { |
| 174 | g_timeout_counter ++; |
| 175 | c1->sendInterest(name, closure, selectors); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE (TestTimeout) |
| 179 | { |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 180 | setup(); |
Zhenkai Zhu | e8c8c8a | 2013-01-28 22:38:32 -0800 | [diff] [blame] | 181 | g_dataCallback_counter = 0; |
| 182 | g_timeout_counter = 0; |
| 183 | Closure closure (bind(dataCallback, _1, _2), bind(reexpress, _1, _2, _3)); |
| 184 | |
| 185 | Selectors selectors; |
| 186 | selectors.interestLifetime(1); |
| 187 | |
| 188 | string n1 = "/random/04"; |
| 189 | c1->sendInterest(Name(n1), closure, selectors); |
| 190 | usleep(3500000); |
| 191 | c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 192 | usleep(100000); |
Zhenkai Zhu | e8c8c8a | 2013-01-28 22:38:32 -0800 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 194 | BOOST_CHECK_EQUAL(g_timeout_counter, 3); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 195 | teardown(); |
Zhenkai Zhu | e8c8c8a | 2013-01-28 22:38:32 -0800 | [diff] [blame] | 196 | } |
Zhenkai Zhu | fbf08d3 | 2013-01-28 22:42:29 -0800 | [diff] [blame] | 197 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE (TestUnsigned) |
Zhenkai Zhu | fbf08d3 | 2013-01-28 22:42:29 -0800 | [diff] [blame] | 199 | { |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 200 | setup(); |
| 201 | string n1 = "/xxxxxx/unsigned/01"; |
| 202 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 203 | |
| 204 | g_dataCallback_counter = 0; |
| 205 | c1->sendInterest(Name(n1), closure); |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 206 | usleep(100000); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 207 | c2->publishUnsignedData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 208 | usleep(100000); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 210 | |
| 211 | string n2 = "/xxxxxx/signed/01"; |
| 212 | Bytes content = c1->createContentObject(Name(n1), (const unsigned char *)n2.c_str(), n2.size(), 1); |
| 213 | c1->publishUnsignedData(Name(n2), head(content), content.size(), 1); |
| 214 | Closure encapClosure(bind(encapCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 215 | c2->sendInterest(Name(n2), encapClosure); |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame^] | 216 | usleep(4000000); |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 218 | teardown(); |
Zhenkai Zhu | fbf08d3 | 2013-01-28 22:42:29 -0800 | [diff] [blame] | 219 | } |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 220 | |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame^] | 221 | |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 222 | /* |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 223 | BOOST_AUTO_TEST_CASE (CcnxWrapperUnsigningTest) |
| 224 | { |
| 225 | setup(); |
| 226 | Bytes data; |
| 227 | data.resize(1024); |
| 228 | for (int i = 0; i < 1024; i++) |
| 229 | { |
| 230 | data[i] = 'm'; |
| 231 | } |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 232 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 233 | Name name("/unsigningtest"); |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 234 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 235 | posix_time::ptime start = posix_time::second_clock::local_time(); |
| 236 | for (uint64_t i = 0; i < 100000; i++) |
| 237 | { |
| 238 | Name n = name; |
| 239 | n.appendComp(i); |
| 240 | c1->publishUnsignedData(n, data, 10); |
| 241 | } |
| 242 | posix_time::ptime end = posix_time::second_clock::local_time(); |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 243 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 244 | posix_time::time_duration duration = end - start; |
| 245 | |
| 246 | cout << "Publishing 100000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl; |
| 247 | cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 100000.0 << " milliseconds" << endl; |
| 248 | teardown(); |
| 249 | } |
Zhenkai Zhu | d5d99be | 2013-03-13 19:15:56 -0700 | [diff] [blame] | 250 | */ |
Zhenkai Zhu | 6fe6d88 | 2013-01-23 21:33:53 -0800 | [diff] [blame] | 251 | |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame] | 252 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 253 | BOOST_AUTO_TEST_SUITE_END() |