blob: 1224ee05594039f9779f338307ab47a2350bbbe0 [file] [log] [blame]
Davide Pesavento44deacc2014-02-19 10:48:07 +01001/* -*- 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 Afanasyev0eb70652014-02-27 18:35:07 -08007#include "face/ethernet-factory.hpp"
Davide Pesaventob60cc122014-03-19 19:26:02 +01008#include "core/network-interface.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -07009#include "tests/test-common.hpp"
Davide Pesavento44deacc2014-02-19 10:48:07 +010010
Davide Pesaventob60cc122014-03-19 19:26:02 +010011#include <ndn-cpp-dev/security/key-chain.hpp>
12#include <pcap/pcap.h>
13
Davide Pesavento44deacc2014-02-19 10:48:07 +010014namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015namespace tests {
Davide Pesavento44deacc2014-02-19 10:48:07 +010016
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017BOOST_FIXTURE_TEST_SUITE(FaceEthernet, BaseFixture)
Davide Pesavento44deacc2014-02-19 10:48:07 +010018
Davide Pesaventob60cc122014-03-19 19:26:02 +010019class InterfacesFixture : protected BaseFixture
20{
21protected:
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
43protected:
44 std::list< shared_ptr<NetworkInterfaceInfo> > m_interfaces;
45};
46
47
48BOOST_FIXTURE_TEST_CASE(MulticastFacesMap, InterfacesFixture)
Davide Pesavento44deacc2014-02-19 10:48:07 +010049{
Alexander Afanasyev0eb70652014-02-27 18:35:07 -080050 EthernetFactory factory;
Davide Pesavento44deacc2014-02-19 10:48:07 +010051
Davide Pesaventob60cc122014-03-19 19:26:02 +010052 if (m_interfaces.empty())
Davide Pesavento44deacc2014-02-19 10:48:07 +010053 {
Davide Pesaventob60cc122014-03-19 19:26:02 +010054 BOOST_WARN_MESSAGE(false, "No interfaces available, cannot perform MulticastFacesMap test");
Davide Pesavento44deacc2014-02-19 10:48:07 +010055 return;
56 }
57
Davide Pesaventob60cc122014-03-19 19:26:02 +010058 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
85BOOST_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 Pesavento44deacc2014-02-19 10:48:07 +010097
98 BOOST_REQUIRE(static_cast<bool>(face));
Alexander Afanasyev355c0662014-03-20 18:08:17 -070099
100 BOOST_CHECK(!face->isOnDemand());
Davide Pesavento44deacc2014-02-19 10:48:07 +0100101 BOOST_CHECK_EQUAL(face->isLocal(), false);
Junxiao Shi79494162014-04-02 18:25:11 -0700102 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 Pesavento44deacc2014-02-19 10:48:07 +0100106
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
141BOOST_AUTO_TEST_SUITE_END()
142
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700143} // namespace tests
Davide Pesavento44deacc2014-02-19 10:48:07 +0100144} // namespace nfd