cfg_iface_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcp/dhcp4.h>
  8. #include <dhcp/tests/iface_mgr_test_config.h>
  9. #include <dhcpsrv/cfg_iface.h>
  10. #include <gtest/gtest.h>
  11. using namespace isc;
  12. using namespace isc::dhcp;
  13. using namespace isc::dhcp::test;
  14. namespace {
  15. /// @brief Test fixture class for testing the @c CfgIface class.
  16. class CfgIfaceTest : public ::testing::Test {
  17. public:
  18. /// @brief Constructor.
  19. ///
  20. /// By initializing the @c IfaceMgrTestConfig object it creates a set of
  21. /// fake interfaces: lo, eth0, eth1.
  22. CfgIfaceTest() :
  23. iface_mgr_test_config_(true) {
  24. }
  25. /// @brief Checks if socket of the specified family is opened on interface.
  26. ///
  27. /// @param iface_name Interface name.
  28. /// @param family One of: AF_INET or AF_INET6
  29. bool socketOpen(const std::string& iface_name, const int family) const;
  30. /// @brief Checks if socket is opened on the specified interface and bound
  31. /// to a specific IPv4 address.
  32. ///
  33. /// @param iface_name Interface name.
  34. /// @param address Address that the socket should be bound to.
  35. bool socketOpen(const std::string& iface_name,
  36. const std::string& address) const;
  37. /// @brief Checks if unicast socket is opened on interface.
  38. ///
  39. /// @param iface_name Interface name.
  40. bool unicastOpen(const std::string& iface_name) const;
  41. /// @brief Holds a fake configuration of the interfaces.
  42. IfaceMgrTestConfig iface_mgr_test_config_;
  43. };
  44. bool
  45. CfgIfaceTest::socketOpen(const std::string& iface_name,
  46. const int family) const {
  47. return (iface_mgr_test_config_.socketOpen(iface_name, family));
  48. }
  49. bool
  50. CfgIfaceTest::socketOpen(const std::string& iface_name,
  51. const std::string& address) const {
  52. return (iface_mgr_test_config_.socketOpen(iface_name, address));
  53. }
  54. bool
  55. CfgIfaceTest::unicastOpen(const std::string& iface_name) const {
  56. return (iface_mgr_test_config_.unicastOpen(iface_name));
  57. }
  58. // This test checks that the interface names can be explicitly selected
  59. // by their names and IPv4 sockets are opened on these interfaces.
  60. TEST_F(CfgIfaceTest, explicitNamesV4) {
  61. CfgIface cfg;
  62. // Specify valid interface names. There should be no error.
  63. ASSERT_NO_THROW(cfg.use(AF_INET, "eth0"));
  64. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));
  65. // Open sockets on specified interfaces.
  66. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  67. // Sockets should be now open on eth0 and eth1, but not on loopback.
  68. EXPECT_TRUE(socketOpen("eth0", AF_INET));
  69. EXPECT_TRUE(socketOpen("eth1", AF_INET));
  70. EXPECT_FALSE(socketOpen("lo", AF_INET));
  71. // No IPv6 sockets should be present because we wanted IPv4 sockets.
  72. EXPECT_FALSE(socketOpen("eth0", AF_INET6));
  73. EXPECT_FALSE(socketOpen("eth1", AF_INET6));
  74. EXPECT_FALSE(socketOpen("lo", AF_INET6));
  75. // Close all sockets and make sure they are really closed.
  76. cfg.closeSockets();
  77. ASSERT_FALSE(socketOpen("eth0", AF_INET));
  78. ASSERT_FALSE(socketOpen("eth1", AF_INET));
  79. ASSERT_FALSE(socketOpen("lo", AF_INET));
  80. // Reset configuration and select only one interface this time.
  81. cfg.reset();
  82. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));
  83. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  84. // Socket should be open on eth1 only.
  85. EXPECT_FALSE(socketOpen("eth0", AF_INET));
  86. EXPECT_TRUE(socketOpen("eth1", AF_INET));
  87. EXPECT_FALSE(socketOpen("lo", AF_INET));
  88. }
  89. // This test checks that it is possible to specify an interface and address
  90. // on this interface to which the socket should be bound. The sockets should
  91. // not be opened on other addresses on this interface.
  92. TEST_F(CfgIfaceTest, explicitNamesAndAddressesV4) {
  93. CfgIface cfg;
  94. ASSERT_NO_THROW(cfg.use(AF_INET, "eth0/10.0.0.1"));
  95. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"));
  96. // Open sockets on specified interfaces and addresses.
  97. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  98. EXPECT_TRUE(socketOpen("eth0", "10.0.0.1"));
  99. EXPECT_TRUE(socketOpen("eth1", "192.0.2.3"));
  100. EXPECT_FALSE(socketOpen("eth1", "192.0.2.5"));
  101. // Close all sockets and make sure they are really closed.
  102. cfg.closeSockets();
  103. ASSERT_FALSE(socketOpen("eth0", "10.0.0.1"));
  104. ASSERT_FALSE(socketOpen("eth1", "192.0.2.3"));
  105. ASSERT_FALSE(socketOpen("eth1", "192.0.2.5"));
  106. // Reset configuration.
  107. cfg.reset();
  108. // Now check that the socket can be bound to a different address on
  109. // eth1.
  110. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.5"));
  111. // Open sockets according to the new configuration.
  112. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  113. EXPECT_FALSE(socketOpen("eth0", "10.0.0.1"));
  114. EXPECT_FALSE(socketOpen("eth1", "192.0.2.3"));
  115. EXPECT_TRUE(socketOpen("eth1", "192.0.2.5"));
  116. }
  117. // This test checks that the invalid interface name and/or IPv4 address
  118. // results in error.
  119. TEST_F(CfgIfaceTest, explicitNamesAndAddressesInvalidV4) {
  120. CfgIface cfg;
  121. // An address not assigned to the interface.
  122. EXPECT_THROW(cfg.use(AF_INET, "eth0/10.0.0.2"), NoSuchAddress);
  123. // IPv6 address.
  124. EXPECT_THROW(cfg.use(AF_INET, "eth0/2001:db8:1::1"), InvalidIfaceName);
  125. // Wildcard interface name with an address.
  126. EXPECT_THROW(cfg.use(AF_INET, "*/10.0.0.1"), InvalidIfaceName);
  127. // Duplicated interface.
  128. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1"));
  129. EXPECT_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"), DuplicateIfaceName);
  130. }
  131. // This test checks that it is possible to explicitly select multiple
  132. // IPv4 addresses on a single interface.
  133. TEST_F(CfgIfaceTest, multipleAddressesSameInterfaceV4) {
  134. CfgIface cfg;
  135. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"));
  136. // Cannot add the same address twice.
  137. ASSERT_THROW(cfg.use(AF_INET, "eth1/192.0.2.3"), DuplicateAddress);
  138. // Can add another address on this interface.
  139. ASSERT_NO_THROW(cfg.use(AF_INET, "eth1/192.0.2.5"));
  140. // Can't select the whole interface.
  141. ASSERT_THROW(cfg.use(AF_INET, "eth1"), DuplicateIfaceName);
  142. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  143. EXPECT_FALSE(socketOpen("eth0", "10.0.0.1"));
  144. EXPECT_TRUE(socketOpen("eth1", "192.0.2.3"));
  145. EXPECT_TRUE(socketOpen("eth1", "192.0.2.5"));
  146. }
  147. // This test checks that the interface names can be explicitly selected
  148. // by their names and IPv6 sockets are opened on these interfaces.
  149. TEST_F(CfgIfaceTest, explicitNamesV6) {
  150. CfgIface cfg;
  151. // Specify valid interface names. There should be no error.
  152. ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0"));
  153. ASSERT_NO_THROW(cfg.use(AF_INET6, "eth1"));
  154. // Open sockets on specified interfaces.
  155. cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
  156. // Sockets should be now open on eth0 and eth1, but not on loopback.
  157. EXPECT_TRUE(socketOpen("eth0", AF_INET6));
  158. EXPECT_TRUE(socketOpen("eth1", AF_INET6));
  159. EXPECT_FALSE(socketOpen("lo", AF_INET6));
  160. // No IPv4 sockets should be present because we wanted IPv6 sockets.
  161. EXPECT_FALSE(socketOpen("eth0", AF_INET));
  162. EXPECT_FALSE(socketOpen("eth1", AF_INET));
  163. EXPECT_FALSE(socketOpen("lo", AF_INET));
  164. // Close all sockets and make sure they are really closed.
  165. cfg.closeSockets();
  166. ASSERT_FALSE(socketOpen("eth0", AF_INET6));
  167. ASSERT_FALSE(socketOpen("eth1", AF_INET6));
  168. ASSERT_FALSE(socketOpen("lo", AF_INET6));
  169. // Reset configuration and select only one interface this time.
  170. cfg.reset();
  171. ASSERT_NO_THROW(cfg.use(AF_INET6, "eth1"));
  172. cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
  173. // Socket should be open on eth1 only.
  174. EXPECT_FALSE(socketOpen("eth0", AF_INET6));
  175. EXPECT_TRUE(socketOpen("eth1", AF_INET6));
  176. EXPECT_FALSE(socketOpen("lo", AF_INET6));
  177. }
  178. // This test checks that the wildcard interface name can be specified to
  179. // select all interfaces to open IPv4 sockets.
  180. TEST_F(CfgIfaceTest, wildcardV4) {
  181. CfgIface cfg;
  182. ASSERT_NO_THROW(cfg.use(AF_INET, "*"));
  183. cfg.openSockets(AF_INET, DHCP4_SERVER_PORT);
  184. // Sockets should be now open on eth0 and eth1, but not on loopback.
  185. EXPECT_TRUE(socketOpen("eth0", AF_INET));
  186. EXPECT_TRUE(socketOpen("eth1", AF_INET));
  187. EXPECT_FALSE(socketOpen("lo", AF_INET));
  188. // No IPv6 sockets should be present because we wanted IPv4 sockets.
  189. EXPECT_FALSE(socketOpen("eth0", AF_INET6));
  190. EXPECT_FALSE(socketOpen("eth1", AF_INET6));
  191. EXPECT_FALSE(socketOpen("lo", AF_INET6));
  192. }
  193. // This test checks that the wildcard interface name can be specified to
  194. // select all interfaces to open IPv6 sockets.
  195. TEST_F(CfgIfaceTest, wildcardV6) {
  196. CfgIface cfg;
  197. ASSERT_NO_THROW(cfg.use(AF_INET6, "*"));
  198. cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
  199. // Sockets should be now open on eth0 and eth1, but not on loopback.
  200. EXPECT_TRUE(socketOpen("eth0", AF_INET6));
  201. EXPECT_TRUE(socketOpen("eth1", AF_INET6));
  202. EXPECT_FALSE(socketOpen("lo", AF_INET6));
  203. // No IPv6 sockets should be present because we wanted IPv6 sockets.
  204. EXPECT_FALSE(socketOpen("eth0", AF_INET));
  205. EXPECT_FALSE(socketOpen("eth1", AF_INET));
  206. EXPECT_FALSE(socketOpen("lo", AF_INET));
  207. }
  208. // Test that unicast address can be specified for the socket to be opened on
  209. // the interface on which the socket bound to link local address is also
  210. // opened.
  211. TEST_F(CfgIfaceTest, validUnicast) {
  212. CfgIface cfg;
  213. // One socket will be opened on link-local address, one on unicast but
  214. // on the same interface.
  215. ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0"));
  216. ASSERT_NO_THROW(cfg.use(AF_INET6, "eth0/2001:db8:1::1"));
  217. cfg.openSockets(AF_INET6, DHCP6_SERVER_PORT);
  218. EXPECT_TRUE(socketOpen("eth0", AF_INET6));
  219. EXPECT_TRUE(unicastOpen("eth0"));
  220. }
  221. // Test that when invalid interface names are specified an exception is thrown.
  222. TEST_F(CfgIfaceTest, invalidValues) {
  223. CfgIface cfg;
  224. ASSERT_THROW(cfg.use(AF_INET, ""), InvalidIfaceName);
  225. ASSERT_THROW(cfg.use(AF_INET, " "), InvalidIfaceName);
  226. ASSERT_THROW(cfg.use(AF_INET, "bogus"), NoSuchIface);
  227. ASSERT_NO_THROW(cfg.use(AF_INET, "eth0"));
  228. ASSERT_THROW(cfg.use(AF_INET, "eth0"), DuplicateIfaceName);
  229. ASSERT_THROW(cfg.use(AF_INET, "eth0/2001:db8:1::1"), InvalidIfaceName);
  230. ASSERT_THROW(cfg.use(AF_INET6, "eth0/"), InvalidIfaceName);
  231. ASSERT_THROW(cfg.use(AF_INET6, "/2001:db8:1::1"), InvalidIfaceName);
  232. ASSERT_THROW(cfg.use(AF_INET6, "*/2001:db8:1::1"), InvalidIfaceName);
  233. ASSERT_THROW(cfg.use(AF_INET6, "bogus/2001:db8:1::1"), NoSuchIface);
  234. ASSERT_THROW(cfg.use(AF_INET6, "eth0/2001:db8:1::2"), NoSuchAddress);
  235. ASSERT_NO_THROW(cfg.use(AF_INET6, "*"));
  236. ASSERT_THROW(cfg.use(AF_INET6, "*"), DuplicateIfaceName);
  237. }
  238. // Test that the equality and inequality operators work fine for CfgIface.
  239. TEST_F(CfgIfaceTest, equality) {
  240. CfgIface cfg1;
  241. CfgIface cfg2;
  242. // Initially objects must be equal.
  243. EXPECT_TRUE(cfg1 == cfg2);
  244. EXPECT_FALSE(cfg1 != cfg2);
  245. // Differ by one interface.
  246. cfg1.use(AF_INET, "eth0");
  247. EXPECT_FALSE(cfg1 == cfg2);
  248. EXPECT_TRUE(cfg1 != cfg2);
  249. // Now interfaces should be equal.
  250. cfg2.use(AF_INET, "eth0");
  251. EXPECT_TRUE(cfg1 == cfg2);
  252. EXPECT_FALSE(cfg1 != cfg2);
  253. // Differ by unicast address.
  254. cfg1.use(AF_INET6, "eth0/2001:db8:1::1");
  255. EXPECT_FALSE(cfg1 == cfg2);
  256. EXPECT_TRUE(cfg1 != cfg2);
  257. // Differ by unicast address and one interface.
  258. cfg2.use(AF_INET6, "eth1");
  259. EXPECT_FALSE(cfg1 == cfg2);
  260. EXPECT_TRUE(cfg1 != cfg2);
  261. // Now, the unicast addresses are equal but still differ by one interface.
  262. cfg2.use(AF_INET6, "eth0/2001:db8:1::1");
  263. EXPECT_FALSE(cfg1 == cfg2);
  264. EXPECT_TRUE(cfg1 != cfg2);
  265. // They should be now back to equal.
  266. cfg1.use(AF_INET6, "eth1");
  267. EXPECT_TRUE(cfg1 == cfg2);
  268. EXPECT_FALSE(cfg1 != cfg2);
  269. // Even though the wildcard doesn't change anything because all interfaces
  270. // are already in use, the fact that the wildcard is specified should
  271. // cause them to be not equal.
  272. cfg1.use(AF_INET6, "*");
  273. EXPECT_FALSE(cfg1 == cfg2);
  274. EXPECT_TRUE(cfg1 != cfg2);
  275. // Finally, both are equal as they use wildacard.
  276. cfg2.use(AF_INET, "*");
  277. EXPECT_TRUE(cfg1 == cfg2);
  278. EXPECT_FALSE(cfg1 != cfg2);
  279. // Differ by socket type.
  280. cfg1.useSocketType(AF_INET, "udp");
  281. EXPECT_FALSE(cfg1 == cfg2);
  282. EXPECT_TRUE(cfg1 != cfg2);
  283. // Now, both should use the same socket type.
  284. cfg2.useSocketType(AF_INET, "udp");
  285. EXPECT_TRUE(cfg1 == cfg2);
  286. EXPECT_FALSE(cfg1 != cfg2);
  287. }
  288. // This test verifies that it is possible to specify the socket
  289. // type to be used by the DHCPv4 server.
  290. // This test is enabled on LINUX and BSD only, because the
  291. // direct traffic is only supported on those systems.
  292. #if defined OS_LINUX || defined OS_BSD
  293. TEST(CfgIfaceNoStubTest, useSocketType) {
  294. CfgIface cfg;
  295. // Select datagram sockets.
  296. ASSERT_NO_THROW(cfg.useSocketType(AF_INET, "udp"));
  297. EXPECT_EQ("udp", cfg.socketTypeToText());
  298. ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
  299. // For datagram sockets, the direct traffic is not supported.
  300. ASSERT_TRUE(!IfaceMgr::instance().isDirectResponseSupported());
  301. // Select raw sockets.
  302. ASSERT_NO_THROW(cfg.useSocketType(AF_INET, "raw"));
  303. EXPECT_EQ("raw", cfg.socketTypeToText());
  304. ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
  305. // For raw sockets, the direct traffic is supported.
  306. ASSERT_TRUE(IfaceMgr::instance().isDirectResponseSupported());
  307. ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_UDP));
  308. EXPECT_EQ("udp", cfg.socketTypeToText());
  309. ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
  310. ASSERT_TRUE(!IfaceMgr::instance().isDirectResponseSupported());
  311. ASSERT_NO_THROW(cfg.useSocketType(AF_INET, CfgIface::SOCKET_RAW));
  312. EXPECT_EQ("raw", cfg.socketTypeToText());
  313. ASSERT_NO_THROW(cfg.openSockets(AF_INET, 10067, true));
  314. ASSERT_TRUE(IfaceMgr::instance().isDirectResponseSupported());
  315. // Test invalid values.
  316. EXPECT_THROW(cfg.useSocketType(AF_INET, "default"),
  317. InvalidSocketType);
  318. EXPECT_THROW(cfg.useSocketType(AF_INET6, "udp"),
  319. InvalidSocketType);
  320. }
  321. #endif
  322. } // end of anonymous namespace