lease_unittest.cc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. // Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_address.h>
  8. #include <dhcp/duid.h>
  9. #include <dhcpsrv/lease.h>
  10. #include <util/pointer_util.h>
  11. #include <testutils/test_to_element.h>
  12. #include <cc/data.h>
  13. #include <gtest/gtest.h>
  14. #include <vector>
  15. #include <sstream>
  16. using namespace isc;
  17. using namespace isc::asiolink;
  18. using namespace isc::dhcp;
  19. using namespace isc::data;
  20. using namespace isc::test;
  21. namespace {
  22. /// Hardware address used by different tests.
  23. const uint8_t HWADDR[] = {0x08, 0x00, 0x2b, 0x02, 0x3f, 0x4e};
  24. /// Client id used by different tests.
  25. const uint8_t CLIENTID[] = {0x17, 0x34, 0xe2, 0xff, 0x09, 0x92, 0x54};
  26. /// Valid lifetime value used by different tests.
  27. const uint32_t VALID_LIFETIME = 500;
  28. /// Subnet ID used by different tests.
  29. const uint32_t SUBNET_ID = 42;
  30. /// IAID value used by different tests.
  31. const uint32_t IAID = 7;
  32. /// @brief Creates an instance of the lease with certain FQDN data.
  33. ///
  34. /// @param hostname Hostname.
  35. /// @param fqdn_fwd Forward FQDN update setting for a created lease.
  36. /// @param fqdn_rev Reverse FQDN update setting for a created lease.
  37. ///
  38. /// @return Instance of the created lease.
  39. Lease4 createLease4(const std::string& hostname, const bool fqdn_fwd,
  40. const bool fqdn_rev) {
  41. Lease4 lease;
  42. lease.hostname_ = hostname;
  43. lease.fqdn_fwd_ = fqdn_fwd;
  44. lease.fqdn_rev_ = fqdn_rev;
  45. return (lease);
  46. }
  47. /// @brief Fixture class used in Lease4 testing.
  48. class Lease4Test : public ::testing::Test {
  49. public:
  50. /// @brief Default constructor
  51. ///
  52. /// Currently it only initializes hardware address.
  53. Lease4Test() {
  54. hwaddr_.reset(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  55. clientid_.reset(new ClientId(CLIENTID, sizeof(CLIENTID)));
  56. }
  57. /// Hardware address, used by tests.
  58. HWAddrPtr hwaddr_;
  59. /// Pointer to the client identifier used by tests.
  60. ClientIdPtr clientid_;
  61. };
  62. // This test checks if the Lease4 structure can be instantiated correctly.
  63. TEST_F(Lease4Test, constructor) {
  64. // Get current time for the use in Lease.
  65. const time_t current_time = time(NULL);
  66. // We want to check that various addresses work, so let's iterate over
  67. // these.
  68. const uint32_t ADDRESS[] = {
  69. 0x00000000, 0x01020304, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff
  70. };
  71. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  72. // Create the lease
  73. Lease4 lease(ADDRESS[i], hwaddr_, clientid_, VALID_LIFETIME, 0, 0,
  74. current_time, SUBNET_ID, true, true,
  75. "hostname.example.com.");
  76. EXPECT_EQ(ADDRESS[i], lease.addr_.toUint32());
  77. EXPECT_TRUE(util::equalValues(hwaddr_, lease.hwaddr_));
  78. EXPECT_TRUE(util::equalValues(clientid_, lease.client_id_));
  79. EXPECT_EQ(0, lease.t1_);
  80. EXPECT_EQ(0, lease.t2_);
  81. EXPECT_EQ(VALID_LIFETIME, lease.valid_lft_);
  82. EXPECT_EQ(current_time, lease.cltt_);
  83. EXPECT_EQ(SUBNET_ID, lease.subnet_id_);
  84. EXPECT_EQ("hostname.example.com.", lease.hostname_);
  85. EXPECT_TRUE(lease.fqdn_fwd_);
  86. EXPECT_TRUE(lease.fqdn_rev_);
  87. EXPECT_EQ(Lease::STATE_DEFAULT, lease.state_);
  88. }
  89. }
  90. // This test verifies that copy constructor copies Lease4 fields correctly.
  91. TEST_F(Lease4Test, copyConstructor) {
  92. // Get current time for the use in Lease4.
  93. const time_t current_time = time(NULL);
  94. // Create the lease
  95. Lease4 lease(0xffffffff, hwaddr_, clientid_, VALID_LIFETIME, 0, 0, current_time,
  96. SUBNET_ID);
  97. // Declined is a non-default state. We'll see if the state will be copied
  98. // or the default state will be set for the copied lease.
  99. lease.state_ = Lease::STATE_DECLINED;
  100. // Use copy constructor to copy the lease.
  101. Lease4 copied_lease(lease);
  102. // Both leases should be now equal. When doing this check we assume that
  103. // the equality operator works correctly.
  104. EXPECT_TRUE(lease == copied_lease);
  105. // Client IDs are equal, but they should be in two distinct pointers.
  106. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  107. // Hardware addresses are equal, but they should point to two objects,
  108. // each holding the same data. The content should be equal...
  109. EXPECT_TRUE(*lease.hwaddr_ == *copied_lease.hwaddr_);
  110. // ... but it should point to different objects.
  111. EXPECT_FALSE(lease.hwaddr_ == copied_lease.hwaddr_);
  112. // Now let's check that the hwaddr pointer is copied even if it's NULL:
  113. lease.hwaddr_.reset();
  114. Lease4 copied_lease2(lease);
  115. EXPECT_TRUE(lease == copied_lease2);
  116. }
  117. // This test verifies that the assignment operator copies all Lease4 fields
  118. // correctly.
  119. TEST_F(Lease4Test, operatorAssign) {
  120. // Get the current time for the use in Lease4.
  121. const time_t current_time = time(NULL);
  122. // Create the lease
  123. Lease4 lease(0xffffffff, hwaddr_, clientid_, VALID_LIFETIME, 0, 0, current_time,
  124. SUBNET_ID);
  125. // Declined is a non-default state. We'll see if the state will be copied
  126. // or the default state will be set for the copied lease.
  127. lease.state_ = Lease::STATE_DECLINED;
  128. // Create a default lease.
  129. Lease4 copied_lease;
  130. // Use assignment operator to assign new lease.
  131. copied_lease = lease;
  132. // Both leases should be now equal. When doing this check we assume that
  133. // the equality operator works correctly.
  134. EXPECT_TRUE(lease == copied_lease);
  135. // Client IDs are equal, but they should be in two distinct pointers.
  136. EXPECT_FALSE(lease.client_id_ == copied_lease.client_id_);
  137. // Hardware addresses are equal, but they should point to two objects,
  138. // each holding the same data. The content should be equal...
  139. EXPECT_TRUE(*lease.hwaddr_ == *copied_lease.hwaddr_);
  140. // ... but it should point to different objects.
  141. EXPECT_FALSE(lease.hwaddr_ == copied_lease.hwaddr_);
  142. // Now let's check that the hwaddr pointer is copied even if it's NULL:
  143. lease.hwaddr_.reset();
  144. copied_lease = lease;
  145. EXPECT_TRUE(lease == copied_lease);
  146. }
  147. // This test verifies that it is correctly determined when the lease
  148. // belongs to the particular client identified by the client identifier
  149. // and hw address.
  150. TEST_F(Lease4Test, leaseBelongsToClient) {
  151. // Client identifier that matches the one in the lease.
  152. ClientIdPtr matching_client_id = ClientId::fromText("01:02:03:04");
  153. // Client identifier that doesn't match the one in the lease.
  154. ClientIdPtr diff_client_id = ClientId::fromText("01:02:03:05");
  155. // Null (no) client identifier.
  156. ClientIdPtr null_client_id;
  157. // HW Address that matches the one in the lease.
  158. HWAddrPtr matching_hw(new HWAddr(HWAddr::fromText("00:01:02:03:04:05",
  159. HTYPE_ETHER)));
  160. // HW Address that doesn't match the one in the lease.
  161. HWAddrPtr diff_hw(new HWAddr(HWAddr::fromText("00:01:02:03:04:06",
  162. HTYPE_ETHER)));
  163. // Null HW Address.
  164. HWAddrPtr null_hw;
  165. // Create the lease with MAC address and Client Identifier.
  166. Lease4 lease(IOAddress("192.0.2.1"), matching_hw, matching_client_id,
  167. 60, time(NULL), 0, 0, 1);
  168. // Verify cases for lease that has both hw address and client identifier.
  169. EXPECT_TRUE(lease.belongsToClient(matching_hw, matching_client_id));
  170. EXPECT_FALSE(lease.belongsToClient(matching_hw, diff_client_id));
  171. EXPECT_TRUE(lease.belongsToClient(matching_hw, null_client_id));
  172. EXPECT_TRUE(lease.belongsToClient(diff_hw, matching_client_id));
  173. EXPECT_FALSE(lease.belongsToClient(diff_hw, diff_client_id));
  174. EXPECT_FALSE(lease.belongsToClient(diff_hw, null_client_id));
  175. EXPECT_TRUE(lease.belongsToClient(null_hw, matching_client_id));
  176. EXPECT_FALSE(lease.belongsToClient(null_hw, diff_client_id));
  177. EXPECT_FALSE(lease.belongsToClient(null_hw, null_client_id));
  178. // Verify cases for lease that has only HW address.
  179. lease.client_id_ = null_client_id;
  180. EXPECT_TRUE(lease.belongsToClient(matching_hw, matching_client_id));
  181. EXPECT_TRUE(lease.belongsToClient(matching_hw, diff_client_id));
  182. EXPECT_TRUE(lease.belongsToClient(matching_hw, null_client_id));
  183. EXPECT_FALSE(lease.belongsToClient(diff_hw, matching_client_id));
  184. EXPECT_FALSE(lease.belongsToClient(diff_hw, diff_client_id));
  185. EXPECT_FALSE(lease.belongsToClient(diff_hw, null_client_id));
  186. EXPECT_FALSE(lease.belongsToClient(null_hw, matching_client_id));
  187. EXPECT_FALSE(lease.belongsToClient(null_hw, diff_client_id));
  188. EXPECT_FALSE(lease.belongsToClient(null_hw, null_client_id));
  189. // Verify cases for lease that has only client identifier.
  190. lease.client_id_ = matching_client_id;
  191. lease.hwaddr_ = null_hw;
  192. EXPECT_TRUE(lease.belongsToClient(matching_hw, matching_client_id));
  193. EXPECT_FALSE(lease.belongsToClient(matching_hw, diff_client_id));
  194. EXPECT_FALSE(lease.belongsToClient(matching_hw, null_client_id));
  195. EXPECT_TRUE(lease.belongsToClient(diff_hw, matching_client_id));
  196. EXPECT_FALSE(lease.belongsToClient(diff_hw, diff_client_id));
  197. EXPECT_FALSE(lease.belongsToClient(diff_hw, null_client_id));
  198. EXPECT_TRUE(lease.belongsToClient(null_hw, matching_client_id));
  199. EXPECT_FALSE(lease.belongsToClient(null_hw, diff_client_id));
  200. EXPECT_FALSE(lease.belongsToClient(null_hw, null_client_id));
  201. }
  202. /// @brief Lease4 Equality Test
  203. ///
  204. /// Checks that the operator==() correctly compares two leases for equality.
  205. /// As operator!=() is also defined for this class, every check on operator==()
  206. /// is followed by the reverse check on operator!=().
  207. TEST_F(Lease4Test, operatorEquals) {
  208. // Random values for the tests
  209. const uint32_t ADDRESS = 0x01020304;
  210. const time_t current_time = time(NULL);
  211. // Check when the leases are equal.
  212. Lease4 lease1(ADDRESS, hwaddr_, clientid_, VALID_LIFETIME, current_time, 0,
  213. 0, SUBNET_ID);
  214. // We need to make an explicit copy. Otherwise the second lease will just
  215. // store a pointer and we'll have two leases pointing to a single HWAddr
  216. // or client. That would make modifications to only one impossible.
  217. HWAddrPtr hwcopy(new HWAddr(*hwaddr_));
  218. ClientIdPtr clientid_copy(new ClientId(*clientid_));
  219. Lease4 lease2(ADDRESS, hwcopy, clientid_copy, VALID_LIFETIME, current_time,
  220. 0, 0, SUBNET_ID);
  221. EXPECT_TRUE(lease1 == lease2);
  222. EXPECT_FALSE(lease1 != lease2);
  223. // Now vary individual fields in a lease and check that the leases compare
  224. // not equal in every case.
  225. lease1.addr_ = IOAddress(ADDRESS + 1);
  226. EXPECT_FALSE(lease1 == lease2);
  227. EXPECT_TRUE(lease1 != lease2);
  228. lease1.addr_ = lease2.addr_;
  229. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  230. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  231. ++lease1.hwaddr_->hwaddr_[0];
  232. EXPECT_FALSE(lease1 == lease2);
  233. EXPECT_TRUE(lease1 != lease2);
  234. lease1.hwaddr_ = lease2.hwaddr_;
  235. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  236. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  237. std::vector<uint8_t> clientid_vec = clientid_->getClientId();
  238. ++clientid_vec[0];
  239. lease1.client_id_.reset(new ClientId(clientid_vec));
  240. EXPECT_FALSE(lease1 == lease2);
  241. EXPECT_TRUE(lease1 != lease2);
  242. --clientid_vec[0];
  243. lease1.client_id_.reset(new ClientId(clientid_vec));
  244. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  245. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  246. ++lease1.t1_;
  247. EXPECT_FALSE(lease1 == lease2);
  248. EXPECT_TRUE(lease1 != lease2);
  249. lease1.t1_ = lease2.t1_;
  250. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  251. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  252. ++lease1.t2_;
  253. EXPECT_FALSE(lease1 == lease2);
  254. EXPECT_TRUE(lease1 != lease2);
  255. lease1.t2_ = lease2.t2_;
  256. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  257. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  258. ++lease1.valid_lft_;
  259. EXPECT_FALSE(lease1 == lease2);
  260. EXPECT_TRUE(lease1 != lease2);
  261. lease1.valid_lft_ = lease2.valid_lft_;
  262. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  263. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  264. ++lease1.cltt_;
  265. EXPECT_FALSE(lease1 == lease2);
  266. EXPECT_TRUE(lease1 != lease2);
  267. lease1.cltt_ = lease2.cltt_;
  268. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  269. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  270. ++lease1.subnet_id_;
  271. EXPECT_FALSE(lease1 == lease2);
  272. EXPECT_TRUE(lease1 != lease2);
  273. lease1.subnet_id_ = lease2.subnet_id_;
  274. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  275. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  276. lease1.hostname_ += std::string("Something random");
  277. EXPECT_FALSE(lease1 == lease2);
  278. EXPECT_TRUE(lease1 != lease2);
  279. lease1.hostname_ = lease2.hostname_;
  280. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  281. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  282. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  283. EXPECT_FALSE(lease1 == lease2);
  284. EXPECT_TRUE(lease1 != lease2);
  285. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  286. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  287. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  288. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  289. EXPECT_FALSE(lease1 == lease2);
  290. EXPECT_TRUE(lease1 != lease2);
  291. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  292. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  293. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  294. lease1.state_ += 1;
  295. EXPECT_FALSE(lease1 == lease2);
  296. EXPECT_TRUE(lease1 != lease2);
  297. lease2.state_ += 1;
  298. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  299. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  300. }
  301. // Verify that the client id can be returned as a vector object and if client
  302. // id is NULL the empty vector is returned.
  303. TEST_F(Lease4Test, getClientIdVector) {
  304. // Create a lease.
  305. Lease4 lease;
  306. // By default, the lease should have client id set to NULL. If it doesn't,
  307. // continuing the test makes no sense.
  308. ASSERT_FALSE(lease.client_id_);
  309. // When client id is NULL the vector returned should be empty.
  310. EXPECT_TRUE(lease.getClientIdVector().empty());
  311. // Initialize client identifier to non-null value.
  312. lease.client_id_ = clientid_;
  313. // Check that the returned vector, encapsulating client id is equal to
  314. // the one that has been used to set the client id for the lease.
  315. std::vector<uint8_t> returned_vec = lease.getClientIdVector();
  316. EXPECT_TRUE(returned_vec == clientid_->getClientId());
  317. }
  318. // Verify the behavior of the function which checks FQDN data for equality.
  319. TEST_F(Lease4Test, hasIdenticalFqdn) {
  320. Lease4 lease = createLease4("myhost.example.com.", true, true);
  321. EXPECT_TRUE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  322. true, true)));
  323. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("other.example.com.",
  324. true, true)));
  325. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  326. false, true)));
  327. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  328. true, false)));
  329. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("myhost.example.com.",
  330. false, false)));
  331. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease4("other.example.com.",
  332. false, false)));
  333. }
  334. // Verify that toText() method reports Lease4 structure properly.
  335. TEST_F(Lease4Test, toText) {
  336. const time_t current_time = 12345678;
  337. Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
  338. 456, current_time, 789);
  339. std::stringstream expected;
  340. expected << "Address: 192.0.2.3\n"
  341. << "Valid life: 3600\n"
  342. << "T1: 123\n"
  343. << "T2: 456\n"
  344. << "Cltt: 12345678\n"
  345. << "Hardware addr: " << hwaddr_->toText(false) << "\n"
  346. << "Client id: " << clientid_->toText() << "\n"
  347. << "Subnet ID: 789\n"
  348. << "State: default\n";
  349. EXPECT_EQ(expected.str(), lease.toText());
  350. // Now let's try with a lease without hardware address and client identifier.
  351. lease.hwaddr_.reset();
  352. lease.client_id_.reset();
  353. expected.str("");
  354. expected << "Address: 192.0.2.3\n"
  355. << "Valid life: 3600\n"
  356. << "T1: 123\n"
  357. << "T2: 456\n"
  358. << "Cltt: 12345678\n"
  359. << "Hardware addr: (none)\n"
  360. << "Client id: (none)\n"
  361. << "Subnet ID: 789\n"
  362. << "State: default\n";
  363. EXPECT_EQ(expected.str(), lease.toText());
  364. }
  365. // Verify that Lease4 structure can be converted to JSON properly.
  366. TEST_F(Lease4Test, toElement) {
  367. const time_t current_time = 12345678;
  368. Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
  369. 456, current_time, 789, true, true, "urania.example.org");
  370. std::string expected = "{"
  371. "\"client-id\": \"17:34:e2:ff:09:92:54\","
  372. "\"cltt\": 12345678,"
  373. "\"fqdn-fwd\": true,"
  374. "\"fqdn-rev\": true,"
  375. "\"hostname\": \"urania.example.org\","
  376. "\"hw-address\": \"08:00:2b:02:3f:4e\","
  377. "\"ip-address\": \"192.0.2.3\","
  378. "\"state\": 0,"
  379. "\"subnet-id\": 789,"
  380. "\"valid-lft\": 3600 "
  381. "}";
  382. runToElementTest<Lease4>(expected, lease);
  383. // Now let's try with a lease without client-id.
  384. lease.client_id_.reset();
  385. expected = "{"
  386. "\"cltt\": 12345678,"
  387. "\"fqdn-fwd\": true,"
  388. "\"fqdn-rev\": true,"
  389. "\"hostname\": \"urania.example.org\","
  390. "\"hw-address\": \"08:00:2b:02:3f:4e\","
  391. "\"ip-address\": \"192.0.2.3\","
  392. "\"state\": 0,"
  393. "\"subnet-id\": 789,"
  394. "\"valid-lft\": 3600 "
  395. "}";
  396. runToElementTest<Lease4>(expected, lease);
  397. }
  398. // Verify that decline() method properly clears up specific fields.
  399. TEST_F(Lease4Test, decline) {
  400. const time_t current_time = 12345678;
  401. Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
  402. 456, current_time, 789);
  403. lease.hostname_="foo.example.org";
  404. lease.fqdn_fwd_ = true;
  405. lease.fqdn_rev_ = true;
  406. time_t now = time(NULL);
  407. // Move lease to declined state and set its valid-lifetime to 123 seconds
  408. lease.decline(123);
  409. ASSERT_TRUE(lease.hwaddr_);
  410. EXPECT_EQ("", lease.hwaddr_->toText(false));
  411. EXPECT_FALSE(lease.client_id_);
  412. EXPECT_EQ(0, lease.t1_);
  413. EXPECT_EQ(0, lease.t2_);
  414. EXPECT_TRUE(now <= lease.cltt_);
  415. EXPECT_TRUE(lease.cltt_ <= now + 1);
  416. EXPECT_EQ("", lease.hostname_);
  417. EXPECT_FALSE(lease.fqdn_fwd_);
  418. EXPECT_FALSE(lease.fqdn_rev_);
  419. EXPECT_EQ(Lease::STATE_DECLINED, lease.state_);
  420. EXPECT_EQ(123, lease.valid_lft_);
  421. }
  422. // Verify that the lease states are correctly returned in the textual format.
  423. TEST_F(Lease4Test, stateToText) {
  424. EXPECT_EQ("default", Lease4::statesToText(Lease::STATE_DEFAULT));
  425. EXPECT_EQ("declined", Lease4::statesToText(Lease::STATE_DECLINED));
  426. EXPECT_EQ("expired-reclaimed", Lease4::statesToText(Lease::STATE_EXPIRED_RECLAIMED));
  427. }
  428. /// @brief Creates an instance of the lease with certain FQDN data.
  429. ///
  430. /// @param hostname Hostname.
  431. /// @param fqdn_fwd Forward FQDN update setting for a created lease.
  432. /// @param fqdn_rev Reverse FQDN update setting for a created lease.
  433. ///
  434. /// @return Instance of the created lease.
  435. Lease6 createLease6(const std::string& hostname, const bool fqdn_fwd,
  436. const bool fqdn_rev) {
  437. Lease6 lease;
  438. lease.hostname_ = hostname;
  439. lease.fqdn_fwd_ = fqdn_fwd;
  440. lease.fqdn_rev_ = fqdn_rev;
  441. return (lease);
  442. }
  443. // Lease6 is also defined in lease_mgr.h, so is tested in this file as well.
  444. // This test checks if the Lease6 structure can be instantiated correctly
  445. TEST(Lease6Test, Lease6ConstructorDefault) {
  446. // check a variety of addresses with different bits set.
  447. const char* ADDRESS[] = {
  448. "::", "::1", "2001:db8:1::456",
  449. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  450. "8000::", "8000::1",
  451. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  452. };
  453. // Other values
  454. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  455. DuidPtr duid(new DUID(llt, sizeof(llt)));
  456. uint32_t iaid = IAID; // Just a number
  457. SubnetID subnet_id = 8; // Just another number
  458. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  459. IOAddress addr(ADDRESS[i]);
  460. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  461. duid, iaid, 100, 200, 50, 80,
  462. subnet_id));
  463. EXPECT_TRUE(lease->addr_ == addr);
  464. EXPECT_TRUE(*lease->duid_ == *duid);
  465. EXPECT_TRUE(lease->iaid_ == iaid);
  466. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  467. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  468. EXPECT_TRUE(lease->preferred_lft_ == 100);
  469. EXPECT_TRUE(lease->valid_lft_ == 200);
  470. EXPECT_TRUE(lease->t1_ == 50);
  471. EXPECT_TRUE(lease->t2_ == 80);
  472. EXPECT_FALSE(lease->fqdn_fwd_);
  473. EXPECT_FALSE(lease->fqdn_rev_);
  474. EXPECT_TRUE(lease->hostname_.empty());
  475. }
  476. // Lease6 must be instantiated with a DUID, not with NULL pointer
  477. IOAddress addr(ADDRESS[0]);
  478. Lease6Ptr lease2;
  479. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  480. DuidPtr(), iaid, 100, 200, 50, 80,
  481. subnet_id)), InvalidOperation);
  482. }
  483. // This test verifies that the Lease6 constructor which accepts FQDN data,
  484. // sets the data correctly for the lease.
  485. TEST(Lease6Test, Lease6ConstructorWithFQDN) {
  486. // check a variety of addresses with different bits set.
  487. const char* ADDRESS[] = {
  488. "::", "::1", "2001:db8:1::456",
  489. "7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
  490. "8000::", "8000::1",
  491. "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
  492. };
  493. // Other values
  494. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  495. DuidPtr duid(new DUID(llt, sizeof(llt)));
  496. uint32_t iaid = IAID; // Just a number
  497. SubnetID subnet_id = 8; // Just another number
  498. for (int i = 0; i < sizeof(ADDRESS) / sizeof(ADDRESS[0]); ++i) {
  499. IOAddress addr(ADDRESS[i]);
  500. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
  501. duid, iaid, 100, 200, 50, 80, subnet_id,
  502. true, true, "host.example.com."));
  503. EXPECT_TRUE(lease->addr_ == addr);
  504. EXPECT_TRUE(*lease->duid_ == *duid);
  505. EXPECT_TRUE(lease->iaid_ == iaid);
  506. EXPECT_TRUE(lease->subnet_id_ == subnet_id);
  507. EXPECT_TRUE(lease->type_ == Lease::TYPE_NA);
  508. EXPECT_TRUE(lease->preferred_lft_ == 100);
  509. EXPECT_TRUE(lease->valid_lft_ == 200);
  510. EXPECT_TRUE(lease->t1_ == 50);
  511. EXPECT_TRUE(lease->t2_ == 80);
  512. EXPECT_TRUE(lease->fqdn_fwd_);
  513. EXPECT_TRUE(lease->fqdn_rev_);
  514. EXPECT_EQ("host.example.com.", lease->hostname_);
  515. }
  516. // Lease6 must be instantiated with a DUID, not with NULL pointer
  517. IOAddress addr(ADDRESS[0]);
  518. Lease6Ptr lease2;
  519. EXPECT_THROW(lease2.reset(new Lease6(Lease::TYPE_NA, addr,
  520. DuidPtr(), iaid, 100, 200, 50, 80,
  521. subnet_id)), InvalidOperation);
  522. }
  523. /// @brief Lease6 Equality Test
  524. ///
  525. /// Checks that the operator==() correctly compares two leases for equality.
  526. /// As operator!=() is also defined for this class, every check on operator==()
  527. /// is followed by the reverse check on operator!=().
  528. TEST(Lease6Test, operatorEquals) {
  529. // check a variety of addresses with different bits set.
  530. const IOAddress addr("2001:db8:1::456");
  531. uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  532. DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  533. uint32_t iaid = IAID; // just a number
  534. SubnetID subnet_id = 8; // just another number
  535. // Check for equality.
  536. Lease6 lease1(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  537. subnet_id);
  538. Lease6 lease2(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  539. subnet_id);
  540. // cltt_ constructs with time(NULL), make sure they are always equal
  541. lease1.cltt_ = lease2.cltt_;
  542. EXPECT_TRUE(lease1 == lease2);
  543. EXPECT_FALSE(lease1 != lease2);
  544. // Go through and alter all the fields one by one
  545. lease1.addr_ = IOAddress("::1");
  546. EXPECT_FALSE(lease1 == lease2);
  547. EXPECT_TRUE(lease1 != lease2);
  548. lease1.addr_ = lease2.addr_;
  549. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  550. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  551. lease1.type_ = Lease::TYPE_PD;
  552. EXPECT_FALSE(lease1 == lease2);
  553. EXPECT_TRUE(lease1 != lease2);
  554. lease1.type_ = lease2.type_;
  555. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  556. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  557. ++lease1.prefixlen_;
  558. EXPECT_FALSE(lease1 == lease2);
  559. EXPECT_TRUE(lease1 != lease2);
  560. lease1.prefixlen_ = lease2.prefixlen_;
  561. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  562. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  563. ++lease1.iaid_;
  564. EXPECT_FALSE(lease1 == lease2);
  565. EXPECT_TRUE(lease1 != lease2);
  566. lease1.iaid_ = lease2.iaid_;
  567. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  568. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  569. ++duid_array[0];
  570. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  571. EXPECT_FALSE(lease1 == lease2);
  572. EXPECT_TRUE(lease1 != lease2);
  573. --duid_array[0];
  574. lease1.duid_.reset(new DUID(duid_array, sizeof(duid_array)));
  575. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  576. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  577. ++lease1.preferred_lft_;
  578. EXPECT_FALSE(lease1 == lease2);
  579. EXPECT_TRUE(lease1 != lease2);
  580. lease1.preferred_lft_ = lease2.preferred_lft_;
  581. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  582. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  583. ++lease1.valid_lft_;
  584. EXPECT_FALSE(lease1 == lease2);
  585. EXPECT_TRUE(lease1 != lease2);
  586. lease1.valid_lft_ = lease2.valid_lft_;
  587. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  588. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  589. ++lease1.t1_;
  590. EXPECT_FALSE(lease1 == lease2);
  591. EXPECT_TRUE(lease1 != lease2);
  592. lease1.t1_ = lease2.t1_;
  593. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  594. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  595. ++lease1.t2_;
  596. EXPECT_FALSE(lease1 == lease2);
  597. EXPECT_TRUE(lease1 != lease2);
  598. lease1.t2_ = lease2.t2_;
  599. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  600. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  601. ++lease1.cltt_;
  602. EXPECT_FALSE(lease1 == lease2);
  603. EXPECT_TRUE(lease1 != lease2);
  604. lease1.cltt_ = lease2.cltt_;
  605. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  606. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  607. ++lease1.subnet_id_;
  608. EXPECT_FALSE(lease1 == lease2);
  609. EXPECT_TRUE(lease1 != lease2);
  610. lease1.subnet_id_ = lease2.subnet_id_;
  611. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  612. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  613. lease1.hostname_ += std::string("Something random");
  614. EXPECT_FALSE(lease1 == lease2);
  615. EXPECT_TRUE(lease1 != lease2);
  616. lease1.hostname_ = lease2.hostname_;
  617. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  618. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  619. lease1.fqdn_fwd_ = !lease1.fqdn_fwd_;
  620. EXPECT_FALSE(lease1 == lease2);
  621. EXPECT_TRUE(lease1 != lease2);
  622. lease1.fqdn_fwd_ = lease2.fqdn_fwd_;
  623. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  624. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  625. lease1.fqdn_rev_ = !lease1.fqdn_rev_;
  626. EXPECT_FALSE(lease1 == lease2);
  627. EXPECT_TRUE(lease1 != lease2);
  628. lease1.fqdn_rev_ = lease2.fqdn_rev_;
  629. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  630. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  631. lease1.state_ += 1;
  632. EXPECT_FALSE(lease1 == lease2);
  633. EXPECT_TRUE(lease1 != lease2);
  634. lease2.state_ += 1;
  635. EXPECT_TRUE(lease1 == lease2); // Check that the reversion has made the
  636. EXPECT_FALSE(lease1 != lease2); // ... leases equal
  637. }
  638. // Checks if lease expiration is calculated properly
  639. TEST(Lease6Test, Lease6Expired) {
  640. const IOAddress addr("2001:db8:1::456");
  641. const uint8_t duid_array[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  642. const DuidPtr duid(new DUID(duid_array, sizeof(duid_array)));
  643. const uint32_t iaid = IAID; // Just a number
  644. const SubnetID subnet_id = 8; // Just another number
  645. Lease6 lease(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50, 80,
  646. subnet_id);
  647. // Case 1: a second before expiration
  648. lease.cltt_ = time(NULL) - 100;
  649. lease.valid_lft_ = 101;
  650. EXPECT_FALSE(lease.expired());
  651. // Case 2: the lease will expire after this second is concluded
  652. lease.cltt_ = time(NULL) - 101;
  653. EXPECT_FALSE(lease.expired());
  654. // Case 3: the lease is expired
  655. lease.cltt_ = time(NULL) - 102;
  656. EXPECT_TRUE(lease.expired());
  657. }
  658. // Verify that the DUID can be returned as a vector object and if DUID is NULL
  659. // the empty vector is returned.
  660. TEST(Lease6Test, getDuidVector) {
  661. // Create a lease.
  662. Lease6 lease;
  663. // By default, the lease should have client id set to NULL. If it doesn't,
  664. // continuing the test makes no sense.
  665. ASSERT_FALSE(lease.duid_);
  666. // When client id is NULL the vector returned should be empty.
  667. EXPECT_TRUE(lease.getDuidVector().empty());
  668. // Now, let's set the non NULL DUID. Fill it with the 8 bytes, each
  669. // holding a value of 0x42.
  670. std::vector<uint8_t> duid_vec(8, 0x42);
  671. lease.duid_ = DuidPtr(new DUID(duid_vec));
  672. // Check that the returned vector, encapsulating DUID is equal to
  673. // the one that has been used to set the DUID for the lease.
  674. std::vector<uint8_t> returned_vec = lease.getDuidVector();
  675. EXPECT_TRUE(returned_vec == duid_vec);
  676. }
  677. // Verify that decline() method properly clears up specific fields.
  678. TEST(Lease6Test, decline) {
  679. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  680. DuidPtr duid(new DUID(llt, sizeof(llt)));
  681. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  682. // Let's create a lease for 2001:db8::1, DUID, iaid=1234,
  683. // t1=1000, t2=2000, pref=3000, valid=4000, subnet-id = 1
  684. Lease6 lease(Lease::TYPE_NA, IOAddress("2001:db8::1"), duid,
  685. 1234, 3000, 4000, 1000, 2000, 1, hwaddr);
  686. lease.cltt_ = 12345678;
  687. lease.hostname_ = "foo.example.org";
  688. lease.fqdn_fwd_ = true;
  689. lease.fqdn_rev_ = true;
  690. time_t now = time(NULL);
  691. // Move the lease to declined state and set probation-period to 123 seconds
  692. lease.decline(123);
  693. ASSERT_TRUE(lease.duid_);
  694. ASSERT_EQ("00", lease.duid_->toText());
  695. ASSERT_FALSE(lease.hwaddr_);
  696. EXPECT_EQ(0, lease.t1_);
  697. EXPECT_EQ(0, lease.t2_);
  698. EXPECT_EQ(0, lease.preferred_lft_);
  699. EXPECT_TRUE(now <= lease.cltt_);
  700. EXPECT_TRUE(lease.cltt_ <= now + 1);
  701. EXPECT_EQ("", lease.hostname_);
  702. EXPECT_FALSE(lease.fqdn_fwd_);
  703. EXPECT_FALSE(lease.fqdn_rev_);
  704. EXPECT_EQ(Lease::STATE_DECLINED, lease.state_);
  705. EXPECT_EQ(123, lease.valid_lft_);
  706. }
  707. // Verify the behavior of the function which checks FQDN data for equality.
  708. TEST(Lease6Test, hasIdenticalFqdn) {
  709. Lease6 lease = createLease6("myhost.example.com.", true, true);
  710. EXPECT_TRUE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  711. true, true)));
  712. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("other.example.com.",
  713. true, true)));
  714. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  715. false, true)));
  716. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  717. true, false)));
  718. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("myhost.example.com.",
  719. false, false)));
  720. EXPECT_FALSE(lease.hasIdenticalFqdn(createLease6("other.example.com.",
  721. false, false)));
  722. }
  723. // Verify that toText() method reports Lease6 structure properly.
  724. TEST(Lease6Test, toText) {
  725. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  726. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  727. DuidPtr duid(new DUID(llt, sizeof(llt)));
  728. Lease6 lease(Lease::TYPE_NA, IOAddress("2001:db8::1"), duid, 123456,
  729. 400, 800, 100, 200, 5678, hwaddr, 128);
  730. lease.cltt_ = 12345678;
  731. lease.state_ = Lease::STATE_DECLINED;
  732. std::stringstream expected;
  733. expected << "Type: IA_NA(" << static_cast<int>(Lease::TYPE_NA) << ")\n"
  734. << "Address: 2001:db8::1\n"
  735. << "Prefix length: 128\n"
  736. << "IAID: 123456\n"
  737. << "Pref life: 400\n"
  738. << "Valid life: 800\n"
  739. << "Cltt: 12345678\n"
  740. << "DUID: 00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\n"
  741. << "Hardware addr: " << hwaddr->toText(false) << "\n"
  742. << "Subnet ID: 5678\n"
  743. << "State: declined\n";
  744. EXPECT_EQ(expected.str(), lease.toText());
  745. // Now let's try with a lease without hardware address.
  746. lease.hwaddr_.reset();
  747. expected.str("");
  748. expected << "Type: IA_NA(" << static_cast<int>(Lease::TYPE_NA) << ")\n"
  749. << "Address: 2001:db8::1\n"
  750. << "Prefix length: 128\n"
  751. << "IAID: 123456\n"
  752. << "Pref life: 400\n"
  753. << "Valid life: 800\n"
  754. << "Cltt: 12345678\n"
  755. << "DUID: 00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\n"
  756. << "Hardware addr: (none)\n"
  757. << "Subnet ID: 5678\n"
  758. << "State: declined\n";
  759. EXPECT_EQ(expected.str(), lease.toText());
  760. }
  761. // Verify that Lease6 structure can be converted to JSON properly.
  762. // This tests an address lease conversion.
  763. TEST(Lease6Test, toElementAddress) {
  764. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  765. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  766. DuidPtr duid(new DUID(llt, sizeof(llt)));
  767. Lease6 lease(Lease::TYPE_NA, IOAddress("2001:db8::1"), duid, 123456,
  768. 400, 800, 100, 200, 5678, hwaddr, 128);
  769. lease.cltt_ = 12345678;
  770. lease.state_ = Lease::STATE_DECLINED;
  771. lease.hostname_ = "urania.example.org";
  772. std::string expected = "{"
  773. "\"cltt\": 12345678,"
  774. "\"duid\": \"00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\","
  775. "\"fqdn-fwd\": false,"
  776. "\"fqdn-rev\": false,"
  777. "\"hostname\": \"urania.example.org\","
  778. "\"hw-address\": \"08:00:2b:02:3f:4e\","
  779. "\"iaid\": 123456,"
  780. "\"ip-address\": \"2001:db8::1\","
  781. "\"preferred-lft\": 400,"
  782. "\"state\": 1,"
  783. "\"subnet-id\": 5678,"
  784. "\"type\": \"IA_NA\","
  785. "\"valid-lft\": 800"
  786. "}";
  787. runToElementTest<Lease6>(expected, lease);
  788. // Now let's try with a lease without hardware address.
  789. lease.hwaddr_.reset();
  790. expected = "{"
  791. "\"cltt\": 12345678,"
  792. "\"duid\": \"00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f\","
  793. "\"fqdn-fwd\": false,"
  794. "\"fqdn-rev\": false,"
  795. "\"hostname\": \"urania.example.org\","
  796. "\"iaid\": 123456,"
  797. "\"ip-address\": \"2001:db8::1\","
  798. "\"preferred-lft\": 400,"
  799. "\"state\": 1,"
  800. "\"subnet-id\": 5678,"
  801. "\"type\": \"IA_NA\","
  802. "\"valid-lft\": 800"
  803. "}";
  804. runToElementTest<Lease6>(expected, lease);
  805. }
  806. // Verify that Lease6 structure can be converted to JSON properly.
  807. // This tests an address lease conversion.
  808. TEST(Lease6Test, toElementPrefix) {
  809. HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
  810. uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  811. DuidPtr duid(new DUID(llt, sizeof(llt)));
  812. Lease6 lease(Lease::TYPE_PD, IOAddress("2001:db8::"), duid, 123456,
  813. 400, 800, 100, 200, 5678, hwaddr, 56);
  814. lease.cltt_ = 12345678;
  815. lease.state_ = Lease::STATE_DEFAULT;
  816. lease.hostname_ = "urania.example.org";
  817. ElementPtr l = lease.toElement();
  818. ASSERT_TRUE(l);
  819. ASSERT_TRUE(l->contains("ip-address"));
  820. EXPECT_EQ("2001:db8::", l->get("ip-address")->stringValue());
  821. ASSERT_TRUE(l->contains("type"));
  822. EXPECT_EQ("IA_PD", l->get("type")->stringValue());
  823. // This is a prefix lease, it must have a prefix length.
  824. ASSERT_TRUE(l->contains("prefix-len"));
  825. EXPECT_EQ(56, l->get("prefix-len")->intValue());
  826. ASSERT_TRUE(l->contains("iaid"));
  827. EXPECT_EQ(123456, l->get("iaid")->intValue());
  828. ASSERT_TRUE(l->contains("preferred-lft"));
  829. EXPECT_EQ(400, l->get("preferred-lft")->intValue());
  830. ASSERT_TRUE(l->contains("valid-lft"));
  831. EXPECT_EQ(800, l->get("valid-lft")->intValue());
  832. ASSERT_TRUE(l->contains("duid"));
  833. EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f",
  834. l->get("duid")->stringValue());
  835. ASSERT_TRUE(l->contains("hw-address"));
  836. EXPECT_EQ(hwaddr->toText(false), l->get("hw-address")->stringValue());
  837. ASSERT_TRUE(l->contains("subnet-id"));
  838. EXPECT_EQ(5678, l->get("subnet-id")->intValue());
  839. ASSERT_TRUE(l->contains("state"));
  840. EXPECT_EQ(static_cast<int>(Lease::STATE_DEFAULT),
  841. l->get("state")->intValue());
  842. ASSERT_TRUE(l->contains("fqdn-fwd"));
  843. EXPECT_FALSE(l->get("fqdn-fwd")->boolValue());
  844. ASSERT_TRUE(l->contains("fqdn-rev"));
  845. EXPECT_FALSE(l->get("fqdn-rev")->boolValue());
  846. ASSERT_TRUE(l->contains("hostname"));
  847. EXPECT_EQ("urania.example.org", l->get("hostname")->stringValue());
  848. // Now let's try with a lease without hardware address.
  849. lease.hwaddr_.reset();
  850. l = lease.toElement();
  851. EXPECT_FALSE(l->contains("hw-address"));
  852. }
  853. // Verify that the lease states are correctly returned in the textual format.
  854. TEST(Lease6Test, stateToText) {
  855. EXPECT_EQ("default", Lease6::statesToText(Lease::STATE_DEFAULT));
  856. EXPECT_EQ("declined", Lease6::statesToText(Lease::STATE_DECLINED));
  857. EXPECT_EQ("expired-reclaimed", Lease6::statesToText(Lease::STATE_EXPIRED_RECLAIMED));
  858. }
  859. }; // end of anonymous namespace