memfile_lease_mgr_unittest.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. // Checks that declined leases can be returned correctly.
  865. TEST_F(MemfileLeaseMgrTest, getDeclined4) {
  866. startBackend(V4);
  867. testGetDeclinedLeases4();
  868. }
  869. // This test checks that the backend reads DHCPv4 lease data from multiple
  870. // files.
  871. TEST_F(MemfileLeaseMgrTest, load4MultipleLeaseFiles) {
  872. LeaseFileIO io2(getLeaseFilePath("leasefile4_0.csv.2"));
  873. io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  874. "fqdn_fwd,fqdn_rev,hostname,state\n"
  875. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n"
  876. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,200,8,1,1,,1\n");
  877. LeaseFileIO io1(getLeaseFilePath("leasefile4_0.csv.1"));
  878. io1.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  879. "fqdn_fwd,fqdn_rev,hostname,state\n"
  880. "192.0.2.1,01:01:01:01:01:01,,200,200,8,1,1,,1\n"
  881. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,400,8,1,1,,1\n"
  882. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,200,8,1,1,,1\n");
  883. LeaseFileIO io(getLeaseFilePath("leasefile4_0.csv"));
  884. io.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  885. "fqdn_fwd,fqdn_rev,hostname,state\n"
  886. "192.0.2.10,0a:0a:0a:0a:0a:0a,,200,200,8,1,1,,1\n"
  887. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,400,8,1,1,,1\n");
  888. startBackend(V4);
  889. // This lease only exists in the second file and the cltt should
  890. // be 0.
  891. Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
  892. ASSERT_TRUE(lease);
  893. EXPECT_EQ(0, lease->cltt_);
  894. // This lease only exists in the first file and the cltt should
  895. // be 0.
  896. lease = lmptr_->getLease4(IOAddress("192.0.2.2"));
  897. ASSERT_TRUE(lease);
  898. EXPECT_EQ(0, lease->cltt_);
  899. // This lease only exists in the third file and the cltt should
  900. // be 0.
  901. lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
  902. ASSERT_TRUE(lease);
  903. EXPECT_EQ(0, lease->cltt_);
  904. // This lease exists in the first and second file and the cltt
  905. // should be calculated using the expiration time and the
  906. // valid lifetime from the second file.
  907. lease = lmptr_->getLease4(IOAddress("192.0.2.11"));
  908. ASSERT_TRUE(lease);
  909. EXPECT_EQ(200, lease->cltt_);
  910. // This lease exists in the second and third file and the cltt
  911. // should be calculated using the expiration time and the
  912. // valid lifetime from the third file.
  913. lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
  914. ASSERT_TRUE(lease);
  915. EXPECT_EQ(200, lease->cltt_);
  916. }
  917. // This test checks that the lease database backend loads the file with
  918. // the .completed postfix instead of files with postfixes .1 and .2 if
  919. // the file with .completed postfix exists.
  920. TEST_F(MemfileLeaseMgrTest, load4CompletedFile) {
  921. LeaseFileIO io2(getLeaseFilePath("leasefile4_0.csv.2"));
  922. io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  923. "fqdn_fwd,fqdn_rev,hostname,state\n"
  924. "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,1\n"
  925. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,200,8,1,1,,1\n");
  926. LeaseFileIO io1(getLeaseFilePath("leasefile4_0.csv.1"));
  927. io1.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  928. "fqdn_fwd,fqdn_rev,hostname,state\n"
  929. "192.0.2.1,01:01:01:01:01:01,,200,200,8,1,1,,1\n"
  930. "192.0.2.11,bb:bb:bb:bb:bb:bb,,200,400,8,1,1,,1\n"
  931. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,200,8,1,1,,1\n");
  932. LeaseFileIO io(getLeaseFilePath("leasefile4_0.csv"));
  933. io.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  934. "fqdn_fwd,fqdn_rev,hostname,state\n"
  935. "192.0.2.10,0a:0a:0a:0a:0a:0a,,200,200,8,1,1,,1\n"
  936. "192.0.2.12,cc:cc:cc:cc:cc:cc,,200,400,8,1,1,,1\n");
  937. LeaseFileIO ioc(getLeaseFilePath("leasefile4_0.csv.completed"));
  938. ioc.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
  939. "fqdn_fwd,fqdn_rev,hostname,state\n"
  940. "192.0.2.13,ff:ff:ff:ff:ff:ff,,200,200,8,1,1,,1\n");
  941. startBackend(V4);
  942. // We expect that this file only holds leases that belong to the
  943. // lease file or to the file with .completed postfix.
  944. Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
  945. ASSERT_TRUE(lease);
  946. EXPECT_EQ(0, lease->cltt_);
  947. lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
  948. ASSERT_TRUE(lease);
  949. EXPECT_EQ(200, lease->cltt_);
  950. // This lease is in the .completed file.
  951. lease = lmptr_->getLease4(IOAddress("192.0.2.13"));
  952. ASSERT_TRUE(lease);
  953. EXPECT_EQ(0, lease->cltt_);
  954. // Leases from the .1 and .2 files should not be loaded.
  955. EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.11")));
  956. EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
  957. }
  958. // This test checks that backend constructor refuses to load leases from the
  959. // lease files if the LFC is in progress.
  960. TEST_F(MemfileLeaseMgrTest, load4LFCInProgress) {
  961. // Create the backend configuration.
  962. LeaseMgr::ParameterMap pmap;
  963. pmap["type"] = "memfile";
  964. pmap["universe"] = "4";
  965. pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
  966. pmap["lfc-interval"] = "1";
  967. // Create a pid file holding the PID of the current process. Choosing the
  968. // pid of the current process guarantees that when the backend starts up
  969. // the process is alive.
  970. PIDFile pid_file(Memfile_LeaseMgr::appendSuffix(pmap["name"], Memfile_LeaseMgr::FILE_PID));
  971. pid_file.write();
  972. // There is a pid file and the process which pid is in the file is
  973. // running, so the backend should refuse to start.
  974. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr;
  975. ASSERT_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)),
  976. DbOpenError);
  977. // Remove the pid file, and retry. The bakckend should be created.
  978. pid_file.deleteFile();
  979. ASSERT_NO_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)));
  980. }
  981. // This test checks that the backend reads DHCPv6 lease data from multiple
  982. // files.
  983. TEST_F(MemfileLeaseMgrTest, load6MultipleLeaseFiles) {
  984. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  985. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  986. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  987. "state\n"
  988. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  989. "200,200,8,100,0,7,0,1,1,,,1\n"
  990. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  991. "200,200,8,100,0,7,0,1,1,,,1\n");
  992. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  993. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  994. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  995. "state\n"
  996. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  997. "200,200,8,100,0,7,0,1,1,,,1\n"
  998. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  999. "300,800,8,100,0,7,0,1,1,,,1\n"
  1000. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1001. "200,200,8,100,0,7,0,1,1,,,1\n");
  1002. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1003. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1004. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1005. "state\n"
  1006. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1007. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1008. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1009. "200,200,8,100,0,7,0,1,1,,,1\n");
  1010. startBackend(V6);
  1011. // This lease only exists in the first file and the cltt should be 0.
  1012. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
  1013. IOAddress("2001:db8:1::1"));
  1014. ASSERT_TRUE(lease);
  1015. EXPECT_EQ(0, lease->cltt_);
  1016. // This lease exists in the first and second file and the cltt should
  1017. // be calculated using the expiration time and the valid lifetime
  1018. // from the second file.
  1019. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1020. ASSERT_TRUE(lease);
  1021. EXPECT_EQ(500, lease->cltt_);
  1022. // This lease only exists in the second file and the cltt should be 0.
  1023. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
  1024. ASSERT_TRUE(lease);
  1025. EXPECT_EQ(0, lease->cltt_);
  1026. // This lease exists in the second and third file and the cltt should
  1027. // be calculated using the expiration time and the valid lifetime
  1028. // from the third file.
  1029. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1030. ASSERT_TRUE(lease);
  1031. EXPECT_EQ(600, lease->cltt_);
  1032. // This lease only exists in the third file and the cltt should be 0.
  1033. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1034. ASSERT_TRUE(lease);
  1035. EXPECT_EQ(0, lease->cltt_);
  1036. }
  1037. // This test checks that the backend reads DHCPv6 lease data from the
  1038. // leasefile without the postfix and the file with a .1 postfix when
  1039. // the file with the .2 postfix is missing.
  1040. TEST_F(MemfileLeaseMgrTest, load6MultipleNoSecondFile) {
  1041. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  1042. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1043. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1044. "state\n"
  1045. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  1046. "200,200,8,100,0,7,0,1,1,,,1\n"
  1047. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1048. "300,800,8,100,0,7,0,1,1,,,1\n"
  1049. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1050. "200,200,8,100,0,7,0,1,1,,,1\n");
  1051. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1052. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1053. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1054. "state\n"
  1055. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1056. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1057. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1058. "200,200,8,100,0,7,0,1,1,,,1\n");
  1059. startBackend(V6);
  1060. // Check that leases from the leasefile6_0 and leasefile6_0.1 have
  1061. // been loaded.
  1062. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1063. ASSERT_TRUE(lease);
  1064. EXPECT_EQ(500, lease->cltt_);
  1065. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
  1066. ASSERT_TRUE(lease);
  1067. EXPECT_EQ(0, lease->cltt_);
  1068. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1069. ASSERT_TRUE(lease);
  1070. EXPECT_EQ(600, lease->cltt_);
  1071. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1072. ASSERT_TRUE(lease);
  1073. EXPECT_EQ(0, lease->cltt_);
  1074. // Make sure that a lease which is not in those files is not loaded.
  1075. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
  1076. }
  1077. // This test checks that the backend reads DHCPv6 lease data from the
  1078. // leasefile without the postfix and the file with a .2 postfix when
  1079. // the file with the .1 postfix is missing.
  1080. TEST_F(MemfileLeaseMgrTest, load6MultipleNoFirstFile) {
  1081. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  1082. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1083. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1084. "state\n"
  1085. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  1086. "200,200,8,100,0,7,0,1,1,,,1\n"
  1087. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1088. "200,200,8,100,0,7,0,1,1,,,1\n");
  1089. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1090. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1091. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1092. "state\n"
  1093. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1094. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1095. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1096. "200,200,8,100,0,7,0,1,1,,,1\n");
  1097. startBackend(V6);
  1098. // Verify that leases which belong to the leasefile6_0.csv and
  1099. // leasefile6_0.2 are loaded.
  1100. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
  1101. IOAddress("2001:db8:1::1"));
  1102. ASSERT_TRUE(lease);
  1103. EXPECT_EQ(0, lease->cltt_);
  1104. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
  1105. ASSERT_TRUE(lease);
  1106. EXPECT_EQ(0, lease->cltt_);
  1107. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1108. ASSERT_TRUE(lease);
  1109. EXPECT_EQ(600, lease->cltt_);
  1110. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1111. ASSERT_TRUE(lease);
  1112. EXPECT_EQ(0, lease->cltt_);
  1113. // A lease which doesn't belong to these files should not be loaded.
  1114. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3")));
  1115. }
  1116. // This test checks that the lease database backend loads the file with
  1117. // the .completed postfix instead of files with postfixes .1 and .2 if
  1118. // the file with .completed postfix exists.
  1119. TEST_F(MemfileLeaseMgrTest, load6CompletedFile) {
  1120. LeaseFileIO io2(getLeaseFilePath("leasefile6_0.csv.2"));
  1121. io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1122. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1123. "state\n"
  1124. "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
  1125. "200,200,8,100,0,7,0,1,1,,,1\n"
  1126. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1127. "200,200,8,100,0,7,0,1,1,,,1\n");
  1128. LeaseFileIO io1(getLeaseFilePath("leasefile6_0.csv.1"));
  1129. io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1130. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1131. "state\n"
  1132. "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
  1133. "200,200,8,100,0,7,0,1,1,,,1\n"
  1134. "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
  1135. "300,800,8,100,0,7,0,1,1,,,1\n"
  1136. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1137. "200,200,8,100,0,7,0,1,1,,,1\n");
  1138. LeaseFileIO io(getLeaseFilePath("leasefile6_0.csv"));
  1139. io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1140. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1141. "state\n"
  1142. "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
  1143. "400,1000,8,100,0,7,0,1,1,,,1\n"
  1144. "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
  1145. "200,200,8,100,0,7,0,1,1,,,1\n");
  1146. LeaseFileIO ioc(getLeaseFilePath("leasefile6_0.csv.completed"));
  1147. ioc.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
  1148. "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,"
  1149. "state\n"
  1150. "2001:db8:1::125,ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff,"
  1151. "400,1000,8,100,0,7,0,1,1,,,1\n");
  1152. startBackend(V6);
  1153. // We expect that this file only holds leases that belong to the
  1154. // lease file or to the file with .completed postfix.
  1155. Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
  1156. ASSERT_TRUE(lease);
  1157. EXPECT_EQ(600, lease->cltt_);
  1158. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
  1159. ASSERT_TRUE(lease);
  1160. EXPECT_EQ(0, lease->cltt_);
  1161. lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::125"));
  1162. ASSERT_TRUE(lease);
  1163. EXPECT_EQ(600, lease->cltt_);
  1164. // Leases from the .1 and .2 files should not be loaded.
  1165. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1")));
  1166. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2")));
  1167. EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3")));
  1168. }
  1169. // This test checks that backend constructor refuses to load leases from the
  1170. // lease files if the LFC is in progress.
  1171. TEST_F(MemfileLeaseMgrTest, load6LFCInProgress) {
  1172. // Create the backend configuration.
  1173. LeaseMgr::ParameterMap pmap;
  1174. pmap["type"] = "memfile";
  1175. pmap["universe"] = "6";
  1176. pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
  1177. pmap["lfc-interval"] = "1";
  1178. // Create a pid file holding the PID of the current process. Choosing the
  1179. // pid of the current process guarantees that when the backend starts up
  1180. // the process is alive.
  1181. PIDFile pid_file(Memfile_LeaseMgr::appendSuffix(pmap["name"], Memfile_LeaseMgr::FILE_PID));
  1182. pid_file.write();
  1183. // There is a pid file and the process which pid is in the file is
  1184. // running, so the backend should refuse to start.
  1185. boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr;
  1186. ASSERT_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)),
  1187. DbOpenError);
  1188. // Remove the pid file, and retry. The bakckend should be created.
  1189. pid_file.deleteFile();
  1190. ASSERT_NO_THROW(lease_mgr.reset(new NakedMemfileLeaseMgr(pmap)));
  1191. }
  1192. }; // end of anonymous namespace