lease_unittest.cc 33 KB

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