lease_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // Copyright (C) 2013-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 <dhcpsrv/lease.h>
  18. #include <gtest/gtest.h>
  19. #include <vector>
  20. #include <sstream>
  21. using namespace isc;
  22. using namespace isc::asiolink;
  23. using namespace isc::dhcp;
  24. namespace {
  25. /// Hardware address used by different tests.
  26. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  27. /// Client id used by different tests.
  28. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  29. /// Valid lifetime value used by different tests.
  30. const uint32_t VALID_LIFETIME = 500;
  31. /// Subnet ID used by different tests.
  32. const uint32_t SUBNET_ID = 42;
  33. /// IAID value used by different tests.
  34. const uint32_t IAID = 7;
  35. /// @brief Creates an instance of the lease with certain FQDN data.
  36. ///
  37. /// @param hostname Hostname.
  38. /// @param fqdn_fwd Forward FQDN update setting for a created lease.
  39. /// @param fqdn_rev Reverse FQDN update setting for a created lease.
  40. ///
  41. /// @return Instance of the created lease.
  42. Lease4 createLease4(const std::string& hostname, const bool fqdn_fwd,
  43. const bool fqdn_rev) {
  44. Lease4 lease;
  45. lease.hostname_ = hostname;
  46. lease.fqdn_fwd_ = fqdn_fwd;
  47. lease.fqdn_rev_ = fqdn_rev;
  48. return (lease);
  49. }
  50. /// @brief Fixture class used in Lease4 testing.
  51. class Lease4Test : public ::testing::Test {
  52. public:
  53. /// Default constructor
  54. ///
  55. /// Currently it only initializes hardware address.
  56. Lease4Test() {
  57. hwaddr_.reset(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  58. }
  59. /// Hardware address, used by tests.
  60. HWAddrPtr hwaddr_;
  61. };
  62. /// Lease4 is also defined in lease_mgr.h, so is tested in this file as well.
  63. // This test checks if the Lease4 structure can be instantiated correctly
  64. TEST_F(Lease4Test, constructor) {
  65. // Random values for the tests
  66. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  67. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  68. ClientId clientid(clientid_vec);
  69. // ...and a time
  70. const time_t current_time = time(NULL);
  71. // Other random constants.
  72. const uint32_t SUBNET_ID = 42;
  73. const uint32_t VALID_LIFETIME = 500;
  74. // We want to check that various addresses work, so let's iterate over
  75. // these.
  76. const uint32_t ADDRESS[] = {
  77. 0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
  78. };
  79. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  80. // Create the lease
  81. Lease4 lease(ADDRESS[i], hwaddr_,
  82. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0,
  83. current_time, SUBNET_ID, true, true,
  84. "hostname.example.com.");
  85. EXPECT_EQ(ADDRESS[i], static_cast<uint32_t>(lease.addr_));
  86. EXPECT_EQ(0, lease.ext_);
  87. EXPECT_TRUE(hwaddr_ == lease.hwaddr_);
  88. EXPECT_TRUE(clientid == *lease.client_id_);
  89. EXPECT_EQ(0, lease.t1_);
  90. EXPECT_EQ(0, lease.t2_);
  91. EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
  92. EXPECT_EQ(current_time, lease.cltt_);
  93. EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
  94. EXPECT_FALSE(lease.fixed_);
  95. EXPECT_EQ("hostname.example.com.", lease.hostname_);
  96. EXPECT_TRUE(lease.fqdn_fwd_);
  97. EXPECT_TRUE(lease.fqdn_rev_);
  98. EXPECT_TRUE(lease.comments_.empty());
  99. }
  100. }
  101. // This test verfies that copy constructor copies Lease4 fields correctly.
  102. TEST_F(Lease4Test, copyConstructor) {
  103. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  104. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  105. ClientId clientid(clientid_vec);
  106. // ...and a time
  107. const time_t current_time = time(NULL);
  108. // Other random constants.
  109. const uint32_t SUBNET_ID = 42;
  110. const uint32_t VALID_LIFETIME = 500;
  111. // Create the lease
  112. Lease4 lease(0xffffffff, hwaddr_,
  113. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  114. SUBNET_ID);
  115. // Use copy constructor to copy the lease.
  116. Lease4 copied_lease(lease);
  117. // Both leases should be now equal. When doing this check we assume that
  118. // the equality operator works correctly.
  119. EXPECT_TRUE(lease == copied_lease);
  120. // Client IDs are equal, but they should be in two distinct pointers.
  121. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  122. // Hardware addresses are equal, but they should point to two objects,
  123. // each holding the same data. The content should be equal...
  124. EXPECT_TRUE(*lease.hwaddr_ == *copied_lease.hwaddr_);
  125. // ... but it should point to different objects.
  126. EXPECT_FALSE(lease.hwaddr_ == copied_lease.hwaddr_);
  127. // Now let's check that the hwaddr pointer is copied even if it's NULL:
  128. lease.hwaddr_.reset();
  129. Lease4 copied_lease2(lease);
  130. EXPECT_TRUE(lease == copied_lease2);
  131. }
  132. // This test verfies that the assignment operator copies all Lease4 fields
  133. // correctly.
  134. TEST_F(Lease4Test, operatorAssign) {
  135. // Random values for the tests
  136. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  137. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  138. ClientId clientid(clientid_vec);
  139. // ...and a time
  140. const time_t current_time = time(NULL);
  141. // Other random constants.
  142. const uint32_t SUBNET_ID = 42;
  143. const uint32_t VALID_LIFETIME = 500;
  144. // Create the lease
  145. Lease4 lease(0xffffffff, hwaddr_,
  146. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  147. SUBNET_ID);
  148. // Use assignment operator to assign the lease.
  149. Lease4 copied_lease = lease;
  150. // Both leases should be now equal. When doing this check we assume that
  151. // the equality operator works correctly.
  152. EXPECT_TRUE(lease == copied_lease);
  153. // Client IDs are equal, but they should be in two distinct pointers.
  154. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  155. // Hardware addresses are equal, but they should point to two objects,
  156. // each holding the same data. The content should be equal...
  157. EXPECT_TRUE(*lease.hwaddr_ == *copied_lease.hwaddr_);
  158. // ... but it should point to different objects.
  159. EXPECT_FALSE(lease.hwaddr_ == copied_lease.hwaddr_);
  160. // Now let's check that the hwaddr pointer is copied even if it's NULL:
  161. lease.hwaddr_.reset();
  162. copied_lease = lease;
  163. EXPECT_TRUE(lease == copied_lease);
  164. }
  165. // This test verifies that the matches() returns true if two leases differ
  166. // by values other than address, HW address, Client ID and ext_.
  167. TEST_F(Lease4Test, matches) {
  168. // Create two leases which share the same address, HW address, client id
  169. // and ext_ value.
  170. const time_t current_time = time(NULL);
  171. Lease4 lease1(IOAddress("192.0.2.3"), hwaddr_, CLIENTID,
  172. sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
  173. SUBNET_ID);
  174. lease1.hostname_ = "lease1.example.com.";
  175. lease1.fqdn_fwd_ = true;
  176. lease1.fqdn_rev_ = true;
  177. // We need to make an explicit copy. Otherwise the second lease will just
  178. // store a pointer and we'll have two leases pointing to a single HWAddr.
  179. // That would make modifications to only one impossible.
  180. HWAddrPtr hwcopy(new HWAddr(*hwaddr_));
  181. Lease4 lease2(IOAddress("192.0.2.3"), hwcopy, CLIENTID,
  182. sizeof(CLIENTID), VALID_LIFETIME + 10, current_time - 10,
  183. 100, 200, SUBNET_ID);
  184. lease2.hostname_ = "lease2.example.com.";
  185. lease2.fqdn_fwd_ = false;
  186. lease2.fqdn_rev_ = true;
  187. // Leases should match.
  188. EXPECT_TRUE(lease1.matches(lease2));
  189. EXPECT_TRUE(lease2.matches(lease1));
  190. // Change address, leases should not match anymore.
  191. lease1.addr_ = IOAddress("192.0.2.4");
  192. EXPECT_FALSE(lease1.matches(lease2));
  193. lease1.addr_ = lease2.addr_;
  194. // Change HW address, leases should not match.
  195. lease1.hwaddr_->hwaddr_[1] += 1;
  196. EXPECT_FALSE(lease1.matches(lease2));
  197. lease1.hwaddr_ = lease2.hwaddr_;
  198. // Chanage client id, leases should not match.
  199. std::vector<uint8_t> client_id = lease1.client_id_->getClientId();
  200. client_id[1] += 1;
  201. lease1.client_id_.reset(new ClientId(client_id));
  202. EXPECT_FALSE(lease1.matches(lease2));
  203. lease1.client_id_ = lease2.client_id_;
  204. // Change ext_, leases should not match.
  205. lease1.ext_ += 1;
  206. EXPECT_FALSE(lease1.matches(lease2));
  207. lease1.ext_ = lease2.ext_;
  208. }
  209. /// @brief Lease4 Equality Test
  210. ///
  211. /// Checks that the operator==() correctly compares two leases for equality.
  212. /// As operator!=() is also defined for this class, every check on operator==()
  213. /// is followed by the reverse check on operator!=().
  214. TEST_F(Lease4Test, operatorEquals) {
  215. // Random values for the tests
  216. const uint32_t ADDRESS = 0x01020304;
  217. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  218. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  219. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  220. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  221. ClientId clientid(clientid_vec);
  222. const time_t current_time = time(NULL);
  223. const uint32_t SUBNET_ID = 42;
  224. const uint32_t VALID_LIFETIME = 500;
  225. // Check when the leases are equal.
  226. Lease4 lease1(ADDRESS, hwaddr_,
  227. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0,
  228. 0, SUBNET_ID);
  229. // We need to make an explicit copy. Otherwise the second lease will just
  230. // store a pointer and we'll have two leases pointing to a single HWAddr.
  231. // That would make modifications to only one impossible.
  232. HWAddrPtr hwcopy(new HWAddr(*hwaddr_));
  233. Lease4 lease2(ADDRESS, hwcopy,
  234. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
  235. SUBNET_ID);
  236. EXPECT_TRUE(lease1 == lease2);
  237. EXPECT_FALSE(lease1 != lease2);
  238. // Now vary individual fields in a lease and check that the leases compare
  239. // not equal in every case.
  240. lease1.addr_ = IOAddress(ADDRESS + 1);
  241. EXPECT_FALSE(lease1 == lease2);
  242. EXPECT_TRUE(lease1 != lease2);
  243. lease1.addr_ = lease2.addr_;
  244. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  245. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  246. ++lease1.ext_;
  247. EXPECT_FALSE(lease1 == lease2);
  248. EXPECT_TRUE(lease1 != lease2);
  249. lease1.ext_ = lease2.ext_;
  250. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  251. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  252. ++lease1.hwaddr_->hwaddr_[0];
  253. EXPECT_FALSE(lease1 == lease2);
  254. EXPECT_TRUE(lease1 != lease2);
  255. lease1.hwaddr_ = lease2.hwaddr_;
  256. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  257. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  258. ++clientid_vec[0];
  259. lease1.client_id_.reset(new ClientId(clientid_vec));
  260. EXPECT_FALSE(lease1 == lease2);
  261. EXPECT_TRUE(lease1 != lease2);
  262. --clientid_vec[0];
  263. lease1.client_id_.reset(new ClientId(clientid_vec));
  264. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  265. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  266. ++lease1.t1_;
  267. EXPECT_FALSE(lease1 == lease2);
  268. EXPECT_TRUE(lease1 != lease2);
  269. lease1.t1_ = lease2.t1_;
  270. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  271. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  272. ++lease1.t2_;
  273. EXPECT_FALSE(lease1 == lease2);
  274. EXPECT_TRUE(lease1 != lease2);
  275. lease1.t2_ = lease2.t2_;
  276. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  277. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  278. ++lease1.valid_lft_;
  279. EXPECT_FALSE(lease1 == lease2);
  280. EXPECT_TRUE(lease1 != lease2);
  281. lease1.valid_lft_ = lease2.valid_lft_;
  282. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  283. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  284. ++lease1.cltt_;
  285. EXPECT_FALSE(lease1 == lease2);
  286. EXPECT_TRUE(lease1 != lease2);
  287. lease1.cltt_ = lease2.cltt_;
  288. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  289. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  290. ++lease1.subnet_id_;
  291. EXPECT_FALSE(lease1 == lease2);
  292. EXPECT_TRUE(lease1 != lease2);
  293. lease1.subnet_id_ = lease2.subnet_id_;
  294. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  295. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  296. lease1.fixed_ = !lease1.fixed_;
  297. EXPECT_FALSE(lease1 == lease2);
  298. EXPECT_TRUE(lease1 != lease2);
  299. lease1.fixed_ = lease2.fixed_;
  300. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  301. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  302. lease1.hostname_ += std::string("Something random");
  303. EXPECT_FALSE(lease1 == lease2);
  304. EXPECT_TRUE(lease1 != lease2);
  305. lease1.hostname_ = lease2.hostname_;
  306. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  307. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  308. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  309. EXPECT_FALSE(lease1 == lease2);
  310. EXPECT_TRUE(lease1 != lease2);
  311. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  312. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  313. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  314. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  315. EXPECT_FALSE(lease1 == lease2);
  316. EXPECT_TRUE(lease1 != lease2);
  317. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  318. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  319. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  320. lease1.comments_ += std::string("Something random");
  321. EXPECT_FALSE(lease1 == lease2);
  322. EXPECT_TRUE(lease1 != lease2);
  323. lease1.comments_ = lease2.comments_;
  324. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  325. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  326. }
  327. // Verify that the client id can be returned as a vector object and if client
  328. // id is NULL the empty vector is returned.
  329. TEST(Lease4, getClientIdVector) {
  330. // Create a lease.
  331. Lease4 lease;
  332. // By default, the lease should have client id set to NULL. If it doesn't,
  333. // continuing the test makes no sense.
  334. ASSERT_FALSE(lease.client_id_);
  335. // When client id is NULL the vector returned should be empty.
  336. EXPECT_TRUE(lease.getClientIdVector().empty());
  337. // Now, let's set the non NULL client id. Fill it with the 8 bytes, each
  338. // holding a value of 0x42.
  339. std::vector<uint8_t> client_id_vec(8, 0x42);
  340. lease.client_id_ = ClientIdPtr(new ClientId(client_id_vec));
  341. // Check that the returned vector, encapsulating client id is equal to
  342. // the one that has been used to set the client id for the lease.
  343. std::vector<uint8_t> returned_vec = lease.getClientIdVector();
  344. EXPECT_TRUE(returned_vec == client_id_vec);
  345. }
  346. // Verify the behavior of the function which checks FQDN data for equality.
  347. TEST(Lease4, hasIdenticalFqdn) {
  348. Lease4 lease = createLease4("myhost.example.com.", true, true);
  349. EXPECT_TRUE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  350. true, true)));
  351. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("other.example.com.",
  352. true, true)));
  353. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  354. false, true)));
  355. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  356. true, false)));
  357. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  358. false, false)));
  359. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("other.example.com.",
  360. false, false)));
  361. }
  362. // Verify that toText() method reports Lease4 structure properly.
  363. TEST_F(Lease4Test, toText) {
  364. const time_t current_time = 12345678;
  365. Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, CLIENTID, sizeof(CLIENTID),
  366. 3600, 123, 456, current_time, 789);
  367. std::stringstream expected;
  368. expected << "Address: 192.0.2.3\n"
  369. << "Valid life: 3600\n"
  370. << "T1: 123\n"
  371. << "T2: 456\n"
  372. << "Cltt: 12345678\n"
  373. << "Hardware addr: " << hwaddr_->toText(false) << "\n"
  374. << "Subnet ID: 789\n";
  375. EXPECT_EQ(expected.str(), lease.toText());
  376. // Now let's try with a lease without hardware address.
  377. lease.hwaddr_.reset();
  378. expected.str("");
  379. expected << "Address: 192.0.2.3\n"
  380. << "Valid life: 3600\n"
  381. << "T1: 123\n"
  382. << "T2: 456\n"
  383. << "Cltt: 12345678\n"
  384. << "Hardware addr: (none)\n"
  385. << "Subnet ID: 789\n";
  386. EXPECT_EQ(expected.str(), lease.toText());
  387. }
  388. /// @brief Creates an instance of the lease with certain FQDN data.
  389. ///
  390. /// @param hostname Hostname.
  391. /// @param fqdn_fwd Forward FQDN update setting for a created lease.
  392. /// @param fqdn_rev Reverse FQDN update setting for a created lease.
  393. ///
  394. /// @return Instance of the created lease.
  395. Lease6 createLease6(const std::string& hostname, const bool fqdn_fwd,
  396. const bool fqdn_rev) {
  397. Lease6 lease;
  398. lease.hostname_ = hostname;
  399. lease.fqdn_fwd_ = fqdn_fwd;
  400. lease.fqdn_rev_ = fqdn_rev;
  401. return (lease);
  402. }
  403. // Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
  404. // This test checks if the Lease6 structure can be instantiated correctly
  405. TEST(Lease6, Lease6ConstructorDefault) {
  406. // check a variety of addresses with different bits set.
  407. const char* ADDRESS[] = {
  408. "::", "::1", "2001:db8:1::456",
  409. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  410. "8000::", "8000::1",
  411. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  412. };
  413. // Other values
  414. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  415. DuidPtr duid(new DUID(llt, sizeof(llt)));
  416. uint32_t iaid = 7; // Just a number
  417. SubnetID subnet_id = 8; // Just another number
  418. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  419. IOAddress addr(ADDRESS[i]);
  420. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  421. duid, iaid, 100, 200, 50, 80,
  422. subnet_id));
  423. EXPECT_TRUE(lease->addr_ == addr);
  424. EXPECT_TRUE(*lease->duid_ == *duid);
  425. EXPECT_TRUE(lease->iaid_ == iaid);
  426. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  427. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  428. EXPECT_TRUE(lease->preferred_lft_ == 100);
  429. EXPECT_TRUE(lease->valid_lft_ == 200);
  430. EXPECT_TRUE(lease->t1_ == 50);
  431. EXPECT_TRUE(lease->t2_ == 80);
  432. EXPECT_FALSE(lease->fqdn_fwd_);
  433. EXPECT_FALSE(lease->fqdn_rev_);
  434. EXPECT_TRUE(lease->hostname_.empty());
  435. }
  436. // Lease6 must be instantiated with a DUID, not with NULL pointer
  437. IOAddress addr(ADDRESS[0]);
  438. Lease6Ptr lease2;
  439. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  440. DuidPtr(), iaid, 100, 200, 50, 80,
  441. subnet_id)), InvalidOperation);
  442. }
  443. // This test verifies that the Lease6 constructor which accepts FQDN data,
  444. // sets the data correctly for the lease.
  445. TEST(Lease6, Lease6ConstructorWithFQDN) {
  446. // check a variety of addresses with different bits set.
  447. const char* ADDRESS[] = {
  448. "::", "::1", "2001:db8:1::456",
  449. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  450. "8000::", "8000::1",
  451. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  452. };
  453. // Other values
  454. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  455. DuidPtr duid(new DUID(llt, sizeof(llt)));
  456. uint32_t iaid = 7; // Just a number
  457. SubnetID subnet_id = 8; // Just another number
  458. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  459. IOAddress addr(ADDRESS[i]);
  460. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  461. duid, iaid, 100, 200, 50, 80, subnet_id,
  462. true, true, "host.example.com."));
  463. EXPECT_TRUE(lease->addr_ == addr);
  464. EXPECT_TRUE(*lease->duid_ == *duid);
  465. EXPECT_TRUE(lease->iaid_ == iaid);
  466. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  467. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  468. EXPECT_TRUE(lease->preferred_lft_ == 100);
  469. EXPECT_TRUE(lease->valid_lft_ == 200);
  470. EXPECT_TRUE(lease->t1_ == 50);
  471. EXPECT_TRUE(lease->t2_ == 80);
  472. EXPECT_TRUE(lease->fqdn_fwd_);
  473. EXPECT_TRUE(lease->fqdn_rev_);
  474. EXPECT_EQ("host.example.com.", lease->hostname_);
  475. }
  476. // Lease6 must be instantiated with a DUID, not with NULL pointer
  477. IOAddress addr(ADDRESS[0]);
  478. Lease6Ptr lease2;
  479. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  480. DuidPtr(), iaid, 100, 200, 50, 80,
  481. subnet_id)), InvalidOperation);
  482. }
  483. // This test verifies that the matches() function returns true if two leases
  484. // differ by values other than address, type, prefix length, IAID and DUID.
  485. TEST(Lease6, matches) {
  486. // Create two matching leases.
  487. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  488. DuidPtr duid(new DUID(llt, sizeof(llt)));
  489. Lease6 lease1(Lease6::TYPE_NA, IOAddress("2001:db8:1::1"), duid,
  490. IAID, 100, 200, 50, 80,
  491. SUBNET_ID);
  492. lease1.hostname_ = "lease1.example.com.";
  493. lease1.fqdn_fwd_ = true;
  494. lease1.fqdn_rev_ = true;
  495. Lease6 lease2(Lease6::TYPE_NA, IOAddress("2001:db8:1::1"), duid,
  496. IAID, 200, 300, 90, 70,
  497. SUBNET_ID);
  498. lease2.hostname_ = "lease1.example.com.";
  499. lease2.fqdn_fwd_ = false;
  500. lease2.fqdn_rev_ = true;
  501. EXPECT_TRUE(lease1.matches(lease2));
  502. // Modify each value used to match both leases, and make sure that
  503. // leases don't match.
  504. // Modify address.
  505. lease1.addr_ = IOAddress("2001:db8:1::2");
  506. EXPECT_FALSE(lease1.matches(lease2));
  507. lease1.addr_ = lease2.addr_;
  508. // Modify lease type.
  509. lease1.type_ = Lease6::TYPE_TA;
  510. EXPECT_FALSE(lease1.matches(lease2));
  511. lease1.type_ = lease2.type_;
  512. // Modify prefix length.
  513. lease1.prefixlen_ += 1;
  514. EXPECT_FALSE(lease1.matches(lease2));
  515. lease1.prefixlen_ = lease2.prefixlen_;
  516. // Modify IAID.
  517. lease1.iaid_ += 1;
  518. EXPECT_FALSE(lease1.matches(lease2));
  519. lease1.iaid_ = lease2.iaid_;
  520. // Modify DUID.
  521. llt[1] += 1;
  522. duid.reset(new DUID(llt, sizeof(llt)));
  523. lease1.duid_ = duid;
  524. EXPECT_FALSE(lease1.matches(lease2));
  525. lease1.duid_ = lease2.duid_;
  526. // Hardware address checks
  527. EXPECT_TRUE(lease1.matches(lease2)); // Neither lease have hardware address.
  528. // Let's add a hardware lease to the first one.
  529. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  530. lease1.hwaddr_ = hwaddr;
  531. // Only the first one has a hardware address, so not equal.
  532. EXPECT_FALSE(lease1.matches(lease2));
  533. // Only the second one has it, so still not equal.
  534. lease1.hwaddr_.reset();
  535. lease2.hwaddr_ = hwaddr;
  536. EXPECT_FALSE(lease1.matches(lease2));
  537. // Ok, now both have it - they should be equal.
  538. lease1.hwaddr_ = hwaddr;
  539. EXPECT_TRUE(lease1.matches(lease2));
  540. // Let's create a second instance that have the same values.
  541. HWAddrPtr hwaddr2(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  542. lease2.hwaddr_ = hwaddr2;
  543. EXPECT_TRUE(lease1.matches(lease2));
  544. // Let's modify the second address and check that they won't be equal anymore.
  545. hwaddr2->hwaddr_[0]++;
  546. EXPECT_FALSE(lease1.matches(lease2));
  547. }
  548. /// @brief Lease6 Equality Test
  549. ///
  550. /// Checks that the operator==() correctly compares two leases for equality.
  551. /// As operator!=() is also defined for this class, every check on operator==()
  552. /// is followed by the reverse check on operator!=().
  553. TEST(Lease6, OperatorEquals) {
  554. // check a variety of addresses with different bits set.
  555. const IOAddress addr("2001:db8:1::456");
  556. uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  557. DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  558. uint32_t iaid = 7; // just a number
  559. SubnetID subnet_id = 8; // just another number
  560. // Check for equality.
  561. Lease6 lease1(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  562. subnet_id);
  563. Lease6 lease2(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  564. subnet_id);
  565. // cltt_ constructs with time(NULL), make sure they are always equal
  566. lease1.cltt_ = lease2.cltt_;
  567. EXPECT_TRUE(lease1 == lease2);
  568. EXPECT_FALSE(lease1 != lease2);
  569. // Go through and alter all the fields one by one
  570. lease1.addr_ = IOAddress("::1");
  571. EXPECT_FALSE(lease1 == lease2);
  572. EXPECT_TRUE(lease1 != lease2);
  573. lease1.addr_ = lease2.addr_;
  574. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  575. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  576. lease1.type_ = Lease::TYPE_PD;
  577. EXPECT_FALSE(lease1 == lease2);
  578. EXPECT_TRUE(lease1 != lease2);
  579. lease1.type_ = lease2.type_;
  580. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  581. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  582. ++lease1.prefixlen_;
  583. EXPECT_FALSE(lease1 == lease2);
  584. EXPECT_TRUE(lease1 != lease2);
  585. lease1.prefixlen_ = lease2.prefixlen_;
  586. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  587. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  588. ++lease1.iaid_;
  589. EXPECT_FALSE(lease1 == lease2);
  590. EXPECT_TRUE(lease1 != lease2);
  591. lease1.iaid_ = lease2.iaid_;
  592. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  593. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  594. ++duid_array[0];
  595. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  596. EXPECT_FALSE(lease1 == lease2);
  597. EXPECT_TRUE(lease1 != lease2);
  598. --duid_array[0];
  599. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  600. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  601. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  602. ++lease1.preferred_lft_;
  603. EXPECT_FALSE(lease1 == lease2);
  604. EXPECT_TRUE(lease1 != lease2);
  605. lease1.preferred_lft_ = lease2.preferred_lft_;
  606. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  607. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  608. ++lease1.valid_lft_;
  609. EXPECT_FALSE(lease1 == lease2);
  610. EXPECT_TRUE(lease1 != lease2);
  611. lease1.valid_lft_ = lease2.valid_lft_;
  612. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  613. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  614. ++lease1.t1_;
  615. EXPECT_FALSE(lease1 == lease2);
  616. EXPECT_TRUE(lease1 != lease2);
  617. lease1.t1_ = lease2.t1_;
  618. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  619. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  620. ++lease1.t2_;
  621. EXPECT_FALSE(lease1 == lease2);
  622. EXPECT_TRUE(lease1 != lease2);
  623. lease1.t2_ = lease2.t2_;
  624. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  625. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  626. ++lease1.cltt_;
  627. EXPECT_FALSE(lease1 == lease2);
  628. EXPECT_TRUE(lease1 != lease2);
  629. lease1.cltt_ = lease2.cltt_;
  630. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  631. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  632. ++lease1.subnet_id_;
  633. EXPECT_FALSE(lease1 == lease2);
  634. EXPECT_TRUE(lease1 != lease2);
  635. lease1.subnet_id_ = lease2.subnet_id_;
  636. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  637. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  638. lease1.fixed_ = !lease1.fixed_;
  639. EXPECT_FALSE(lease1 == lease2);
  640. EXPECT_TRUE(lease1 != lease2);
  641. lease1.fixed_ = lease2.fixed_;
  642. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  643. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  644. lease1.hostname_ += std::string("Something random");
  645. EXPECT_FALSE(lease1 == lease2);
  646. EXPECT_TRUE(lease1 != lease2);
  647. lease1.hostname_ = lease2.hostname_;
  648. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  649. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  650. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  651. EXPECT_FALSE(lease1 == lease2);
  652. EXPECT_TRUE(lease1 != lease2);
  653. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  654. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  655. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  656. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  657. EXPECT_FALSE(lease1 == lease2);
  658. EXPECT_TRUE(lease1 != lease2);
  659. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  660. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  661. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  662. lease1.comments_ += std::string("Something random");
  663. EXPECT_FALSE(lease1 == lease2);
  664. EXPECT_TRUE(lease1 != lease2);
  665. lease1.comments_ = lease2.comments_;
  666. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  667. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  668. }
  669. // Checks if lease expiration is calculated properly
  670. TEST(Lease6, Lease6Expired) {
  671. const IOAddress addr("2001:db8:1::456");
  672. const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  673. const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  674. const uint32_t iaid = 7; // Just a number
  675. const SubnetID subnet_id = 8; // Just another number
  676. Lease6 lease(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  677. subnet_id);
  678. // Case 1: a second before expiration
  679. lease.cltt_ = time(NULL) - 100;
  680. lease.valid_lft_ = 101;
  681. EXPECT_FALSE(lease.expired());
  682. // Case 2: the lease will expire after this second is concluded
  683. lease.cltt_ = time(NULL) - 101;
  684. EXPECT_FALSE(lease.expired());
  685. // Case 3: the lease is expired
  686. lease.cltt_ = time(NULL) - 102;
  687. EXPECT_TRUE(lease.expired());
  688. }
  689. // Verify that the DUID can be returned as a vector object and if DUID is NULL
  690. // the empty vector is returned.
  691. TEST(Lease6, getDuidVector) {
  692. // Create a lease.
  693. Lease6 lease;
  694. // By default, the lease should have client id set to NULL. If it doesn't,
  695. // continuing the test makes no sense.
  696. ASSERT_FALSE(lease.duid_);
  697. // When client id is NULL the vector returned should be empty.
  698. EXPECT_TRUE(lease.getDuidVector().empty());
  699. // Now, let's set the non NULL DUID. Fill it with the 8 bytes, each
  700. // holding a value of 0x42.
  701. std::vector<uint8_t> duid_vec(8, 0x42);
  702. lease.duid_ = DuidPtr(new DUID(duid_vec));
  703. // Check that the returned vector, encapsulating DUID is equal to
  704. // the one that has been used to set the DUID for the lease.
  705. std::vector<uint8_t> returned_vec = lease.getDuidVector();
  706. EXPECT_TRUE(returned_vec == duid_vec);
  707. }
  708. // Verify the behavior of the function which checks FQDN data for equality.
  709. TEST(Lease6, hasIdenticalFqdn) {
  710. Lease6 lease = createLease6("myhost.example.com.", true, true);
  711. EXPECT_TRUE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  712. true, true)));
  713. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("other.example.com.",
  714. true, true)));
  715. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  716. false, true)));
  717. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  718. true, false)));
  719. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  720. false, false)));
  721. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("other.example.com.",
  722. false, false)));
  723. }
  724. // Verify that toText() method reports Lease4 structure properly.
  725. TEST(Lease6, toText) {
  726. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  727. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  728. DuidPtr duid(new DUID(llt, sizeof(llt)));
  729. Lease6 lease(Lease::TYPE_NA, IOAddress("2001:db8::1"), duid, 123456,
  730. 400, 800, 100, 200, 5678, hwaddr, 128);
  731. lease.cltt_ = 12345678;
  732. std::stringstream expected;
  733. expected << "Type: IA_NA(" << static_cast<int>(Lease::TYPE_NA) << ")\n"
  734. << "Address: 2001:db8::1\n"
  735. << "Prefix length: 128\n"
  736. << "IAID: 123456\n"
  737. << "Pref life: 400\n"
  738. << "Valid life: 800\n"
  739. << "Cltt: 12345678\n"
  740. << "Hardware addr: " << hwaddr->toText(false) << "\n"
  741. << "Subnet ID: 5678\n";
  742. EXPECT_EQ(expected.str(), lease.toText());
  743. // Now let's try with a lease without hardware address.
  744. lease.hwaddr_.reset();
  745. expected.str("");
  746. expected << "Type: IA_NA(" << static_cast<int>(Lease::TYPE_NA) << ")\n"
  747. << "Address: 2001:db8::1\n"
  748. << "Prefix length: 128\n"
  749. << "IAID: 123456\n"
  750. << "Pref life: 400\n"
  751. << "Valid life: 800\n"
  752. << "Cltt: 12345678\n"
  753. << "Hardware addr: (none)\n"
  754. << "Subnet ID: 5678\n";
  755. EXPECT_EQ(expected.str(), lease.toText());
  756. }
  757. }; // end of anonymous namespace