Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 13 | #include "face.hpp" |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 14 | #include "util/scheduler.hpp" |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 16 | #include "boost-test.hpp" |
| 17 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 18 | namespace ndn { |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 20 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 21 | class FacesFixture |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 22 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 23 | public: |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 24 | FacesFixture() |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 25 | : nData(0) |
| 26 | , nTimeouts(0) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 27 | , regPrefixId(0) |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 28 | , nInInterests(0) |
| 29 | , nInInterests2(0) |
| 30 | , nRegFailures(0) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 31 | { |
| 32 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 34 | void |
| 35 | onData() |
| 36 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 37 | ++nData; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
| 41 | onTimeout() |
| 42 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 43 | ++nTimeouts; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 47 | onInterest(Face& face, |
| 48 | const Name&, const Interest&) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 50 | ++nInInterests; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 51 | |
| 52 | face.unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 56 | onInterest2(Face& face, |
| 57 | const Name&, const Interest&) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 59 | ++nInInterests2; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 60 | |
| 61 | face.unsetInterestFilter(regPrefixId2); |
| 62 | } |
| 63 | |
| 64 | void |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 65 | onInterestRegex(Face& face, |
| 66 | const InterestFilter&, const Interest&) |
| 67 | { |
| 68 | ++nInInterests; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | onInterestRegexError(Face& face, |
| 73 | const Name&, const Interest&) |
| 74 | { |
| 75 | BOOST_FAIL("InterestFilter::Error should have been triggered"); |
| 76 | } |
| 77 | |
| 78 | void |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 79 | onRegFailed() |
| 80 | { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 81 | ++nRegFailures; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
| 85 | expressInterest(Face& face, const Name& name) |
| 86 | { |
| 87 | Interest i(name); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | i.setInterestLifetime(time::milliseconds(50)); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 89 | face.expressInterest(i, |
| 90 | bind(&FacesFixture::onData, this), |
| 91 | bind(&FacesFixture::onTimeout, this)); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | terminate(Face& face) |
| 96 | { |
| 97 | face.shutdown(); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 100 | uint32_t nData; |
| 101 | uint32_t nTimeouts; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 102 | |
| 103 | const RegisteredPrefixId* regPrefixId; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 104 | const RegisteredPrefixId* regPrefixId2; |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 105 | uint32_t nInInterests; |
| 106 | uint32_t nInInterests2; |
| 107 | uint32_t nRegFailures; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 110 | BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture) |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(Unix) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 113 | { |
| 114 | Face face; |
| 115 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 116 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
| 117 | bind(&FacesFixture::onData, this), |
| 118 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 119 | |
| 120 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 121 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 122 | BOOST_CHECK_EQUAL(nData, 1); |
| 123 | BOOST_CHECK_EQUAL(nTimeouts, 0); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 125 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 126 | time::milliseconds(50)), |
| 127 | bind(&FacesFixture::onData, this), |
| 128 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 130 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 131 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 132 | BOOST_CHECK_EQUAL(nData, 1); |
| 133 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 136 | BOOST_AUTO_TEST_CASE(Tcp) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 137 | { |
| 138 | Face face("localhost"); |
| 139 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 140 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 141 | bind(&FacesFixture::onData, this), |
| 142 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 143 | |
| 144 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 145 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 146 | BOOST_CHECK_EQUAL(nData, 1); |
| 147 | BOOST_CHECK_EQUAL(nTimeouts, 0); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 149 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 150 | time::milliseconds(50)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 151 | bind(&FacesFixture::onData, this), |
| 152 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 153 | |
| 154 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 155 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 156 | BOOST_CHECK_EQUAL(nData, 1); |
| 157 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 161 | BOOST_AUTO_TEST_CASE(SetFilter) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 162 | { |
| 163 | Face face; |
| 164 | Face face2(face.ioService()); |
| 165 | Scheduler scheduler(*face.ioService()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 166 | scheduler.scheduleEvent(time::milliseconds(300), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 167 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 169 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 170 | bind(&FacesFixture::onInterest, this, ref(face), _1, _2), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 171 | bind(&FacesFixture::onRegFailed, this)); |
| 172 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 173 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 174 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 175 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 176 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 177 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 178 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 179 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 180 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 181 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 182 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 185 | BOOST_AUTO_TEST_CASE(SetTwoFilters) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 186 | { |
| 187 | Face face; |
| 188 | Face face2(face.ioService()); |
| 189 | Scheduler scheduler(*face.ioService()); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 190 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 191 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 193 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 194 | bind(&FacesFixture::onInterest, this, ref(face), _1, _2), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 195 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 196 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 197 | regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 198 | bind(&FacesFixture::onInterest2, this, ref(face), _1, _2), |
| 199 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 200 | |
| 201 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 202 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 203 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 204 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 205 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 206 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 207 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 208 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 209 | BOOST_CHECK_EQUAL(nInInterests, 1); |
| 210 | BOOST_CHECK_EQUAL(nInInterests2, 0); |
| 211 | BOOST_CHECK_EQUAL(nTimeouts, 1); |
| 212 | BOOST_CHECK_EQUAL(nData, 0); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame^] | 215 | BOOST_AUTO_TEST_CASE(SetRegexFilterError) |
| 216 | { |
| 217 | Face face; |
| 218 | Face face2(face.getIoService()); |
| 219 | Scheduler scheduler(*face.ioService()); |
| 220 | scheduler.scheduleEvent(time::seconds(1), |
| 221 | bind(&FacesFixture::terminate, this, ref(face))); |
| 222 | |
| 223 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 224 | bind(&FacesFixture::onInterestRegexError, this, |
| 225 | ref(face), _1, _2), |
| 226 | bind(&FacesFixture::onRegFailed, this)); |
| 227 | |
| 228 | scheduler.scheduleEvent(time::milliseconds(300), |
| 229 | bind(&FacesFixture::expressInterest, this, |
| 230 | ref(face2), Name("/Hello/World/XXX/b/c"))); // should match |
| 231 | |
| 232 | BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_CASE(SetRegexFilter) |
| 236 | { |
| 237 | Face face; |
| 238 | Face face2(face.getIoService()); |
| 239 | Scheduler scheduler(*face.ioService()); |
| 240 | scheduler.scheduleEvent(time::seconds(1), |
| 241 | bind(&FacesFixture::terminate, this, ref(face))); |
| 242 | |
| 243 | scheduler.scheduleEvent(time::seconds(2), |
| 244 | bind(&FacesFixture::terminate, this, ref(face))); |
| 245 | |
| 246 | regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"), |
| 247 | bind(&FacesFixture::onInterestRegex, this, |
| 248 | ref(face), _1, _2), |
| 249 | bind(&FacesFixture::onRegFailed, this)); |
| 250 | |
| 251 | scheduler.scheduleEvent(time::milliseconds(200), |
| 252 | bind(&FacesFixture::expressInterest, this, |
| 253 | ref(face2), Name("/Hello/World/a"))); // shouldn't match |
| 254 | |
| 255 | scheduler.scheduleEvent(time::milliseconds(300), |
| 256 | bind(&FacesFixture::expressInterest, this, |
| 257 | ref(face2), Name("/Hello/World/a/b"))); // should match |
| 258 | |
| 259 | scheduler.scheduleEvent(time::milliseconds(400), |
| 260 | bind(&FacesFixture::expressInterest, this, |
| 261 | ref(face2), Name("/Hello/World/a/b/c"))); // should match |
| 262 | |
| 263 | scheduler.scheduleEvent(time::milliseconds(500), |
| 264 | bind(&FacesFixture::expressInterest, this, |
| 265 | ref(face2), Name("/Hello/World/a/b/d"))); // should not match |
| 266 | |
| 267 | face.processEvents(); |
| 268 | // BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 269 | |
| 270 | BOOST_CHECK_EQUAL(nRegFailures, 0); |
| 271 | BOOST_CHECK_EQUAL(nInInterests, 2); |
| 272 | BOOST_CHECK_EQUAL(nTimeouts, 2); |
| 273 | BOOST_CHECK_EQUAL(nData, 0); |
| 274 | } |
| 275 | |
| 276 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 277 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 278 | |
| 279 | } // namespace ndn |