iface_mgr_unittest.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. namespace {
  27. const char* const INTERFACE_FILE = TEST_DATA_BUILDDIR "/interfaces.txt";
  28. class NakedIfaceMgr: public IfaceMgr {
  29. // "naked" Interface Manager, exposes internal fields
  30. public:
  31. NakedIfaceMgr() { }
  32. IfaceLst & getIfacesLst() { return ifaces_; }
  33. void setSendSock(int sock) { sendsock_ = sock; }
  34. void setRecvSock(int sock) { recvsock_ = sock; }
  35. int openSocket(const std::string& ifname,
  36. const isc::asiolink::IOAddress& addr,
  37. int port) {
  38. return IfaceMgr::openSocket(ifname, addr, port);
  39. }
  40. };
  41. // dummy class for now, but this will be expanded when needed
  42. class IfaceMgrTest : public ::testing::Test {
  43. public:
  44. IfaceMgrTest() {
  45. }
  46. };
  47. // uncomment this test to create packet writer. It will
  48. // write incoming DHCPv6 packets as C arrays. That is useful
  49. // for generating test sequences based on actual traffic
  50. //
  51. // TODO: this potentially should be moved to a separate tool
  52. //
  53. #if 0
  54. TEST_F(IfaceMgrTest, dhcp6Sniffer) {
  55. // testing socket operation in a portable way is tricky
  56. // without interface detection implemented
  57. unlink("interfaces.txt");
  58. ofstream interfaces("interfaces.txt", ios::ate);
  59. interfaces << "eth0 fe80::21e:8cff:fe9b:7349";
  60. interfaces.close();
  61. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  62. Pkt6 * pkt = 0;
  63. int cnt = 0;
  64. cout << "---8X-----------------------------------------" << endl;
  65. while (true) {
  66. pkt = ifacemgr->receive();
  67. cout << "// Received " << pkt->data_len_ << " bytes packet:" << endl;
  68. cout << "Pkt6 *capture" << cnt++ << "() {" << endl;
  69. cout << " Pkt6* pkt;" << endl;
  70. cout << " pkt = new Pkt6(" << pkt->data_len_ << ");" << endl;
  71. cout << " pkt->remote_port_ = " << pkt-> remote_port_ << ";" << endl;
  72. cout << " pkt->remote_addr_ = IOAddress(\"" << pkt->remote_addr_.toText() << "\");" << endl;
  73. cout << " pkt->local_port_ = " << pkt-> local_port_ << ";" << endl;
  74. cout << " pkt->local_addr_ = IOAddress(\"" << pkt->local_addr_.toText() << "\");" << endl;
  75. cout << " pkt->ifindex_ = " << pkt->ifindex_ << ";" << endl;
  76. cout << " pkt->iface_ = \"" << pkt->iface_ << "\";" << endl;
  77. for (int i=0; i< pkt->data_len_; i++) {
  78. cout << " pkt->data_[" << i << "]=" << (int)(unsigned char)pkt->data_[i] << "; ";
  79. if (!(i%4))
  80. cout << endl;
  81. }
  82. cout << endl;
  83. cout << " return (pkt);" << endl;
  84. cout << "}" << endl << endl;
  85. delete pkt;
  86. }
  87. cout << "---8X-----------------------------------------" << endl;
  88. // never happens. Infinite loop is infinite
  89. delete pkt;
  90. delete ifacemgr;
  91. }
  92. #endif
  93. TEST_F(IfaceMgrTest, basic) {
  94. // checks that IfaceManager can be instantiated
  95. IfaceMgr & ifacemgr = IfaceMgr::instance();
  96. ASSERT_TRUE(&ifacemgr != 0);
  97. }
  98. TEST_F(IfaceMgrTest, ifaceClass) {
  99. // basic tests for Iface inner class
  100. IfaceMgr::Iface * iface = new IfaceMgr::Iface("eth5", 7);
  101. EXPECT_STREQ("eth5/7", iface->getFullName().c_str());
  102. delete iface;
  103. }
  104. // TODO: Implement getPlainMac() test as soon as interface detection is implemented.
  105. TEST_F(IfaceMgrTest, getIface) {
  106. cout << "Interface checks. Please ignore socket binding errors." << endl;
  107. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  108. // interface name, ifindex
  109. IfaceMgr::Iface iface1("lo", 1);
  110. IfaceMgr::Iface iface2("eth5", 2);
  111. IfaceMgr::Iface iface3("en3", 5);
  112. IfaceMgr::Iface iface4("e1000g0", 3);
  113. ifacemgr->getIfacesLst().push_back(iface1);
  114. ifacemgr->getIfacesLst().push_back(iface2);
  115. ifacemgr->getIfacesLst().push_back(iface3);
  116. ifacemgr->getIfacesLst().push_back(iface4);
  117. // check that interface can be retrieved by ifindex
  118. IfaceMgr::Iface * tmp = ifacemgr->getIface(5);
  119. // ASSERT_NE(NULL, tmp); is not supported. hmmmm.
  120. ASSERT_TRUE( tmp != NULL );
  121. EXPECT_STREQ( "en3", tmp->name_.c_str() );
  122. EXPECT_EQ(5, tmp->ifindex_);
  123. // check that interface can be retrieved by name
  124. tmp = ifacemgr->getIface("lo");
  125. ASSERT_TRUE( tmp != NULL );
  126. EXPECT_STREQ( "lo", tmp->name_.c_str() );
  127. EXPECT_EQ(1, tmp->ifindex_);
  128. // check that non-existing interfaces are not returned
  129. EXPECT_EQ(static_cast<void*>(NULL), ifacemgr->getIface("wifi0") );
  130. delete ifacemgr;
  131. }
  132. TEST_F(IfaceMgrTest, detectIfaces) {
  133. // test detects that interfaces can be detected
  134. // there is no code for that now, but interfaces are
  135. // read from file
  136. fstream fakeifaces(INTERFACE_FILE, ios::out|ios::trunc);
  137. fakeifaces << "eth0 fe80::1234";
  138. fakeifaces.close();
  139. // this is not usable on systems that don't have eth0
  140. // interfaces. Nevertheless, this fake interface should
  141. // be on list, but if_nametoindex() will fail.
  142. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  143. ASSERT_TRUE( ifacemgr->getIface("eth0") != NULL );
  144. IfaceMgr::Iface * eth0 = ifacemgr->getIface("eth0");
  145. // there should be one address
  146. EXPECT_EQ(1, eth0->addrs_.size());
  147. IOAddress * addr = &(*eth0->addrs_.begin());
  148. ASSERT_TRUE( addr != NULL );
  149. EXPECT_STREQ( "fe80::1234", addr->toText().c_str() );
  150. delete ifacemgr;
  151. }
  152. // TODO: disabled due to other naming on various systems
  153. // (lo in Linux, lo0 in BSD systems)
  154. // Fix for this is available on 1186 branch, will reenable
  155. // this test once 1186 is merged
  156. TEST_F(IfaceMgrTest, DISABLED_sockets) {
  157. // testing socket operation in a portable way is tricky
  158. // without interface detection implemented
  159. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  160. IOAddress loAddr("::1");
  161. // bind multicast socket to port 10547
  162. int socket1 = ifacemgr->openSocket("lo", loAddr, 10547);
  163. EXPECT_GT(socket1, 0); // socket > 0
  164. // bind unicast socket to port 10548
  165. int socket2 = ifacemgr->openSocket("lo", loAddr, 10548);
  166. EXPECT_GT(socket2, 0);
  167. // expect success. This address/port is already bound, but
  168. // we are using SO_REUSEADDR, so we can bind it twice
  169. int socket3 = ifacemgr->openSocket("lo", loAddr, 10547);
  170. EXPECT_GT(socket3, 0); // socket > 0
  171. // we now have 3 sockets open at the same time. Looks good.
  172. close(socket1);
  173. close(socket2);
  174. close(socket3);
  175. delete ifacemgr;
  176. }
  177. // TODO: disabled due to other naming on various systems
  178. // (lo in Linux, lo0 in BSD systems)
  179. TEST_F(IfaceMgrTest, DISABLED_socketsMcast) {
  180. // testing socket operation in a portable way is tricky
  181. // without interface detection implemented
  182. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  183. IOAddress loAddr("::1");
  184. IOAddress mcastAddr("ff02::1:2");
  185. // bind multicast socket to port 10547
  186. int socket1 = ifacemgr->openSocket("lo", mcastAddr, 10547);
  187. EXPECT_GT(socket1, 0); // socket > 0
  188. // expect success. This address/port is already bound, but
  189. // we are using SO_REUSEADDR, so we can bind it twice
  190. int socket2 = ifacemgr->openSocket("lo", mcastAddr, 10547);
  191. EXPECT_GT(socket2, 0);
  192. // there's no good way to test negative case here.
  193. // we would need non-multicast interface. We will be able
  194. // to iterate thru available interfaces and check if there
  195. // are interfaces without multicast-capable flag.
  196. close(socket1);
  197. close(socket2);
  198. delete ifacemgr;
  199. }
  200. // TODO: disabled due to other naming on various systems
  201. // (lo in Linux, lo0 in BSD systems)
  202. // Fix for this is available on 1186 branch, will reenable
  203. // this test once 1186 is merged
  204. TEST_F(IfaceMgrTest, DISABLED_sendReceive) {
  205. // testing socket operation in a portable way is tricky
  206. // without interface detection implemented
  207. fstream fakeifaces(INTERFACE_FILE, ios::out|ios::trunc);
  208. fakeifaces << "lo ::1";
  209. fakeifaces.close();
  210. NakedIfaceMgr * ifacemgr = new NakedIfaceMgr();
  211. // let's assume that every supported OS have lo interface
  212. IOAddress loAddr("::1");
  213. int socket1 = ifacemgr->openSocket("lo", loAddr, 10547);
  214. int socket2 = ifacemgr->openSocket("lo", loAddr, 10546);
  215. ifacemgr->setSendSock(socket2);
  216. ifacemgr->setRecvSock(socket1);
  217. Pkt6 sendPkt(128);
  218. // prepare dummy payload
  219. for (int i=0;i<128; i++) {
  220. sendPkt.data_[i] = i;
  221. }
  222. sendPkt.remote_port_ = 10547;
  223. sendPkt.remote_addr_ = IOAddress("::1");
  224. sendPkt.ifindex_ = 1;
  225. sendPkt.iface_ = "lo";
  226. Pkt6 * rcvPkt;
  227. EXPECT_EQ(true, ifacemgr->send(sendPkt));
  228. rcvPkt = ifacemgr->receive();
  229. ASSERT_TRUE( rcvPkt != NULL ); // received our own packet
  230. // let's check that we received what was sent
  231. EXPECT_EQ(sendPkt.data_len_, rcvPkt->data_len_);
  232. EXPECT_EQ(0, memcmp(&sendPkt.data_[0], &rcvPkt->data_[0],
  233. rcvPkt->data_len_) );
  234. EXPECT_EQ(sendPkt.remote_addr_.toText(), rcvPkt->remote_addr_.toText());
  235. EXPECT_EQ(rcvPkt->remote_port_, 10546);
  236. delete rcvPkt;
  237. delete ifacemgr;
  238. }
  239. }