blob: 12eef0f5cc72dfce0fbdb458d9c6b8813709e980 [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);
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000102 BOOST_CHECK_EQUAL(face->getUri().toString(),
Davide Pesaventob60cc122014-03-19 19:26:02 +0100103 "ether://" + m_interfaces.front()->name + "/" +
Alexander Afanasyeva39b90b2014-03-05 15:31:00 +0000104 ethernet::getDefaultMulticastAddress().toString(':'));
Davide Pesavento44deacc2014-02-19 10:48:07 +0100105
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
140BOOST_AUTO_TEST_SUITE_END()
141
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700142} // namespace tests
Davide Pesavento44deacc2014-02-19 10:48:07 +0100143} // namespace nfd