lease_mgr_unittest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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-id
  101. ///
  102. /// There can be at most one lease for a given HW address in a single
  103. /// pool, so this method with either return a single lease or NULL.
  104. ///
  105. /// @param clientid client identifier
  106. /// @param subnet_id identifier of the subnet that lease must belong to
  107. ///
  108. /// @return a pointer to the lease (or NULL if a lease is not found)
  109. virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
  110. return (Lease4Ptr());
  111. }
  112. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  113. ///
  114. /// @param addr address of the searched lease
  115. ///
  116. /// @return smart pointer to the lease (or NULL if a lease is not found)
  117. virtual Lease6Ptr getLease6(Lease::Type /* not used yet */,
  118. const isc::asiolink::IOAddress&) const {
  119. return (Lease6Ptr());
  120. }
  121. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  122. ///
  123. /// @param duid ignored
  124. /// @param iaid ignored
  125. ///
  126. /// @return whatever is set in leases6_ field
  127. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  128. const DUID&, uint32_t) const {
  129. return (leases6_);
  130. }
  131. /// @brief Returns existing IPv6 lease for a given DUID+IA+subnet-id combination
  132. ///
  133. /// @param duid ignored
  134. /// @param iaid ignored
  135. /// @param subnet_id ignored
  136. ///
  137. /// @return whatever is set in leases6_ field
  138. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  139. const DUID&, uint32_t, SubnetID) const {
  140. return (leases6_);
  141. }
  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 updateLease4(const Lease4Ptr&) {}
  148. /// @brief Updates IPv4 lease.
  149. ///
  150. /// @param lease4 The lease to be updated.
  151. ///
  152. /// If no such lease is present, an exception will be thrown.
  153. virtual void updateLease6(const Lease6Ptr&) {}
  154. /// @brief Deletes a lease.
  155. ///
  156. /// @param addr Address of the lease to be deleted. (This can be either
  157. /// a V4 address or a V6 address.)
  158. ///
  159. /// @return true if deletion was successful, false if no such lease exists
  160. virtual bool deleteLease(const isc::asiolink::IOAddress&) {
  161. return (false);
  162. }
  163. /// @brief Returns backend type.
  164. ///
  165. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  166. ///
  167. /// @return Type of the backend.
  168. virtual std::string getType() const {
  169. return (std::string("concrete"));
  170. }
  171. /// @brief Returns backend name.
  172. ///
  173. /// If the backend is a database, this is the name of the database or the
  174. /// file. Otherwise it is just the same as the type.
  175. ///
  176. /// @return Name of the backend.
  177. virtual std::string getName() const {
  178. return (std::string("concrete"));
  179. }
  180. /// @brief Returns description of the backend.
  181. ///
  182. /// This description may be multiline text that describes the backend.
  183. ///
  184. /// @return Description of the backend.
  185. virtual std::string getDescription() const {
  186. return (std::string("This is a dummy concrete backend implementation."));
  187. }
  188. /// @brief Returns backend version.
  189. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  190. return (make_pair(uint32_t(0), uint32_t(0)));
  191. }
  192. /// @brief Commit transactions
  193. virtual void commit() {
  194. }
  195. /// @brief Rollback transactions
  196. virtual void rollback() {
  197. }
  198. // We need to use it in ConcreteLeaseMgr
  199. using LeaseMgr::getLease6;
  200. Lease6Collection leases6_; ///< getLease6 methods return this as is
  201. };
  202. class LeaseMgrTest : public GenericLeaseMgrTest {
  203. public:
  204. LeaseMgrTest() {
  205. }
  206. };
  207. namespace {
  208. /// @brief getParameter test
  209. ///
  210. /// This test checks if the LeaseMgr can be instantiated and that it
  211. /// parses parameters string properly.
  212. TEST_F(LeaseMgrTest, getParameter) {
  213. LeaseMgr::ParameterMap pmap;
  214. pmap[std::string("param1")] = std::string("value1");
  215. pmap[std::string("param2")] = std::string("value2");
  216. ConcreteLeaseMgr leasemgr(pmap);
  217. EXPECT_EQ("value1", leasemgr.getParameter("param1"));
  218. EXPECT_EQ("value2", leasemgr.getParameter("param2"));
  219. EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
  220. }
  221. // This test checks if getLease6() method is working properly for 0 (NULL),
  222. // 1 (return the lease) and more than 1 leases (throw).
  223. TEST_F(LeaseMgrTest, getLease6) {
  224. LeaseMgr::ParameterMap pmap;
  225. boost::scoped_ptr<ConcreteLeaseMgr> mgr(new ConcreteLeaseMgr(pmap));
  226. vector<Lease6Ptr> leases = createLeases6();
  227. mgr->leases6_.clear();
  228. // For no leases, the function should return NULL pointer
  229. Lease6Ptr lease;
  230. // the getLease6() is calling getLeases6(), which is a dummy. It returns
  231. // whatever is there in leases6_ field.
  232. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  233. leases[1]->iaid_,
  234. leases[1]->subnet_id_));
  235. EXPECT_TRUE(Lease6Ptr() == lease);
  236. // For a single lease, the function should return that lease
  237. mgr->leases6_.push_back(leases[1]);
  238. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  239. leases[1]->iaid_,
  240. leases[1]->subnet_id_));
  241. EXPECT_TRUE(lease);
  242. EXPECT_NO_THROW(detailCompareLease(lease, leases[1]));
  243. // Add one more lease. There are 2 now. It should throw
  244. mgr->leases6_.push_back(leases[2]);
  245. EXPECT_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  246. leases[1]->iaid_,
  247. leases[1]->subnet_id_),
  248. MultipleRecords);
  249. }
  250. // There's no point in calling any other methods in LeaseMgr, as they
  251. // are purely virtual, so we would only call ConcreteLeaseMgr methods.
  252. // Those methods are just stubs that do not return anything.
  253. /// @brief Lease4 Constructor Test
  254. ///
  255. /// Lease4 is also defined in lease_mgr.h, so is tested in this file as well.
  256. // This test checks if the Lease4 structure can be instantiated correctly
  257. TEST(Lease4, constructor) {
  258. // Random values for the tests
  259. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  260. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  261. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  262. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  263. ClientId clientid(clientid_vec);
  264. // ...and a time
  265. const time_t current_time = time(NULL);
  266. // Other random constants.
  267. const uint32_t SUBNET_ID = 42;
  268. const uint32_t VALID_LIFETIME = 500;
  269. // We want to check that various addresses work, so let's iterate over
  270. // these.
  271. const uint32_t ADDRESS[] = {
  272. 0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
  273. };
  274. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  275. // Create the lease
  276. Lease4 lease(ADDRESS[i], HWADDR, sizeof(HWADDR),
  277. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0,
  278. current_time, SUBNET_ID, true, true,
  279. "hostname.example.com.");
  280. EXPECT_EQ(ADDRESS[i], static_cast<uint32_t>(lease.addr_));
  281. EXPECT_EQ(0, lease.ext_);
  282. EXPECT_TRUE(hwaddr == lease.hwaddr_);
  283. EXPECT_TRUE(clientid == *lease.client_id_);
  284. EXPECT_EQ(0, lease.t1_);
  285. EXPECT_EQ(0, lease.t2_);
  286. EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
  287. EXPECT_EQ(current_time, lease.cltt_);
  288. EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
  289. EXPECT_FALSE(lease.fixed_);
  290. EXPECT_EQ("hostname.example.com.", lease.hostname_);
  291. EXPECT_TRUE(lease.fqdn_fwd_);
  292. EXPECT_TRUE(lease.fqdn_rev_);
  293. EXPECT_TRUE(lease.comments_.empty());
  294. }
  295. }
  296. // This test verfies that copy constructor copies Lease4 fields correctly.
  297. TEST(Lease4, copyConstructor) {
  298. // Random values for the tests
  299. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  300. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  301. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  302. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  303. ClientId clientid(clientid_vec);
  304. // ...and a time
  305. const time_t current_time = time(NULL);
  306. // Other random constants.
  307. const uint32_t SUBNET_ID = 42;
  308. const uint32_t VALID_LIFETIME = 500;
  309. // Create the lease
  310. Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
  311. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  312. SUBNET_ID);
  313. // Use copy constructor to copy the lease.
  314. Lease4 copied_lease(lease);
  315. // Both leases should be now equal. When doing this check we assume that
  316. // the equality operator works correctly.
  317. EXPECT_TRUE(lease == copied_lease);
  318. // Client IDs are equal, but they should be in two distinct pointers.
  319. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  320. }
  321. // This test verfies that the assignment operator copies all Lease4 fields
  322. // correctly.
  323. TEST(Lease4, operatorAssign) {
  324. // Random values for the tests
  325. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  326. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  327. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  328. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  329. ClientId clientid(clientid_vec);
  330. // ...and a time
  331. const time_t current_time = time(NULL);
  332. // Other random constants.
  333. const uint32_t SUBNET_ID = 42;
  334. const uint32_t VALID_LIFETIME = 500;
  335. // Create the lease
  336. Lease4 lease(0xffffffff, HWADDR, sizeof(HWADDR),
  337. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, 0, 0, current_time,
  338. SUBNET_ID);
  339. // Use assignment operator to assign the lease.
  340. Lease4 copied_lease = lease;
  341. // Both leases should be now equal. When doing this check we assume that
  342. // the equality operator works correctly.
  343. EXPECT_TRUE(lease == copied_lease);
  344. // Client IDs are equal, but they should be in two distinct pointers.
  345. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  346. }
  347. /// @brief Lease4 Equality Test
  348. ///
  349. /// Checks that the operator==() correctly compares two leases for equality.
  350. /// As operator!=() is also defined for this class, every check on operator==()
  351. /// is followed by the reverse check on operator!=().
  352. TEST(Lease4, operatorEquals) {
  353. // Random values for the tests
  354. const uint32_t ADDRESS = 0x01020304;
  355. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  356. std::vector<uint8_t> hwaddr(HWADDR, HWADDR + sizeof(HWADDR));
  357. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  358. std::vector<uint8_t> clientid_vec(CLIENTID, CLIENTID + sizeof(CLIENTID));
  359. ClientId clientid(clientid_vec);
  360. const time_t current_time = time(NULL);
  361. const uint32_t SUBNET_ID = 42;
  362. const uint32_t VALID_LIFETIME = 500;
  363. // Check when the leases are equal.
  364. Lease4 lease1(ADDRESS, HWADDR, sizeof(HWADDR),
  365. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0,
  366. 0, SUBNET_ID);
  367. Lease4 lease2(ADDRESS, HWADDR, sizeof(HWADDR),
  368. CLIENTID, sizeof(CLIENTID), VALID_LIFETIME, current_time, 0, 0,
  369. SUBNET_ID);
  370. EXPECT_TRUE(lease1 == lease2);
  371. EXPECT_FALSE(lease1 != lease2);
  372. // Now vary individual fields in a lease and check that the leases compare
  373. // not equal in every case.
  374. lease1.addr_ = IOAddress(ADDRESS + 1);
  375. EXPECT_FALSE(lease1 == lease2);
  376. EXPECT_TRUE(lease1 != lease2);
  377. lease1.addr_ = lease2.addr_;
  378. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  379. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  380. ++lease1.ext_;
  381. EXPECT_FALSE(lease1 == lease2);
  382. EXPECT_TRUE(lease1 != lease2);
  383. lease1.ext_ = lease2.ext_;
  384. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  385. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  386. ++lease1.hwaddr_[0];
  387. EXPECT_FALSE(lease1 == lease2);
  388. EXPECT_TRUE(lease1 != lease2);
  389. lease1.hwaddr_ = lease2.hwaddr_;
  390. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  391. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  392. ++clientid_vec[0];
  393. lease1.client_id_.reset(new ClientId(clientid_vec));
  394. EXPECT_FALSE(lease1 == lease2);
  395. EXPECT_TRUE(lease1 != lease2);
  396. --clientid_vec[0];
  397. lease1.client_id_.reset(new ClientId(clientid_vec));
  398. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  399. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  400. ++lease1.t1_;
  401. EXPECT_FALSE(lease1 == lease2);
  402. EXPECT_TRUE(lease1 != lease2);
  403. lease1.t1_ = lease2.t1_;
  404. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  405. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  406. ++lease1.t2_;
  407. EXPECT_FALSE(lease1 == lease2);
  408. EXPECT_TRUE(lease1 != lease2);
  409. lease1.t2_ = lease2.t2_;
  410. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  411. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  412. ++lease1.valid_lft_;
  413. EXPECT_FALSE(lease1 == lease2);
  414. EXPECT_TRUE(lease1 != lease2);
  415. lease1.valid_lft_ = lease2.valid_lft_;
  416. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  417. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  418. ++lease1.cltt_;
  419. EXPECT_FALSE(lease1 == lease2);
  420. EXPECT_TRUE(lease1 != lease2);
  421. lease1.cltt_ = lease2.cltt_;
  422. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  423. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  424. ++lease1.subnet_id_;
  425. EXPECT_FALSE(lease1 == lease2);
  426. EXPECT_TRUE(lease1 != lease2);
  427. lease1.subnet_id_ = lease2.subnet_id_;
  428. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  429. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  430. lease1.fixed_ = !lease1.fixed_;
  431. EXPECT_FALSE(lease1 == lease2);
  432. EXPECT_TRUE(lease1 != lease2);
  433. lease1.fixed_ = lease2.fixed_;
  434. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  435. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  436. lease1.hostname_ += string("Something random");
  437. EXPECT_FALSE(lease1 == lease2);
  438. EXPECT_TRUE(lease1 != lease2);
  439. lease1.hostname_ = lease2.hostname_;
  440. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  441. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  442. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  443. EXPECT_FALSE(lease1 == lease2);
  444. EXPECT_TRUE(lease1 != lease2);
  445. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  446. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  447. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  448. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  449. EXPECT_FALSE(lease1 == lease2);
  450. EXPECT_TRUE(lease1 != lease2);
  451. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  452. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  453. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  454. lease1.comments_ += string("Something random");
  455. EXPECT_FALSE(lease1 == lease2);
  456. EXPECT_TRUE(lease1 != lease2);
  457. lease1.comments_ = lease2.comments_;
  458. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  459. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  460. }
  461. // Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
  462. // This test checks if the Lease6 structure can be instantiated correctly
  463. TEST(Lease6, Lease6ConstructorDefault) {
  464. // check a variety of addresses with different bits set.
  465. const char* ADDRESS[] = {
  466. "::", "::1", "2001:db8:1::456",
  467. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  468. "8000::", "8000::1",
  469. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  470. };
  471. // Other values
  472. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  473. DuidPtr duid(new DUID(llt, sizeof(llt)));
  474. uint32_t iaid = 7; // Just a number
  475. SubnetID subnet_id = 8; // Just another number
  476. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  477. IOAddress addr(ADDRESS[i]);
  478. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  479. duid, iaid, 100, 200, 50, 80,
  480. subnet_id));
  481. EXPECT_TRUE(lease->addr_ == addr);
  482. EXPECT_TRUE(*lease->duid_ == *duid);
  483. EXPECT_TRUE(lease->iaid_ == iaid);
  484. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  485. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  486. EXPECT_TRUE(lease->preferred_lft_ == 100);
  487. EXPECT_TRUE(lease->valid_lft_ == 200);
  488. EXPECT_TRUE(lease->t1_ == 50);
  489. EXPECT_TRUE(lease->t2_ == 80);
  490. EXPECT_FALSE(lease->fqdn_fwd_);
  491. EXPECT_FALSE(lease->fqdn_rev_);
  492. EXPECT_TRUE(lease->hostname_.empty());
  493. }
  494. // Lease6 must be instantiated with a DUID, not with NULL pointer
  495. IOAddress addr(ADDRESS[0]);
  496. Lease6Ptr lease2;
  497. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  498. DuidPtr(), iaid, 100, 200, 50, 80,
  499. subnet_id)), InvalidOperation);
  500. }
  501. // This test verifies that the Lease6 constructor which accepts FQDN data,
  502. // sets the data correctly for the lease.
  503. TEST(Lease6, Lease6ConstructorWithFQDN) {
  504. // check a variety of addresses with different bits set.
  505. const char* ADDRESS[] = {
  506. "::", "::1", "2001:db8:1::456",
  507. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  508. "8000::", "8000::1",
  509. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  510. };
  511. // Other values
  512. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  513. DuidPtr duid(new DUID(llt, sizeof(llt)));
  514. uint32_t iaid = 7; // Just a number
  515. SubnetID subnet_id = 8; // Just another number
  516. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  517. IOAddress addr(ADDRESS[i]);
  518. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  519. duid, iaid, 100, 200, 50, 80, subnet_id,
  520. true, true, "host.example.com."));
  521. EXPECT_TRUE(lease->addr_ == addr);
  522. EXPECT_TRUE(*lease->duid_ == *duid);
  523. EXPECT_TRUE(lease->iaid_ == iaid);
  524. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  525. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  526. EXPECT_TRUE(lease->preferred_lft_ == 100);
  527. EXPECT_TRUE(lease->valid_lft_ == 200);
  528. EXPECT_TRUE(lease->t1_ == 50);
  529. EXPECT_TRUE(lease->t2_ == 80);
  530. EXPECT_TRUE(lease->fqdn_fwd_);
  531. EXPECT_TRUE(lease->fqdn_rev_);
  532. EXPECT_EQ("host.example.com.", lease->hostname_);
  533. }
  534. // Lease6 must be instantiated with a DUID, not with NULL pointer
  535. IOAddress addr(ADDRESS[0]);
  536. Lease6Ptr lease2;
  537. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  538. DuidPtr(), iaid, 100, 200, 50, 80,
  539. subnet_id)), InvalidOperation);
  540. }
  541. /// @brief Lease6 Equality Test
  542. ///
  543. /// Checks that the operator==() correctly compares two leases for equality.
  544. /// As operator!=() is also defined for this class, every check on operator==()
  545. /// is followed by the reverse check on operator!=().
  546. TEST(Lease6, OperatorEquals) {
  547. // check a variety of addresses with different bits set.
  548. const IOAddress addr("2001:db8:1::456");
  549. uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  550. DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  551. uint32_t iaid = 7; // just a number
  552. SubnetID subnet_id = 8; // just another number
  553. // Check for equality.
  554. Lease6 lease1(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  555. subnet_id);
  556. Lease6 lease2(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  557. subnet_id);
  558. // cltt_ constructs with time(NULL), make sure they are always equal
  559. lease1.cltt_ = lease2.cltt_;
  560. EXPECT_TRUE(lease1 == lease2);
  561. EXPECT_FALSE(lease1 != lease2);
  562. // Go through and alter all the fields one by one
  563. lease1.addr_ = IOAddress("::1");
  564. EXPECT_FALSE(lease1 == lease2);
  565. EXPECT_TRUE(lease1 != lease2);
  566. lease1.addr_ = lease2.addr_;
  567. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  568. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  569. lease1.type_ = Lease::TYPE_PD;
  570. EXPECT_FALSE(lease1 == lease2);
  571. EXPECT_TRUE(lease1 != lease2);
  572. lease1.type_ = lease2.type_;
  573. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  574. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  575. ++lease1.prefixlen_;
  576. EXPECT_FALSE(lease1 == lease2);
  577. EXPECT_TRUE(lease1 != lease2);
  578. lease1.prefixlen_ = lease2.prefixlen_;
  579. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  580. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  581. ++lease1.iaid_;
  582. EXPECT_FALSE(lease1 == lease2);
  583. EXPECT_TRUE(lease1 != lease2);
  584. lease1.iaid_ = lease2.iaid_;
  585. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  586. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  587. ++duid_array[0];
  588. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  589. EXPECT_FALSE(lease1 == lease2);
  590. EXPECT_TRUE(lease1 != lease2);
  591. --duid_array[0];
  592. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  593. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  594. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  595. ++lease1.preferred_lft_;
  596. EXPECT_FALSE(lease1 == lease2);
  597. EXPECT_TRUE(lease1 != lease2);
  598. lease1.preferred_lft_ = lease2.preferred_lft_;
  599. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  600. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  601. ++lease1.valid_lft_;
  602. EXPECT_FALSE(lease1 == lease2);
  603. EXPECT_TRUE(lease1 != lease2);
  604. lease1.valid_lft_ = lease2.valid_lft_;
  605. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  606. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  607. ++lease1.t1_;
  608. EXPECT_FALSE(lease1 == lease2);
  609. EXPECT_TRUE(lease1 != lease2);
  610. lease1.t1_ = lease2.t1_;
  611. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  612. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  613. ++lease1.t2_;
  614. EXPECT_FALSE(lease1 == lease2);
  615. EXPECT_TRUE(lease1 != lease2);
  616. lease1.t2_ = lease2.t2_;
  617. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  618. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  619. ++lease1.cltt_;
  620. EXPECT_FALSE(lease1 == lease2);
  621. EXPECT_TRUE(lease1 != lease2);
  622. lease1.cltt_ = lease2.cltt_;
  623. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  624. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  625. ++lease1.subnet_id_;
  626. EXPECT_FALSE(lease1 == lease2);
  627. EXPECT_TRUE(lease1 != lease2);
  628. lease1.subnet_id_ = lease2.subnet_id_;
  629. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  630. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  631. lease1.fixed_ = !lease1.fixed_;
  632. EXPECT_FALSE(lease1 == lease2);
  633. EXPECT_TRUE(lease1 != lease2);
  634. lease1.fixed_ = lease2.fixed_;
  635. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  636. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  637. lease1.hostname_ += string("Something random");
  638. EXPECT_FALSE(lease1 == lease2);
  639. EXPECT_TRUE(lease1 != lease2);
  640. lease1.hostname_ = lease2.hostname_;
  641. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  642. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  643. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  644. EXPECT_FALSE(lease1 == lease2);
  645. EXPECT_TRUE(lease1 != lease2);
  646. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  647. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  648. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  649. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  650. EXPECT_FALSE(lease1 == lease2);
  651. EXPECT_TRUE(lease1 != lease2);
  652. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  653. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  654. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  655. lease1.comments_ += string("Something random");
  656. EXPECT_FALSE(lease1 == lease2);
  657. EXPECT_TRUE(lease1 != lease2);
  658. lease1.comments_ = lease2.comments_;
  659. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  660. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  661. }
  662. // Checks if lease expiration is calculated properly
  663. TEST(Lease6, Lease6Expired) {
  664. const IOAddress addr("2001:db8:1::456");
  665. const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  666. const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  667. const uint32_t iaid = 7; // Just a number
  668. const SubnetID subnet_id = 8; // Just another number
  669. Lease6 lease(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  670. subnet_id);
  671. // Case 1: a second before expiration
  672. lease.cltt_ = time(NULL) - 100;
  673. lease.valid_lft_ = 101;
  674. EXPECT_FALSE(lease.expired());
  675. // Case 2: the lease will expire after this second is concluded
  676. lease.cltt_ = time(NULL) - 101;
  677. EXPECT_FALSE(lease.expired());
  678. // Case 3: the lease is expired
  679. lease.cltt_ = time(NULL) - 102;
  680. EXPECT_TRUE(lease.expired());
  681. }
  682. }; // end of anonymous namespace