Giulio Grassi | 624f6c6 | 2014-02-18 19:42:14 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "face/udp-factory.hpp" |
| 8 | #include "core/scheduler.hpp" |
| 9 | #include "core/face-uri.hpp" |
| 10 | #include "tests/test-common.hpp" |
| 11 | |
| 12 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 13 | #include <boost/asio.hpp> |
| 14 | |
| 15 | namespace nfd { |
| 16 | namespace tests { |
| 17 | |
| 18 | BOOST_FIXTURE_TEST_SUITE(FaceUdp, BaseFixture) |
| 19 | |
| 20 | class FactoryErrorCheck : protected BaseFixture |
| 21 | { |
| 22 | public: |
| 23 | bool isTheSameMulticastEndpoint(const UdpFactory::Error& e) { |
| 24 | return strcmp(e.what(), |
| 25 | "Cannot create the requested UDP unicast channel, local " |
| 26 | "endpoint is already allocated for a UDP multicast face") == 0; |
| 27 | } |
| 28 | |
| 29 | bool isNotMulticastAddress(const UdpFactory::Error& e) { |
| 30 | return strcmp(e.what(), |
| 31 | "Cannot create the requested UDP multicast face, " |
| 32 | "the multicast group given as input is not a multicast address") == 0; |
| 33 | } |
| 34 | |
| 35 | bool isTheSameUnicastEndpoint(const UdpFactory::Error& e) { |
| 36 | return strcmp(e.what(), |
| 37 | "Cannot create the requested UDP multicast face, local " |
| 38 | "endpoint is already allocated for a UDP unicast channel") == 0; |
| 39 | } |
| 40 | |
| 41 | bool isLocalEndpointOnDifferentGroup(const UdpFactory::Error& e) { |
| 42 | return strcmp(e.what(), |
| 43 | "Cannot create the requested UDP multicast face, local " |
| 44 | "endpoint is already allocated for a UDP multicast face " |
| 45 | "on a different multicast group") == 0; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | BOOST_FIXTURE_TEST_CASE(ChannelMapUdp, FactoryErrorCheck) |
| 50 | { |
| 51 | using boost::asio::ip::udp; |
| 52 | |
| 53 | UdpFactory factory = UdpFactory(); |
| 54 | |
| 55 | //to instantiate multicast face on a specific ip address, change interfaceIp |
| 56 | std::string interfaceIp = "0.0.0.0"; |
| 57 | |
| 58 | shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 59 | shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
| 60 | BOOST_CHECK_EQUAL(channel1, channel1a); |
| 61 | |
| 62 | shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 63 | BOOST_CHECK_NE(channel1, channel2); |
| 64 | |
| 65 | shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070"); |
| 66 | |
| 67 | //same endpoint of a unicast channel |
| 68 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, |
| 69 | "224.0.0.1", |
| 70 | "20070"), |
| 71 | UdpFactory::Error, |
| 72 | isTheSameUnicastEndpoint); |
| 73 | |
| 74 | |
| 75 | shared_ptr<MulticastUdpFace> multicastFace1 = factory.createMulticastFace(interfaceIp, |
| 76 | "224.0.0.1", |
| 77 | "20072"); |
| 78 | shared_ptr<MulticastUdpFace> multicastFace1a = factory.createMulticastFace(interfaceIp, |
| 79 | "224.0.0.1", |
| 80 | "20072"); |
| 81 | BOOST_CHECK_EQUAL(multicastFace1, multicastFace1a); |
| 82 | |
| 83 | |
| 84 | //same endpoint of a multicast face |
| 85 | BOOST_CHECK_EXCEPTION(factory.createChannel(interfaceIp, "20072"), |
| 86 | UdpFactory::Error, |
| 87 | isTheSameMulticastEndpoint); |
| 88 | |
| 89 | //same multicast endpoint, different group |
| 90 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, |
| 91 | "224.0.0.42", |
| 92 | "20072"), |
| 93 | UdpFactory::Error, |
| 94 | isLocalEndpointOnDifferentGroup); |
| 95 | |
| 96 | BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp, |
| 97 | "192.168.10.15", |
| 98 | "20025"), |
| 99 | UdpFactory::Error, |
| 100 | isNotMulticastAddress); |
| 101 | |
| 102 | |
| 103 | |
| 104 | // //Test commented because it required to be run in a machine that can resolve ipv6 query |
| 105 | // shared_ptr<UdpChannel> channel1v6 = factory.createChannel(//"::1", |
| 106 | // "fe80::5e96:9dff:fe7d:9c8d%en1", |
| 107 | // //"fe80::aa54:b2ff:fe08:27b8%wlan0", |
| 108 | // "20070"); |
| 109 | // |
| 110 | // //the creation of multicastFace2 works properly. It has been disable because it needs an IP address of |
| 111 | // //an available network interface (different from the first one used) |
| 112 | // shared_ptr<MulticastUdpFace> multicastFace2 = factory.createMulticastFace("192.168.1.17", |
| 113 | // "224.0.0.1", |
| 114 | // "20073"); |
| 115 | // BOOST_CHECK_NE(multicastFace1, multicastFace2); |
| 116 | // |
| 117 | // |
| 118 | // //ipv6 - work in progress |
| 119 | // shared_ptr<MulticastUdpFace> multicastFace3 = factory.createMulticastFace("fe80::5e96:9dff:fe7d:9c8d%en1", |
| 120 | // "FF01:0:0:0:0:0:0:2", |
| 121 | // "20073"); |
| 122 | // |
| 123 | // shared_ptr<MulticastUdpFace> multicastFace4 = factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0", |
| 124 | // "FF01:0:0:0:0:0:0:2", |
| 125 | // "20073"); |
| 126 | // |
| 127 | // BOOST_CHECK_EQUAL(multicastFace3, multicastFace4); |
| 128 | // |
| 129 | // shared_ptr<MulticastUdpFace> multicastFace5 = factory.createMulticastFace("::1", |
| 130 | // "FF01:0:0:0:0:0:0:2", |
| 131 | // "20073"); |
| 132 | // |
| 133 | // BOOST_CHECK_NE(multicastFace3, multicastFace5); |
| 134 | // |
| 135 | // //same local ipv6 endpoint for a different multicast group |
| 136 | // BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0", |
| 137 | // "FE01:0:0:0:0:0:0:2", |
| 138 | // "20073"), |
| 139 | // UdpFactory::Error); |
| 140 | // |
| 141 | // //same local ipv6 (expect for th port number) endpoint for a different multicast group |
| 142 | // BOOST_CHECK_THROW(factory.createMulticastFace("fe80::aa54:b2ff:fe08:27b8%wlan0", |
| 143 | // "FE01:0:0:0:0:0:0:2", |
| 144 | // "20075"), |
| 145 | // UdpFactory::Error); |
| 146 | // |
| 147 | // BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff", |
| 148 | // "FE12:0:0:0:0:0:0:2", |
| 149 | // "20075"), |
| 150 | // UdpFactory::Error); |
| 151 | // |
| 152 | // //not a multicast ipv6 |
| 153 | // BOOST_CHECK_THROW(factory.createMulticastFace("fa80::20a:9dff:fef6:12ff", |
| 154 | // "A112:0:0:0:0:0:0:2", |
| 155 | // "20075"), |
| 156 | // UdpFactory::Error); |
| 157 | |
| 158 | |
| 159 | |
| 160 | } |
| 161 | |
| 162 | class EndToEndFixture : protected BaseFixture |
| 163 | { |
| 164 | public: |
| 165 | void |
| 166 | channel1_onFaceCreated(const shared_ptr<UdpFace>& newFace) |
| 167 | { |
| 168 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 169 | m_face1 = newFace; |
| 170 | m_face1->onReceiveInterest += |
| 171 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
| 172 | m_face1->onReceiveData += |
| 173 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
| 174 | m_face1->onFail += |
| 175 | bind(&EndToEndFixture::face1_onFail, this); |
| 176 | BOOST_CHECK_MESSAGE(true, "channel 1 face created"); |
| 177 | |
| 178 | m_faces.push_back(m_face1); |
| 179 | |
| 180 | this->afterIo(); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | channel1_onConnectFailed(const std::string& reason) |
| 185 | { |
| 186 | BOOST_CHECK_MESSAGE(false, reason); |
| 187 | |
| 188 | this->afterIo(); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | face1_onReceiveInterest(const Interest& interest) |
| 193 | { |
| 194 | m_face1_receivedInterests.push_back(interest); |
| 195 | |
| 196 | this->afterIo(); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | face1_onReceiveData(const Data& data) |
| 201 | { |
| 202 | m_face1_receivedDatas.push_back(data); |
| 203 | |
| 204 | this->afterIo(); |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | face1_onFail() |
| 209 | { |
| 210 | m_face1.reset(); |
| 211 | this->afterIo(); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
| 216 | { |
| 217 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
| 218 | m_face2 = newFace; |
| 219 | m_face2->onReceiveInterest += |
| 220 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
| 221 | m_face2->onReceiveData += |
| 222 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
| 223 | m_face2->onFail += |
| 224 | bind(&EndToEndFixture::face2_onFail, this); |
| 225 | |
| 226 | m_faces.push_back(m_face2); |
| 227 | |
| 228 | BOOST_CHECK_MESSAGE(true, "channel 2 face created"); |
| 229 | this->afterIo(); |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | channel2_onConnectFailed(const std::string& reason) |
| 234 | { |
| 235 | BOOST_CHECK_MESSAGE(false, reason); |
| 236 | |
| 237 | this->afterIo(); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | face2_onReceiveInterest(const Interest& interest) |
| 242 | { |
| 243 | m_face2_receivedInterests.push_back(interest); |
| 244 | |
| 245 | this->afterIo(); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | face2_onReceiveData(const Data& data) |
| 250 | { |
| 251 | m_face2_receivedDatas.push_back(data); |
| 252 | |
| 253 | this->afterIo(); |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | face2_onFail() |
| 258 | { |
| 259 | m_face2.reset(); |
| 260 | this->afterIo(); |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | channel3_onFaceCreated(const shared_ptr<Face>& newFace) |
| 265 | { |
| 266 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 267 | m_face3 = newFace; |
| 268 | m_faces.push_back(newFace); |
| 269 | |
| 270 | this->afterIo(); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void |
| 275 | channel_onFaceCreated(const shared_ptr<UdpFace>& newFace) |
| 276 | { |
| 277 | m_faces.push_back(newFace); |
| 278 | this->afterIo(); |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | channel_onConnectFailed(const std::string& reason) |
| 283 | { |
| 284 | BOOST_CHECK_MESSAGE(false, reason); |
| 285 | |
| 286 | this->afterIo(); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | channel_onConnectFailedOk(const std::string& reason) |
| 291 | { |
| 292 | //it's ok, it was supposed to fail |
| 293 | this->afterIo(); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | checkFaceList(size_t shouldBe) |
| 298 | { |
| 299 | BOOST_CHECK_EQUAL(m_faces.size(), shouldBe); |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | abortTestCase(const std::string& message) |
| 304 | { |
| 305 | g_io.stop(); |
| 306 | BOOST_FAIL(message); |
| 307 | } |
| 308 | |
| 309 | private: |
| 310 | void |
| 311 | afterIo() |
| 312 | { |
| 313 | if (--m_ioRemaining <= 0) g_io.stop(); |
| 314 | } |
| 315 | |
| 316 | public: |
| 317 | int m_ioRemaining; |
| 318 | |
| 319 | shared_ptr<Face> m_face1; |
| 320 | std::vector<Interest> m_face1_receivedInterests; |
| 321 | std::vector<Data> m_face1_receivedDatas; |
| 322 | shared_ptr<Face> m_face2; |
| 323 | std::vector<Interest> m_face2_receivedInterests; |
| 324 | std::vector<Data> m_face2_receivedDatas; |
| 325 | |
| 326 | shared_ptr<Face> m_face3; |
| 327 | |
| 328 | |
| 329 | std::list< shared_ptr<Face> > m_faces; |
| 330 | }; |
| 331 | |
| 332 | |
| 333 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 334 | { |
| 335 | UdpFactory factory = UdpFactory(); |
| 336 | Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 337 | |
| 338 | EventId abortEvent = |
| 339 | scheduler.scheduleEvent(time::seconds(1), |
| 340 | bind(&EndToEndFixture::abortTestCase, this, |
| 341 | "UdpChannel error: cannot connect or cannot accept connection")); |
| 342 | |
| 343 | m_ioRemaining = 1; |
| 344 | |
| 345 | shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 346 | |
| 347 | |
| 348 | //channel2->connect("127.0.0.1", "20070", |
| 349 | // bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 350 | // bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 351 | |
| 352 | factory.createFace(FaceUri("udp4://127.0.0.1:20070"), |
| 353 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 354 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 355 | |
| 356 | |
| 357 | g_io.run(); |
| 358 | g_io.reset(); |
| 359 | scheduler.cancelEvent(abortEvent); |
| 360 | |
| 361 | BOOST_REQUIRE(static_cast<bool>(m_face2)); |
| 362 | |
| 363 | shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 364 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 365 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 366 | |
| 367 | |
| 368 | abortEvent = |
| 369 | scheduler.scheduleEvent(time::seconds(1), |
| 370 | bind(&EndToEndFixture::abortTestCase, this, |
| 371 | "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 372 | |
| 373 | Interest interest1("ndn:/TpnzGvW9R"); |
| 374 | Data data1 ("ndn:/KfczhUqVix"); |
| 375 | data1.setContent(0, 0); |
| 376 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 377 | Data data2 ("ndn:/XNBV796f"); |
| 378 | data2.setContent(0, 0); |
| 379 | Interest interest3("ndn:/QWiIhjgkj5sL"); |
| 380 | Data data3 ("ndn:/XNBV794f"); |
| 381 | data3.setContent(0, 0); |
| 382 | |
| 383 | |
| 384 | ndn::SignatureSha256WithRsa fakeSignature; |
| 385 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, |
| 386 | reinterpret_cast<const uint8_t*>(0), |
| 387 | 0)); |
| 388 | |
| 389 | // set fake signature on data1 and data2 |
| 390 | data1.setSignature(fakeSignature); |
| 391 | data2.setSignature(fakeSignature); |
| 392 | data3.setSignature(fakeSignature); |
| 393 | |
| 394 | m_face2->sendInterest(interest2); |
| 395 | m_face2->sendData (data2 ); |
| 396 | |
| 397 | m_ioRemaining = 3; //2 send + 1 listen return |
| 398 | g_io.run(); |
| 399 | g_io.reset(); |
| 400 | scheduler.cancelEvent(abortEvent); |
| 401 | |
| 402 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 403 | |
| 404 | m_face1->sendInterest(interest1); |
| 405 | m_face1->sendData (data1 ); |
| 406 | |
| 407 | abortEvent = |
| 408 | scheduler.scheduleEvent(time::seconds(1), |
| 409 | bind(&EndToEndFixture::abortTestCase, this, |
| 410 | "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 411 | m_ioRemaining = 2; |
| 412 | g_io.run(); |
| 413 | g_io.reset(); |
| 414 | scheduler.cancelEvent(abortEvent); |
| 415 | |
| 416 | |
| 417 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 418 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 419 | BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 420 | BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 421 | |
| 422 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 423 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 424 | BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 425 | BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 426 | |
| 427 | |
| 428 | |
| 429 | //checking if the connection accepting mechanism works properly. |
| 430 | |
| 431 | m_face2->sendData (data3 ); |
| 432 | m_face2->sendInterest(interest3); |
| 433 | |
| 434 | abortEvent = |
| 435 | scheduler.scheduleEvent(time::seconds(1), |
| 436 | bind(&EndToEndFixture::abortTestCase, this, |
| 437 | "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 438 | m_ioRemaining = 2; |
| 439 | g_io.run(); |
| 440 | g_io.reset(); |
| 441 | scheduler.cancelEvent(abortEvent); |
| 442 | |
| 443 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2); |
| 444 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2); |
| 445 | |
| 446 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName()); |
| 447 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName()); |
| 448 | |
| 449 | } |
| 450 | |
| 451 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 452 | { |
| 453 | Interest interest1("ndn:/TpnzGvW9R"); |
| 454 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 455 | Interest interest3("ndn:/QWiIhjgkj5sL"); |
| 456 | |
| 457 | UdpFactory factory = UdpFactory(); |
| 458 | Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 459 | |
| 460 | EventId abortEvent = |
| 461 | scheduler.scheduleEvent(time::seconds(4), |
| 462 | bind(&EndToEndFixture::abortTestCase, this, |
| 463 | "UdpChannel error: cannot connect or cannot accept connection")); |
| 464 | |
| 465 | shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 466 | shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 467 | |
| 468 | |
| 469 | |
| 470 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 471 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 472 | |
| 473 | channel2->connect("127.0.0.1", "20070", |
| 474 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 475 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 476 | |
| 477 | |
| 478 | m_ioRemaining = 1; |
| 479 | g_io.run(); |
| 480 | g_io.reset(); |
| 481 | scheduler.cancelEvent(abortEvent); |
| 482 | |
| 483 | BOOST_CHECK_EQUAL(m_faces.size(), 1); |
| 484 | |
| 485 | shared_ptr<UdpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
| 486 | channel3->connect("127.0.0.1", "20070", |
| 487 | bind(&EndToEndFixture::channel3_onFaceCreated, this, _1), |
| 488 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 489 | |
| 490 | shared_ptr<UdpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
| 491 | |
| 492 | BOOST_CHECK_NE(channel3, channel4); |
| 493 | |
| 494 | scheduler |
| 495 | .scheduleEvent |
| 496 | (time::seconds(0.5), |
| 497 | bind(&UdpChannel::connect, channel4, |
| 498 | "127.0.0.1", "20070", |
| 499 | // does not work without static_cast |
| 500 | static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture:: |
| 501 | channel_onFaceCreated, |
| 502 | this, |
| 503 | _1)), |
| 504 | static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture:: |
| 505 | channel_onConnectFailed, |
| 506 | this, |
| 507 | _1)))); |
| 508 | |
| 509 | m_ioRemaining = 2; // 2 connects |
| 510 | abortEvent = |
| 511 | scheduler.scheduleEvent(time::seconds(4), |
| 512 | bind(&EndToEndFixture::abortTestCase, this, |
| 513 | "UdpChannel error: cannot connect or cannot accept multiple connections")); |
| 514 | |
| 515 | scheduler.scheduleEvent(time::seconds(0.4), |
| 516 | bind(&EndToEndFixture::checkFaceList, this, 2)); |
| 517 | |
| 518 | g_io.run(); |
| 519 | g_io.reset(); |
| 520 | |
| 521 | BOOST_CHECK_EQUAL(m_faces.size(), 3); |
| 522 | |
| 523 | |
| 524 | m_face2->sendInterest(interest1); |
| 525 | abortEvent = |
| 526 | scheduler.scheduleEvent(time::seconds(1), |
| 527 | bind(&EndToEndFixture::abortTestCase, this, |
| 528 | "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 529 | m_ioRemaining = 1; |
| 530 | g_io.run(); |
| 531 | g_io.reset(); |
| 532 | scheduler.cancelEvent(abortEvent); |
| 533 | |
| 534 | BOOST_CHECK_EQUAL(m_faces.size(), 4); |
| 535 | |
| 536 | m_face3->sendInterest(interest2); |
| 537 | abortEvent = |
| 538 | scheduler.scheduleEvent(time::seconds(1), |
| 539 | bind(&EndToEndFixture::abortTestCase, this, |
| 540 | "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 541 | m_ioRemaining = 1; |
| 542 | g_io.run(); |
| 543 | g_io.reset(); |
| 544 | scheduler.cancelEvent(abortEvent); |
| 545 | |
| 546 | //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest |
| 547 | BOOST_CHECK_EQUAL(m_faces.size(), 5); |
| 548 | BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, |
| 549 | this, |
| 550 | _1), |
| 551 | bind(&EndToEndFixture::channel_onConnectFailedOk, |
| 552 | this, |
| 553 | _1)), |
| 554 | UdpChannel::Error); |
| 555 | } |
| 556 | |
| 557 | //Test commented because it required to be run in a machine that can resolve ipv6 query |
| 558 | //BOOST_FIXTURE_TEST_CASE(EndToEndIpv6, EndToEndFixture) |
| 559 | //{ |
| 560 | // UdpFactory factory = UdpFactory(); |
| 561 | // Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 562 | // |
| 563 | // EventId abortEvent = |
| 564 | // scheduler.scheduleEvent(time::seconds(1), |
| 565 | // bind(&EndToEndFixture::abortTestCase, this, |
| 566 | // "UdpChannel error: cannot connect or cannot accept connection")); |
| 567 | // |
| 568 | // m_ioRemaining = 1; |
| 569 | // |
| 570 | // shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070"); |
| 571 | // shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071"); |
| 572 | // |
| 573 | // channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 574 | // bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 575 | // |
| 576 | // channel2->connect("::1", "20070", |
| 577 | // bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 578 | // bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 579 | // g_io.run(); |
| 580 | // g_io.reset(); |
| 581 | // scheduler.cancelEvent(abortEvent); |
| 582 | // |
| 583 | // BOOST_REQUIRE(static_cast<bool>(m_face2)); |
| 584 | // |
| 585 | // abortEvent = |
| 586 | // scheduler.scheduleEvent(time::seconds(1), |
| 587 | // bind(&EndToEndFixture::abortTestCase, this, |
| 588 | // "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 589 | // |
| 590 | // Interest interest1("ndn:/TpnzGvW9R"); |
| 591 | // Data data1 ("ndn:/KfczhUqVix"); |
| 592 | // data1.setContent(0, 0); |
| 593 | // Interest interest2("ndn:/QWiIMfj5sL"); |
| 594 | // Data data2 ("ndn:/XNBV796f"); |
| 595 | // data2.setContent(0, 0); |
| 596 | // Interest interest3("ndn:/QWiIhjgkj5sL"); |
| 597 | // Data data3 ("ndn:/XNBV794f"); |
| 598 | // data3.setContent(0, 0); |
| 599 | // |
| 600 | // ndn::SignatureSha256WithRsa fakeSignature; |
| 601 | // fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 602 | // |
| 603 | // // set fake signature on data1 and data2 |
| 604 | // data1.setSignature(fakeSignature); |
| 605 | // data2.setSignature(fakeSignature); |
| 606 | // data3.setSignature(fakeSignature); |
| 607 | // |
| 608 | // m_face2->sendInterest(interest2); |
| 609 | // m_face2->sendData (data2 ); |
| 610 | // |
| 611 | // m_ioRemaining = 3; //2 send + 1 listen return |
| 612 | // g_io.run(); |
| 613 | // g_io.reset(); |
| 614 | // scheduler.cancelEvent(abortEvent); |
| 615 | // |
| 616 | // BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 617 | // |
| 618 | // m_face1->sendInterest(interest1); |
| 619 | // m_face1->sendData (data1 ); |
| 620 | // |
| 621 | // abortEvent = |
| 622 | // scheduler.scheduleEvent(time::seconds(1), |
| 623 | // bind(&EndToEndFixture::abortTestCase, this, |
| 624 | // "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 625 | // m_ioRemaining = 2; |
| 626 | // g_io.run(); |
| 627 | // g_io.reset(); |
| 628 | // scheduler.cancelEvent(abortEvent); |
| 629 | // |
| 630 | // BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 631 | // BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 632 | // BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 633 | // BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 634 | // |
| 635 | // BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 636 | // BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 637 | // BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 638 | // BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 639 | // |
| 640 | // //checking if the connection accepting mechanism works properly. |
| 641 | // |
| 642 | // m_face2->sendData (data3 ); |
| 643 | // m_face2->sendInterest(interest3); |
| 644 | // |
| 645 | // abortEvent = |
| 646 | // scheduler.scheduleEvent(time::seconds(1), |
| 647 | // bind(&EndToEndFixture::abortTestCase, this, |
| 648 | // "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 649 | // m_ioRemaining = 2; |
| 650 | // g_io.run(); |
| 651 | // g_io.reset(); |
| 652 | // scheduler.cancelEvent(abortEvent); |
| 653 | // |
| 654 | // BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 2); |
| 655 | // BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 2); |
| 656 | // |
| 657 | // BOOST_CHECK_EQUAL(m_face1_receivedInterests[1].getName(), interest3.getName()); |
| 658 | // BOOST_CHECK_EQUAL(m_face1_receivedDatas [1].getName(), data3.getName()); |
| 659 | // |
| 660 | //} |
| 661 | |
| 662 | |
| 663 | //Test commented because it required to be run in a machine that can resolve ipv6 query |
| 664 | //BOOST_FIXTURE_TEST_CASE(MultipleAcceptsIpv6, EndToEndFixture) |
| 665 | //{ |
| 666 | // Interest interest1("ndn:/TpnzGvW9R"); |
| 667 | // Interest interest2("ndn:/QWiIMfj5sL"); |
| 668 | // Interest interest3("ndn:/QWiIhjgkj5sL"); |
| 669 | // |
| 670 | // UdpFactory factory = UdpFactory(); |
| 671 | // Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 672 | // |
| 673 | // EventId abortEvent = |
| 674 | // scheduler.scheduleEvent(time::seconds(4), |
| 675 | // bind(&EndToEndFixture::abortTestCase, this, |
| 676 | // "UdpChannel error: cannot connect or cannot accept connection")); |
| 677 | // |
| 678 | // shared_ptr<UdpChannel> channel1 = factory.createChannel("::1", "20070"); |
| 679 | // shared_ptr<UdpChannel> channel2 = factory.createChannel("::1", "20071"); |
| 680 | // |
| 681 | // channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 682 | // bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 683 | // |
| 684 | // channel2->connect("::1", "20070", |
| 685 | // bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 686 | // bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 687 | // |
| 688 | // m_ioRemaining = 1; |
| 689 | // g_io.run(); |
| 690 | // g_io.reset(); |
| 691 | // scheduler.cancelEvent(abortEvent); |
| 692 | // |
| 693 | // BOOST_CHECK_EQUAL(m_faces.size(), 1); |
| 694 | // |
| 695 | // shared_ptr<UdpChannel> channel3 = factory.createChannel("::1", "20072"); |
| 696 | // channel3->connect("::1", "20070", |
| 697 | // bind(&EndToEndFixture::channel3_onFaceCreated, this, _1), |
| 698 | // bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 699 | // |
| 700 | // shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20073"); |
| 701 | // |
| 702 | // BOOST_CHECK_NE(channel3, channel4); |
| 703 | // |
| 704 | // scheduler |
| 705 | // .scheduleEvent(time::seconds(0.5), |
| 706 | // bind(&UdpChannel::connect, channel4, |
| 707 | // "::1", "20070", |
| 708 | // static_cast<UdpChannel::FaceCreatedCallback>(bind(&EndToEndFixture:: |
| 709 | // channel_onFaceCreated, this, _1)), |
| 710 | // static_cast<UdpChannel::ConnectFailedCallback>(bind(&EndToEndFixture:: |
| 711 | // channel_onConnectFailed, this, _1)))); |
| 712 | // |
| 713 | // m_ioRemaining = 2; // 2 connects |
| 714 | // abortEvent = |
| 715 | // scheduler.scheduleEvent(time::seconds(4), |
| 716 | // bind(&EndToEndFixture::abortTestCase, this, |
| 717 | // "UdpChannel error: cannot connect or cannot accept multiple connections")); |
| 718 | // |
| 719 | // scheduler.scheduleEvent(time::seconds(0.4), |
| 720 | // bind(&EndToEndFixture::checkFaceList, this, 2)); |
| 721 | // |
| 722 | // g_io.run(); |
| 723 | // g_io.reset(); |
| 724 | // |
| 725 | // BOOST_CHECK_EQUAL(m_faces.size(), 3); |
| 726 | // |
| 727 | // |
| 728 | // m_face2->sendInterest(interest1); |
| 729 | // abortEvent = |
| 730 | // scheduler.scheduleEvent(time::seconds(1), |
| 731 | // bind(&EndToEndFixture::abortTestCase, this, |
| 732 | // "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 733 | // m_ioRemaining = 1; |
| 734 | // g_io.run(); |
| 735 | // g_io.reset(); |
| 736 | // scheduler.cancelEvent(abortEvent); |
| 737 | // |
| 738 | // BOOST_CHECK_EQUAL(m_faces.size(), 4); |
| 739 | // |
| 740 | // m_face3->sendInterest(interest2); |
| 741 | // abortEvent = |
| 742 | // scheduler.scheduleEvent(time::seconds(1), |
| 743 | // bind(&EndToEndFixture::abortTestCase, this, |
| 744 | // "UdpChannel error: cannot send or receive Interest/Data packets")); |
| 745 | // m_ioRemaining = 1; |
| 746 | // g_io.run(); |
| 747 | // g_io.reset(); |
| 748 | // scheduler.cancelEvent(abortEvent); |
| 749 | // |
| 750 | // //channel1 should have created 2 faces, one when m_face2 sent an interest, one when m_face3 sent an interest |
| 751 | // BOOST_CHECK_EQUAL(m_faces.size(), 5); |
| 752 | // BOOST_CHECK_THROW(channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, |
| 753 | // this, |
| 754 | // _1), |
| 755 | // bind(&EndToEndFixture::channel_onConnectFailedOk, |
| 756 | // this, |
| 757 | // _1)), |
| 758 | // UdpChannel::Error); |
| 759 | //} |
| 760 | |
| 761 | |
| 762 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 763 | { |
| 764 | UdpFactory factory = UdpFactory(); |
| 765 | Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 766 | |
| 767 | EventId abortEvent = |
| 768 | scheduler.scheduleEvent(time::seconds(4), |
| 769 | bind(&EndToEndFixture::abortTestCase, this, |
| 770 | "UdpChannel error: cannot connect or cannot accept connection")); |
| 771 | |
| 772 | shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 773 | shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
| 774 | |
| 775 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 776 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 777 | |
| 778 | channel2->connect("127.0.0.1", "20070", |
| 779 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 780 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 781 | |
| 782 | m_ioRemaining = 1; |
| 783 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
| 784 | g_io.reset(); |
| 785 | scheduler.cancelEvent(abortEvent); |
| 786 | |
| 787 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 788 | |
| 789 | abortEvent = |
| 790 | scheduler.scheduleEvent(time::seconds(4), |
| 791 | bind(&EndToEndFixture::abortTestCase, this, |
| 792 | "FaceClosing error: cannot properly close faces")); |
| 793 | |
| 794 | BOOST_CHECK(static_cast<bool>(m_face2)); |
| 795 | |
| 796 | m_ioRemaining = 1; |
| 797 | // just double check that we are calling the virtual method |
| 798 | static_pointer_cast<Face>(m_face2)->close(); |
| 799 | |
| 800 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
| 801 | g_io.reset(); |
| 802 | scheduler.cancelEvent(abortEvent); |
| 803 | |
| 804 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
| 805 | |
| 806 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 807 | } |
| 808 | |
| 809 | //BOOST_FIXTURE_TEST_CASE(MulticastFace, EndToEndFixture) |
| 810 | //{ |
| 811 | // //to instantiate multicast face on a specific ip address, change interfaceIp |
| 812 | // std::string interfaceIp = "0.0.0.0"; |
| 813 | // |
| 814 | // UdpFactory factory = UdpFactory(); |
| 815 | // Scheduler scheduler(g_io); // to limit the amount of time the test may take |
| 816 | // |
| 817 | // shared_ptr<MulticastUdpFace> multicastFace1 = |
| 818 | // factory.createMulticastFace(interfaceIp, "224.0.0.1", "20072"); |
| 819 | // |
| 820 | // BOOST_REQUIRE(static_cast<bool>(multicastFace1)); |
| 821 | // |
| 822 | // channel1_onFaceCreated(multicastFace1); |
| 823 | // |
| 824 | // |
| 825 | // EventId abortEvent = |
| 826 | // scheduler.scheduleEvent(time::seconds(10), |
| 827 | // bind(&EndToEndFixture::abortTestCase, this, |
| 828 | // "MulticastFace error: cannot send or receive Interest/Data packets")); |
| 829 | // |
| 830 | // Interest interest1("ndn:/TpnzGvW9R"); |
| 831 | // Data data1 ("ndn:/KfczhUqVix"); |
| 832 | // data1.setContent(0, 0); |
| 833 | // Interest interest2("ndn:/QWiIMfj5sL"); |
| 834 | // Data data2 ("ndn:/XNBV796f"); |
| 835 | // data2.setContent(0, 0); |
| 836 | // Interest interest3("ndn:/QWiIhjgkj5sL"); |
| 837 | // Data data3 ("ndn:/XNBV794f"); |
| 838 | // data3.setContent(0, 0); |
| 839 | // |
| 840 | // |
| 841 | // ndn::SignatureSha256WithRsa fakeSignature; |
| 842 | // fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 843 | // |
| 844 | // // set fake signature on data1 and data2 |
| 845 | // data1.setSignature(fakeSignature); |
| 846 | // data2.setSignature(fakeSignature); |
| 847 | // data3.setSignature(fakeSignature); |
| 848 | // |
| 849 | // multicastFace1->sendInterest(interest1); |
| 850 | // multicastFace1->sendData (data1 ); |
| 851 | // |
| 852 | // g_io.reset(); |
| 853 | // m_ioRemaining = 2; |
| 854 | // g_io.run(); |
| 855 | // g_io.reset(); |
| 856 | // scheduler.cancelEvent(abortEvent); |
| 857 | // |
| 858 | // BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 859 | // BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 860 | // |
| 861 | // BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest1.getName()); |
| 862 | // BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data1.getName()); |
| 863 | //} |
| 864 | |
| 865 | |
| 866 | |
| 867 | BOOST_AUTO_TEST_SUITE_END() |
| 868 | } // namespace tests |
| 869 | } // namespace nfd |