iface_mgr_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <sstream>
  18. #include <arpa/inet.h>
  19. #include <gtest/gtest.h>
  20. #include "io_address.h"
  21. #include "dhcp/pkt6.h"
  22. #include "dhcp6/iface_mgr.h"
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. // name of loopback interface detection
  28. char LOOPBACK[32] = "lo";
  29. namespace {
  30. const char* const INTERFACE_FILE = TEST_DATA_BUILDDIR "/interfaces.txt";
  31. class NakedIfaceMgr: public IfaceMgr {
  32. // "naked" Interface Manager, exposes internal fields
  33. public:
  34. NakedIfaceMgr() { }
  35. IfaceLst & getIfacesLst() { return ifaces_; }
  36. void setSendSock(int sock) { sendsock_ = sock; }
  37. void setRecvSock(int sock) { recvsock_ = sock; }
  38. int openSocket(const std::string& ifname,
  39. const isc::asiolink::IOAddress& addr,
  40. int port) {
  41. return IfaceMgr::openSocket(ifname, addr, port);
  42. }
  43. };
  44. // dummy class for now, but this will be expanded when needed
  45. class IfaceMgrTest : public ::testing::Test {
  46. public:
  47. IfaceMgrTest() {
  48. }
  49. };
  50. // We need some known interface to work reliably. Loopback interface
  51. // is named lo on Linux and lo0 on BSD boxes. We need to find out
  52. // which is available. This is not a real test, but rather a workaround
  53. // that will go away when interface detection is implemented.
  54. // NOTE: At this stage of development, write access to current directory
  55. // during running tests is required.
  56. TEST_F(IfaceMgrTest, loDetect) {
  57. // poor man's interface detection
  58. // it will go away as soon as proper interface detection
  59. // is implemented
  60. if (if_nametoindex("lo")>0) {
  61. cout << "This is Linux, using lo as loopback." << endl;
  62. sprintf(LOOPBACK, "lo");
  63. } else if (if_nametoindex("lo0")>0) {
  64. cout << "This is BSD, using lo0 as loopback." << endl;
  65. sprintf(LOOPBACK, "lo0");
  66. } else {
  67. cout << "Failed to detect loopback interface. Neither "
  68. << "lo or lo0 worked. I give up." << endl;
  69. ASSERT_TRUE(false);
  70. }
  71. }
  72. // uncomment this test to create packet writer. It will
  73. // write incoming DHCPv6 packets as C arrays. That is useful
  74. // for generating test sequences based on actual traffic
  75. //
  76. // TODO: this potentially should be moved to a separate tool
  77. //
  78. #if 0
  79. TEST_F(IfaceMgrTest, dhcp6Sniffer) {
  80. // testing socket operation in a portable way is tricky
  81. // without interface detection implemented
  82. unlink("interfaces.txt");
  83. ofstream interfaces("interfaces.txt", ios::ate);
  84. interfaces << "eth0 fe80::21e:8cff:fe9b:7349";
  85. interfaces.close();
  86. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  87. Pkt6 * pkt = 0;
  88. int cnt = 0;
  89. cout << "---8X-----------------------------------------" << endl;
  90. while (true) {
  91. pkt = ifacemgr->receive();
  92. cout << "// Received " << pkt->data_len_ << " bytes packet:" << endl;
  93. cout << "Pkt6 *capture" << cnt++ << "() {" << endl;
  94. cout << " Pkt6* pkt;" << endl;
  95. cout << " pkt = new Pkt6(" << pkt->data_len_ << ");" << endl;
  96. cout << " pkt->remote_port_ = " << pkt-> remote_port_ << ";" << endl;
  97. cout << " pkt->remote_addr_ = IOAddress(\""
  98. << pkt->remote_addr_.toText() << "\");" << endl;
  99. cout << " pkt->local_port_ = " << pkt-> local_port_ << ";" << endl;
  100. cout << " pkt->local_addr_ = IOAddress(\""
  101. << pkt->local_addr_.toText() << "\");" << endl;
  102. cout << " pkt->ifindex_ = " << pkt->ifindex_ << ";" << endl;
  103. cout << " pkt->iface_ = \"" << pkt->iface_ << "\";" << endl;
  104. // TODO it is better to declare an array and then memcpy it to
  105. // packet.
  106. for (int i=0; i< pkt->data_len_; i++) {
  107. cout << " pkt->data_[" << i << "]="
  108. << (int)(unsigned char)pkt->data_[i] << "; ";
  109. if (!(i%4))
  110. cout << endl;
  111. }
  112. cout << endl;
  113. cout << " return (pkt);" << endl;
  114. cout << "}" << endl << endl;
  115. delete pkt;
  116. }
  117. cout << "---8X-----------------------------------------" << endl;
  118. // never happens. Infinite loop is infinite
  119. delete pkt;
  120. delete ifacemgr;
  121. }
  122. #endif
  123. TEST_F(IfaceMgrTest, basic) {
  124. // checks that IfaceManager can be instantiated
  125. IfaceMgr & ifacemgr = IfaceMgr::instance();
  126. ASSERT_TRUE(&ifacemgr != 0);
  127. }
  128. TEST_F(IfaceMgrTest, ifaceClass) {
  129. // basic tests for Iface inner class
  130. IfaceMgr::Iface * iface = new IfaceMgr::Iface("eth5", 7);
  131. EXPECT_STREQ("eth5/7", iface->getFullName().c_str());
  132. delete iface;
  133. }
  134. // TODO: Implement getPlainMac() test as soon as interface detection
  135. // is implemented.
  136. TEST_F(IfaceMgrTest, getIface) {
  137. cout << "Interface checks. Please ignore socket binding errors." << endl;
  138. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  139. // interface name, ifindex
  140. IfaceMgr::Iface iface1("lo1", 1);
  141. IfaceMgr::Iface iface2("eth5", 2);
  142. IfaceMgr::Iface iface3("en3", 5);
  143. IfaceMgr::Iface iface4("e1000g0", 3);
  144. // note: real interfaces may be detected as well
  145. ifacemgr->getIfacesLst().push_back(iface1);
  146. ifacemgr->getIfacesLst().push_back(iface2);
  147. ifacemgr->getIfacesLst().push_back(iface3);
  148. ifacemgr->getIfacesLst().push_back(iface4);
  149. cout << "There are " << ifacemgr->getIfacesLst().size()
  150. << " interfaces." << endl;
  151. for (IfaceMgr::IfaceLst::iterator iface=ifacemgr->getIfacesLst().begin();
  152. iface != ifacemgr->getIfacesLst().end();
  153. ++iface) {
  154. cout << " " << iface->name_ << "/" << iface->ifindex_ << endl;
  155. }
  156. // check that interface can be retrieved by ifindex
  157. IfaceMgr::Iface * tmp = ifacemgr->getIface(5);
  158. // ASSERT_NE(NULL, tmp); is not supported. hmmmm.
  159. ASSERT_TRUE( tmp != NULL );
  160. EXPECT_STREQ( "en3", tmp->name_.c_str() );
  161. EXPECT_EQ(5, tmp->ifindex_);
  162. // check that interface can be retrieved by name
  163. tmp = ifacemgr->getIface("lo1");
  164. ASSERT_TRUE( tmp != NULL );
  165. EXPECT_STREQ( "lo1", tmp->name_.c_str() );
  166. EXPECT_EQ(1, tmp->ifindex_);
  167. // check that non-existing interfaces are not returned
  168. EXPECT_EQ(static_cast<void*>(NULL), ifacemgr->getIface("wifi0") );
  169. delete ifacemgr;
  170. }
  171. TEST_F(IfaceMgrTest, detectIfaces) {
  172. // test detects that interfaces can be detected
  173. // there is no code for that now, but interfaces are
  174. // read from file
  175. fstream fakeifaces(INTERFACE_FILE, ios::out|ios::trunc);
  176. fakeifaces << "eth0 fe80::1234";
  177. fakeifaces.close();
  178. // this is not usable on systems that don't have eth0
  179. // interfaces. Nevertheless, this fake interface should
  180. // be on list, but if_nametoindex() will fail.
  181. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  182. ASSERT_TRUE( ifacemgr->getIface("eth0") != NULL );
  183. IfaceMgr::Iface * eth0 = ifacemgr->getIface("eth0");
  184. // there should be one address
  185. EXPECT_EQ(1, eth0->addrs_.size());
  186. IOAddress * addr = &(*eth0->addrs_.begin());
  187. ASSERT_TRUE( addr != NULL );
  188. EXPECT_STREQ( "fe80::1234", addr->toText().c_str() );
  189. delete ifacemgr;
  190. }
  191. // TODO: disabled due to other naming on various systems
  192. // (lo in Linux, lo0 in BSD systems)
  193. // Fix for this is available on 1186 branch, will reenable
  194. // this test once 1186 is merged
  195. TEST_F(IfaceMgrTest, DISABLED_sockets) {
  196. // testing socket operation in a portable way is tricky
  197. // without interface detection implemented
  198. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  199. IOAddress loAddr("::1");
  200. // bind multicast socket to port 10547
  201. int socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547);
  202. EXPECT_GT(socket1, 0); // socket > 0
  203. // bind unicast socket to port 10548
  204. int socket2 = ifacemgr->openSocket(LOOPBACK, loAddr, 10548);
  205. EXPECT_GT(socket2, 0);
  206. // expect success. This address/port is already bound, but
  207. // we are using SO_REUSEADDR, so we can bind it twice
  208. int socket3 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547);
  209. // rebinding succeeds on Linux, fails on BSD
  210. // TODO: add OS-specific defines here (or modify code to
  211. // behave the same way on all OSes, but that may not be
  212. // possible
  213. // EXPECT_GT(socket3, 0); // socket > 0
  214. // we now have 3 sockets open at the same time. Looks good.
  215. close(socket1);
  216. close(socket2);
  217. close(socket3);
  218. delete ifacemgr;
  219. }
  220. // TODO: disabled due to other naming on various systems
  221. // (lo in Linux, lo0 in BSD systems)
  222. TEST_F(IfaceMgrTest, DISABLED_socketsMcast) {
  223. // testing socket operation in a portable way is tricky
  224. // without interface detection implemented
  225. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  226. IOAddress loAddr("::1");
  227. IOAddress mcastAddr("ff02::1:2");
  228. // bind multicast socket to port 10547
  229. int socket1 = ifacemgr->openSocket(LOOPBACK, mcastAddr, 10547);
  230. EXPECT_GT(socket1, 0); // socket > 0
  231. // expect success. This address/port is already bound, but
  232. // we are using SO_REUSEADDR, so we can bind it twice
  233. int socket2 = ifacemgr->openSocket(LOOPBACK, mcastAddr, 10547);
  234. EXPECT_GT(socket2, 0);
  235. // there's no good way to test negative case here.
  236. // we would need non-multicast interface. We will be able
  237. // to iterate thru available interfaces and check if there
  238. // are interfaces without multicast-capable flag.
  239. close(socket1);
  240. close(socket2);
  241. delete ifacemgr;
  242. }
  243. // TODO: disabled due to other naming on various systems
  244. // (lo in Linux, lo0 in BSD systems)
  245. // Fix for this is available on 1186 branch, will reenable
  246. // this test once 1186 is merged
  247. TEST_F(IfaceMgrTest, DISABLED_sendReceive) {
  248. // testing socket operation in a portable way is tricky
  249. // without interface detection implemented
  250. fstream fakeifaces(INTERFACE_FILE, ios::out|ios::trunc);
  251. fakeifaces << LOOPBACK << " ::1";
  252. fakeifaces.close();
  253. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  254. // let's assume that every supported OS have lo interface
  255. IOAddress loAddr("::1");
  256. int socket1 = ifacemgr->openSocket(LOOPBACK, loAddr, 10547);
  257. int socket2 = ifacemgr->openSocket(LOOPBACK, loAddr, 10546);
  258. ifacemgr->setSendSock(socket2);
  259. ifacemgr->setRecvSock(socket1);
  260. boost::shared_ptr<Pkt6> sendPkt(new Pkt6(128) );
  261. // prepare dummy payload
  262. for (int i=0;i<128; i++) {
  263. sendPkt->data_[i] = i;
  264. }
  265. sendPkt->remote_port_ = 10547;
  266. sendPkt->remote_addr_ = IOAddress("::1");
  267. sendPkt->ifindex_ = 1;
  268. sendPkt->iface_ = LOOPBACK;
  269. boost::shared_ptr<Pkt6> rcvPkt;
  270. EXPECT_EQ(true, ifacemgr->send(sendPkt));
  271. rcvPkt = ifacemgr->receive();
  272. ASSERT_TRUE( rcvPkt ); // received our own packet
  273. // let's check that we received what was sent
  274. EXPECT_EQ(sendPkt->data_len_, rcvPkt->data_len_);
  275. EXPECT_EQ(0, memcmp(&sendPkt->data_[0], &rcvPkt->data_[0],
  276. rcvPkt->data_len_) );
  277. EXPECT_EQ(sendPkt->remote_addr_.toText(), rcvPkt->remote_addr_.toText());
  278. EXPECT_EQ(rcvPkt->remote_port_, 10546);
  279. delete ifacemgr;
  280. }
  281. }