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); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(face->getRemoteUri().toString(), |
| 103 | "ether://" + ethernet::getDefaultMulticastAddress().toString(':')); |
| 104 | BOOST_CHECK_EQUAL(face->getLocalUri().toString(), |
| 105 | "dev://" + m_interfaces.front()->name); |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 106 | |
| 107 | Interest interest1("ndn:/TpnzGvW9R"); |
| 108 | Data data1 ("ndn:/KfczhUqVix"); |
| 109 | data1.setContent(0, 0); |
| 110 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 111 | Data data2 ("ndn:/XNBV796f"); |
| 112 | data2.setContent(0, 0); |
| 113 | |
| 114 | ndn::SignatureSha256WithRsa fakeSignature; |
| 115 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 116 | |
| 117 | // set fake signature on data1 and data2 |
| 118 | data1.setSignature(fakeSignature); |
| 119 | data2.setSignature(fakeSignature); |
| 120 | |
| 121 | BOOST_CHECK_NO_THROW(face->sendInterest(interest1)); |
| 122 | BOOST_CHECK_NO_THROW(face->sendData (data1 )); |
| 123 | BOOST_CHECK_NO_THROW(face->sendInterest(interest2)); |
| 124 | BOOST_CHECK_NO_THROW(face->sendData (data2 )); |
| 125 | |
| 126 | // m_ioRemaining = 4; |
| 127 | // m_ioService.run(); |
| 128 | // m_ioService.reset(); |
| 129 | |
| 130 | // BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 131 | // BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 132 | // BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 133 | // BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 134 | |
| 135 | // BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 136 | // BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 137 | // BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 138 | // BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_SUITE_END() |
| 142 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 143 | } // namespace tests |
Davide Pesavento | 44deacc | 2014-02-19 10:48:07 +0100 | [diff] [blame] | 144 | } // namespace nfd |