Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include "ndn.cxx.h" |
| 15 | #include <unistd.h> |
| 16 | #include <fstream> |
| 17 | |
| 18 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 19 | #include <boost/test/unit_test.hpp> |
| 20 | #include <boost/make_shared.hpp> |
| 21 | |
| 22 | #include "logging.h" |
| 23 | |
| 24 | using namespace ndn; |
| 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(WrapperTests) |
| 29 | |
| 30 | WrapperPtr c1; |
| 31 | WrapperPtr c2; |
| 32 | int g_timeout_counter = 0; |
| 33 | int g_dataCallback_counter = 0; |
| 34 | |
| 35 | void publish1(InterestPtr interest) |
| 36 | { |
| 37 | string content = interest->getName ().toUri(); |
| 38 | c1->publishData(interest->getName (), (const unsigned char*)content.c_str(), content.size(), 5); |
| 39 | } |
| 40 | |
| 41 | void publish2(InterestPtr interest) |
| 42 | { |
| 43 | string content = interest->getName ().toUri(); |
| 44 | c2->publishData(interest->getName (), (const unsigned char*)content.c_str(), content.size(), 5); |
| 45 | } |
| 46 | |
| 47 | void dataCallback(const Name &name, ndn::PcoPtr pco) |
| 48 | { |
| 49 | cout << " in data callback" << endl; |
| 50 | BytesPtr content = pco->contentPtr (); |
| 51 | string msg(reinterpret_cast<const char *> (head (*content)), content->size()); |
| 52 | g_dataCallback_counter ++; |
| 53 | BOOST_CHECK_EQUAL(name, msg); |
| 54 | } |
| 55 | |
| 56 | void encapCallback(const Name &name, ndn::PcoPtr pco) |
| 57 | { |
| 58 | cout << " in encap data callback" << endl; |
| 59 | BOOST_CHECK(!c1->verify(pco)); |
| 60 | cout << "++++++++++++++++++ Outer content couldn't be verified, which is expected." << endl; |
| 61 | PcoPtr npco = make_shared<ParsedContentObject> (*(pco->contentPtr())); |
| 62 | g_dataCallback_counter ++; |
| 63 | BOOST_CHECK(npco); |
| 64 | BOOST_CHECK(c1->verify(npco)); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | timeout(const Name &name, const Closure &closure, InterestPtr origInterest) |
| 69 | { |
| 70 | g_timeout_counter ++; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | setup() |
| 75 | { |
| 76 | if (!c1) |
| 77 | { |
| 78 | c1 = make_shared<Wrapper> (); |
| 79 | } |
| 80 | if (!c2) |
| 81 | { |
| 82 | c2 = make_shared<Wrapper> (); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | teardown() |
| 88 | { |
| 89 | if (c1) |
| 90 | { |
| 91 | c1.reset(); |
| 92 | } |
| 93 | if (c2) |
| 94 | { |
| 95 | c2.reset(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | BOOST_AUTO_TEST_CASE (Basic) |
| 101 | { |
| 102 | INIT_LOGGERS (); |
| 103 | |
| 104 | setup(); |
| 105 | Name prefix1("/c1"); |
| 106 | Name prefix2("/c2"); |
| 107 | |
| 108 | c1->setInterestFilter(prefix1, bind(publish1, _1)); |
| 109 | usleep(100000); |
| 110 | c2->setInterestFilter(prefix2, bind(publish2, _1)); |
| 111 | |
| 112 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 113 | |
| 114 | c1->sendInterest(Name("/c2/hi"), closure); |
| 115 | usleep(100000); |
| 116 | c2->sendInterest(Name("/c1/hi"), closure); |
| 117 | sleep(1); |
| 118 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
| 119 | |
| 120 | // reset |
| 121 | g_dataCallback_counter = 0; |
| 122 | g_timeout_counter = 0; |
| 123 | |
| 124 | teardown(); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE (Selector) |
| 128 | { |
| 129 | setup(); |
| 130 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 131 | |
| 132 | Interest interest; |
| 133 | interest |
| 134 | .setInterestLifetime(1) |
| 135 | .setChildSelector (Interest::CHILD_RIGHT); |
| 136 | |
| 137 | string n1 = "/random/01"; |
| 138 | c1->sendInterest (Interest (interest).setName (Name(n1)), closure); |
| 139 | sleep(2); |
| 140 | c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
| 141 | usleep(100000); |
| 142 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 143 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 0); |
| 144 | |
| 145 | string n2 = "/random/02"; |
| 146 | interest.setInterestLifetime(2); |
| 147 | c1->sendInterest(Interest (interest).setName (Name(n2)), closure); |
| 148 | sleep(1); |
| 149 | c2->publishData(Name(n2), (const unsigned char *)n2.c_str(), n2.size(), 1); |
| 150 | usleep(100000); |
| 151 | BOOST_CHECK_EQUAL(g_timeout_counter, 1); |
| 152 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 153 | |
| 154 | // reset |
| 155 | g_dataCallback_counter = 0; |
| 156 | g_timeout_counter = 0; |
| 157 | |
| 158 | teardown(); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | reexpress(const Name &name, const Closure &closure, InterestPtr origInterest) |
| 164 | { |
| 165 | g_timeout_counter ++; |
| 166 | c1->sendInterest (*origInterest, closure); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE (Timeout) |
| 170 | { |
| 171 | setup(); |
| 172 | g_dataCallback_counter = 0; |
| 173 | g_timeout_counter = 0; |
| 174 | Closure closure (bind(dataCallback, _1, _2), bind(reexpress, _1, _2, _3)); |
| 175 | |
| 176 | string n1 = "/random/04"; |
| 177 | |
| 178 | Interest interest; |
| 179 | interest |
| 180 | .setInterestLifetime(1) |
| 181 | .setName (n1); |
| 182 | |
| 183 | c1->sendInterest(interest, closure); |
| 184 | usleep(3500000); |
| 185 | c2->publishData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
| 186 | usleep(100000); |
| 187 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 188 | BOOST_CHECK_EQUAL(g_timeout_counter, 3); |
| 189 | teardown(); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE (Unsigned) |
| 193 | { |
| 194 | setup(); |
| 195 | string n1 = "/xxxxxx/unsigned/001"; |
| 196 | Closure closure (bind(dataCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 197 | |
| 198 | g_dataCallback_counter = 0; |
| 199 | c1->sendInterest(Name(n1), closure); |
| 200 | usleep(100000); |
| 201 | c2->publishUnsignedData(Name(n1), (const unsigned char *)n1.c_str(), n1.size(), 1); |
| 202 | usleep(100000); |
| 203 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 1); |
| 204 | |
| 205 | string n2 = "/xxxxxx/signed/001"; |
| 206 | Bytes content = c1->createContentObject(Name(n1), (const unsigned char *)n2.c_str(), n2.size(), 1); |
| 207 | |
| 208 | c1->publishUnsignedData(Name(n2), head(content), content.size(), 1); |
| 209 | Closure encapClosure(bind(encapCallback, _1, _2), bind(timeout, _1, _2, _3)); |
| 210 | c2->sendInterest(Name(n2), encapClosure); |
| 211 | usleep(4000000); |
| 212 | BOOST_CHECK_EQUAL(g_dataCallback_counter, 2); |
| 213 | teardown(); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /* |
| 218 | BOOST_AUTO_TEST_CASE (ndnWrapperUnsigningTest) |
| 219 | { |
| 220 | setup(); |
| 221 | Bytes data; |
| 222 | data.resize(1024); |
| 223 | for (int i = 0; i < 1024; i++) |
| 224 | { |
| 225 | data[i] = 'm'; |
| 226 | } |
| 227 | |
| 228 | Name name("/unsigningtest"); |
| 229 | |
| 230 | posix_time::ptime start = posix_time::second_clock::local_time(); |
| 231 | for (uint64_t i = 0; i < 100000; i++) |
| 232 | { |
| 233 | Name n = name; |
| 234 | n.appendComp(i); |
| 235 | c1->publishUnsignedData(n, data, 10); |
| 236 | } |
| 237 | posix_time::ptime end = posix_time::second_clock::local_time(); |
| 238 | |
| 239 | posix_time::time_duration duration = end - start; |
| 240 | |
| 241 | cout << "Publishing 100000 1K size content objects costs " <<duration.total_milliseconds() << " milliseconds" << endl; |
| 242 | cout << "Average time to publish one content object is " << (double) duration.total_milliseconds() / 100000.0 << " milliseconds" << endl; |
| 243 | teardown(); |
| 244 | } |
| 245 | */ |
| 246 | |
| 247 | |
| 248 | BOOST_AUTO_TEST_SUITE_END() |