lease_mgr_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. // Copyright (C) 2012-2013 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 <dhcpsrv/memfile_lease_mgr.h>
  18. #include <dhcpsrv/tests/test_utils.h>
  19. #include <gtest/gtest.h>
  20. #include <iostream>
  21. #include <sstream>
  22. #include <time.h>
  23. using namespace std;
  24. using namespace isc;
  25. using namespace isc::asiolink;
  26. using namespace isc::dhcp;
  27. using namespace isc::dhcp::test;
  28. // This is a concrete implementation of a Lease database. It does not do
  29. // anything useful and is used for abstract LeaseMgr class testing.
  30. class ConcreteLeaseMgr : public LeaseMgr {
  31. public:
  32. /// @brief The sole lease manager constructor
  33. ///
  34. /// dbconfig is a generic way of passing parameters. Parameters
  35. /// are passed in the "name=value" format, separated by spaces.
  36. /// Values may be enclosed in double quotes, if needed.
  37. ///
  38. /// @param parameters A data structure relating keywords and values
  39. /// concerned with the database.
  40. ConcreteLeaseMgr(const LeaseMgr::ParameterMap& parameters)
  41. : LeaseMgr(parameters)
  42. {}
  43. /// @brief Destructor
  44. virtual ~ConcreteLeaseMgr()
  45. {}
  46. /// @brief Adds an IPv4 lease.
  47. ///
  48. /// @param lease lease to be added
  49. virtual bool addLease(const Lease4Ptr&) {
  50. return (false);
  51. }
  52. /// @brief Adds an IPv6 lease.
  53. ///
  54. /// @param lease lease to be added
  55. virtual bool addLease(const Lease6Ptr&) {
  56. return (false);
  57. }
  58. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  59. ///
  60. /// @param addr address of the searched lease
  61. ///
  62. /// @return smart pointer to the lease (or NULL if a lease is not found)
  63. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress&) const {
  64. return (Lease4Ptr());
  65. }
  66. /// @brief Returns existing IPv4 leases for specified hardware address.
  67. ///
  68. /// Although in the usual case there will be only one lease, for mobile
  69. /// clients or clients with multiple static/fixed/reserved leases there
  70. /// can be more than one. Thus return type is a container, not a single
  71. /// pointer.
  72. ///
  73. /// @param hwaddr hardware address of the client
  74. ///
  75. /// @return lease collection
  76. virtual Lease4Collection getLease4(const HWAddr&) const {
  77. return (Lease4Collection());
  78. }
  79. /// @brief Returns existing IPv4 leases for specified hardware address
  80. /// and a subnet
  81. ///
  82. /// There can be at most one lease for a given HW address in a single
  83. /// pool, so this method with either return a single lease or NULL.
  84. ///
  85. /// @param hwaddr hardware address of the client
  86. /// @param subnet_id identifier of the subnet that lease must belong to
  87. ///
  88. /// @return a pointer to the lease (or NULL if a lease is not found)
  89. virtual Lease4Ptr getLease4(const HWAddr&, SubnetID) const {
  90. return (Lease4Ptr());
  91. }
  92. /// @brief Returns existing IPv4 lease for specified client-id
  93. ///
  94. /// @param clientid client identifier
  95. ///
  96. /// @return lease collection
  97. virtual Lease4Collection getLease4(const ClientId&) const {
  98. return (Lease4Collection());
  99. }
  100. /// @brief Returns existing IPv4 lease for specified client identifier,
  101. /// HW address and subnet identifier.
  102. ///
  103. /// @param client_id Aclient identifier
  104. /// @param hwaddr A HW address.
  105. /// @param subnet_id A subnet identifier.
  106. ///
  107. /// @return A pointer to an existing lease or NULL if lease not found.
  108. virtual Lease4Ptr
  109. getLease4(const ClientId&, const HWAddr&, SubnetID) const {
  110. return (Lease4Ptr());
  111. }
  112. /// @brief Returns existing IPv4 lease for specified client-id
  113. ///
  114. /// There can be at most one lease for a given HW address in a single
  115. /// pool, so this method with either return a single lease or NULL.
  116. ///
  117. /// @param clientid client identifier
  118. /// @param subnet_id identifier of the subnet that lease must belong to
  119. ///
  120. /// @return a pointer to the lease (or NULL if a lease is not found)
  121. virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
  122. return (Lease4Ptr());
  123. }
  124. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  125. ///
  126. /// @param addr address of the searched lease
  127. ///
  128. /// @return smart pointer to the lease (or NULL if a lease is not found)
  129. virtual Lease6Ptr getLease6(Lease::Type /* not used yet */,
  130. const isc::asiolink::IOAddress&) const {
  131. return (Lease6Ptr());
  132. }
  133. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  134. ///
  135. /// @param duid ignored
  136. /// @param iaid ignored
  137. ///
  138. /// @return whatever is set in leases6_ field
  139. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  140. const DUID&, uint32_t) const {
  141. return (leases6_);
  142. }
  143. /// @brief Returns existing IPv6 lease for a given DUID+IA+subnet-id combination
  144. ///
  145. /// @param duid ignored
  146. /// @param iaid ignored
  147. /// @param subnet_id ignored
  148. ///
  149. /// @return whatever is set in leases6_ field
  150. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  151. const DUID&, uint32_t, SubnetID) const {
  152. return (leases6_);
  153. }
  154. /// @brief Updates IPv4 lease.
  155. ///
  156. /// @param lease4 The lease to be updated.
  157. ///
  158. /// If no such lease is present, an exception will be thrown.
  159. virtual void updateLease4(const Lease4Ptr&) {}
  160. /// @brief Updates IPv4 lease.
  161. ///
  162. /// @param lease4 The lease to be updated.
  163. ///
  164. /// If no such lease is present, an exception will be thrown.
  165. virtual void updateLease6(const Lease6Ptr&) {}
  166. /// @brief Deletes a lease.
  167. ///
  168. /// @param addr Address of the lease to be deleted. (This can be either
  169. /// a V4 address or a V6 address.)
  170. ///
  171. /// @return true if deletion was successful, false if no such lease exists
  172. virtual bool deleteLease(const isc::asiolink::IOAddress&) {
  173. return (false);
  174. }
  175. /// @brief Returns backend type.
  176. ///
  177. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  178. ///
  179. /// @return Type of the backend.
  180. virtual std::string getType() const {
  181. return (std::string("concrete"));
  182. }
  183. /// @brief Returns backend name.
  184. ///
  185. /// If the backend is a database, this is the name of the database or the
  186. /// file. Otherwise it is just the same as the type.
  187. ///
  188. /// @return Name of the backend.
  189. virtual std::string getName() const {
  190. return (std::string("concrete"));
  191. }
  192. /// @brief Returns description of the backend.
  193. ///
  194. /// This description may be multiline text that describes the backend.
  195. ///
  196. /// @return Description of the backend.
  197. virtual std::string getDescription() const {
  198. return (std::string("This is a dummy concrete backend implementation."));
  199. }
  200. /// @brief Returns backend version.
  201. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  202. return (make_pair(uint32_t(0), uint32_t(0)));
  203. }
  204. /// @brief Commit transactions
  205. virtual void commit() {
  206. }
  207. /// @brief Rollback transactions
  208. virtual void rollback() {
  209. }
  210. // We need to use it in ConcreteLeaseMgr
  211. using LeaseMgr::getLease6;
  212. Lease6Collection leases6_; ///< getLease6 methods return this as is
  213. };
  214. class LeaseMgrTest : public GenericLeaseMgrTest {
  215. public:
  216. LeaseMgrTest() {
  217. }
  218. };
  219. namespace {
  220. /// Hardware address used by different tests.
  221. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  222. /// Client id used by different tests.
  223. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  224. /// Valid lifetime value used by different tests.
  225. const uint32_t VALID_LIFETIME = 500;
  226. /// Subnet ID used by different tests.
  227. const uint32_t SUBNET_ID = 42;
  228. /// IAID value used by different tests.
  229. const uint32_t IAID = 7;
  230. /// @brief getParameter test
  231. ///
  232. /// This test checks if the LeaseMgr can be instantiated and that it
  233. /// parses parameters string properly.
  234. TEST_F(LeaseMgrTest, getParameter) {
  235. LeaseMgr::ParameterMap pmap;
  236. pmap[std::string("param1")] = std::string("value1");
  237. pmap[std::string("param2")] = std::string("value2");
  238. ConcreteLeaseMgr leasemgr(pmap);
  239. EXPECT_EQ("value1", leasemgr.getParameter("param1"));
  240. EXPECT_EQ("value2", leasemgr.getParameter("param2"));
  241. EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
  242. }
  243. // This test checks if getLease6() method is working properly for 0 (NULL),
  244. // 1 (return the lease) and more than 1 leases (throw).
  245. TEST_F(LeaseMgrTest, getLease6) {
  246. LeaseMgr::ParameterMap pmap;
  247. boost::scoped_ptr<ConcreteLeaseMgr> mgr(new ConcreteLeaseMgr(pmap));
  248. vector<Lease6Ptr> leases = createLeases6();
  249. mgr->leases6_.clear();
  250. // For no leases, the function should return NULL pointer
  251. Lease6Ptr lease;
  252. // the getLease6() is calling getLeases6(), which is a dummy. It returns
  253. // whatever is there in leases6_ field.
  254. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  255. leases[1]->iaid_,
  256. leases[1]->subnet_id_));
  257. EXPECT_TRUE(Lease6Ptr() == lease);
  258. // For a single lease, the function should return that lease
  259. mgr->leases6_.push_back(leases[1]);
  260. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  261. leases[1]->iaid_,
  262. leases[1]->subnet_id_));
  263. EXPECT_TRUE(lease);
  264. EXPECT_NO_THROW(detailCompareLease(lease, leases[1]));
  265. // Add one more lease. There are 2 now. It should throw
  266. mgr->leases6_.push_back(leases[2]);
  267. EXPECT_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  268. leases[1]->iaid_,
  269. leases[1]->subnet_id_),
  270. MultipleRecords);
  271. }
  272. // There's no point in calling any other methods in LeaseMgr, as they
  273. // are purely virtual, so we would only call ConcreteLeaseMgr methods.
  274. // Those methods are just stubs that do not return anything.
  275. /// @brief Lease4 Constructor Test
  276. ///
  277. /// Lease4 is also defined in lease_mgr.h, so is tested in this file as well.
  278. // This test checks if the Lease4 structure can be instantiated correctly
  279. TEST(Lease4, constructor) {
  280. // Random values for the tests
  281. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  282. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  283. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  284. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  285. ClientId clientid(clientid_vec);
  286. // ...and a time
  287. const time_t current_time = time(NULL);
  288. // Other random constants.
  289. const uint32_t SUBNET_ID = 42;
  290. const uint32_t VALID_LIFETIME = 500;
  291. // We want to check that various addresses work, so let's iterate over
  292. // these.
  293. const uint32_t ADDRESS[] = {
  294. 0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
  295. };
  296. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  297. // Create the lease
  298. Lease4 lease(ADDRESS[i], HWADDR, sizeof(HWADDR),
  299. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0,
  300. current_time, SUBNET_ID, true, true,
  301. "hostname.example.com.");
  302. EXPECT_EQ(ADDRESS[i], static_cast<uint32_t>(lease.addr_));
  303. EXPECT_EQ(0, lease.ext_);
  304. EXPECT_TRUE(hwaddr == lease.hwaddr_);
  305. EXPECT_TRUE(clientid == *lease.client_id_);
  306. EXPECT_EQ(0, lease.t1_);
  307. EXPECT_EQ(0, lease.t2_);
  308. EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
  309. EXPECT_EQ(current_time, lease.cltt_);
  310. EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
  311. EXPECT_FALSE(lease.fixed_);
  312. EXPECT_EQ("hostname.example.com.", lease.hostname_);
  313. EXPECT_TRUE(lease.fqdn_fwd_);
  314. EXPECT_TRUE(lease.fqdn_rev_);
  315. EXPECT_TRUE(lease.comments_.empty());
  316. }
  317. }
  318. // This test verfies that copy constructor copies Lease4 fields correctly.
  319. TEST(Lease4, copyConstructor) {
  320. // Random values for the tests
  321. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  322. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  323. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  324. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  325. ClientId clientid(clientid_vec);
  326. // ...and a time
  327. const time_t current_time = time(NULL);
  328. // Other random constants.
  329. const uint32_t SUBNET_ID = 42;
  330. const uint32_t VALID_LIFETIME = 500;
  331. // Create the lease
  332. Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
  333. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  334. SUBNET_ID);
  335. // Use copy constructor to copy the lease.
  336. Lease4 copied_lease(lease);
  337. // Both leases should be now equal. When doing this check we assume that
  338. // the equality operator works correctly.
  339. EXPECT_TRUE(lease == copied_lease);
  340. // Client IDs are equal, but they should be in two distinct pointers.
  341. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  342. }
  343. // This test verfies that the assignment operator copies all Lease4 fields
  344. // correctly.
  345. TEST(Lease4, operatorAssign) {
  346. // Random values for the tests
  347. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  348. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  349. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  350. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  351. ClientId clientid(clientid_vec);
  352. // ...and a time
  353. const time_t current_time = time(NULL);
  354. // Other random constants.
  355. const uint32_t SUBNET_ID = 42;
  356. const uint32_t VALID_LIFETIME = 500;
  357. // Create the lease
  358. Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
  359. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  360. SUBNET_ID);
  361. // Use assignment operator to assign the lease.
  362. Lease4 copied_lease = lease;
  363. // Both leases should be now equal. When doing this check we assume that
  364. // the equality operator works correctly.
  365. EXPECT_TRUE(lease == copied_lease);
  366. // Client IDs are equal, but they should be in two distinct pointers.
  367. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  368. }
  369. // This test verifies that the matches() returns true if two leases differ
  370. // by values other than address, HW address, Client ID and ext_.
  371. TEST(Lease4, matches) {
  372. // Create two leases which share the same address, HW address, client id
  373. // and ext_ value.
  374. const time_t current_time = time(NULL);
  375. Lease4 lease1(IOAddress("192.0.2.3"), HWADDR, sizeof(HWADDR), CLIENTID,
  376. sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
  377. SUBNET_ID);
  378. lease1.hostname_ = "lease1.example.com.";
  379. lease1.fqdn_fwd_ = true;
  380. lease1.fqdn_rev_ = true;
  381. Lease4 lease2(IOAddress("192.0.2.3"), HWADDR, sizeof(HWADDR), CLIENTID,
  382. sizeof(CLIENTID), VALID_LIFETIME + 10, current_time - 10,
  383. 100, 200, SUBNET_ID);
  384. lease2.hostname_ = "lease2.example.com.";
  385. lease2.fqdn_fwd_ = false;
  386. lease2.fqdn_rev_ = true;
  387. // Leases should match.
  388. EXPECT_TRUE(lease1.matches(lease2));
  389. EXPECT_TRUE(lease2.matches(lease1));
  390. // Change address, leases should not match anymore.
  391. lease1.addr_ = IOAddress("192.0.2.4");
  392. EXPECT_FALSE(lease1.matches(lease2));
  393. lease1.addr_ = lease2.addr_;
  394. // Change HW address, leases should not match.
  395. lease1.hwaddr_[1] += 1;
  396. EXPECT_FALSE(lease1.matches(lease2));
  397. lease1.hwaddr_ = lease2.hwaddr_;
  398. // Chanage client id, leases should not match.
  399. std::vector<uint8_t> client_id = lease1.client_id_->getClientId();
  400. client_id[1] += 1;
  401. lease1.client_id_.reset(new ClientId(client_id));
  402. EXPECT_FALSE(lease1.matches(lease2));
  403. lease1.client_id_ = lease2.client_id_;
  404. // Change ext_, leases should not match.
  405. lease1.ext_ += 1;
  406. EXPECT_FALSE(lease1.matches(lease2));
  407. lease1.ext_ = lease2.ext_;
  408. }
  409. /// @brief Lease4 Equality Test
  410. ///
  411. /// Checks that the operator==() correctly compares two leases for equality.
  412. /// As operator!=() is also defined for this class, every check on operator==()
  413. /// is followed by the reverse check on operator!=().
  414. TEST(Lease4, operatorEquals) {
  415. // Random values for the tests
  416. const uint32_t ADDRESS = 0x01020304;
  417. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  418. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  419. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  420. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  421. ClientId clientid(clientid_vec);
  422. const time_t current_time = time(NULL);
  423. const uint32_t SUBNET_ID = 42;
  424. const uint32_t VALID_LIFETIME = 500;
  425. // Check when the leases are equal.
  426. Lease4 lease1(ADDRESS, HWADDR, sizeof(HWADDR),
  427. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0,
  428. 0, SUBNET_ID);
  429. Lease4 lease2(ADDRESS, HWADDR, sizeof(HWADDR),
  430. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
  431. SUBNET_ID);
  432. EXPECT_TRUE(lease1 == lease2);
  433. EXPECT_FALSE(lease1 != lease2);
  434. // Now vary individual fields in a lease and check that the leases compare
  435. // not equal in every case.
  436. lease1.addr_ = IOAddress(ADDRESS + 1);
  437. EXPECT_FALSE(lease1 == lease2);
  438. EXPECT_TRUE(lease1 != lease2);
  439. lease1.addr_ = lease2.addr_;
  440. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  441. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  442. ++lease1.ext_;
  443. EXPECT_FALSE(lease1 == lease2);
  444. EXPECT_TRUE(lease1 != lease2);
  445. lease1.ext_ = lease2.ext_;
  446. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  447. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  448. ++lease1.hwaddr_[0];
  449. EXPECT_FALSE(lease1 == lease2);
  450. EXPECT_TRUE(lease1 != lease2);
  451. lease1.hwaddr_ = lease2.hwaddr_;
  452. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  453. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  454. ++clientid_vec[0];
  455. lease1.client_id_.reset(new ClientId(clientid_vec));
  456. EXPECT_FALSE(lease1 == lease2);
  457. EXPECT_TRUE(lease1 != lease2);
  458. --clientid_vec[0];
  459. lease1.client_id_.reset(new ClientId(clientid_vec));
  460. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  461. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  462. ++lease1.t1_;
  463. EXPECT_FALSE(lease1 == lease2);
  464. EXPECT_TRUE(lease1 != lease2);
  465. lease1.t1_ = lease2.t1_;
  466. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  467. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  468. ++lease1.t2_;
  469. EXPECT_FALSE(lease1 == lease2);
  470. EXPECT_TRUE(lease1 != lease2);
  471. lease1.t2_ = lease2.t2_;
  472. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  473. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  474. ++lease1.valid_lft_;
  475. EXPECT_FALSE(lease1 == lease2);
  476. EXPECT_TRUE(lease1 != lease2);
  477. lease1.valid_lft_ = lease2.valid_lft_;
  478. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  479. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  480. ++lease1.cltt_;
  481. EXPECT_FALSE(lease1 == lease2);
  482. EXPECT_TRUE(lease1 != lease2);
  483. lease1.cltt_ = lease2.cltt_;
  484. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  485. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  486. ++lease1.subnet_id_;
  487. EXPECT_FALSE(lease1 == lease2);
  488. EXPECT_TRUE(lease1 != lease2);
  489. lease1.subnet_id_ = lease2.subnet_id_;
  490. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  491. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  492. lease1.fixed_ = !lease1.fixed_;
  493. EXPECT_FALSE(lease1 == lease2);
  494. EXPECT_TRUE(lease1 != lease2);
  495. lease1.fixed_ = lease2.fixed_;
  496. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  497. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  498. lease1.hostname_ += string("Something random");
  499. EXPECT_FALSE(lease1 == lease2);
  500. EXPECT_TRUE(lease1 != lease2);
  501. lease1.hostname_ = lease2.hostname_;
  502. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  503. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  504. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  505. EXPECT_FALSE(lease1 == lease2);
  506. EXPECT_TRUE(lease1 != lease2);
  507. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  508. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  509. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  510. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  511. EXPECT_FALSE(lease1 == lease2);
  512. EXPECT_TRUE(lease1 != lease2);
  513. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  514. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  515. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  516. lease1.comments_ += string("Something random");
  517. EXPECT_FALSE(lease1 == lease2);
  518. EXPECT_TRUE(lease1 != lease2);
  519. lease1.comments_ = lease2.comments_;
  520. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  521. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  522. }
  523. // Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
  524. // This test checks if the Lease6 structure can be instantiated correctly
  525. TEST(Lease6, Lease6ConstructorDefault) {
  526. // check a variety of addresses with different bits set.
  527. const char* ADDRESS[] = {
  528. "::", "::1", "2001:db8:1::456",
  529. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  530. "8000::", "8000::1",
  531. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  532. };
  533. // Other values
  534. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  535. DuidPtr duid(new DUID(llt, sizeof(llt)));
  536. uint32_t iaid = 7; // Just a number
  537. SubnetID subnet_id = 8; // Just another number
  538. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  539. IOAddress addr(ADDRESS[i]);
  540. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  541. duid, iaid, 100, 200, 50, 80,
  542. subnet_id));
  543. EXPECT_TRUE(lease->addr_ == addr);
  544. EXPECT_TRUE(*lease->duid_ == *duid);
  545. EXPECT_TRUE(lease->iaid_ == iaid);
  546. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  547. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  548. EXPECT_TRUE(lease->preferred_lft_ == 100);
  549. EXPECT_TRUE(lease->valid_lft_ == 200);
  550. EXPECT_TRUE(lease->t1_ == 50);
  551. EXPECT_TRUE(lease->t2_ == 80);
  552. EXPECT_FALSE(lease->fqdn_fwd_);
  553. EXPECT_FALSE(lease->fqdn_rev_);
  554. EXPECT_TRUE(lease->hostname_.empty());
  555. }
  556. // Lease6 must be instantiated with a DUID, not with NULL pointer
  557. IOAddress addr(ADDRESS[0]);
  558. Lease6Ptr lease2;
  559. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  560. DuidPtr(), iaid, 100, 200, 50, 80,
  561. subnet_id)), InvalidOperation);
  562. }
  563. // This test verifies that the Lease6 constructor which accepts FQDN data,
  564. // sets the data correctly for the lease.
  565. TEST(Lease6, Lease6ConstructorWithFQDN) {
  566. // check a variety of addresses with different bits set.
  567. const char* ADDRESS[] = {
  568. "::", "::1", "2001:db8:1::456",
  569. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  570. "8000::", "8000::1",
  571. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  572. };
  573. // Other values
  574. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  575. DuidPtr duid(new DUID(llt, sizeof(llt)));
  576. uint32_t iaid = 7; // Just a number
  577. SubnetID subnet_id = 8; // Just another number
  578. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  579. IOAddress addr(ADDRESS[i]);
  580. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  581. duid, iaid, 100, 200, 50, 80, subnet_id,
  582. true, true, "host.example.com."));
  583. EXPECT_TRUE(lease->addr_ == addr);
  584. EXPECT_TRUE(*lease->duid_ == *duid);
  585. EXPECT_TRUE(lease->iaid_ == iaid);
  586. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  587. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  588. EXPECT_TRUE(lease->preferred_lft_ == 100);
  589. EXPECT_TRUE(lease->valid_lft_ == 200);
  590. EXPECT_TRUE(lease->t1_ == 50);
  591. EXPECT_TRUE(lease->t2_ == 80);
  592. EXPECT_TRUE(lease->fqdn_fwd_);
  593. EXPECT_TRUE(lease->fqdn_rev_);
  594. EXPECT_EQ("host.example.com.", lease->hostname_);
  595. }
  596. // Lease6 must be instantiated with a DUID, not with NULL pointer
  597. IOAddress addr(ADDRESS[0]);
  598. Lease6Ptr lease2;
  599. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  600. DuidPtr(), iaid, 100, 200, 50, 80,
  601. subnet_id)), InvalidOperation);
  602. }
  603. // This test verifies that the matches() function returns true if two leases
  604. // differ by values other than address, type, prefix length, IAID and DUID.
  605. TEST(Lease6, matches) {
  606. // Create two matching leases.
  607. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  608. DuidPtr duid(new DUID(llt, sizeof(llt)));
  609. Lease6 lease1(Lease6::TYPE_NA, IOAddress("2001:db8:1::1"), duid,
  610. IAID, 100, 200, 50, 80,
  611. SUBNET_ID);
  612. lease1.hostname_ = "lease1.example.com.";
  613. lease1.fqdn_fwd_ = true;
  614. lease1.fqdn_rev_ = true;
  615. Lease6 lease2(Lease6::TYPE_NA, IOAddress("2001:db8:1::1"), duid,
  616. IAID, 200, 300, 90, 70,
  617. SUBNET_ID);
  618. lease2.hostname_ = "lease1.example.com.";
  619. lease2.fqdn_fwd_ = false;
  620. lease2.fqdn_rev_ = true;
  621. EXPECT_TRUE(lease1.matches(lease2));
  622. // Modify each value used to match both leases, and make sure that
  623. // leases don't match.
  624. // Modify address.
  625. lease1.addr_ = IOAddress("2001:db8:1::2");
  626. EXPECT_FALSE(lease1.matches(lease2));
  627. lease1.addr_ = lease2.addr_;
  628. // Modify lease type.
  629. lease1.type_ = Lease6::TYPE_TA;
  630. EXPECT_FALSE(lease1.matches(lease2));
  631. lease1.type_ = lease2.type_;
  632. // Modify prefix length.
  633. lease1.prefixlen_ += 1;
  634. EXPECT_FALSE(lease1.matches(lease2));
  635. lease1.prefixlen_ = lease2.prefixlen_;
  636. // Modify IAID.
  637. lease1.iaid_ += 1;
  638. EXPECT_FALSE(lease1.matches(lease2));
  639. lease1.iaid_ = lease2.iaid_;
  640. // Modify DUID.
  641. llt[1] += 1;
  642. duid.reset(new DUID(llt, sizeof(llt)));
  643. lease1.duid_ = duid;
  644. EXPECT_FALSE(lease1.matches(lease2));
  645. lease1.duid_ = lease2.duid_;
  646. }
  647. /// @brief Lease6 Equality Test
  648. ///
  649. /// Checks that the operator==() correctly compares two leases for equality.
  650. /// As operator!=() is also defined for this class, every check on operator==()
  651. /// is followed by the reverse check on operator!=().
  652. TEST(Lease6, OperatorEquals) {
  653. // check a variety of addresses with different bits set.
  654. const IOAddress addr("2001:db8:1::456");
  655. uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  656. DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  657. uint32_t iaid = 7; // just a number
  658. SubnetID subnet_id = 8; // just another number
  659. // Check for equality.
  660. Lease6 lease1(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  661. subnet_id);
  662. Lease6 lease2(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  663. subnet_id);
  664. // cltt_ constructs with time(NULL), make sure they are always equal
  665. lease1.cltt_ = lease2.cltt_;
  666. EXPECT_TRUE(lease1 == lease2);
  667. EXPECT_FALSE(lease1 != lease2);
  668. // Go through and alter all the fields one by one
  669. lease1.addr_ = IOAddress("::1");
  670. EXPECT_FALSE(lease1 == lease2);
  671. EXPECT_TRUE(lease1 != lease2);
  672. lease1.addr_ = lease2.addr_;
  673. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  674. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  675. lease1.type_ = Lease::TYPE_PD;
  676. EXPECT_FALSE(lease1 == lease2);
  677. EXPECT_TRUE(lease1 != lease2);
  678. lease1.type_ = lease2.type_;
  679. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  680. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  681. ++lease1.prefixlen_;
  682. EXPECT_FALSE(lease1 == lease2);
  683. EXPECT_TRUE(lease1 != lease2);
  684. lease1.prefixlen_ = lease2.prefixlen_;
  685. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  686. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  687. ++lease1.iaid_;
  688. EXPECT_FALSE(lease1 == lease2);
  689. EXPECT_TRUE(lease1 != lease2);
  690. lease1.iaid_ = lease2.iaid_;
  691. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  692. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  693. ++duid_array[0];
  694. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  695. EXPECT_FALSE(lease1 == lease2);
  696. EXPECT_TRUE(lease1 != lease2);
  697. --duid_array[0];
  698. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  699. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  700. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  701. ++lease1.preferred_lft_;
  702. EXPECT_FALSE(lease1 == lease2);
  703. EXPECT_TRUE(lease1 != lease2);
  704. lease1.preferred_lft_ = lease2.preferred_lft_;
  705. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  706. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  707. ++lease1.valid_lft_;
  708. EXPECT_FALSE(lease1 == lease2);
  709. EXPECT_TRUE(lease1 != lease2);
  710. lease1.valid_lft_ = lease2.valid_lft_;
  711. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  712. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  713. ++lease1.t1_;
  714. EXPECT_FALSE(lease1 == lease2);
  715. EXPECT_TRUE(lease1 != lease2);
  716. lease1.t1_ = lease2.t1_;
  717. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  718. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  719. ++lease1.t2_;
  720. EXPECT_FALSE(lease1 == lease2);
  721. EXPECT_TRUE(lease1 != lease2);
  722. lease1.t2_ = lease2.t2_;
  723. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  724. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  725. ++lease1.cltt_;
  726. EXPECT_FALSE(lease1 == lease2);
  727. EXPECT_TRUE(lease1 != lease2);
  728. lease1.cltt_ = lease2.cltt_;
  729. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  730. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  731. ++lease1.subnet_id_;
  732. EXPECT_FALSE(lease1 == lease2);
  733. EXPECT_TRUE(lease1 != lease2);
  734. lease1.subnet_id_ = lease2.subnet_id_;
  735. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  736. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  737. lease1.fixed_ = !lease1.fixed_;
  738. EXPECT_FALSE(lease1 == lease2);
  739. EXPECT_TRUE(lease1 != lease2);
  740. lease1.fixed_ = lease2.fixed_;
  741. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  742. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  743. lease1.hostname_ += string("Something random");
  744. EXPECT_FALSE(lease1 == lease2);
  745. EXPECT_TRUE(lease1 != lease2);
  746. lease1.hostname_ = lease2.hostname_;
  747. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  748. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  749. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  750. EXPECT_FALSE(lease1 == lease2);
  751. EXPECT_TRUE(lease1 != lease2);
  752. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  753. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  754. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  755. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  756. EXPECT_FALSE(lease1 == lease2);
  757. EXPECT_TRUE(lease1 != lease2);
  758. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  759. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  760. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  761. lease1.comments_ += string("Something random");
  762. EXPECT_FALSE(lease1 == lease2);
  763. EXPECT_TRUE(lease1 != lease2);
  764. lease1.comments_ = lease2.comments_;
  765. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  766. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  767. }
  768. // Checks if lease expiration is calculated properly
  769. TEST(Lease6, Lease6Expired) {
  770. const IOAddress addr("2001:db8:1::456");
  771. const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  772. const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  773. const uint32_t iaid = 7; // Just a number
  774. const SubnetID subnet_id = 8; // Just another number
  775. Lease6 lease(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  776. subnet_id);
  777. // Case 1: a second before expiration
  778. lease.cltt_ = time(NULL) - 100;
  779. lease.valid_lft_ = 101;
  780. EXPECT_FALSE(lease.expired());
  781. // Case 2: the lease will expire after this second is concluded
  782. lease.cltt_ = time(NULL) - 101;
  783. EXPECT_FALSE(lease.expired());
  784. // Case 3: the lease is expired
  785. lease.cltt_ = time(NULL) - 102;
  786. EXPECT_TRUE(lease.expired());
  787. }
  788. }; // end of anonymous namespace