memfile_lease_mgr_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright (C) 2012-2015 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 <dhcp/duid.h>
  17. #include <dhcpsrv/cfgmgr.h>
  18. #include <dhcpsrv/lease_mgr.h>
  19. #include <dhcpsrv/lease_mgr_factory.h>
  20. #include <dhcpsrv/memfile_lease_mgr.h>
  21. #include <dhcpsrv/tests/lease_file_io.h>
  22. #include <dhcpsrv/tests/test_utils.h>
  23. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  24. #include <gtest/gtest.h>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <sstream>
  28. using namespace std;
  29. using namespace isc;
  30. using namespace isc::asiolink;
  31. using namespace isc::dhcp;
  32. using namespace isc::dhcp::test;
  33. namespace {
  34. // empty class for now, but may be extended once Addr6 becomes bigger
  35. class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
  36. public:
  37. /// @brief memfile lease mgr test constructor
  38. ///
  39. /// Creates memfile and stores it in lmptr_ pointer
  40. MemfileLeaseMgrTest() :
  41. io4_(getLeaseFilePath("leasefile4_0.csv")),
  42. io6_(getLeaseFilePath("leasefile6_0.csv")) {
  43. // Make sure there are no dangling files after previous tests.
  44. io4_.removeFile();
  45. io6_.removeFile();
  46. }
  47. /// @brief Reopens the connection to the backend.
  48. ///
  49. /// This function is called by unit tests to force reconnection of the
  50. /// backend to check that the leases are stored and can be retrieved
  51. /// from the storage.
  52. ///
  53. /// @param u Universe (V4 or V6)
  54. virtual void reopen(Universe u) {
  55. LeaseMgrFactory::destroy();
  56. startBackend(u);
  57. }
  58. /// @brief destructor
  59. ///
  60. /// destroys lease manager backend.
  61. virtual ~MemfileLeaseMgrTest() {
  62. LeaseMgrFactory::destroy();
  63. }
  64. /// @brief Return path to the lease file used by unit tests.
  65. ///
  66. /// @param filename Name of the lease file appended to the path to the
  67. /// directory where test data is held.
  68. ///
  69. /// @return Full path to the lease file.
  70. static std::string getLeaseFilePath(const std::string& filename) {
  71. std::ostringstream s;
  72. s << TEST_DATA_BUILDDIR << "/" << filename;
  73. return (s.str());
  74. }
  75. /// @brief Returns the configuration string for the backend.
  76. ///
  77. /// This string configures the @c LeaseMgrFactory to create the memfile
  78. /// backend and use leasefile4_0.csv and leasefile6_0.csv files as
  79. /// storage for leases.
  80. ///
  81. /// @param no_universe Indicates whether universe parameter should be
  82. /// included (false), or not (true).
  83. ///
  84. /// @return Configuration string for @c LeaseMgrFactory.
  85. static std::string getConfigString(Universe u) {
  86. std::ostringstream s;
  87. s << "type=memfile " << (u == V4 ? "universe=4 " : "universe=6 ")
  88. << "name="
  89. << getLeaseFilePath(u == V4 ? "leasefile4_0.csv" : "leasefile6_0.csv");
  90. return (s.str());
  91. }
  92. /// @brief Creates instance of the backend.
  93. ///
  94. /// @param u Universe (v4 or V6).
  95. void startBackend(Universe u) {
  96. try {
  97. LeaseMgrFactory::create(getConfigString(u));
  98. } catch (...) {
  99. std::cerr << "*** ERROR: unable to create instance of the Memfile\n"
  100. " lease database backend.\n";
  101. throw;
  102. }
  103. lmptr_ = &(LeaseMgrFactory::instance());
  104. }
  105. /// @brief Object providing access to v4 lease IO.
  106. LeaseFileIO io4_;
  107. /// @brief Object providing access to v6 lease IO.
  108. LeaseFileIO io6_;
  109. };
  110. // This test checks if the LeaseMgr can be instantiated and that it
  111. // parses parameters string properly.
  112. TEST_F(MemfileLeaseMgrTest, constructor) {
  113. LeaseMgr::ParameterMap pmap;
  114. pmap["universe"] = "4";
  115. pmap["persist"] = "false";
  116. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
  117. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  118. pmap["persist"] = "true";
  119. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  120. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  121. // Expecting that persist parameter is yes or no. Everything other than
  122. // that is wrong.
  123. pmap["persist"] = "bogus";
  124. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  125. EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
  126. }
  127. // Checks if the getType() and getName() methods both return "memfile".
  128. TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
  129. startBackend(V4);
  130. EXPECT_EQ(std::string("memfile"), lmptr_->getType());
  131. EXPECT_EQ(std::string("memory"), lmptr_->getName());
  132. }
  133. // Checks if the path to the lease files is initialized correctly.
  134. TEST_F(MemfileLeaseMgrTest, getLeaseFilePath) {
  135. // Initialize IO objects, so as the test csv files get removed after the
  136. // test (when destructors are called).
  137. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  138. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  139. LeaseMgr::ParameterMap pmap;
  140. pmap["universe"] = "4";
  141. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  142. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  143. EXPECT_EQ(pmap["name"],
  144. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  145. pmap["persist"] = "false";
  146. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  147. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4).empty());
  148. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6).empty());
  149. }
  150. // Check if the persitLeases correctly checks that leases should not be written
  151. // to disk when disabled through configuration.
  152. TEST_F(MemfileLeaseMgrTest, persistLeases) {
  153. // Initialize IO objects, so as the test csv files get removed after the
  154. // test (when destructors are called).
  155. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  156. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  157. LeaseMgr::ParameterMap pmap;
  158. pmap["universe"] = "4";
  159. // Specify the names of the lease files. Leases will be written.
  160. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  161. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  162. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  163. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  164. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  165. pmap["universe"] = "6";
  166. pmap["name"] = getLeaseFilePath("leasefile6_1.csv");
  167. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  168. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  169. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  170. // This should disable writes of leases to disk.
  171. pmap["persist"] = "false";
  172. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  173. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  174. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  175. }
  176. // Checks that adding/getting/deleting a Lease6 object works.
  177. TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
  178. startBackend(V6);
  179. testAddGetDelete6(true); // true - check T1,T2 values
  180. // memfile is able to preserve those values, but some other
  181. // backends can't do that.
  182. }
  183. /// @brief Basic Lease4 Checks
  184. ///
  185. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  186. /// IPv4 address) works.
  187. TEST_F(MemfileLeaseMgrTest, basicLease4) {
  188. startBackend(V4);
  189. testBasicLease4();
  190. }
  191. /// @todo Write more memfile tests
  192. // Simple test about lease4 retrieval through client id method
  193. TEST_F(MemfileLeaseMgrTest, getLease4ClientId) {
  194. startBackend(V4);
  195. testGetLease4ClientId();
  196. }
  197. // Checks that lease4 retrieval client id is null is working
  198. TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) {
  199. startBackend(V4);
  200. testGetLease4NullClientId();
  201. }
  202. // Checks lease4 retrieval through HWAddr
  203. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr1) {
  204. startBackend(V4);
  205. testGetLease4HWAddr1();
  206. }
  207. /// @brief Check GetLease4 methods - access by Hardware Address
  208. ///
  209. /// Adds leases to the database and checks that they can be accessed via
  210. /// a combination of DUID and IAID.
  211. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr2) {
  212. startBackend(V4);
  213. testGetLease4HWAddr2();
  214. }
  215. // Checks lease4 retrieval with clientId, HWAddr and subnet_id
  216. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) {
  217. startBackend(V4);
  218. testGetLease4ClientIdHWAddrSubnetId();
  219. }
  220. /// @brief Basic Lease4 Checks
  221. ///
  222. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  223. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  224. /// (client-id is optional and may not be present)
  225. TEST_F(MemfileLeaseMgrTest, lease4NullClientId) {
  226. startBackend(V4);
  227. testLease4NullClientId();
  228. }
  229. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  230. ///
  231. /// Adds leases to the database and checks that they can be accessed via
  232. /// a combination of hardware address and subnet ID
  233. TEST_F(MemfileLeaseMgrTest, DISABLED_getLease4HwaddrSubnetId) {
  234. /// @todo: fails on memfile. It's probably a memfile bug.
  235. startBackend(V4);
  236. testGetLease4HWAddrSubnetId();
  237. }
  238. /// @brief Check GetLease4 methods - access by Client ID
  239. ///
  240. /// Adds leases to the database and checks that they can be accessed via
  241. /// the Client ID.
  242. TEST_F(MemfileLeaseMgrTest, getLease4ClientId2) {
  243. startBackend(V4);
  244. testGetLease4ClientId2();
  245. }
  246. // @brief Get Lease4 by client ID
  247. //
  248. // Check that the system can cope with a client ID of any size.
  249. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSize) {
  250. startBackend(V4);
  251. testGetLease4ClientIdSize();
  252. }
  253. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  254. ///
  255. /// Adds leases to the database and checks that they can be accessed via
  256. /// a combination of client and subnet IDs.
  257. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSubnetId) {
  258. startBackend(V4);
  259. testGetLease4ClientIdSubnetId();
  260. }
  261. /// @brief Basic Lease6 Checks
  262. ///
  263. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  264. /// IPv6 address) works.
  265. TEST_F(MemfileLeaseMgrTest, basicLease6) {
  266. startBackend(V6);
  267. testBasicLease6();
  268. }
  269. /// @brief Check GetLease6 methods - access by DUID/IAID
  270. ///
  271. /// Adds leases to the database and checks that they can be accessed via
  272. /// a combination of DUID and IAID.
  273. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  274. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  275. TEST_F(MemfileLeaseMgrTest, getLeases6DuidIaid) {
  276. startBackend(V6);
  277. testGetLeases6DuidIaid();
  278. }
  279. // Check that the system can cope with a DUID of allowed size.
  280. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  281. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  282. TEST_F(MemfileLeaseMgrTest, getLeases6DuidSize) {
  283. startBackend(V6);
  284. testGetLeases6DuidSize();
  285. }
  286. /// @brief Check that getLease6 methods discriminate by lease type.
  287. ///
  288. /// Adds six leases, two per lease type all with the same duid and iad but
  289. /// with alternating subnet_ids.
  290. /// It then verifies that all of getLeases6() method variants correctly
  291. /// discriminate between the leases based on lease type alone.
  292. /// @todo: Disabled, because type parameter in Memfile_LeaseMgr::getLease6
  293. /// (Lease::Type, const isc::asiolink::IOAddress& addr) const is not used.
  294. TEST_F(MemfileLeaseMgrTest, lease6LeaseTypeCheck) {
  295. startBackend(V6);
  296. testLease6LeaseTypeCheck();
  297. }
  298. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  299. ///
  300. /// Adds leases to the database and checks that they can be accessed via
  301. /// a combination of DIUID and IAID.
  302. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetId) {
  303. startBackend(V6);
  304. testGetLease6DuidIaidSubnetId();
  305. }
  306. /// Checks that getLease6(type, duid, iaid, subnet-id) works with different
  307. /// DUID sizes
  308. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  309. startBackend(V6);
  310. testGetLease6DuidIaidSubnetIdSize();
  311. }
  312. /// @brief Lease4 update tests
  313. ///
  314. /// Checks that we are able to update a lease in the database.
  315. /// @todo: Disabled, because memfile does not throw when lease is updated.
  316. /// We should reconsider if lease{4,6} structures should have a limit
  317. /// implemented in them.
  318. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease4) {
  319. startBackend(V4);
  320. testUpdateLease4();
  321. }
  322. /// @brief Lease6 update tests
  323. ///
  324. /// Checks that we are able to update a lease in the database.
  325. /// @todo: Disabled, because memfile does not throw when lease is updated.
  326. /// We should reconsider if lease{4,6} structures should have a limit
  327. /// implemented in them.
  328. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease6) {
  329. startBackend(V6);
  330. testUpdateLease6();
  331. }
  332. /// @brief DHCPv4 Lease recreation tests
  333. ///
  334. /// Checks that the lease can be created, deleted and recreated with
  335. /// different parameters. It also checks that the re-created lease is
  336. /// correctly stored in the lease database.
  337. TEST_F(MemfileLeaseMgrTest, testRecreateLease4) {
  338. startBackend(V4);
  339. testRecreateLease4();
  340. }
  341. /// @brief DHCPv6 Lease recreation tests
  342. ///
  343. /// Checks that the lease can be created, deleted and recreated with
  344. /// different parameters. It also checks that the re-created lease is
  345. /// correctly stored in the lease database.
  346. TEST_F(MemfileLeaseMgrTest, testRecreateLease6) {
  347. startBackend(V6);
  348. testRecreateLease6();
  349. }
  350. // The following tests are not applicable for memfile. When adding
  351. // new tests to the list here, make sure to provide brief explanation
  352. // why they are not applicable:
  353. //
  354. // testGetLease4HWAddrSubnetIdSize() - memfile just keeps Lease structure
  355. // and does not do any checks of HWAddr content
  356. /// @brief Checks that null DUID is not allowed.
  357. /// Test is disabled as Memfile does not currently defend against a null DUID.
  358. TEST_F(MemfileLeaseMgrTest, DISABLED_nullDuid) {
  359. // Create leases, although we need only one.
  360. vector<Lease6Ptr> leases = createLeases6();
  361. leases[1]->duid_.reset();
  362. ASSERT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  363. }
  364. /// @brief Tests whether memfile can store and retrieve hardware addresses
  365. TEST_F(MemfileLeaseMgrTest, testLease6Mac) {
  366. startBackend(V6);
  367. testLease6MAC();
  368. }
  369. /// @brief Tests whether memfile is able to work with old CSV file (without mac)
  370. ///
  371. /// Ticket #3555 introduced MAC address support in Lease6. Instead of developing
  372. /// an upgrade script, the code is written in a way that allows reading old CSV
  373. /// (i.e. format that was used in Kea 0.9), hence no upgrade is necessary.
  374. TEST_F(MemfileLeaseMgrTest, testUpgrade0_9_0_to_0_9_1) {
  375. // Let's write a CSV file without hwaddr column. Sorry about the long
  376. // lines, but nobody was around to whine about 80 columns limit when CSV
  377. // format was invented :).
  378. string csv_nohwaddr =
  379. "address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname\n"
  380. "2001:db8::1,42:42:42:42:42:42:42:42,3677,127133,73,3600,1,42,0,0,1,myhost.example.com.\n"
  381. "2001:db8::2,3a:3a:3a:3a:3a:3a:3a:3a,5412,239979,73,1800,2,89,7,0,0,myhost.example.com.\n"
  382. "2001:db8::3,1f:20:21:22:23:24:25:26,7000,241567,37,7200,0,4294967294,28,1,0,myhost.example.com.\n";
  383. ofstream csv(getLeaseFilePath("leasefile6_0.csv").c_str(), ios::out | ios::trunc);
  384. ASSERT_TRUE(csv.is_open());
  385. csv << csv_nohwaddr;
  386. csv.close();
  387. startBackend(V6);
  388. // None of the leases should have any hardware addresses assigned.
  389. Lease6Ptr stored1 = lmptr_->getLease6(leasetype6_[1], ioaddress6_[1]);
  390. ASSERT_TRUE(stored1);
  391. EXPECT_FALSE(stored1->hwaddr_);
  392. Lease6Ptr stored2 = lmptr_->getLease6(leasetype6_[2], ioaddress6_[2]);
  393. ASSERT_TRUE(stored2);
  394. EXPECT_FALSE(stored2->hwaddr_);
  395. Lease6Ptr stored3 = lmptr_->getLease6(leasetype6_[3], ioaddress6_[3]);
  396. ASSERT_TRUE(stored3);
  397. EXPECT_FALSE(stored3->hwaddr_);
  398. }
  399. // Check that memfile reports version correctly.
  400. TEST_F(MemfileLeaseMgrTest, versionCheck) {
  401. // Check that V4 backend reports versions correctly.
  402. startBackend(V4);
  403. testVersion(Memfile_LeaseMgr::MAJOR_VERSION,
  404. Memfile_LeaseMgr::MINOR_VERSION);
  405. LeaseMgrFactory::destroy();
  406. // Check that V6 backends reports them ok, too.
  407. startBackend(V6);
  408. testVersion(Memfile_LeaseMgr::MAJOR_VERSION,
  409. Memfile_LeaseMgr::MINOR_VERSION);
  410. LeaseMgrFactory::destroy();
  411. }
  412. // This test checks that the backend reads DHCPv4 lease data from multiple
  413. // files.
  414. TEST_F(MemfileLeaseMgrTest, load4MultipleLeaseFiles) {
  415. LeaseFileIO io2("leasefile4_0.csv.2");
  416. io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  417. "fqdn_fwd,fqdn_rev,hostname\n"
  418. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,\n"
  419. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,200,8,1,1,,\n");
  420. LeaseFileIO io1("leasefile4_0.csv.1");
  421. io1.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  422. "fqdn_fwd,fqdn_rev,hostname\n"
  423. "192.0.2.1,01:01:01:01:01:01,,200,200,8,1,1,,\n"
  424. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,400,8,1,1,,\n"
  425. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,200,8,1,1,,\n");
  426. LeaseFileIO io("leasefile4_0.csv");
  427. io.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  428. "fqdn_fwd,fqdn_rev,hostname\n"
  429. "192.0.2.10,0a:0a:0a:0a:0a:0a,,200,200,8,1,1,,\n"
  430. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,400,8,1,1,,\n");
  431. startBackend(V4);
  432. // This lease only exists in the second file and the cltt should
  433. // be 0.
  434. Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
  435. ASSERT_TRUE(lease);
  436. EXPECT_EQ(0, lease->cltt_);
  437. // This lease only exists in the first file and the cltt should
  438. // be 0.
  439. lease = lmptr_->getLease4(IOAddress("192.0.2.2"));
  440. ASSERT_TRUE(lease);
  441. EXPECT_EQ(0, lease->cltt_);
  442. // This lease only exists in the third file and the cltt should
  443. // be 0.
  444. lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
  445. ASSERT_TRUE(lease);
  446. EXPECT_EQ(0, lease->cltt_);
  447. // This lease exists in the first and second file and the cltt
  448. // should be calculated using the expiration time and the
  449. // valid lifetime from the second file.
  450. lease = lmptr_->getLease4(IOAddress("192.0.2.11"));
  451. ASSERT_TRUE(lease);
  452. EXPECT_EQ(200, lease->cltt_);
  453. // Thsi lease exists in the second and third file and the cltt
  454. // should be calculated using the expiration time and the
  455. // valid lifetime from the third file.
  456. lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
  457. ASSERT_TRUE(lease);
  458. EXPECT_EQ(200, lease->cltt_);
  459. }
  460. // This test checks that the backend reads DHCPv6 lease data from multiple
  461. // files.
  462. TEST_F(MemfileLeaseMgrTest, load6MultipleLeaseFiles) {
  463. LeaseFileIO io2("leasefile6_0.csv.2");
  464. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  465. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
  466. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  467. "200,200,8,100,0,7,0,1,1,,\n"
  468. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  469. "200,200,8,100,0,7,0,1,1,,\n");
  470. LeaseFileIO io1("leasefile6_0.csv.1");
  471. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  472. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
  473. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  474. "200,200,8,100,0,7,0,1,1,,\n"
  475. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  476. "300,800,8,100,0,7,0,1,1,,\n"
  477. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  478. "200,200,8,100,0,7,0,1,1,,\n");
  479. LeaseFileIO io("leasefile6_0.csv");
  480. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  481. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
  482. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  483. "400,1000,8,100,0,7,0,1,1,,\n"
  484. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  485. "200,200,8,100,0,7,0,1,1,,\n");
  486. startBackend(V6);
  487. // This lease only exists in the first file and the cltt should be 0.
  488. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
  489. IOAddress("2001:db8:1::1"));
  490. ASSERT_TRUE(lease);
  491. EXPECT_EQ(0, lease->cltt_);
  492. // This lease exists in the first and second file and the cltt should
  493. // be calculated using the expiration time and the valid lifetime
  494. // from the second file.
  495. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  496. ASSERT_TRUE(lease);
  497. EXPECT_EQ(500, lease->cltt_);
  498. // This lease only exists in the second file and the cltt should be 0.
  499. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
  500. ASSERT_TRUE(lease);
  501. EXPECT_EQ(0, lease->cltt_);
  502. // This lease exists in the second and third file and the cltt should
  503. // be calculated using the expiration time and the valid lifetime
  504. // from the third file.
  505. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  506. ASSERT_TRUE(lease);
  507. EXPECT_EQ(600, lease->cltt_);
  508. // This lease only exists in the third file and the cltt should be 0.
  509. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  510. ASSERT_TRUE(lease);
  511. EXPECT_EQ(0, lease->cltt_);
  512. }
  513. }; // end of anonymous namespace