test_utils.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 "test_utils.h"
  15. #include <asiolink/io_address.h>
  16. #include <gtest/gtest.h>
  17. #include <sstream>
  18. using namespace std;
  19. using namespace isc::asiolink;
  20. namespace isc {
  21. namespace dhcp {
  22. namespace test {
  23. // IPv4 and IPv6 addresses used in the tests
  24. const char* ADDRESS4[] = {
  25. "192.0.2.0", "192.0.2.1", "192.0.2.2", "192.0.2.3",
  26. "192.0.2.4", "192.0.2.5", "192.0.2.6", "192.0.2.7",
  27. NULL
  28. };
  29. const char* ADDRESS6[] = {
  30. "2001:db8::0", "2001:db8::1", "2001:db8::2", "2001:db8::3",
  31. "2001:db8::4", "2001:db8::5", "2001:db8::6", "2001:db8::7",
  32. NULL
  33. };
  34. // Lease types that correspond to ADDRESS6 leases
  35. static const Lease::Type LEASETYPE6[] = {
  36. Lease::TYPE_NA, Lease::TYPE_TA, Lease::TYPE_PD, Lease::TYPE_NA,
  37. Lease::TYPE_TA, Lease::TYPE_PD, Lease::TYPE_NA, Lease::TYPE_TA
  38. };
  39. void
  40. detailCompareLease(const Lease4Ptr& first, const Lease4Ptr& second) {
  41. // Compare address strings. Comparison of address objects is not used, as
  42. // odd things happen when they are different: the EXPECT_EQ macro appears to
  43. // call the operator uint32_t() function, which causes an exception to be
  44. // thrown for IPv6 addresses.
  45. EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
  46. EXPECT_TRUE(first->hwaddr_ == second->hwaddr_);
  47. if (first->client_id_ && second->client_id_) {
  48. EXPECT_TRUE(*first->client_id_ == *second->client_id_);
  49. } else {
  50. if (first->client_id_ && !second->client_id_) {
  51. ADD_FAILURE() << "Client-id present in first lease ("
  52. << first->client_id_->getClientId().size()
  53. << " bytes), but missing in second.";
  54. }
  55. if (!first->client_id_ && second->client_id_) {
  56. ADD_FAILURE() << "Client-id missing in first lease, but present in second ("
  57. << second->client_id_->getClientId().size()
  58. << " bytes).";
  59. }
  60. // else here would mean that both leases do not have client_id_
  61. // which makes them equal in that regard. It is ok.
  62. }
  63. EXPECT_EQ(first->valid_lft_, second->valid_lft_);
  64. EXPECT_EQ(first->cltt_, second->cltt_);
  65. EXPECT_EQ(first->subnet_id_, second->subnet_id_);
  66. EXPECT_EQ(first->fqdn_fwd_, second->fqdn_fwd_);
  67. EXPECT_EQ(first->fqdn_rev_, second->fqdn_rev_);
  68. EXPECT_EQ(first->hostname_, second->hostname_);
  69. }
  70. void
  71. detailCompareLease(const Lease6Ptr& first, const Lease6Ptr& second) {
  72. EXPECT_EQ(first->type_, second->type_);
  73. // Compare address strings. Comparison of address objects is not used, as
  74. // odd things happen when they are different: the EXPECT_EQ macro appears to
  75. // call the operator uint32_t() function, which causes an exception to be
  76. // thrown for IPv6 addresses.
  77. EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
  78. EXPECT_EQ(first->prefixlen_, second->prefixlen_);
  79. EXPECT_EQ(first->iaid_, second->iaid_);
  80. ASSERT_TRUE(first->duid_);
  81. ASSERT_TRUE(second->duid_);
  82. EXPECT_TRUE(*first->duid_ == *second->duid_);
  83. EXPECT_EQ(first->preferred_lft_, second->preferred_lft_);
  84. EXPECT_EQ(first->valid_lft_, second->valid_lft_);
  85. EXPECT_EQ(first->cltt_, second->cltt_);
  86. EXPECT_EQ(first->subnet_id_, second->subnet_id_);
  87. EXPECT_EQ(first->fqdn_fwd_, second->fqdn_fwd_);
  88. EXPECT_EQ(first->fqdn_rev_, second->fqdn_rev_);
  89. EXPECT_EQ(first->hostname_, second->hostname_);
  90. }
  91. GenericLeaseMgrTest::GenericLeaseMgrTest()
  92. : lmptr_(NULL) {
  93. // Initialize address strings and IOAddresses
  94. for (int i = 0; ADDRESS4[i] != NULL; ++i) {
  95. string addr(ADDRESS4[i]);
  96. straddress4_.push_back(addr);
  97. IOAddress ioaddr(addr);
  98. ioaddress4_.push_back(ioaddr);
  99. }
  100. for (int i = 0; ADDRESS6[i] != NULL; ++i) {
  101. string addr(ADDRESS6[i]);
  102. straddress6_.push_back(addr);
  103. IOAddress ioaddr(addr);
  104. ioaddress6_.push_back(ioaddr);
  105. /// Let's create different lease types. We use LEASETYPE6 values as
  106. /// a template
  107. leasetype6_.push_back(LEASETYPE6[i]);
  108. }
  109. }
  110. GenericLeaseMgrTest::~GenericLeaseMgrTest() {
  111. // Does nothing. The derived classes are expected to clean up, i.e.
  112. // remove the lmptr_ pointer.
  113. }
  114. /// @brief Initialize Lease4 Fields
  115. ///
  116. /// Returns a pointer to a Lease4 structure. Different values are put into
  117. /// the lease according to the address passed.
  118. ///
  119. /// This is just a convenience function for the test methods.
  120. ///
  121. /// @param address Address to use for the initialization
  122. ///
  123. /// @return Lease4Ptr. This will not point to anything if the
  124. /// initialization failed (e.g. unknown address).
  125. Lease4Ptr
  126. GenericLeaseMgrTest::initializeLease4(std::string address) {
  127. Lease4Ptr lease(new Lease4());
  128. // Set the address of the lease
  129. lease->addr_ = IOAddress(address);
  130. // Initialize unused fields.
  131. lease->ext_ = 0; // Not saved
  132. lease->t1_ = 0; // Not saved
  133. lease->t2_ = 0; // Not saved
  134. lease->fixed_ = false; // Unused
  135. lease->comments_ = std::string(""); // Unused
  136. // Set other parameters. For historical reasons, address 0 is not used.
  137. if (address == straddress4_[0]) {
  138. lease->hwaddr_ = vector<uint8_t>(6, 0x08);
  139. lease->client_id_ = ClientIdPtr(new ClientId(vector<uint8_t>(8, 0x42)));
  140. lease->valid_lft_ = 8677;
  141. lease->cltt_ = 168256;
  142. lease->subnet_id_ = 23;
  143. lease->fqdn_rev_ = true;
  144. lease->fqdn_fwd_ = false;
  145. lease->hostname_ = "myhost.example.com.";
  146. } else if (address == straddress4_[1]) {
  147. lease->hwaddr_ = vector<uint8_t>(6, 0x19);
  148. lease->client_id_ = ClientIdPtr(
  149. new ClientId(vector<uint8_t>(8, 0x53)));
  150. lease->valid_lft_ = 3677;
  151. lease->cltt_ = 123456;
  152. lease->subnet_id_ = 73;
  153. lease->fqdn_rev_ = true;
  154. lease->fqdn_fwd_ = true;
  155. lease->hostname_ = "myhost.example.com.";
  156. } else if (address == straddress4_[2]) {
  157. lease->hwaddr_ = vector<uint8_t>(6, 0x2a);
  158. lease->client_id_ = ClientIdPtr(new ClientId(vector<uint8_t>(8, 0x64)));
  159. lease->valid_lft_ = 5412;
  160. lease->cltt_ = 234567;
  161. lease->subnet_id_ = 73; // Same as lease 1
  162. lease->fqdn_rev_ = false;
  163. lease->fqdn_fwd_ = false;
  164. lease->hostname_ = "";
  165. } else if (address == straddress4_[3]) {
  166. lease->hwaddr_ = vector<uint8_t>(6, 0x19); // Same as lease 1
  167. lease->client_id_ = ClientIdPtr(
  168. new ClientId(vector<uint8_t>(8, 0x75)));
  169. // The times used in the next tests are deliberately restricted - we
  170. // should be able to cope with valid lifetimes up to 0xffffffff.
  171. // However, this will lead to overflows.
  172. // @TODO: test overflow conditions when code has been fixed
  173. lease->valid_lft_ = 7000;
  174. lease->cltt_ = 234567;
  175. lease->subnet_id_ = 37;
  176. lease->fqdn_rev_ = true;
  177. lease->fqdn_fwd_ = true;
  178. lease->hostname_ = "otherhost.example.com.";
  179. } else if (address == straddress4_[4]) {
  180. lease->hwaddr_ = vector<uint8_t>(6, 0x4c);
  181. // Same ClientId as straddr4_[1]
  182. lease->client_id_ = ClientIdPtr(
  183. new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
  184. lease->valid_lft_ = 7736;
  185. lease->cltt_ = 222456;
  186. lease->subnet_id_ = 85;
  187. lease->fqdn_rev_ = true;
  188. lease->fqdn_fwd_ = true;
  189. lease->hostname_ = "otherhost.example.com.";
  190. } else if (address == straddress4_[5]) {
  191. lease->hwaddr_ = vector<uint8_t>(6, 0x19); // Same as lease 1
  192. // Same ClientId and IAID as straddress4_1
  193. lease->client_id_ = ClientIdPtr(
  194. new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
  195. lease->valid_lft_ = 7832;
  196. lease->cltt_ = 227476;
  197. lease->subnet_id_ = 175;
  198. lease->fqdn_rev_ = false;
  199. lease->fqdn_fwd_ = false;
  200. lease->hostname_ = "otherhost.example.com.";
  201. } else if (address == straddress4_[6]) {
  202. lease->hwaddr_ = vector<uint8_t>(6, 0x6e);
  203. // Same ClientId as straddress4_1
  204. lease->client_id_ = ClientIdPtr(
  205. new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
  206. lease->valid_lft_ = 1832;
  207. lease->cltt_ = 627476;
  208. lease->subnet_id_ = 112;
  209. lease->fqdn_rev_ = false;
  210. lease->fqdn_fwd_ = true;
  211. lease->hostname_ = "myhost.example.com.";
  212. } else if (address == straddress4_[7]) {
  213. lease->hwaddr_ = vector<uint8_t>(); // Empty
  214. lease->client_id_ = ClientIdPtr(); // Empty
  215. lease->valid_lft_ = 7975;
  216. lease->cltt_ = 213876;
  217. lease->subnet_id_ = 19;
  218. lease->fqdn_rev_ = true;
  219. lease->fqdn_fwd_ = true;
  220. lease->hostname_ = "myhost.example.com.";
  221. } else {
  222. // Unknown address, return an empty pointer.
  223. lease.reset();
  224. }
  225. return (lease);
  226. }
  227. /// @brief Initialize Lease6 Fields
  228. ///
  229. /// Returns a pointer to a Lease6 structure. Different values are put into
  230. /// the lease according to the address passed.
  231. ///
  232. /// This is just a convenience function for the test methods.
  233. ///
  234. /// @param address Address to use for the initialization
  235. ///
  236. /// @return Lease6Ptr. This will not point to anything if the initialization
  237. /// failed (e.g. unknown address).
  238. Lease6Ptr
  239. GenericLeaseMgrTest::initializeLease6(std::string address) {
  240. Lease6Ptr lease(new Lease6());
  241. // Set the address of the lease
  242. lease->addr_ = IOAddress(address);
  243. // Initialize unused fields.
  244. lease->t1_ = 0; // Not saved
  245. lease->t2_ = 0; // Not saved
  246. lease->fixed_ = false; // Unused
  247. lease->comments_ = std::string(""); // Unused
  248. // Set other parameters. For historical reasons, address 0 is not used.
  249. if (address == straddress6_[0]) {
  250. lease->type_ = leasetype6_[0];
  251. lease->prefixlen_ = 4;
  252. lease->iaid_ = 142;
  253. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x77)));
  254. lease->preferred_lft_ = 900;
  255. lease->valid_lft_ = 8677;
  256. lease->cltt_ = 168256;
  257. lease->subnet_id_ = 23;
  258. lease->fqdn_fwd_ = true;
  259. lease->fqdn_rev_ = true;
  260. lease->hostname_ = "myhost.example.com.";
  261. } else if (address == straddress6_[1]) {
  262. lease->type_ = leasetype6_[1];
  263. lease->prefixlen_ = 0;
  264. lease->iaid_ = 42;
  265. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
  266. lease->preferred_lft_ = 3600;
  267. lease->valid_lft_ = 3677;
  268. lease->cltt_ = 123456;
  269. lease->subnet_id_ = 73;
  270. lease->fqdn_fwd_ = false;
  271. lease->fqdn_rev_ = true;
  272. lease->hostname_ = "myhost.example.com.";
  273. } else if (address == straddress6_[2]) {
  274. lease->type_ = leasetype6_[2];
  275. lease->prefixlen_ = 7;
  276. lease->iaid_ = 89;
  277. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x3a)));
  278. lease->preferred_lft_ = 1800;
  279. lease->valid_lft_ = 5412;
  280. lease->cltt_ = 234567;
  281. lease->subnet_id_ = 73; // Same as lease 1
  282. lease->fqdn_fwd_ = false;
  283. lease->fqdn_rev_ = false;
  284. lease->hostname_ = "myhost.example.com.";
  285. } else if (address == straddress6_[3]) {
  286. lease->type_ = leasetype6_[3];
  287. lease->prefixlen_ = 28;
  288. lease->iaid_ = 0xfffffffe;
  289. vector<uint8_t> duid;
  290. for (uint8_t i = 31; i < 126; ++i) {
  291. duid.push_back(i);
  292. }
  293. lease->duid_ = DuidPtr(new DUID(duid));
  294. // The times used in the next tests are deliberately restricted - we
  295. // should be able to cope with valid lifetimes up to 0xffffffff.
  296. // However, this will lead to overflows.
  297. // @TODO: test overflow conditions when code has been fixed
  298. lease->preferred_lft_ = 7200;
  299. lease->valid_lft_ = 7000;
  300. lease->cltt_ = 234567;
  301. lease->subnet_id_ = 37;
  302. lease->fqdn_fwd_ = true;
  303. lease->fqdn_rev_ = false;
  304. lease->hostname_ = "myhost.example.com.";
  305. } else if (address == straddress6_[4]) {
  306. // Same DUID and IAID as straddress6_1
  307. lease->type_ = leasetype6_[4];
  308. lease->prefixlen_ = 15;
  309. lease->iaid_ = 42;
  310. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
  311. lease->preferred_lft_ = 4800;
  312. lease->valid_lft_ = 7736;
  313. lease->cltt_ = 222456;
  314. lease->subnet_id_ = 671;
  315. lease->fqdn_fwd_ = true;
  316. lease->fqdn_rev_ = true;
  317. lease->hostname_ = "otherhost.example.com.";
  318. } else if (address == straddress6_[5]) {
  319. // Same DUID and IAID as straddress6_1
  320. lease->type_ = leasetype6_[5];
  321. lease->prefixlen_ = 24;
  322. lease->iaid_ = 42; // Same as lease 4
  323. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
  324. // Same as lease 4
  325. lease->preferred_lft_ = 5400;
  326. lease->valid_lft_ = 7832;
  327. lease->cltt_ = 227476;
  328. lease->subnet_id_ = 175;
  329. lease->fqdn_fwd_ = false;
  330. lease->fqdn_rev_ = true;
  331. lease->hostname_ = "hostname.example.com.";
  332. } else if (address == straddress6_[6]) {
  333. // Same DUID as straddress6_1
  334. lease->type_ = leasetype6_[6];
  335. lease->prefixlen_ = 24;
  336. lease->iaid_ = 93;
  337. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
  338. // Same as lease 4
  339. lease->preferred_lft_ = 5400;
  340. lease->valid_lft_ = 1832;
  341. lease->cltt_ = 627476;
  342. lease->subnet_id_ = 112;
  343. lease->fqdn_fwd_ = false;
  344. lease->fqdn_rev_ = true;
  345. lease->hostname_ = "hostname.example.com.";
  346. } else if (address == straddress6_[7]) {
  347. // Same IAID as straddress6_1
  348. lease->type_ = leasetype6_[7];
  349. lease->prefixlen_ = 24;
  350. lease->iaid_ = 42;
  351. lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0xe5)));
  352. lease->preferred_lft_ = 5600;
  353. lease->valid_lft_ = 7975;
  354. lease->cltt_ = 213876;
  355. lease->subnet_id_ = 19;
  356. lease->fqdn_fwd_ = false;
  357. lease->fqdn_rev_ = true;
  358. lease->hostname_ = "hostname.example.com.";
  359. } else {
  360. // Unknown address, return an empty pointer.
  361. lease.reset();
  362. }
  363. return (lease);
  364. }
  365. /// @brief Check Leases present and different
  366. ///
  367. /// Checks a vector of lease pointers and ensures that all the leases
  368. /// they point to are present and different. If not, a GTest assertion
  369. /// will fail.
  370. ///
  371. /// @param leases Vector of pointers to leases
  372. template <typename T>
  373. void GenericLeaseMgrTest::checkLeasesDifferent(const std::vector<T>& leases) const {
  374. // Check they were created
  375. for (int i = 0; i < leases.size(); ++i) {
  376. ASSERT_TRUE(leases[i]);
  377. }
  378. // Check they are different
  379. for (int i = 0; i < (leases.size() - 1); ++i) {
  380. for (int j = (i + 1); j < leases.size(); ++j) {
  381. stringstream s;
  382. s << "Comparing leases " << i << " & " << j << " for equality";
  383. SCOPED_TRACE(s.str());
  384. EXPECT_TRUE(*leases[i] != *leases[j]);
  385. }
  386. }
  387. }
  388. /// @brief Creates leases for the test
  389. ///
  390. /// Creates all leases for the test and checks that they are different.
  391. ///
  392. /// @return vector<Lease4Ptr> Vector of pointers to leases
  393. vector<Lease4Ptr>
  394. GenericLeaseMgrTest::createLeases4() {
  395. // Create leases for each address
  396. vector<Lease4Ptr> leases;
  397. for (int i = 0; i < straddress4_.size(); ++i) {
  398. leases.push_back(initializeLease4(straddress4_[i]));
  399. }
  400. EXPECT_EQ(8, leases.size());
  401. // Check all were created and that they are different.
  402. checkLeasesDifferent(leases);
  403. return (leases);
  404. }
  405. /// @brief Creates leases for the test
  406. ///
  407. /// Creates all leases for the test and checks that they are different.
  408. ///
  409. /// @return vector<Lease6Ptr> Vector of pointers to leases
  410. vector<Lease6Ptr>
  411. GenericLeaseMgrTest::createLeases6() {
  412. // Create leases for each address
  413. vector<Lease6Ptr> leases;
  414. for (int i = 0; i < straddress6_.size(); ++i) {
  415. leases.push_back(initializeLease6(straddress6_[i]));
  416. }
  417. EXPECT_EQ(8, leases.size());
  418. // Check all were created and that they are different.
  419. checkLeasesDifferent(leases);
  420. return (leases);
  421. }
  422. void
  423. GenericLeaseMgrTest::testGetLease4ClientId() {
  424. // Let's initialize a specific lease ...
  425. Lease4Ptr lease = initializeLease4(straddress4_[1]);
  426. EXPECT_TRUE(lmptr_->addLease(lease));
  427. Lease4Collection returned = lmptr_->getLease4(*lease->client_id_);
  428. ASSERT_EQ(1, returned.size());
  429. // We should retrieve our lease...
  430. detailCompareLease(lease, *returned.begin());
  431. lease = initializeLease4(straddress4_[2]);
  432. returned = lmptr_->getLease4(*lease->client_id_);
  433. ASSERT_EQ(0, returned.size());
  434. }
  435. void
  436. GenericLeaseMgrTest::testGetLease4NullClientId() {
  437. // Let's initialize a specific lease ... But this time
  438. // We keep its client id for further lookup and
  439. // We clearly 'reset' it ...
  440. Lease4Ptr leaseA = initializeLease4(straddress4_[4]);
  441. ClientIdPtr client_id = leaseA->client_id_;
  442. leaseA->client_id_ = ClientIdPtr();
  443. ASSERT_TRUE(lmptr_->addLease(leaseA));
  444. Lease4Collection returned = lmptr_->getLease4(*client_id);
  445. // Shouldn't have our previous lease ...
  446. ASSERT_TRUE(returned.empty());
  447. // Add another lease with the non-NULL client id, and make sure that the
  448. // lookup will not break due to existence of both leases with non-NULL and
  449. // NULL client ids.
  450. Lease4Ptr leaseB = initializeLease4(straddress4_[0]);
  451. // Shouldn't throw any null pointer exception
  452. ASSERT_TRUE(lmptr_->addLease(leaseB));
  453. // Try to get the lease.
  454. returned = lmptr_->getLease4(*client_id);
  455. ASSERT_TRUE(returned.empty());
  456. // Let's make it more interesting and add another lease with NULL client id.
  457. Lease4Ptr leaseC = initializeLease4(straddress4_[5]);
  458. leaseC->client_id_.reset();
  459. ASSERT_TRUE(lmptr_->addLease(leaseC));
  460. returned = lmptr_->getLease4(*client_id);
  461. ASSERT_TRUE(returned.empty());
  462. // But getting the lease with non-NULL client id should be successful.
  463. returned = lmptr_->getLease4(*leaseB->client_id_);
  464. ASSERT_EQ(1, returned.size());
  465. }
  466. void
  467. GenericLeaseMgrTest::testGetLease4HWAddr() {
  468. const LeaseMgr::ParameterMap pmap;
  469. // Let's initialize two different leases 4 and just add the first ...
  470. Lease4Ptr leaseA = initializeLease4(straddress4_[5]);
  471. HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER);
  472. HWAddr hwaddrB(vector<uint8_t>(6, 0x80), HTYPE_ETHER);
  473. EXPECT_TRUE(lmptr_->addLease(leaseA));
  474. // we should not have a lease, with this MAC Addr
  475. Lease4Collection returned = lmptr_->getLease4(hwaddrB);
  476. ASSERT_EQ(0, returned.size());
  477. // But with this one
  478. returned = lmptr_->getLease4(hwaddrA);
  479. ASSERT_EQ(1, returned.size());
  480. }
  481. void
  482. GenericLeaseMgrTest::testLease4ClientIdHWAddrSubnetId() {
  483. Lease4Ptr leaseA = initializeLease4(straddress4_[4]);
  484. Lease4Ptr leaseB = initializeLease4(straddress4_[5]);
  485. Lease4Ptr leaseC = initializeLease4(straddress4_[6]);
  486. // Set NULL client id for one of the leases. This is to make sure that such
  487. // a lease may coexist with other leases with non NULL client id.
  488. leaseC->client_id_.reset();
  489. HWAddr hwaddrA(leaseA->hwaddr_, HTYPE_ETHER);
  490. HWAddr hwaddrB(leaseB->hwaddr_, HTYPE_ETHER);
  491. HWAddr hwaddrC(leaseC->hwaddr_, HTYPE_ETHER);
  492. EXPECT_TRUE(lmptr_->addLease(leaseA));
  493. EXPECT_TRUE(lmptr_->addLease(leaseB));
  494. EXPECT_TRUE(lmptr_->addLease(leaseC));
  495. // First case we should retrieve our lease
  496. Lease4Ptr lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrA, leaseA->subnet_id_);
  497. detailCompareLease(lease, leaseA);
  498. // Retrieve the other lease.
  499. lease = lmptr_->getLease4(*leaseB->client_id_, hwaddrB, leaseB->subnet_id_);
  500. detailCompareLease(lease, leaseB);
  501. // The last lease has NULL client id so we will use a different getLease4 function
  502. // which doesn't require client id (just a hwaddr and subnet id).
  503. lease = lmptr_->getLease4(hwaddrC, leaseC->subnet_id_);
  504. detailCompareLease(lease, leaseC);
  505. // An attempt to retrieve the lease with non matching lease parameters should
  506. // result in NULL pointer being returned.
  507. lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrB, leaseA->subnet_id_);
  508. EXPECT_FALSE(lease);
  509. lease = lmptr_->getLease4(*leaseA->client_id_, hwaddrA, leaseB->subnet_id_);
  510. EXPECT_FALSE(lease);
  511. }
  512. };
  513. };
  514. };