Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +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 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 7 | #include "face/ethernet-factory.hpp" |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 8 | #include "core/network-interface.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 9 | #include "tests/test-common.hpp" |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 10 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 11 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 12 | #include <pcap/pcap.h> |
| 13 | |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 14 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 15 | namespace tests { |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 16 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | BOOST_FIXTURE_TEST_SUITE(FaceEthernet, BaseFixture) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 18 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 19 | class InterfacesFixture : protected BaseFixture |
| 20 | { |
| 21 | protected: |
| 22 | InterfacesFixture() |
| 23 | { |
| 24 | std::list< shared_ptr<NetworkInterfaceInfo> > ifs = listNetworkInterfaces(); |
| 25 | for (std::list< shared_ptr<NetworkInterfaceInfo> >::const_iterator i = ifs.begin(); |
| 26 | i != ifs.end(); |
| 27 | ++i) |
| 28 | { |
| 29 | if (!(*i)->isLoopback() && (*i)->isUp()) |
| 30 | { |
| 31 | pcap_t* p = pcap_create((*i)->name.c_str(), 0); |
| 32 | if (!p) |
| 33 | continue; |
| 34 | |
| 35 | if (pcap_activate(p) == 0) |
| 36 | m_interfaces.push_back(*i); |
| 37 | |
| 38 | pcap_close(p); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | std::list< shared_ptr<NetworkInterfaceInfo> > m_interfaces; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | BOOST_FIXTURE_TEST_CASE(MulticastFacesMap, InterfacesFixture) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 50 | EthernetFactory factory; |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 51 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 52 | if (m_interfaces.empty()) |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 53 | { |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 54 | BOOST_WARN_MESSAGE(false, "No interfaces available, cannot perform MulticastFacesMap test"); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 58 | shared_ptr<EthernetFace> face1; |
| 59 | BOOST_REQUIRE_NO_THROW(face1 = factory.createMulticastFace(m_interfaces.front(), |
| 60 | ethernet::getBroadcastAddress())); |
| 61 | shared_ptr<EthernetFace> face1bis; |
| 62 | BOOST_REQUIRE_NO_THROW(face1bis = factory.createMulticastFace(m_interfaces.front(), |
| 63 | ethernet::getBroadcastAddress())); |
| 64 | BOOST_CHECK_EQUAL(face1, face1bis); |
| 65 | |
| 66 | if (m_interfaces.size() > 1) |
| 67 | { |
| 68 | shared_ptr<EthernetFace> face2; |
| 69 | BOOST_REQUIRE_NO_THROW(face2 = factory.createMulticastFace(m_interfaces.back(), |
| 70 | ethernet::getBroadcastAddress())); |
| 71 | BOOST_CHECK_NE(face1, face2); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | BOOST_WARN_MESSAGE(false, "Cannot test second EthernetFace creation, " |
| 76 | "only one interface available"); |
| 77 | } |
| 78 | |
| 79 | shared_ptr<EthernetFace> face3; |
| 80 | BOOST_REQUIRE_NO_THROW(face3 = factory.createMulticastFace(m_interfaces.front(), |
| 81 | ethernet::getDefaultMulticastAddress())); |
| 82 | BOOST_CHECK_NE(face1, face3); |
| 83 | } |
| 84 | |
| 85 | BOOST_FIXTURE_TEST_CASE(SendPacket, InterfacesFixture) |
| 86 | { |
| 87 | EthernetFactory factory; |
| 88 | |
| 89 | if (m_interfaces.empty()) |
| 90 | { |
| 91 | BOOST_WARN_MESSAGE(false, "No interfaces available for pcap, cannot perform SendPacket test"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | shared_ptr<EthernetFace> face = factory.createMulticastFace(m_interfaces.front(), |
| 96 | ethernet::getDefaultMulticastAddress()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 97 | |
| 98 | BOOST_REQUIRE(static_cast<bool>(face)); |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 99 | |
| 100 | BOOST_CHECK(!face->isOnDemand()); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(face->isLocal(), false); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(face->getUri().toString(), |
Davide Pesavento | b60cc12 | 2014-03-19 19:26:02 +0100 | [diff] [blame^] | 103 | "ether://" + m_interfaces.front()->name + "/" + |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 104 | ethernet::getDefaultMulticastAddress().toString(':')); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 105 | |
| 106 | Interest interest1("ndn:/TpnzGvW9R"); |
| 107 | Data data1 ("ndn:/KfczhUqVix"); |
| 108 | data1.setContent(0, 0); |
| 109 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 110 | Data data2 ("ndn:/XNBV796f"); |
| 111 | data2.setContent(0, 0); |
| 112 | |
| 113 | ndn::SignatureSha256WithRsa fakeSignature; |
| 114 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 115 | |
| 116 | // set fake signature on data1 and data2 |
| 117 | data1.setSignature(fakeSignature); |
| 118 | data2.setSignature(fakeSignature); |
| 119 | |
| 120 | BOOST_CHECK_NO_THROW(face->sendInterest(interest1)); |
| 121 | BOOST_CHECK_NO_THROW(face->sendData (data1 )); |
| 122 | BOOST_CHECK_NO_THROW(face->sendInterest(interest2)); |
| 123 | BOOST_CHECK_NO_THROW(face->sendData (data2 )); |
| 124 | |
| 125 | // m_ioRemaining = 4; |
| 126 | // m_ioService.run(); |
| 127 | // m_ioService.reset(); |
| 128 | |
| 129 | // BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 130 | // BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 131 | // BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 132 | // BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 133 | |
| 134 | // BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 135 | // BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 136 | // BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 137 | // BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_SUITE_END() |
| 141 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 142 | } // namespace tests |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 143 | } // namespace nfd |