lease_unittest.cc 35 KB

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