cfg_hosts_unittest.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // Copyright (C) 2014 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 <asiolink/io_address.h>
  16. #include <dhcp/duid.h>
  17. #include <dhcp/hwaddr.h>
  18. #include <dhcpsrv/cfg_hosts.h>
  19. #include <dhcpsrv/host.h>
  20. #include <gtest/gtest.h>
  21. #include <set>
  22. using namespace isc;
  23. using namespace isc::dhcp;
  24. using namespace isc::asiolink;
  25. namespace {
  26. /// @brief Test fixture class for testing @c CfgHost object holding
  27. /// host reservations specified in the configuration file.
  28. class CfgHostsTest : public ::testing::Test {
  29. public:
  30. /// @brief Constructor.
  31. ///
  32. /// This constructor allocates a collection of @c HWAddr and @c DuidPtr
  33. /// objects used by the unit tests.
  34. ///
  35. /// The allocated HW addresses use the following pattern: 01:02:0A:BB:03:XX
  36. /// where XX is a number between 0 and 0x32. All of them are of the
  37. /// HTYPE_ETHER type.
  38. ///
  39. /// The allocated DUID LLTs use the following pattern:
  40. /// 01:02:03:04:05:06:07:08:09:0A:XX where the XX is a number between
  41. /// 0 and 0x32.
  42. CfgHostsTest();
  43. /// @brief Increases last byte of an address.
  44. ///
  45. /// @param address Address to be increased.
  46. IOAddress increase(const IOAddress& address, const uint8_t num) const;
  47. /// @brief Collection of HW address objects allocated for unit tests.
  48. std::vector<HWAddrPtr> hwaddrs_;
  49. /// @brief Collection of DUIDs allocated for unit tests.
  50. std::vector<DuidPtr> duids_;
  51. };
  52. CfgHostsTest::CfgHostsTest() {
  53. const uint8_t mac_template[] = {
  54. 0x01, 0x02, 0x0A, 0xBB, 0x03, 0x00
  55. };
  56. for (int i = 0; i < 50; ++i) {
  57. std::vector<uint8_t> vec(mac_template,
  58. mac_template + sizeof(mac_template));
  59. vec[vec.size() - 1] = i;
  60. HWAddrPtr hwaddr(new HWAddr(vec, HTYPE_ETHER));
  61. hwaddrs_.push_back(hwaddr);
  62. }
  63. const uint8_t duid_template[] = {
  64. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x00
  65. };
  66. for (int i = 0; i < 50; ++i) {
  67. std::vector<uint8_t> vec(duid_template,
  68. duid_template + sizeof(mac_template));
  69. vec[vec.size() - 1] = i;
  70. DuidPtr duid(new DUID(vec));
  71. duids_.push_back(duid);
  72. }
  73. }
  74. IOAddress
  75. CfgHostsTest::increase(const IOAddress& address, const uint8_t num) const {
  76. std::vector<uint8_t> vec = address.toBytes();
  77. if (!vec.empty()) {
  78. vec[vec.size() - 1] += num;
  79. return (IOAddress::fromBytes(address.getFamily(), &vec[0]));
  80. }
  81. return (address);
  82. }
  83. // This test checks that hosts with unique HW addresses and DUIDs can be
  84. // retrieved from the host configuration.
  85. TEST_F(CfgHostsTest, getAllNonRepeatingHosts) {
  86. CfgHosts cfg;
  87. // Add 25 hosts identified by HW address and 25 hosts identified by
  88. // DUID. They are added to different subnets.
  89. for (int i = 0; i < 25; ++i) {
  90. cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
  91. "hw-address",
  92. SubnetID(i % 10 + 1), SubnetID(i % 5 + 1),
  93. IOAddress("192.0.2.5"))));
  94. cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
  95. SubnetID(i % 5 + 1), SubnetID(i % 10 + 1),
  96. IOAddress("192.0.2.10"))));
  97. }
  98. // Try to retrieve each added reservation using HW address and DUID. Do it
  99. // in the reverse order to make sure that the order doesn't matter.
  100. for (int i = 24; i >= 0; --i) {
  101. // Get host identified by HW address. The DUID is non-zero but it
  102. // points to a host for which the reservation hasn't been added.
  103. HostCollection hosts = cfg.getAll(hwaddrs_[i], duids_[i + 25]);
  104. ASSERT_EQ(1, hosts.size());
  105. EXPECT_EQ(i % 10 + 1, hosts[0]->getIPv4SubnetID());
  106. EXPECT_EQ("192.0.2.5", hosts[0]->getIPv4Reservation().toText());
  107. // Get host identified by DUID. The HW address is non-null but it
  108. // points to a host for which the reservation hasn't been added.
  109. hosts = cfg.getAll(hwaddrs_[i + 25], duids_[i]);
  110. ASSERT_EQ(1, hosts.size());
  111. EXPECT_EQ(i % 5 + 1, hosts[0]->getIPv4SubnetID());
  112. EXPECT_EQ("192.0.2.10", hosts[0]->getIPv4Reservation().toText());
  113. }
  114. // Make sure that the reservations do not exist for the hardware addresses
  115. // and DUIDs from the range of 25 to 49.
  116. for (int i = 49; i >= 25; --i) {
  117. EXPECT_TRUE(cfg.getAll(hwaddrs_[i]).empty());
  118. EXPECT_TRUE(cfg.getAll(HWAddrPtr(), duids_[i]).empty());
  119. }
  120. }
  121. // This test verifies that the host can be added to multiple subnets and
  122. // that the getAll message retrieves all instances of the host.
  123. TEST_F(CfgHostsTest, getAllRepeatingHosts) {
  124. CfgHosts cfg;
  125. // Add hosts.
  126. for (int i = 0; i < 25; ++i) {
  127. // Add two hosts, using the same HW address to two distnict subnets.
  128. cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
  129. "hw-address",
  130. SubnetID(1), SubnetID(2),
  131. IOAddress("192.0.2.5"))));
  132. cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
  133. "hw-address",
  134. SubnetID(2), SubnetID(3),
  135. IOAddress("10.0.0.5"))));
  136. // Add two hosts, using the same DUID to two distnict subnets.
  137. cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
  138. SubnetID(1), SubnetID(2),
  139. IOAddress("192.0.2.10"))));
  140. cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
  141. SubnetID(2), SubnetID(3),
  142. IOAddress("10.0.2.10"))));
  143. }
  144. // Verify that hosts can be retrieved.
  145. for (int i = 0; i < 25; ++i) {
  146. // Get host by HW address. The DUID is non-null but the reservation
  147. // should be returned for the HW address because there are no
  148. // reservations for the DUIDs from the range of 25 to 49.
  149. HostCollection hosts = cfg.getAll(hwaddrs_[i], duids_[i + 25]);
  150. ASSERT_EQ(2, hosts.size());
  151. EXPECT_EQ(1, hosts[0]->getIPv4SubnetID());
  152. EXPECT_EQ("192.0.2.5", hosts[0]->getIPv4Reservation().toText());
  153. EXPECT_EQ(2, hosts[1]->getIPv4SubnetID());
  154. EXPECT_EQ("10.0.0.5", hosts[1]->getIPv4Reservation().toText());
  155. // Get host by DUID. The HW address is non-null but the reservation
  156. // should be returned for the DUID because there are no
  157. // reservations for the HW addresses from the range of 25 to 49.
  158. hosts = cfg.getAll(hwaddrs_[i + 25], duids_[i]);
  159. ASSERT_EQ(2, hosts.size());
  160. EXPECT_EQ(1, hosts[0]->getIPv4SubnetID());
  161. EXPECT_EQ(2, hosts[1]->getIPv4SubnetID());
  162. }
  163. // The getAll function should return empty containers for the HW addresses
  164. // and DUIDs for which the reservations haven't been added.
  165. for (int i = 25; i < 50; ++i) {
  166. EXPECT_TRUE(cfg.getAll(hwaddrs_[i]).empty());
  167. EXPECT_TRUE(cfg.getAll(HWAddrPtr(), duids_[i]).empty());
  168. }
  169. }
  170. // This test checks that all reservations for the specified IPv4 address can
  171. // be retrieved.
  172. TEST_F(CfgHostsTest, getAll4ByAddress) {
  173. CfgHosts cfg;
  174. // Add hosts.
  175. for (int i = 0; i < 25; ++i) {
  176. // Add host identified by the HW address.
  177. cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
  178. "hw-address",
  179. SubnetID(1 + i), SubnetID(0),
  180. IOAddress("192.0.2.5"))));
  181. // Add host identified by the DUID.
  182. cfg.add(HostPtr(new Host(duids_[i]->toText(),
  183. "duid",
  184. SubnetID(1 + i), SubnetID(0),
  185. IOAddress("192.0.2.10"))));
  186. }
  187. HostCollection hosts = cfg.getAll4(IOAddress("192.0.2.10"));
  188. std::set<uint32_t> subnet_ids;
  189. for (HostCollection::const_iterator host = hosts.begin(); host != hosts.end();
  190. ++host) {
  191. subnet_ids.insert((*host)->getIPv4SubnetID());
  192. }
  193. ASSERT_EQ(25, subnet_ids.size());
  194. EXPECT_EQ(1, *subnet_ids.begin());
  195. EXPECT_EQ(25, *subnet_ids.rbegin());
  196. }
  197. // This test checks that the reservations can be retrieved for the particular
  198. // host connected to the specific IPv4 subnet (by subnet id).
  199. TEST_F(CfgHostsTest, get4) {
  200. CfgHosts cfg;
  201. // Add hosts.
  202. for (int i = 0; i < 25; ++i) {
  203. // Add host identified by HW address.
  204. cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false),
  205. "hw-address",
  206. SubnetID(1 + i % 2), SubnetID(13),
  207. increase(IOAddress("192.0.2.5"), i))));
  208. // Add host identified by DUID.
  209. cfg.add(HostPtr(new Host(duids_[i]->toText(), "duid",
  210. SubnetID(1 + i % 2), SubnetID(13),
  211. increase(IOAddress("192.0.2.100"), i))));
  212. }
  213. for (int i = 0; i < 25; ++i) {
  214. // Retrieve host by HW address. The DUID is non-null but there is no
  215. // reservation made for the DUID so the reservation is returned for
  216. // HW address.
  217. HostPtr host = cfg.get4(SubnetID(1 + i % 2), hwaddrs_[i],
  218. duids_[i + 25]);
  219. ASSERT_TRUE(host);
  220. EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
  221. EXPECT_EQ(increase(IOAddress("192.0.2.5"), i),
  222. host->getIPv4Reservation());
  223. // Retrieve host by DUID. The HW address is non-null but there is no
  224. // reservation made for the HW address so the reservation is returned
  225. // for the DUID.
  226. host = cfg.get4(SubnetID(1 + i % 2), hwaddrs_[i + 25], duids_[i]);
  227. ASSERT_TRUE(host);
  228. EXPECT_EQ(1 + i % 2, host->getIPv4SubnetID());
  229. EXPECT_EQ(increase(IOAddress("192.0.2.100"), i),
  230. host->getIPv4Reservation());
  231. }
  232. // Also check that when the get4 finds multiple Host objects that fulfil
  233. // search criteria, it will throw an exception. Note that the reservation
  234. // exist both for hwaddrs_[0] and duids_[0].
  235. EXPECT_THROW(cfg.get4(SubnetID(1), hwaddrs_[0], duids_[0]), DuplicateHost);
  236. }
  237. // This test checks that the reservations can be retrieved for the particular
  238. // host connected to the specific IPv6 subnet (by subnet id).
  239. TEST_F(CfgHostsTest, get6) {
  240. CfgHosts cfg;
  241. // Add hosts.
  242. for (int i = 0; i < 25; ++i) {
  243. // Add host identified by HW address.
  244. HostPtr host = HostPtr(new Host(hwaddrs_[i]->toText(false),
  245. "hw-address",
  246. SubnetID(10), SubnetID(1 + i % 2),
  247. IOAddress("0.0.0.0")));
  248. host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
  249. increase(IOAddress("2001:db8:1::1"),
  250. i)));
  251. cfg.add(host);
  252. // Add host identified by DUID.
  253. host = HostPtr(new Host(duids_[i]->toText(), "duid",
  254. SubnetID(10), SubnetID(1 + i % 2),
  255. IOAddress("0.0.0.0")));
  256. host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
  257. increase(IOAddress("2001:db8:2::1"),
  258. i)));
  259. cfg.add(host);
  260. }
  261. for (int i = 0; i < 25; ++i) {
  262. // Retrieve host by HW address. The DUID is non-null but there is no
  263. // reservation made for the DUID so the reservation is returned for
  264. // HW address.
  265. HostPtr host = cfg.get6(SubnetID(1 + i % 2), duids_[i + 25],
  266. hwaddrs_[i]);
  267. ASSERT_TRUE(host);
  268. EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
  269. IPv6ResrvRange reservations =
  270. host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  271. ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
  272. EXPECT_EQ(increase(IOAddress("2001:db8:1::1"), i),
  273. reservations.first->second.getPrefix());
  274. // Retrieve host by DUID. The HW address is non-null but there is no
  275. // reservation made for the HW address so the reservation is returned
  276. // for the DUID.
  277. host = cfg.get6(SubnetID(1 + i % 2), duids_[i], hwaddrs_[i + 25]);
  278. ASSERT_TRUE(host);
  279. EXPECT_EQ(1 + i % 2, host->getIPv6SubnetID());
  280. reservations = host->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  281. ASSERT_EQ(1, std::distance(reservations.first, reservations.second));
  282. EXPECT_EQ(increase(IOAddress("2001:db8:2::1"), i),
  283. reservations.first->second.getPrefix());
  284. }
  285. // Also check that when the get6 finds multiple Host objects that fulfil
  286. // search criteria, it will throw an exception. Note that the reservation
  287. // exist both for hwaddrs_[0] and duids_[0].
  288. EXPECT_THROW(cfg.get6(SubnetID(1), duids_[0], hwaddrs_[0]), DuplicateHost);
  289. }
  290. TEST_F(CfgHostsTest, zeroSubnetIDs) {
  291. CfgHosts cfg;
  292. ASSERT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  293. "hw-address",
  294. SubnetID(0), SubnetID(0),
  295. IOAddress("10.0.0.1")))),
  296. isc::BadValue);
  297. }
  298. // This test verifies that it is not possible to add the same Host to the
  299. // same IPv4 subnet twice.
  300. TEST_F(CfgHostsTest, duplicatesSubnet4HWAddr) {
  301. CfgHosts cfg;
  302. // Add a host.
  303. ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  304. "hw-address",
  305. SubnetID(10), SubnetID(0),
  306. IOAddress("10.0.0.1")))));
  307. // Try to add the host with the same HW address to the same subnet. The fact
  308. // that the IP address is different here shouldn't really matter.
  309. EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  310. "hw-address",
  311. SubnetID(10), SubnetID(0),
  312. IOAddress("10.0.0.10")))),
  313. isc::dhcp::DuplicateHost);
  314. // Now try to add it to a different subnet. It should go through.
  315. EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  316. "hw-address",
  317. SubnetID(11), SubnetID(0),
  318. IOAddress("10.0.0.10")))));
  319. }
  320. // This test verifies that it is not possible to add the same Host to the
  321. // same IPv4 subnet twice.
  322. TEST_F(CfgHostsTest, duplicatesSubnet4DUID) {
  323. CfgHosts cfg;
  324. // Add a host.
  325. ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  326. "duid",
  327. SubnetID(10), SubnetID(0),
  328. IOAddress("10.0.0.1")))));
  329. // Try to add the host with the same DUID to the same subnet. The fact
  330. // that the IP address is different here shouldn't really matter.
  331. EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  332. "duid",
  333. SubnetID(10), SubnetID(0),
  334. IOAddress("10.0.0.10")))),
  335. isc::dhcp::DuplicateHost);
  336. // Now try to add it to a different subnet. It should go through.
  337. EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  338. "duid",
  339. SubnetID(11), SubnetID(0),
  340. IOAddress("10.0.0.10")))));
  341. }
  342. // This test verifies that it is not possible to add the same Host to the
  343. // same IPv6 subnet twice.
  344. TEST_F(CfgHostsTest, duplicatesSubnet6HWAddr) {
  345. CfgHosts cfg;
  346. // Add a host.
  347. ASSERT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  348. "hw-address",
  349. SubnetID(0), SubnetID(1),
  350. IOAddress("0.0.0.0"),
  351. "foo.example.com"))));
  352. // Try to add the host with the same HW address to the same subnet. The fact
  353. // that the IP address is different here shouldn't really matter.
  354. EXPECT_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  355. "hw-address",
  356. SubnetID(0), SubnetID(1),
  357. IOAddress("0.0.0.0"),
  358. "foo.example.com"))),
  359. isc::dhcp::DuplicateHost);
  360. // Now try to add it to a different subnet. It should go through.
  361. EXPECT_NO_THROW(cfg.add(HostPtr(new Host(hwaddrs_[0]->toText(false),
  362. "hw-address",
  363. SubnetID(0), SubnetID(2),
  364. IOAddress("0.0.0.0"),
  365. "foo.example.com"))));
  366. }
  367. // This test verifies that it is not possible to add the same Host to the
  368. // same IPv6 subnet twice.
  369. TEST_F(CfgHostsTest, duplicatesSubnet6DUID) {
  370. CfgHosts cfg;
  371. // Add a host.
  372. ASSERT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  373. "duid",
  374. SubnetID(0), SubnetID(1),
  375. IOAddress("0.0.0.0"),
  376. "foo.example.com"))));
  377. // Try to add the host with the same DUID to the same subnet. The fact
  378. // that the IP address is different here shouldn't really matter.
  379. EXPECT_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  380. "duid",
  381. SubnetID(0), SubnetID(1),
  382. IOAddress("0.0.0.0"),
  383. "foo.example.com"))),
  384. isc::dhcp::DuplicateHost);
  385. // Now try to add it to a different subnet. It should go through.
  386. EXPECT_NO_THROW(cfg.add(HostPtr(new Host(duids_[0]->toText(),
  387. "duid",
  388. SubnetID(0), SubnetID(2),
  389. IOAddress("0.0.0.0"),
  390. "foo.example.com"))));
  391. }
  392. } // end of anonymous namespace