memfile_lease_mgr_unittest.cc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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 <util/pid_file.h>
  25. #include <gtest/gtest.h>
  26. #include <boost/bind.hpp>
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <fstream>
  30. #include <queue>
  31. #include <sstream>
  32. #include <unistd.h>
  33. using namespace std;
  34. using namespace isc;
  35. using namespace isc::asiolink;
  36. using namespace isc::dhcp;
  37. using namespace isc::dhcp::test;
  38. using namespace isc::util;
  39. namespace {
  40. /// @brief Class derived from @c Memfile_LeaseMgr to test LFC timer.
  41. ///
  42. /// This class provides a custom callback function which is invoked
  43. /// when the timer for Lease File Cleanup goes off. It is used to
  44. /// test that the timer is correctly installed.
  45. class LFCMemfileLeaseMgr : public Memfile_LeaseMgr {
  46. public:
  47. /// @brief Constructor.
  48. ///
  49. /// Sets the counter for callbacks to 0.
  50. LFCMemfileLeaseMgr(const ParameterMap& parameters)
  51. : Memfile_LeaseMgr(parameters), lfc_cnt_(0) {
  52. }
  53. /// @brief Returns the number of callback executions.
  54. int getLFCCount() {
  55. return (lfc_cnt_);
  56. }
  57. protected:
  58. /// @brief Custom callback.
  59. ///
  60. /// This callback function increases the counter of callback executions.
  61. /// By examining the counter value a test may verify that the callback
  62. /// was triggered an expected number of times.
  63. virtual void lfcCallback() {
  64. ++lfc_cnt_;
  65. }
  66. private:
  67. /// @brief Counter of callback function executions.
  68. int lfc_cnt_;
  69. };
  70. /// @brief A derivation of the lease manager exposing protected methods.
  71. class NakedMemfileLeaseMgr : public Memfile_LeaseMgr {
  72. public:
  73. /// @brief Constructor.
  74. ///
  75. /// Creates instance of the lease manager.
  76. NakedMemfileLeaseMgr(const ParameterMap& parameters)
  77. : Memfile_LeaseMgr(parameters) {
  78. }
  79. using Memfile_LeaseMgr::lfcCallback;
  80. };
  81. /// @brief Test fixture class for @c Memfile_LeaseMgr
  82. class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
  83. public:
  84. /// @brief memfile lease mgr test constructor
  85. ///
  86. /// Creates memfile and stores it in lmptr_ pointer
  87. MemfileLeaseMgrTest() :
  88. io4_(getLeaseFilePath("leasefile4_0.csv")),
  89. io6_(getLeaseFilePath("leasefile6_0.csv")),
  90. io_service_(),
  91. fail_on_callback_(false) {
  92. std::ostringstream s;
  93. s << KEA_LFC_BUILD_DIR << "/kea-lfc";
  94. setenv("KEA_LFC_EXECUTABLE", s.str().c_str(), 1);
  95. // Remove lease files and products of Lease File Cleanup.
  96. removeFiles(getLeaseFilePath("leasefile4_0.csv"));
  97. removeFiles(getLeaseFilePath("leasefile6_0.csv"));
  98. }
  99. /// @brief Reopens the connection to the backend.
  100. ///
  101. /// This function is called by unit tests to force reconnection of the
  102. /// backend to check that the leases are stored and can be retrieved
  103. /// from the storage.
  104. ///
  105. /// @param u Universe (V4 or V6)
  106. virtual void reopen(Universe u) {
  107. LeaseMgrFactory::destroy();
  108. startBackend(u);
  109. }
  110. /// @brief destructor
  111. ///
  112. /// destroys lease manager backend.
  113. virtual ~MemfileLeaseMgrTest() {
  114. LeaseMgrFactory::destroy();
  115. // Remove lease files and products of Lease File Cleanup.
  116. removeFiles(getLeaseFilePath("leasefile4_0.csv"));
  117. removeFiles(getLeaseFilePath("leasefile6_0.csv"));
  118. }
  119. /// @brief Remove files being products of Lease File Cleanup.
  120. ///
  121. /// @param base_name Path to the lease file name. This file is removed
  122. /// and all files which names are crated from this name (having specific
  123. /// suffixes used by Lease File Cleanup mechanism).
  124. void removeFiles(const std::string& base_name) const {
  125. // Generate suffixes and append them to the base name. The
  126. // resulting file names are the ones that may exist as a
  127. // result of LFC.
  128. for (int i = static_cast<int>(Memfile_LeaseMgr::FILE_CURRENT);
  129. i <= static_cast<int>(Memfile_LeaseMgr::FILE_FINISH);
  130. ++i) {
  131. Memfile_LeaseMgr::LFCFileType type = static_cast<
  132. Memfile_LeaseMgr::LFCFileType>(i);
  133. LeaseFileIO io(Memfile_LeaseMgr::appendSuffix(base_name, type));
  134. io.removeFile();
  135. }
  136. }
  137. /// @brief Return path to the lease file used by unit tests.
  138. ///
  139. /// @param filename Name of the lease file appended to the path to the
  140. /// directory where test data is held.
  141. ///
  142. /// @return Full path to the lease file.
  143. static std::string getLeaseFilePath(const std::string& filename) {
  144. std::ostringstream s;
  145. s << TEST_DATA_BUILDDIR << "/" << filename;
  146. return (s.str());
  147. }
  148. /// @brief Returns the configuration string for the backend.
  149. ///
  150. /// This string configures the @c LeaseMgrFactory to create the memfile
  151. /// backend and use leasefile4_0.csv and leasefile6_0.csv files as
  152. /// storage for leases.
  153. ///
  154. /// @param no_universe Indicates whether universe parameter should be
  155. /// included (false), or not (true).
  156. ///
  157. /// @return Configuration string for @c LeaseMgrFactory.
  158. static std::string getConfigString(Universe u) {
  159. std::ostringstream s;
  160. s << "type=memfile " << (u == V4 ? "universe=4 " : "universe=6 ")
  161. << "name="
  162. << getLeaseFilePath(u == V4 ? "leasefile4_0.csv" : "leasefile6_0.csv");
  163. return (s.str());
  164. }
  165. /// @brief Creates instance of the backend.
  166. ///
  167. /// @param u Universe (v4 or V6).
  168. void startBackend(Universe u) {
  169. try {
  170. LeaseMgrFactory::create(getConfigString(u));
  171. } catch (...) {
  172. std::cerr << "*** ERROR: unable to create instance of the Memfile\n"
  173. " lease database backend.\n";
  174. throw;
  175. }
  176. lmptr_ = &(LeaseMgrFactory::instance());
  177. }
  178. void setTestTime(const uint32_t ms) {
  179. IntervalTimer::Callback cb =
  180. boost::bind(&MemfileLeaseMgrTest::testTimerCallback, this);
  181. test_timer_.reset(new IntervalTimer(*io_service_));
  182. test_timer_->setup(cb, ms, IntervalTimer::ONE_SHOT);
  183. }
  184. /// @brief Test timer callback function.
  185. void testTimerCallback() {
  186. io_service_->stop();
  187. if (fail_on_callback_) {
  188. FAIL() << "Test timeout reached";
  189. }
  190. }
  191. /// @brief Waits for the specified process to finish.
  192. ///
  193. /// @param process An object which started the process.
  194. /// @param timeout Timeout in seconds.
  195. ///
  196. /// @return true if the process ended, false otherwise
  197. bool waitForProcess(const Memfile_LeaseMgr& lease_mgr,
  198. const uint8_t timeout) {
  199. uint32_t iterations = 0;
  200. const uint32_t iterations_max = timeout * 1000;
  201. while (lease_mgr.isLFCRunning() && (iterations < iterations_max)) {
  202. usleep(1000);
  203. ++iterations;
  204. }
  205. return (iterations < iterations_max);
  206. }
  207. /// @brief Object providing access to v4 lease IO.
  208. LeaseFileIO io4_;
  209. /// @brief Object providing access to v6 lease IO.
  210. LeaseFileIO io6_;
  211. /// @brief Test timer for the test.
  212. boost::shared_ptr<IntervalTimer> test_timer_;
  213. /// @brief IO service object used for the timer tests.
  214. asiolink::IOServicePtr io_service_;
  215. /// @brief Indicates if the @c testTimerCallback should cause test failure.
  216. bool fail_on_callback_;
  217. };
  218. // This test checks if the LeaseMgr can be instantiated and that it
  219. // parses parameters string properly.
  220. TEST_F(MemfileLeaseMgrTest, constructor) {
  221. LeaseMgr::ParameterMap pmap;
  222. pmap["universe"] = "4";
  223. pmap["persist"] = "false";
  224. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
  225. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  226. pmap["lfc-interval"] = "10";
  227. pmap["persist"] = "true";
  228. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  229. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  230. // Expecting that persist parameter is yes or no. Everything other than
  231. // that is wrong.
  232. pmap["persist"] = "bogus";
  233. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  234. EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
  235. // The lfc-interval must be an integer.
  236. pmap["persist"] = "true";
  237. pmap["lfc-interval"] = "bogus";
  238. EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
  239. }
  240. // Checks if the getType() and getName() methods both return "memfile".
  241. TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
  242. startBackend(V4);
  243. EXPECT_EQ(std::string("memfile"), lmptr_->getType());
  244. EXPECT_EQ(std::string("memory"), lmptr_->getName());
  245. }
  246. // Checks if the path to the lease files is initialized correctly.
  247. TEST_F(MemfileLeaseMgrTest, getLeaseFilePath) {
  248. // Initialize IO objects, so as the test csv files get removed after the
  249. // test (when destructors are called).
  250. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  251. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  252. LeaseMgr::ParameterMap pmap;
  253. pmap["universe"] = "4";
  254. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  255. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  256. EXPECT_EQ(pmap["name"],
  257. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  258. pmap["persist"] = "false";
  259. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  260. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4).empty());
  261. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6).empty());
  262. }
  263. // Check if the persitLeases correctly checks that leases should not be written
  264. // to disk when disabled through configuration.
  265. TEST_F(MemfileLeaseMgrTest, persistLeases) {
  266. // Initialize IO objects, so as the test csv files get removed after the
  267. // test (when destructors are called).
  268. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  269. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  270. LeaseMgr::ParameterMap pmap;
  271. pmap["universe"] = "4";
  272. // Specify the names of the lease files. Leases will be written.
  273. pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
  274. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  275. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  276. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  277. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  278. pmap["universe"] = "6";
  279. pmap["name"] = getLeaseFilePath("leasefile6_1.csv");
  280. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  281. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  282. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  283. // This should disable writes of leases to disk.
  284. pmap["persist"] = "false";
  285. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  286. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  287. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  288. }
  289. // Check if it is possible to schedule the timer to perform the Lease
  290. // File Cleanup periodically.
  291. TEST_F(MemfileLeaseMgrTest, lfcTimer) {
  292. LeaseMgr::ParameterMap pmap;
  293. pmap["type"] = "memfile";
  294. pmap["universe"] = "4";
  295. // Specify the names of the lease files. Leases will be written.
  296. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  297. pmap["lfc-interval"] = "1";
  298. boost::scoped_ptr<LFCMemfileLeaseMgr>
  299. lease_mgr(new LFCMemfileLeaseMgr(pmap));
  300. // Check that the interval is correct.
  301. EXPECT_EQ(1, lease_mgr->getIOServiceExecInterval());
  302. io_service_ = lease_mgr->getIOService();
  303. // Run the test for at most 2.9 seconds.
  304. setTestTime(2900);
  305. // Run the IO service to execute timers.
  306. io_service_->run();
  307. // Within 2.9 we should record two LFC executions.
  308. EXPECT_EQ(2, lease_mgr->getLFCCount());
  309. }
  310. // This test checks if the LFC timer is disabled (doesn't trigger)
  311. // cleanups when the lfc-interval is set to 0.
  312. TEST_F(MemfileLeaseMgrTest, lfcTimerDisabled) {
  313. LeaseMgr::ParameterMap pmap;
  314. pmap["type"] = "memfile";
  315. pmap["universe"] = "4";
  316. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  317. // Set the LFC interval to 0, which should effectively disable it.
  318. pmap["lfc-interval"] = "0";
  319. boost::scoped_ptr<LFCMemfileLeaseMgr>
  320. lease_mgr(new LFCMemfileLeaseMgr(pmap));
  321. EXPECT_EQ(0, lease_mgr->getIOServiceExecInterval());
  322. io_service_ = lease_mgr->getIOService();
  323. // Run the test for at most 1.9 seconds.
  324. setTestTime(1900);
  325. // Run the IO service to execute timers.
  326. io_service_->run();
  327. // There should be no LFC execution recorded.
  328. EXPECT_EQ(0, lease_mgr->getLFCCount());
  329. }
  330. // This test checks that the callback function executing the cleanup of the
  331. // DHCPv4 lease file works as expected.
  332. TEST_F(MemfileLeaseMgrTest, leaseFileCleanup4) {
  333. // This string contains the lease file header, which matches
  334. // the contents of the new file in which no leases have been
  335. // stored.
  336. std::string new_file_contents =
  337. "address,hwaddr,client_id,valid_lifetime,expire,"
  338. "subnet_id,fqdn_fwd,fqdn_rev,hostname,state\n";
  339. // This string contains the contents of the lease file with exactly
  340. // one lease, but two entries. One of the entries should be removed
  341. // as a result of lease file cleanup.
  342. std::string current_file_contents = new_file_contents +
  343. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n"
  344. "192.0.2.2,02:02:02:02:02:02,,200,800,8,1,1,,1\n";
  345. LeaseFileIO current_file(getLeaseFilePath("leasefile4_0.csv"));
  346. current_file.writeFile(current_file_contents);
  347. std::string previous_file_contents = new_file_contents +
  348. "192.0.2.3,03:03:03:03:03:03,,200,200,8,1,1,,1\n"
  349. "192.0.2.3,03:03:03:03:03:03,,200,800,8,1,1,,1\n";
  350. LeaseFileIO previous_file(getLeaseFilePath("leasefile4_0.csv.2"));
  351. previous_file.writeFile(previous_file_contents);
  352. // Create the backend.
  353. LeaseMgr::ParameterMap pmap;
  354. pmap["type"] = "memfile";
  355. pmap["universe"] = "4";
  356. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  357. pmap["lfc-interval"] = "1";
  358. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
  359. // Try to run the lease file cleanup.
  360. ASSERT_NO_THROW(lease_mgr->lfcCallback());
  361. // The new lease file should have been created and it should contain
  362. // no leases.
  363. ASSERT_TRUE(current_file.exists());
  364. EXPECT_EQ(new_file_contents, current_file.readFile());
  365. // Wait for the LFC process to complete.
  366. ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
  367. // And make sure it has returned an exit status of 0.
  368. EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
  369. << "Executing the LFC process failed: make sure that"
  370. " the kea-lfc program has been compiled.";
  371. // Check if we can still write to the lease file.
  372. std::vector<uint8_t> hwaddr_vec(6);
  373. HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
  374. Lease4Ptr new_lease(new Lease4(IOAddress("192.0.2.45"), hwaddr, 0, 0,
  375. 100, 50, 60, 0, 1));
  376. ASSERT_NO_THROW(lease_mgr->addLease(new_lease));
  377. std::string updated_file_contents = new_file_contents +
  378. "192.0.2.45,00:00:00:00:00:00,,100,100,1,0,0,,0\n";
  379. EXPECT_EQ(updated_file_contents, current_file.readFile());
  380. // This string contains the contents of the lease file we
  381. // expect after the LFC run. It has two leases with one
  382. // entry each.
  383. std::string result_file_contents = new_file_contents +
  384. "192.0.2.2,02:02:02:02:02:02,,200,800,8,1,1,,1\n"
  385. "192.0.2.3,03:03:03:03:03:03,,200,800,8,1,1,,1\n";
  386. // The LFC should have created a file with the two leases and moved it
  387. // to leasefile4_0.csv.2
  388. LeaseFileIO input_file(getLeaseFilePath("leasefile4_0.csv.2"), false);
  389. ASSERT_TRUE(input_file.exists());
  390. // And this file should contain the contents of the result file.
  391. EXPECT_EQ(result_file_contents, input_file.readFile());
  392. }
  393. // This test checks that the callback function executing the cleanup of the
  394. // DHCPv6 lease file works as expected.
  395. TEST_F(MemfileLeaseMgrTest, leaseFileCleanup6) {
  396. // This string contains the lease file header, which matches
  397. // the contents of the new file in which no leases have been
  398. // stored.
  399. std::string new_file_contents =
  400. "address,duid,valid_lifetime,expire,subnet_id,"
  401. "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
  402. "fqdn_rev,hostname,hwaddr,state\n";
  403. // This string contains the contents of the lease file with exactly
  404. // one lease, but two entries. One of the entries should be removed
  405. // as a result of lease file cleanup.
  406. std::string current_file_contents = new_file_contents +
  407. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,200,"
  408. "8,100,0,7,0,1,1,,,1\n"
  409. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
  410. "8,100,0,7,0,1,1,,,1\n";
  411. LeaseFileIO current_file(getLeaseFilePath("leasefile6_0.csv"));
  412. current_file.writeFile(current_file_contents);
  413. std::string previous_file_contents = new_file_contents +
  414. "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,200,"
  415. "8,100,0,7,0,1,1,,,1\n"
  416. "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
  417. "8,100,0,7,0,1,1,,,1\n";
  418. LeaseFileIO previous_file(getLeaseFilePath("leasefile6_0.csv.2"));
  419. previous_file.writeFile(previous_file_contents);
  420. // Create the backend.
  421. LeaseMgr::ParameterMap pmap;
  422. pmap["type"] = "memfile";
  423. pmap["universe"] = "6";
  424. pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
  425. pmap["lfc-interval"] = "1";
  426. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
  427. // Try to run the lease file cleanup.
  428. ASSERT_NO_THROW(lease_mgr->lfcCallback());
  429. // The new lease file should have been created and it should contain
  430. // no leases.
  431. ASSERT_TRUE(current_file.exists());
  432. EXPECT_EQ(new_file_contents, current_file.readFile());
  433. // Wait for the LFC process to complete.
  434. ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
  435. // And make sure it has returned an exit status of 0.
  436. EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
  437. << "Executing the LFC process failed: make sure that"
  438. " the kea-lfc program has been compiled.";
  439. // Check if we can still write to the lease file.
  440. std::vector<uint8_t> duid_vec(13);
  441. DuidPtr duid(new DUID(duid_vec));
  442. Lease6Ptr new_lease(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),duid,
  443. 123, 300, 400, 100, 300, 2));
  444. new_lease->cltt_ = 0;
  445. ASSERT_NO_THROW(lease_mgr->addLease(new_lease));
  446. std::string update_file_contents = new_file_contents +
  447. "3000::1,00:00:00:00:00:00:00:00:00:00:00:00:00,400,"
  448. "400,2,300,0,123,128,0,0,,,0\n";
  449. EXPECT_EQ(update_file_contents, current_file.readFile());
  450. // This string contains the contents of the lease file we
  451. // expect after the LFC run. It has two leases with one
  452. // entry each.
  453. std::string result_file_contents = new_file_contents +
  454. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
  455. "8,100,0,7,0,1,1,,,1\n"
  456. "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
  457. "8,100,0,7,0,1,1,,,1\n";
  458. // The LFC should have created a file with the two leases and moved it
  459. // to leasefile6_0.csv.2
  460. LeaseFileIO input_file(getLeaseFilePath("leasefile6_0.csv.2"), false);
  461. ASSERT_TRUE(input_file.exists());
  462. // And this file should contain the contents of the result file.
  463. EXPECT_EQ(result_file_contents, input_file.readFile());
  464. }
  465. // This test verifies that EXIT_FAILURE status code is returned when
  466. // the LFC process fails to start.
  467. TEST_F(MemfileLeaseMgrTest, leaseFileCleanupStartFail) {
  468. // This string contains the lease file header, which matches
  469. // the contents of the new file in which no leases have been
  470. // stored.
  471. std::string new_file_contents =
  472. "address,hwaddr,client_id,valid_lifetime,expire,"
  473. "subnet_id,fqdn_fwd,fqdn_rev,hostname,state\n";
  474. // Create the lease file to be used by the backend.
  475. std::string current_file_contents = new_file_contents +
  476. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n";
  477. LeaseFileIO current_file(getLeaseFilePath("leasefile4_0.csv"));
  478. current_file.writeFile(current_file_contents);
  479. // Specify invalid path to the kea-lfc executable. This should result
  480. // in failure status code returned by the child process.
  481. setenv("KEA_LFC_EXECUTABLE", "foobar", 1);
  482. // Create the backend.
  483. LeaseMgr::ParameterMap pmap;
  484. pmap["type"] = "memfile";
  485. pmap["universe"] = "4";
  486. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  487. pmap["lfc-interval"] = "1";
  488. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
  489. // Try to run the lease file cleanup.
  490. ASSERT_NO_THROW(lease_mgr->lfcCallback());
  491. // Wait for the LFC process to complete.
  492. ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
  493. // And make sure it has returned an error.
  494. EXPECT_EQ(EXIT_FAILURE, lease_mgr->getLFCExitStatus())
  495. << "Executing the LFC process failed: make sure that"
  496. " the kea-lfc program has been compiled.";
  497. }
  498. // This test checks that the callback function executing the cleanup of the
  499. // files doesn't move the current file if the finish file exists
  500. TEST_F(MemfileLeaseMgrTest, leaseFileFinish) {
  501. // This string contains the lease file header, which matches
  502. // the contents of the new file in which no leases have been
  503. // stored.
  504. std::string new_file_contents =
  505. "address,duid,valid_lifetime,expire,subnet_id,"
  506. "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
  507. "fqdn_rev,hostname,hwaddr,state\n";
  508. // This string contains the contents of the current lease file.
  509. // It should not be moved.
  510. std::string current_file_contents = new_file_contents +
  511. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,200,"
  512. "8,100,0,7,0,1,1,,,1\n"
  513. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
  514. "8,100,0,7,0,1,1,,,1\n";
  515. LeaseFileIO current_file(getLeaseFilePath("leasefile6_0.csv"));
  516. current_file.writeFile(current_file_contents);
  517. // This string contains the contents of the finish file. It should
  518. // be moved to the previous file.
  519. std::string finish_file_contents = new_file_contents +
  520. "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
  521. "8,100,0,7,0,1,1,,,1\n";
  522. LeaseFileIO finish_file(getLeaseFilePath("leasefile6_0.csv.completed"));
  523. finish_file.writeFile(finish_file_contents);
  524. // Create the backend.
  525. LeaseMgr::ParameterMap pmap;
  526. pmap["type"] = "memfile";
  527. pmap["universe"] = "6";
  528. pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
  529. pmap["lfc-interval"] = "1";
  530. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
  531. // Try to run the lease file cleanup.
  532. ASSERT_NO_THROW(lease_mgr->lfcCallback());
  533. // The current lease file should not have been touched.
  534. ASSERT_TRUE(current_file.exists());
  535. EXPECT_EQ(current_file_contents, current_file.readFile());
  536. // Wait for the LFC process to complete.
  537. ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
  538. // And make sure it has returned an exit status of 0.
  539. EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
  540. << "Executing the LFC process failed: make sure that"
  541. " the kea-lfc program has been compiled.";
  542. // The LFC should have moved the finish file to the previous file -
  543. // leasefile6_0.csv.2
  544. LeaseFileIO previous_file(getLeaseFilePath("leasefile6_0.csv.2"), false);
  545. ASSERT_TRUE(previous_file.exists());
  546. // And this file should contain the contents of the finish file.
  547. EXPECT_EQ(finish_file_contents, previous_file.readFile());
  548. // The finish file should have been removed
  549. ASSERT_FALSE(finish_file.exists());
  550. }
  551. // This test checks that the callback function executing the cleanup of the
  552. // files doesn't move the current file if the copy file exists
  553. TEST_F(MemfileLeaseMgrTest, leaseFileCopy) {
  554. // This string contains the lease file header, which matches
  555. // the contents of the new file in which no leases have been
  556. // stored.
  557. std::string new_file_contents =
  558. "address,duid,valid_lifetime,expire,subnet_id,"
  559. "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
  560. "fqdn_rev,hostname,hwaddr,state\n";
  561. // This string contains the contents of the current lease file.
  562. // It should not be moved.
  563. std::string current_file_contents = new_file_contents +
  564. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,200,"
  565. "8,100,0,7,0,1,1,,,1\n"
  566. "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
  567. "8,100,0,7,0,1,1,,,1\n";
  568. LeaseFileIO current_file(getLeaseFilePath("leasefile6_0.csv"));
  569. current_file.writeFile(current_file_contents);
  570. // This string contains the contents of the copy file. It should
  571. // be processed and moved to the previous file. As there is only
  572. // one lease the processing should result in the previous file being
  573. // the same.
  574. std::string input_file_contents = new_file_contents +
  575. "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
  576. "8,100,0,7,0,1,1,,,1\n";
  577. LeaseFileIO input_file(getLeaseFilePath("leasefile6_0.csv.1"));
  578. input_file.writeFile(input_file_contents);
  579. // Create the backend.
  580. LeaseMgr::ParameterMap pmap;
  581. pmap["type"] = "memfile";
  582. pmap["universe"] = "6";
  583. pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
  584. pmap["lfc-interval"] = "1";
  585. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
  586. // Try to run the lease file cleanup.
  587. ASSERT_NO_THROW(lease_mgr->lfcCallback());
  588. // The current lease file should not have been touched.
  589. ASSERT_TRUE(current_file.exists());
  590. EXPECT_EQ(current_file_contents, current_file.readFile());
  591. // Wait for the LFC process to complete.
  592. ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
  593. // And make sure it has returned an exit status of 0.
  594. EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
  595. << "Executing the LFC process failed: make sure that"
  596. " the kea-lfc program has been compiled.";
  597. // The LFC should have processed the lease and moved it to the previous
  598. // file - leasefile6_0.csv.2
  599. LeaseFileIO previous_file(getLeaseFilePath("leasefile6_0.csv.2"), false);
  600. ASSERT_TRUE(previous_file.exists());
  601. // And this file should contain the contents of the copy file.
  602. EXPECT_EQ(input_file_contents, previous_file.readFile());
  603. // The input file should have been removed
  604. ASSERT_FALSE(input_file.exists());
  605. }
  606. // Test that the backend returns a correct value of the interval
  607. // at which the IOService must be executed to run the handlers
  608. // for the installed timers.
  609. TEST_F(MemfileLeaseMgrTest, getIOServiceExecInterval) {
  610. LeaseMgr::ParameterMap pmap;
  611. pmap["type"] = "memfile";
  612. pmap["universe"] = "4";
  613. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  614. // The lfc-interval is not set, so the returned value should be 0.
  615. boost::scoped_ptr<LFCMemfileLeaseMgr> lease_mgr(new LFCMemfileLeaseMgr(pmap));
  616. EXPECT_EQ(0, lease_mgr->getIOServiceExecInterval());
  617. // lfc-interval = 10
  618. pmap["lfc-interval"] = "10";
  619. lease_mgr.reset();
  620. lease_mgr.reset(new LFCMemfileLeaseMgr(pmap));
  621. EXPECT_EQ(10, lease_mgr->getIOServiceExecInterval());
  622. // lfc-interval = 20
  623. pmap["lfc-interval"] = "20";
  624. lease_mgr.reset();
  625. lease_mgr.reset(new LFCMemfileLeaseMgr(pmap));
  626. EXPECT_EQ(20, lease_mgr->getIOServiceExecInterval());
  627. }
  628. // Checks that adding/getting/deleting a Lease6 object works.
  629. TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
  630. startBackend(V6);
  631. testAddGetDelete6(true); // true - check T1,T2 values
  632. // memfile is able to preserve those values, but some other
  633. // backends can't do that.
  634. }
  635. /// @brief Basic Lease4 Checks
  636. ///
  637. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  638. /// IPv4 address) works.
  639. TEST_F(MemfileLeaseMgrTest, basicLease4) {
  640. startBackend(V4);
  641. testBasicLease4();
  642. }
  643. /// @todo Write more memfile tests
  644. // Simple test about lease4 retrieval through client id method
  645. TEST_F(MemfileLeaseMgrTest, getLease4ClientId) {
  646. startBackend(V4);
  647. testGetLease4ClientId();
  648. }
  649. // Checks that lease4 retrieval client id is null is working
  650. TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) {
  651. startBackend(V4);
  652. testGetLease4NullClientId();
  653. }
  654. // Checks lease4 retrieval through HWAddr
  655. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr1) {
  656. startBackend(V4);
  657. testGetLease4HWAddr1();
  658. }
  659. /// @brief Check GetLease4 methods - access by Hardware Address
  660. ///
  661. /// Adds leases to the database and checks that they can be accessed via
  662. /// a combination of DUID and IAID.
  663. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr2) {
  664. startBackend(V4);
  665. testGetLease4HWAddr2();
  666. }
  667. // Checks lease4 retrieval with clientId, HWAddr and subnet_id
  668. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) {
  669. startBackend(V4);
  670. testGetLease4ClientIdHWAddrSubnetId();
  671. }
  672. /// @brief Basic Lease4 Checks
  673. ///
  674. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  675. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  676. /// (client-id is optional and may not be present)
  677. TEST_F(MemfileLeaseMgrTest, lease4NullClientId) {
  678. startBackend(V4);
  679. testLease4NullClientId();
  680. }
  681. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  682. ///
  683. /// Adds leases to the database and checks that they can be accessed via
  684. /// a combination of hardware address and subnet ID
  685. TEST_F(MemfileLeaseMgrTest, DISABLED_getLease4HwaddrSubnetId) {
  686. /// @todo: fails on memfile. It's probably a memfile bug.
  687. startBackend(V4);
  688. testGetLease4HWAddrSubnetId();
  689. }
  690. /// @brief Check GetLease4 methods - access by Client ID
  691. ///
  692. /// Adds leases to the database and checks that they can be accessed via
  693. /// the Client ID.
  694. TEST_F(MemfileLeaseMgrTest, getLease4ClientId2) {
  695. startBackend(V4);
  696. testGetLease4ClientId2();
  697. }
  698. // @brief Get Lease4 by client ID
  699. //
  700. // Check that the system can cope with a client ID of any size.
  701. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSize) {
  702. startBackend(V4);
  703. testGetLease4ClientIdSize();
  704. }
  705. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  706. ///
  707. /// Adds leases to the database and checks that they can be accessed via
  708. /// a combination of client and subnet IDs.
  709. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSubnetId) {
  710. startBackend(V4);
  711. testGetLease4ClientIdSubnetId();
  712. }
  713. /// @brief Basic Lease6 Checks
  714. ///
  715. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  716. /// IPv6 address) works.
  717. TEST_F(MemfileLeaseMgrTest, basicLease6) {
  718. startBackend(V6);
  719. testBasicLease6();
  720. }
  721. /// @brief Check GetLease6 methods - access by DUID/IAID
  722. ///
  723. /// Adds leases to the database and checks that they can be accessed via
  724. /// a combination of DUID and IAID.
  725. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  726. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  727. TEST_F(MemfileLeaseMgrTest, getLeases6DuidIaid) {
  728. startBackend(V6);
  729. testGetLeases6DuidIaid();
  730. }
  731. /// @brief Check that the system can cope with a DUID of allowed size.
  732. TEST_F(MemfileLeaseMgrTest, getLeases6DuidSize) {
  733. startBackend(V6);
  734. testGetLeases6DuidSize();
  735. }
  736. /// @brief Check that the expired DHCPv4 leases can be retrieved.
  737. ///
  738. /// This test adds a number of leases to the lease database and marks
  739. /// some of them as expired. Then it queries for expired leases and checks
  740. /// whether only expired leases are returned, and that they are returned in
  741. /// the order from most to least expired. It also checks that the lease
  742. /// which is marked as 'reclaimed' is not returned.
  743. TEST_F(MemfileLeaseMgrTest, getExpiredLeases4) {
  744. startBackend(V4);
  745. testGetExpiredLeases4();
  746. }
  747. /// @brief Check that the expired DHCPv6 leases can be retrieved.
  748. ///
  749. /// This test adds a number of leases to the lease database and marks
  750. /// some of them as expired. Then it queries for expired leases and checks
  751. /// whether only expired leases are returned, and that they are returned in
  752. /// the order from most to least expired. It also checks that the lease
  753. /// which is marked as 'reclaimed' is not returned.
  754. TEST_F(MemfileLeaseMgrTest, getExpiredLeases6) {
  755. startBackend(V6);
  756. testGetExpiredLeases6();
  757. }
  758. /// @brief Check that expired reclaimed DHCPv6 leases are removed.
  759. TEST_F(MemfileLeaseMgrTest, deleteExpiredReclaimedLeases6) {
  760. startBackend(V6);
  761. testDeleteExpiredReclaimedLeases6();
  762. }
  763. /// @brief Check that expired reclaimed DHCPv4 leases are removed.
  764. TEST_F(MemfileLeaseMgrTest, deleteExpiredReclaimedLeases4) {
  765. startBackend(V4);
  766. testDeleteExpiredReclaimedLeases4();
  767. }
  768. /// @brief Check that getLease6 methods discriminate by lease type.
  769. ///
  770. /// Adds six leases, two per lease type all with the same duid and iad but
  771. /// with alternating subnet_ids.
  772. /// It then verifies that all of getLeases6() method variants correctly
  773. /// discriminate between the leases based on lease type alone.
  774. /// @todo: Disabled, because type parameter in Memfile_LeaseMgr::getLease6
  775. /// (Lease::Type, const isc::asiolink::IOAddress& addr) const is not used.
  776. TEST_F(MemfileLeaseMgrTest, lease6LeaseTypeCheck) {
  777. startBackend(V6);
  778. testLease6LeaseTypeCheck();
  779. }
  780. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  781. ///
  782. /// Adds leases to the database and checks that they can be accessed via
  783. /// a combination of DIUID and IAID.
  784. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetId) {
  785. startBackend(V6);
  786. testGetLease6DuidIaidSubnetId();
  787. }
  788. /// Checks that getLease6(type, duid, iaid, subnet-id) works with different
  789. /// DUID sizes
  790. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  791. startBackend(V6);
  792. testGetLease6DuidIaidSubnetIdSize();
  793. }
  794. /// @brief Lease4 update tests
  795. ///
  796. /// Checks that we are able to update a lease in the database.
  797. /// @todo: Disabled, because memfile does not throw when lease is updated.
  798. /// We should reconsider if lease{4,6} structures should have a limit
  799. /// implemented in them.
  800. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease4) {
  801. startBackend(V4);
  802. testUpdateLease4();
  803. }
  804. /// @brief Lease6 update tests
  805. ///
  806. /// Checks that we are able to update a lease in the database.
  807. /// @todo: Disabled, because memfile does not throw when lease is updated.
  808. /// We should reconsider if lease{4,6} structures should have a limit
  809. /// implemented in them.
  810. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease6) {
  811. startBackend(V6);
  812. testUpdateLease6();
  813. }
  814. /// @brief DHCPv4 Lease recreation tests
  815. ///
  816. /// Checks that the lease can be created, deleted and recreated with
  817. /// different parameters. It also checks that the re-created lease is
  818. /// correctly stored in the lease database.
  819. TEST_F(MemfileLeaseMgrTest, testRecreateLease4) {
  820. startBackend(V4);
  821. testRecreateLease4();
  822. }
  823. /// @brief DHCPv6 Lease recreation tests
  824. ///
  825. /// Checks that the lease can be created, deleted and recreated with
  826. /// different parameters. It also checks that the re-created lease is
  827. /// correctly stored in the lease database.
  828. TEST_F(MemfileLeaseMgrTest, testRecreateLease6) {
  829. startBackend(V6);
  830. testRecreateLease6();
  831. }
  832. // The following tests are not applicable for memfile. When adding
  833. // new tests to the list here, make sure to provide brief explanation
  834. // why they are not applicable:
  835. //
  836. // testGetLease4HWAddrSubnetIdSize() - memfile just keeps Lease structure
  837. // and does not do any checks of HWAddr content
  838. /// @brief Checks that null DUID is not allowed.
  839. /// Test is disabled as Memfile does not currently defend against a null DUID.
  840. TEST_F(MemfileLeaseMgrTest, DISABLED_nullDuid) {
  841. // Create leases, although we need only one.
  842. vector<Lease6Ptr> leases = createLeases6();
  843. leases[1]->duid_.reset();
  844. ASSERT_THROW(lmptr_->addLease(leases[1]), DbOperationError);
  845. }
  846. /// @brief Tests whether memfile can store and retrieve hardware addresses
  847. TEST_F(MemfileLeaseMgrTest, testLease6Mac) {
  848. startBackend(V6);
  849. testLease6MAC();
  850. }
  851. // Check that memfile reports version correctly.
  852. TEST_F(MemfileLeaseMgrTest, versionCheck) {
  853. // Check that V4 backend reports versions correctly.
  854. startBackend(V4);
  855. testVersion(Memfile_LeaseMgr::MAJOR_VERSION,
  856. Memfile_LeaseMgr::MINOR_VERSION);
  857. LeaseMgrFactory::destroy();
  858. // Check that V6 backends reports them ok, too.
  859. startBackend(V6);
  860. testVersion(Memfile_LeaseMgr::MAJOR_VERSION,
  861. Memfile_LeaseMgr::MINOR_VERSION);
  862. LeaseMgrFactory::destroy();
  863. }
  864. // This test checks that the backend reads DHCPv4 lease data from multiple
  865. // files.
  866. TEST_F(MemfileLeaseMgrTest, load4MultipleLeaseFiles) {
  867. LeaseFileIO io2(getLeaseFilePath("leasefile4_0.csv.2"));
  868. io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  869. "fqdn_fwd,fqdn_rev,hostname,state\n"
  870. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n"
  871. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,200,8,1,1,,1\n");
  872. LeaseFileIO io1(getLeaseFilePath("leasefile4_0.csv.1"));
  873. io1.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  874. "fqdn_fwd,fqdn_rev,hostname,state\n"
  875. "192.0.2.1,01:01:01:01:01:01,,200,200,8,1,1,,1\n"
  876. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,400,8,1,1,,1\n"
  877. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,200,8,1,1,,1\n");
  878. LeaseFileIO io(getLeaseFilePath("leasefile4_0.csv"));
  879. io.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  880. "fqdn_fwd,fqdn_rev,hostname,state\n"
  881. "192.0.2.10,0a:0a:0a:0a:0a:0a,,200,200,8,1,1,,1\n"
  882. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,400,8,1,1,,1\n");
  883. startBackend(V4);
  884. // This lease only exists in the second file and the cltt should
  885. // be 0.
  886. Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
  887. ASSERT_TRUE(lease);
  888. EXPECT_EQ(0, lease->cltt_);
  889. // This lease only exists in the first file and the cltt should
  890. // be 0.
  891. lease = lmptr_->getLease4(IOAddress("192.0.2.2"));
  892. ASSERT_TRUE(lease);
  893. EXPECT_EQ(0, lease->cltt_);
  894. // This lease only exists in the third file and the cltt should
  895. // be 0.
  896. lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
  897. ASSERT_TRUE(lease);
  898. EXPECT_EQ(0, lease->cltt_);
  899. // This lease exists in the first and second file and the cltt
  900. // should be calculated using the expiration time and the
  901. // valid lifetime from the second file.
  902. lease = lmptr_->getLease4(IOAddress("192.0.2.11"));
  903. ASSERT_TRUE(lease);
  904. EXPECT_EQ(200, lease->cltt_);
  905. // This lease exists in the second and third file and the cltt
  906. // should be calculated using the expiration time and the
  907. // valid lifetime from the third file.
  908. lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
  909. ASSERT_TRUE(lease);
  910. EXPECT_EQ(200, lease->cltt_);
  911. }
  912. // This test checks that the lease database backend loads the file with
  913. // the .completed postfix instead of files with postfixes .1 and .2 if
  914. // the file with .completed postfix exists.
  915. TEST_F(MemfileLeaseMgrTest, load4CompletedFile) {
  916. LeaseFileIO io2(getLeaseFilePath("leasefile4_0.csv.2"));
  917. io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  918. "fqdn_fwd,fqdn_rev,hostname,state\n"
  919. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n"
  920. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,200,8,1,1,,1\n");
  921. LeaseFileIO io1(getLeaseFilePath("leasefile4_0.csv.1"));
  922. io1.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  923. "fqdn_fwd,fqdn_rev,hostname,state\n"
  924. "192.0.2.1,01:01:01:01:01:01,,200,200,8,1,1,,1\n"
  925. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,400,8,1,1,,1\n"
  926. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,200,8,1,1,,1\n");
  927. LeaseFileIO io(getLeaseFilePath("leasefile4_0.csv"));
  928. io.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  929. "fqdn_fwd,fqdn_rev,hostname,state\n"
  930. "192.0.2.10,0a:0a:0a:0a:0a:0a,,200,200,8,1,1,,1\n"
  931. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,400,8,1,1,,1\n");
  932. LeaseFileIO ioc(getLeaseFilePath("leasefile4_0.csv.completed"));
  933. ioc.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  934. "fqdn_fwd,fqdn_rev,hostname,state\n"
  935. "192.0.2.13,ff:ff:ff:ff:ff:ff,,200,200,8,1,1,,1\n");
  936. startBackend(V4);
  937. // We expect that this file only holds leases that belong to the
  938. // lease file or to the file with .completed postfix.
  939. Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
  940. ASSERT_TRUE(lease);
  941. EXPECT_EQ(0, lease->cltt_);
  942. lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
  943. ASSERT_TRUE(lease);
  944. EXPECT_EQ(200, lease->cltt_);
  945. // This lease is in the .completed file.
  946. lease = lmptr_->getLease4(IOAddress("192.0.2.13"));
  947. ASSERT_TRUE(lease);
  948. EXPECT_EQ(0, lease->cltt_);
  949. // Leases from the .1 and .2 files should not be loaded.
  950. EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.11")));
  951. EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
  952. }
  953. // This test checks that backend constructor refuses to load leases from the
  954. // lease files if the LFC is in progress.
  955. TEST_F(MemfileLeaseMgrTest, load4LFCInProgress) {
  956. // Create the backend configuration.
  957. LeaseMgr::ParameterMap pmap;
  958. pmap["type"] = "memfile";
  959. pmap["universe"] = "4";
  960. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  961. pmap["lfc-interval"] = "1";
  962. // Create a pid file holding the PID of the current process. Choosing the
  963. // pid of the current process guarantees that when the backend starts up
  964. // the process is alive.
  965. PIDFile pid_file(Memfile_LeaseMgr::appendSuffix(pmap["name"], Memfile_LeaseMgr::FILE_PID));
  966. pid_file.write();
  967. // There is a pid file and the process which pid is in the file is
  968. // running, so the backend should refuse to start.
  969. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr;
  970. ASSERT_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)),
  971. DbOpenError);
  972. // Remove the pid file, and retry. The bakckend should be created.
  973. pid_file.deleteFile();
  974. ASSERT_NO_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)));
  975. }
  976. // This test checks that the backend reads DHCPv6 lease data from multiple
  977. // files.
  978. TEST_F(MemfileLeaseMgrTest, load6MultipleLeaseFiles) {
  979. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  980. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  981. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  982. "state\n"
  983. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  984. "200,200,8,100,0,7,0,1,1,,,1\n"
  985. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  986. "200,200,8,100,0,7,0,1,1,,,1\n");
  987. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  988. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  989. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  990. "state\n"
  991. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  992. "200,200,8,100,0,7,0,1,1,,,1\n"
  993. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  994. "300,800,8,100,0,7,0,1,1,,,1\n"
  995. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  996. "200,200,8,100,0,7,0,1,1,,,1\n");
  997. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  998. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  999. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1000. "state\n"
  1001. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1002. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1003. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1004. "200,200,8,100,0,7,0,1,1,,,1\n");
  1005. startBackend(V6);
  1006. // This lease only exists in the first file and the cltt should be 0.
  1007. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
  1008. IOAddress("2001:db8:1::1"));
  1009. ASSERT_TRUE(lease);
  1010. EXPECT_EQ(0, lease->cltt_);
  1011. // This lease exists in the first and second file and the cltt should
  1012. // be calculated using the expiration time and the valid lifetime
  1013. // from the second file.
  1014. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1015. ASSERT_TRUE(lease);
  1016. EXPECT_EQ(500, lease->cltt_);
  1017. // This lease only exists in the second file and the cltt should be 0.
  1018. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
  1019. ASSERT_TRUE(lease);
  1020. EXPECT_EQ(0, lease->cltt_);
  1021. // This lease exists in the second and third file and the cltt should
  1022. // be calculated using the expiration time and the valid lifetime
  1023. // from the third file.
  1024. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1025. ASSERT_TRUE(lease);
  1026. EXPECT_EQ(600, lease->cltt_);
  1027. // This lease only exists in the third file and the cltt should be 0.
  1028. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1029. ASSERT_TRUE(lease);
  1030. EXPECT_EQ(0, lease->cltt_);
  1031. }
  1032. // This test checks that the backend reads DHCPv6 lease data from the
  1033. // leasefile without the postfix and the file with a .1 postfix when
  1034. // the file with the .2 postfix is missing.
  1035. TEST_F(MemfileLeaseMgrTest, load6MultipleNoSecondFile) {
  1036. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  1037. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1038. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1039. "state\n"
  1040. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  1041. "200,200,8,100,0,7,0,1,1,,,1\n"
  1042. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1043. "300,800,8,100,0,7,0,1,1,,,1\n"
  1044. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1045. "200,200,8,100,0,7,0,1,1,,,1\n");
  1046. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1047. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1048. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1049. "state\n"
  1050. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1051. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1052. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1053. "200,200,8,100,0,7,0,1,1,,,1\n");
  1054. startBackend(V6);
  1055. // Check that leases from the leasefile6_0 and leasefile6_0.1 have
  1056. // been loaded.
  1057. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1058. ASSERT_TRUE(lease);
  1059. EXPECT_EQ(500, lease->cltt_);
  1060. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
  1061. ASSERT_TRUE(lease);
  1062. EXPECT_EQ(0, lease->cltt_);
  1063. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1064. ASSERT_TRUE(lease);
  1065. EXPECT_EQ(600, lease->cltt_);
  1066. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1067. ASSERT_TRUE(lease);
  1068. EXPECT_EQ(0, lease->cltt_);
  1069. // Make sure that a lease which is not in those files is not loaded.
  1070. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
  1071. }
  1072. // This test checks that the backend reads DHCPv6 lease data from the
  1073. // leasefile without the postfix and the file with a .2 postfix when
  1074. // the file with the .1 postfix is missing.
  1075. TEST_F(MemfileLeaseMgrTest, load6MultipleNoFirstFile) {
  1076. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  1077. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1078. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1079. "state\n"
  1080. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  1081. "200,200,8,100,0,7,0,1,1,,,1\n"
  1082. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1083. "200,200,8,100,0,7,0,1,1,,,1\n");
  1084. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1085. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1086. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1087. "state\n"
  1088. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1089. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1090. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1091. "200,200,8,100,0,7,0,1,1,,,1\n");
  1092. startBackend(V6);
  1093. // Verify that leases which belong to the leasefile6_0.csv and
  1094. // leasefile6_0.2 are loaded.
  1095. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
  1096. IOAddress("2001:db8:1::1"));
  1097. ASSERT_TRUE(lease);
  1098. EXPECT_EQ(0, lease->cltt_);
  1099. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1100. ASSERT_TRUE(lease);
  1101. EXPECT_EQ(0, lease->cltt_);
  1102. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1103. ASSERT_TRUE(lease);
  1104. EXPECT_EQ(600, lease->cltt_);
  1105. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1106. ASSERT_TRUE(lease);
  1107. EXPECT_EQ(0, lease->cltt_);
  1108. // A lease which doesn't belong to these files should not be loaded.
  1109. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3")));
  1110. }
  1111. // This test checks that the lease database backend loads the file with
  1112. // the .completed postfix instead of files with postfixes .1 and .2 if
  1113. // the file with .completed postfix exists.
  1114. TEST_F(MemfileLeaseMgrTest, load6CompletedFile) {
  1115. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  1116. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1117. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1118. "state\n"
  1119. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  1120. "200,200,8,100,0,7,0,1,1,,,1\n"
  1121. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1122. "200,200,8,100,0,7,0,1,1,,,1\n");
  1123. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  1124. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1125. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1126. "state\n"
  1127. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  1128. "200,200,8,100,0,7,0,1,1,,,1\n"
  1129. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1130. "300,800,8,100,0,7,0,1,1,,,1\n"
  1131. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1132. "200,200,8,100,0,7,0,1,1,,,1\n");
  1133. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1134. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1135. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1136. "state\n"
  1137. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1138. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1139. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1140. "200,200,8,100,0,7,0,1,1,,,1\n");
  1141. LeaseFileIO ioc(getLeaseFilePath("leasefile6_0.csv.completed"));
  1142. ioc.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1143. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1144. "state\n"
  1145. "2001:db8:1::125,ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff,"
  1146. "400,1000,8,100,0,7,0,1,1,,,1\n");
  1147. startBackend(V6);
  1148. // We expect that this file only holds leases that belong to the
  1149. // lease file or to the file with .completed postfix.
  1150. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1151. ASSERT_TRUE(lease);
  1152. EXPECT_EQ(600, lease->cltt_);
  1153. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1154. ASSERT_TRUE(lease);
  1155. EXPECT_EQ(0, lease->cltt_);
  1156. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::125"));
  1157. ASSERT_TRUE(lease);
  1158. EXPECT_EQ(600, lease->cltt_);
  1159. // Leases from the .1 and .2 files should not be loaded.
  1160. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
  1161. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2")));
  1162. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3")));
  1163. }
  1164. // This test checks that backend constructor refuses to load leases from the
  1165. // lease files if the LFC is in progress.
  1166. TEST_F(MemfileLeaseMgrTest, load6LFCInProgress) {
  1167. // Create the backend configuration.
  1168. LeaseMgr::ParameterMap pmap;
  1169. pmap["type"] = "memfile";
  1170. pmap["universe"] = "6";
  1171. pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
  1172. pmap["lfc-interval"] = "1";
  1173. // Create a pid file holding the PID of the current process. Choosing the
  1174. // pid of the current process guarantees that when the backend starts up
  1175. // the process is alive.
  1176. PIDFile pid_file(Memfile_LeaseMgr::appendSuffix(pmap["name"], Memfile_LeaseMgr::FILE_PID));
  1177. pid_file.write();
  1178. // There is a pid file and the process which pid is in the file is
  1179. // running, so the backend should refuse to start.
  1180. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr;
  1181. ASSERT_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)),
  1182. DbOpenError);
  1183. // Remove the pid file, and retry. The bakckend should be created.
  1184. pid_file.deleteFile();
  1185. ASSERT_NO_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)));
  1186. }
  1187. }; // end of anonymous namespace