lease_unittest.cc 32 KB

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