lease_mgr_unittest.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // Copyright (C) 2012 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 <dhcpsrv/lease_mgr.h>
  17. #include <gtest/gtest.h>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <time.h>
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::asiolink;
  24. using namespace isc::dhcp;
  25. // This is a concrete implementation of a Lease database. It does not do
  26. // anything useful and is used for abstract LeaseMgr class testing.
  27. class ConcreteLeaseMgr : public LeaseMgr {
  28. public:
  29. /// @brief The sole lease manager constructor
  30. ///
  31. /// dbconfig is a generic way of passing parameters. Parameters
  32. /// are passed in the "name=value" format, separated by spaces.
  33. /// Values may be enclosed in double quotes, if needed.
  34. ///
  35. /// @param parameters A data structure relating keywords and values
  36. /// concerned with the database.
  37. ConcreteLeaseMgr(const LeaseMgr::ParameterMap& parameters)
  38. : LeaseMgr(parameters)
  39. {}
  40. /// @brief Destructor
  41. virtual ~ConcreteLeaseMgr()
  42. {}
  43. /// @brief Adds an IPv4 lease.
  44. ///
  45. /// @param lease lease to be added
  46. virtual bool addLease(const Lease4Ptr&) {
  47. return (false);
  48. }
  49. /// @brief Adds an IPv6 lease.
  50. ///
  51. /// @param lease lease to be added
  52. virtual bool addLease(const Lease6Ptr&) {
  53. return (false);
  54. }
  55. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  56. ///
  57. /// @param addr address of the searched lease
  58. ///
  59. /// @return smart pointer to the lease (or NULL if a lease is not found)
  60. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress&) const {
  61. return (Lease4Ptr());
  62. }
  63. /// @brief Returns existing IPv4 leases for specified hardware address.
  64. ///
  65. /// Although in the usual case there will be only one lease, for mobile
  66. /// clients or clients with multiple static/fixed/reserved leases there
  67. /// can be more than one. Thus return type is a container, not a single
  68. /// pointer.
  69. ///
  70. /// @param hwaddr hardware address of the client
  71. ///
  72. /// @return lease collection
  73. virtual Lease4Collection getLease4(const HWAddr&) const {
  74. return (Lease4Collection());
  75. }
  76. /// @brief Returns existing IPv4 leases for specified hardware address
  77. /// and a subnet
  78. ///
  79. /// There can be at most one lease for a given HW address in a single
  80. /// pool, so this method with either return a single lease or NULL.
  81. ///
  82. /// @param hwaddr hardware address of the client
  83. /// @param subnet_id identifier of the subnet that lease must belong to
  84. ///
  85. /// @return a pointer to the lease (or NULL if a lease is not found)
  86. virtual Lease4Ptr getLease4(const HWAddr&, SubnetID) const {
  87. return (Lease4Ptr());
  88. }
  89. /// @brief Returns existing IPv4 lease for specified client-id
  90. ///
  91. /// @param clientid client identifier
  92. ///
  93. /// @return lease collection
  94. virtual Lease4Collection getLease4(const ClientId&) const {
  95. return (Lease4Collection());
  96. }
  97. /// @brief Returns existing IPv4 lease for specified client-id
  98. ///
  99. /// There can be at most one lease for a given HW address in a single
  100. /// pool, so this method with either return a single lease or NULL.
  101. ///
  102. /// @param clientid client identifier
  103. /// @param subnet_id identifier of the subnet that lease must belong to
  104. ///
  105. /// @return a pointer to the lease (or NULL if a lease is not found)
  106. virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
  107. return (Lease4Ptr());
  108. }
  109. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  110. ///
  111. /// @param addr address of the searched lease
  112. ///
  113. /// @return smart pointer to the lease (or NULL if a lease is not found)
  114. virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress&) const {
  115. return (Lease6Ptr());
  116. }
  117. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  118. ///
  119. /// @param duid client DUID
  120. /// @param iaid IA identifier
  121. ///
  122. /// @return collection of IPv6 leases
  123. virtual Lease6Collection getLease6(const DUID&, uint32_t) const {
  124. return (Lease6Collection());
  125. }
  126. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  127. ///
  128. /// @param duid client DUID
  129. /// @param iaid IA identifier
  130. /// @param subnet_id identifier of the subnet the lease must belong to
  131. ///
  132. /// @return smart pointer to the lease (or NULL if a lease is not found)
  133. virtual Lease6Ptr getLease6(const DUID&, uint32_t, SubnetID) const {
  134. return (Lease6Ptr());
  135. }
  136. /// @brief Updates IPv4 lease.
  137. ///
  138. /// @param lease4 The lease to be updated.
  139. ///
  140. /// If no such lease is present, an exception will be thrown.
  141. virtual void updateLease4(const Lease4Ptr&) {}
  142. /// @brief Updates IPv4 lease.
  143. ///
  144. /// @param lease4 The lease to be updated.
  145. ///
  146. /// If no such lease is present, an exception will be thrown.
  147. virtual void updateLease6(const Lease6Ptr&) {}
  148. /// @brief Deletes a lease.
  149. ///
  150. /// @param addr Address of the lease to be deleted. (This can be either
  151. /// a V4 address or a V6 address.)
  152. ///
  153. /// @return true if deletion was successful, false if no such lease exists
  154. virtual bool deleteLease(const isc::asiolink::IOAddress&) {
  155. return (false);
  156. }
  157. /// @brief Returns backend type.
  158. ///
  159. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  160. ///
  161. /// @return Type of the backend.
  162. virtual std::string getType() const {
  163. return (std::string("concrete"));
  164. }
  165. /// @brief Returns backend name.
  166. ///
  167. /// If the backend is a database, this is the name of the database or the
  168. /// file. Otherwise it is just the same as the type.
  169. ///
  170. /// @return Name of the backend.
  171. virtual std::string getName() const {
  172. return (std::string("concrete"));
  173. }
  174. /// @brief Returns description of the backend.
  175. ///
  176. /// This description may be multiline text that describes the backend.
  177. ///
  178. /// @return Description of the backend.
  179. virtual std::string getDescription() const {
  180. return (std::string("This is a dummy concrete backend implementation."));
  181. }
  182. /// @brief Returns backend version.
  183. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  184. return (make_pair(uint32_t(0), uint32_t(0)));
  185. }
  186. /// @brief Commit transactions
  187. virtual void commit() {
  188. }
  189. /// @brief Rollback transactions
  190. virtual void rollback() {
  191. }
  192. };
  193. namespace {
  194. /// @brief getParameter test
  195. ///
  196. /// This test checks if the LeaseMgr can be instantiated and that it
  197. /// parses parameters string properly.
  198. TEST(LeaseMgr, getParameter) {
  199. LeaseMgr::ParameterMap pmap;
  200. pmap[std::string("param1")] = std::string("value1");
  201. pmap[std::string("param2")] = std::string("value2");
  202. ConcreteLeaseMgr leasemgr(pmap);
  203. EXPECT_EQ("value1", leasemgr.getParameter("param1"));
  204. EXPECT_EQ("value2", leasemgr.getParameter("param2"));
  205. EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
  206. }
  207. // There's no point in calling any other methods in LeaseMgr, as they
  208. // are purely virtual, so we would only call ConcreteLeaseMgr methods.
  209. // Those methods are just stubs that do not return anything.
  210. /// @brief Lease4 Constructor Test
  211. ///
  212. /// Lease4 is also defined in lease_mgr.h, so is tested in this file as well.
  213. // This test checks if the Lease4 structure can be instantiated correctly
  214. TEST(Lease4, Lease4Constructor) {
  215. // Random values for the tests
  216. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  217. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  218. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  219. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  220. ClientId clientid(clientid_vec);
  221. // ...and a time
  222. const time_t current_time = time(NULL);
  223. // Other random constants.
  224. const uint32_t SUBNET_ID = 42;
  225. const uint32_t VALID_LIFETIME = 500;
  226. // We want to check that various addresses work, so let's iterate over
  227. // these.
  228. const uint32_t ADDRESS[] = {
  229. 0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
  230. };
  231. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  232. // Create the lease
  233. Lease4 lease(ADDRESS[i], HWADDR, sizeof(HWADDR),
  234. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time,
  235. SUBNET_ID);
  236. EXPECT_EQ(ADDRESS[i], static_cast<uint32_t>(lease.addr_));
  237. EXPECT_EQ(0, lease.ext_);
  238. EXPECT_TRUE(hwaddr == lease.hwaddr_);
  239. EXPECT_TRUE(clientid == *lease.client_id_);
  240. EXPECT_EQ(0, lease.t1_);
  241. EXPECT_EQ(0, lease.t2_);
  242. EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
  243. EXPECT_EQ(current_time, lease.cltt_);
  244. EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
  245. EXPECT_FALSE(lease.fixed_);
  246. EXPECT_TRUE(lease.hostname_.empty());
  247. EXPECT_FALSE(lease.fqdn_fwd_);
  248. EXPECT_FALSE(lease.fqdn_rev_);
  249. EXPECT_TRUE(lease.comments_.empty());
  250. }
  251. }
  252. /// @brief Lease4 Equality Test
  253. ///
  254. /// Checks that the operator==() correctly compares two leases for equality.
  255. /// As operator!=() is also defined for this class, every check on operator==()
  256. /// is followed by the reverse check on operator!=().
  257. TEST(Lease4, OperatorEquals) {
  258. // Random values for the tests
  259. const uint32_t ADDRESS = 0x01020304;
  260. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  261. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  262. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  263. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  264. ClientId clientid(clientid_vec);
  265. const time_t current_time = time(NULL);
  266. const uint32_t SUBNET_ID = 42;
  267. const uint32_t VALID_LIFETIME = 500;
  268. // Check when the leases are equal.
  269. Lease4 lease1(ADDRESS, HWADDR, sizeof(HWADDR),
  270. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time,
  271. SUBNET_ID);
  272. Lease4 lease2(ADDRESS, HWADDR, sizeof(HWADDR),
  273. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time,
  274. SUBNET_ID);
  275. EXPECT_TRUE(lease1 == lease2);
  276. EXPECT_FALSE(lease1 != lease2);
  277. // Now vary individual fields in a lease and check that the leases compare
  278. // not equal in every case.
  279. lease1.addr_ = IOAddress(ADDRESS + 1);
  280. EXPECT_FALSE(lease1 == lease2);
  281. EXPECT_TRUE(lease1 != lease2);
  282. lease1.addr_ = lease2.addr_;
  283. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  284. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  285. ++lease1.ext_;
  286. EXPECT_FALSE(lease1 == lease2);
  287. EXPECT_TRUE(lease1 != lease2);
  288. lease1.ext_ = lease2.ext_;
  289. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  290. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  291. ++lease1.hwaddr_[0];
  292. EXPECT_FALSE(lease1 == lease2);
  293. EXPECT_TRUE(lease1 != lease2);
  294. lease1.hwaddr_ = lease2.hwaddr_;
  295. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  296. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  297. ++clientid_vec[0];
  298. lease1.client_id_.reset(new ClientId(clientid_vec));
  299. EXPECT_FALSE(lease1 == lease2);
  300. EXPECT_TRUE(lease1 != lease2);
  301. --clientid_vec[0];
  302. lease1.client_id_.reset(new ClientId(clientid_vec));
  303. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  304. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  305. ++lease1.t1_;
  306. EXPECT_FALSE(lease1 == lease2);
  307. EXPECT_TRUE(lease1 != lease2);
  308. lease1.t1_ = lease2.t1_;
  309. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  310. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  311. ++lease1.t2_;
  312. EXPECT_FALSE(lease1 == lease2);
  313. EXPECT_TRUE(lease1 != lease2);
  314. lease1.t2_ = lease2.t2_;
  315. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  316. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  317. ++lease1.valid_lft_;
  318. EXPECT_FALSE(lease1 == lease2);
  319. EXPECT_TRUE(lease1 != lease2);
  320. lease1.valid_lft_ = lease2.valid_lft_;
  321. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  322. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  323. ++lease1.cltt_;
  324. EXPECT_FALSE(lease1 == lease2);
  325. EXPECT_TRUE(lease1 != lease2);
  326. lease1.cltt_ = lease2.cltt_;
  327. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  328. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  329. ++lease1.subnet_id_;
  330. EXPECT_FALSE(lease1 == lease2);
  331. EXPECT_TRUE(lease1 != lease2);
  332. lease1.subnet_id_ = lease2.subnet_id_;
  333. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  334. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  335. lease1.fixed_ = !lease1.fixed_;
  336. EXPECT_FALSE(lease1 == lease2);
  337. EXPECT_TRUE(lease1 != lease2);
  338. lease1.fixed_ = lease2.fixed_;
  339. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  340. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  341. lease1.hostname_ += string("Something random");
  342. EXPECT_FALSE(lease1 == lease2);
  343. EXPECT_TRUE(lease1 != lease2);
  344. lease1.hostname_ = lease2.hostname_;
  345. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  346. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  347. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  348. EXPECT_FALSE(lease1 == lease2);
  349. EXPECT_TRUE(lease1 != lease2);
  350. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  351. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  352. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  353. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  354. EXPECT_FALSE(lease1 == lease2);
  355. EXPECT_TRUE(lease1 != lease2);
  356. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  357. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  358. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  359. lease1.comments_ += string("Something random");
  360. EXPECT_FALSE(lease1 == lease2);
  361. EXPECT_TRUE(lease1 != lease2);
  362. lease1.comments_ = lease2.comments_;
  363. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  364. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  365. }
  366. // Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
  367. // This test checks if the Lease6 structure can be instantiated correctly
  368. TEST(Lease6, Lease6Constructor) {
  369. // check a variety of addresses with different bits set.
  370. const char* ADDRESS[] = {
  371. "::", "::1", "2001:db8:1::456",
  372. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  373. "8000::", "8000::1",
  374. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  375. };
  376. // Other values
  377. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  378. DuidPtr duid(new DUID(llt, sizeof(llt)));
  379. uint32_t iaid = 7; // just a number
  380. SubnetID subnet_id = 8; // just another number
  381. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  382. IOAddress addr(ADDRESS[i]);
  383. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr,
  384. duid, iaid, 100, 200, 50, 80,
  385. subnet_id));
  386. EXPECT_TRUE(lease->addr_ == addr);
  387. EXPECT_TRUE(*lease->duid_ == *duid);
  388. EXPECT_TRUE(lease->iaid_ == iaid);
  389. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  390. EXPECT_TRUE(lease->type_ == Lease6::LEASE_IA_NA);
  391. EXPECT_TRUE(lease->preferred_lft_ == 100);
  392. EXPECT_TRUE(lease->valid_lft_ == 200);
  393. EXPECT_TRUE(lease->t1_ == 50);
  394. EXPECT_TRUE(lease->t2_ == 80);
  395. }
  396. // Lease6 must be instantiated with a DUID, not with NULL pointer
  397. IOAddress addr(ADDRESS[0]);
  398. EXPECT_THROW(new Lease6(Lease6::LEASE_IA_NA, addr,
  399. DuidPtr(), iaid, 100, 200, 50, 80,
  400. subnet_id), InvalidOperation);
  401. }
  402. /// @brief Lease6 Equality Test
  403. ///
  404. /// Checks that the operator==() correctly compares two leases for equality.
  405. /// As operator!=() is also defined for this class, every check on operator==()
  406. /// is followed by the reverse check on operator!=().
  407. TEST(Lease6, OperatorEquals) {
  408. // check a variety of addressemas with different bits set.
  409. const IOAddress addr("2001:db8:1::456");
  410. uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  411. DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  412. uint32_t iaid = 7; // just a number
  413. SubnetID subnet_id = 8; // just another number
  414. // Check for equality.
  415. Lease6 lease1(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
  416. subnet_id);
  417. Lease6 lease2(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
  418. subnet_id);
  419. EXPECT_TRUE(lease1 == lease2);
  420. EXPECT_FALSE(lease1 != lease2);
  421. // Go through and alter all the fields one by one
  422. lease1.addr_ = IOAddress("::1");
  423. EXPECT_FALSE(lease1 == lease2);
  424. EXPECT_TRUE(lease1 != lease2);
  425. lease1.addr_ = lease2.addr_;
  426. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  427. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  428. lease1.type_ = Lease6::LEASE_IA_PD;
  429. EXPECT_FALSE(lease1 == lease2);
  430. EXPECT_TRUE(lease1 != lease2);
  431. lease1.type_ = lease2.type_;
  432. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  433. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  434. ++lease1.prefixlen_;
  435. EXPECT_FALSE(lease1 == lease2);
  436. EXPECT_TRUE(lease1 != lease2);
  437. lease1.prefixlen_ = lease2.prefixlen_;
  438. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  439. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  440. ++lease1.iaid_;
  441. EXPECT_FALSE(lease1 == lease2);
  442. EXPECT_TRUE(lease1 != lease2);
  443. lease1.iaid_ = lease2.iaid_;
  444. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  445. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  446. ++duid_array[0];
  447. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  448. EXPECT_FALSE(lease1 == lease2);
  449. EXPECT_TRUE(lease1 != lease2);
  450. --duid_array[0];
  451. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  452. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  453. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  454. ++lease1.preferred_lft_;
  455. EXPECT_FALSE(lease1 == lease2);
  456. EXPECT_TRUE(lease1 != lease2);
  457. lease1.preferred_lft_ = lease2.preferred_lft_;
  458. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  459. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  460. ++lease1.valid_lft_;
  461. EXPECT_FALSE(lease1 == lease2);
  462. EXPECT_TRUE(lease1 != lease2);
  463. lease1.valid_lft_ = lease2.valid_lft_;
  464. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  465. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  466. ++lease1.t1_;
  467. EXPECT_FALSE(lease1 == lease2);
  468. EXPECT_TRUE(lease1 != lease2);
  469. lease1.t1_ = lease2.t1_;
  470. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  471. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  472. ++lease1.t2_;
  473. EXPECT_FALSE(lease1 == lease2);
  474. EXPECT_TRUE(lease1 != lease2);
  475. lease1.t2_ = lease2.t2_;
  476. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  477. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  478. ++lease1.cltt_;
  479. EXPECT_FALSE(lease1 == lease2);
  480. EXPECT_TRUE(lease1 != lease2);
  481. lease1.cltt_ = lease2.cltt_;
  482. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  483. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  484. ++lease1.subnet_id_;
  485. EXPECT_FALSE(lease1 == lease2);
  486. EXPECT_TRUE(lease1 != lease2);
  487. lease1.subnet_id_ = lease2.subnet_id_;
  488. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  489. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  490. lease1.fixed_ = !lease1.fixed_;
  491. EXPECT_FALSE(lease1 == lease2);
  492. EXPECT_TRUE(lease1 != lease2);
  493. lease1.fixed_ = lease2.fixed_;
  494. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  495. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  496. lease1.hostname_ += string("Something random");
  497. EXPECT_FALSE(lease1 == lease2);
  498. EXPECT_TRUE(lease1 != lease2);
  499. lease1.hostname_ = lease2.hostname_;
  500. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  501. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  502. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  503. EXPECT_FALSE(lease1 == lease2);
  504. EXPECT_TRUE(lease1 != lease2);
  505. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  506. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  507. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  508. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  509. EXPECT_FALSE(lease1 == lease2);
  510. EXPECT_TRUE(lease1 != lease2);
  511. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  512. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  513. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  514. lease1.comments_ += string("Something random");
  515. EXPECT_FALSE(lease1 == lease2);
  516. EXPECT_TRUE(lease1 != lease2);
  517. lease1.comments_ = lease2.comments_;
  518. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  519. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  520. }
  521. // Checks if lease expiration is calculated properly
  522. TEST(Lease6, Lease6Expired) {
  523. const IOAddress addr("2001:db8:1::456");
  524. const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  525. const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  526. const uint32_t iaid = 7; // just a number
  527. const SubnetID subnet_id = 8; // just another number
  528. Lease6 lease(Lease6::LEASE_IA_NA, addr, duid, iaid, 100, 200, 50, 80,
  529. subnet_id);
  530. // case 1: a second before expiration
  531. lease.cltt_ = time(NULL) - 100;
  532. lease.valid_lft_ = 101;
  533. EXPECT_FALSE(lease.expired());
  534. // case 2: the lease will expire after this second is concluded
  535. lease.cltt_ = time(NULL) - 101;
  536. EXPECT_FALSE(lease.expired());
  537. // case 3: the lease is expired
  538. lease.cltt_ = time(NULL) - 102;
  539. EXPECT_TRUE(lease.expired());
  540. }
  541. }; // end of anonymous namespace