memfile_lease_mgr_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/duid.h>
  17. #include <dhcpsrv/cfgmgr.h>
  18. #include <dhcpsrv/lease_mgr.h>
  19. #include <dhcpsrv/lease_mgr_factory.h>
  20. #include <dhcpsrv/memfile_lease_mgr.h>
  21. #include <dhcpsrv/tests/lease_file_io.h>
  22. #include <dhcpsrv/tests/test_utils.h>
  23. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  24. #include <gtest/gtest.h>
  25. #include <iostream>
  26. #include <sstream>
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::asiolink;
  30. using namespace isc::dhcp;
  31. using namespace isc::dhcp::test;
  32. namespace {
  33. // empty class for now, but may be extended once Addr6 becomes bigger
  34. class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
  35. public:
  36. /// @brief memfile lease mgr test constructor
  37. ///
  38. /// Creates memfile and stores it in lmptr_ pointer
  39. MemfileLeaseMgrTest() :
  40. io4_(getLeaseFilePath("leasefile4_0.csv")),
  41. io6_(getLeaseFilePath("leasefile6_0.csv")) {
  42. // Make sure there are no dangling files after previous tests.
  43. io4_.removeFile();
  44. io6_.removeFile();
  45. try {
  46. LeaseMgrFactory::create(getConfigString());
  47. } catch (...) {
  48. std::cerr << "*** ERROR: unable to create instance of the Memfile\n"
  49. " lease database backend.\n";
  50. throw;
  51. }
  52. lmptr_ = &(LeaseMgrFactory::instance());
  53. }
  54. virtual void reopen() {
  55. LeaseMgrFactory::destroy();
  56. LeaseMgrFactory::create(getConfigString());
  57. lmptr_ = &(LeaseMgrFactory::instance());
  58. }
  59. /// @brief destructor
  60. ///
  61. /// destroys lease manager backend.
  62. virtual ~MemfileLeaseMgrTest() {
  63. LeaseMgrFactory::destroy();
  64. }
  65. /// @brief Return path to the lease file used by unit tests.
  66. ///
  67. /// @param filename Name of the lease file appended to the path to the
  68. /// directory where test data is held.
  69. ///
  70. /// @return Full path to the lease file.
  71. static std::string getLeaseFilePath(const std::string& filename) {
  72. std::ostringstream s;
  73. s << TEST_DATA_BUILDDIR << "/" << filename;
  74. return (s.str());
  75. }
  76. /// @brief Returns the configuration string for the backend.
  77. ///
  78. /// This string configures the @c LeaseMgrFactory to create the memfile
  79. /// backend and use leasefile4_0.csv and leasefile6_0.csv files as
  80. /// storage for leases.
  81. ///
  82. /// @return Configuration string for @c LeaseMgrFactory.
  83. static std::string getConfigString() {
  84. std::ostringstream s;
  85. s << "type=memfile leasefile4=" << getLeaseFilePath("leasefile4_0.csv")
  86. << " leasefile6=" << getLeaseFilePath("leasefile6_0.csv");
  87. return (s.str());
  88. }
  89. /// @brief Object providing access to v4 lease IO.
  90. LeaseFileIO io4_;
  91. /// @brief Object providing access to v6 lease IO.
  92. LeaseFileIO io6_;
  93. };
  94. // This test checks if the LeaseMgr can be instantiated and that it
  95. // parses parameters string properly.
  96. TEST_F(MemfileLeaseMgrTest, constructor) {
  97. LeaseMgr::ParameterMap pmap;
  98. pmap["persist4"] = "no";
  99. pmap["persist6"] = "no";
  100. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
  101. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  102. pmap["persist4"] = "yes";
  103. pmap["persist6"] = "yes";
  104. pmap["leasefile4"] = getLeaseFilePath("leasefile4_1.csv");
  105. pmap["leasefile6"] = getLeaseFilePath("leasefile6_1.csv");
  106. EXPECT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  107. // Expecting that persist parameter is yes or no. Everything other than
  108. // that is wrong.
  109. pmap["persist4"] = "bogus";
  110. pmap["persist6"] = "yes";
  111. pmap["leasefile4"] = getLeaseFilePath("leasefile4_1.csv");
  112. pmap["leasefile6"] = getLeaseFilePath("leasefile6_1.csv");
  113. EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
  114. pmap["persist4"] = "yes";
  115. pmap["persist6"] = "bogus";
  116. pmap["leasefile4"] = getLeaseFilePath("leasefile4_1.csv");
  117. pmap["leasefile6"] = getLeaseFilePath("leasefile6_1.csv");
  118. EXPECT_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)), isc::BadValue);
  119. }
  120. // Checks if the getType() and getName() methods both return "memfile".
  121. TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
  122. EXPECT_EQ(std::string("memfile"), lmptr_->getType());
  123. EXPECT_EQ(std::string("memory"), lmptr_->getName());
  124. }
  125. // Checks if the path to the lease files is initialized correctly.
  126. TEST_F(MemfileLeaseMgrTest, getLeaseFilePath) {
  127. // Initialize IO objects, so as the test csv files get removed after the
  128. // test (when destructors are called).
  129. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  130. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  131. LeaseMgr::ParameterMap pmap;
  132. pmap["leasefile4"] = getLeaseFilePath("leasefile4_1.csv");
  133. pmap["leasefile6"] = getLeaseFilePath("leasefile6_1.csv");
  134. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  135. EXPECT_EQ(pmap["leasefile4"],
  136. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  137. EXPECT_EQ(pmap["leasefile6"],
  138. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
  139. pmap["persist4"] = "no";
  140. pmap["persist6"] = "no";
  141. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  142. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4).empty());
  143. EXPECT_TRUE(lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6).empty());
  144. }
  145. // Check if the persitLeases correctly checks that leases should not be written
  146. // to disk when lease file is set to empty value.
  147. TEST_F(MemfileLeaseMgrTest, persistLeases) {
  148. // Initialize IO objects, so as the test csv files get removed after the
  149. // test (when destructors are called).
  150. LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
  151. LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
  152. LeaseMgr::ParameterMap pmap;
  153. // Specify the names of the lease files. Leases will be written.
  154. pmap["leasefile4"] = getLeaseFilePath("leasefile4_1.csv");
  155. pmap["leasefile6"] = getLeaseFilePath("leasefile6_1.csv");
  156. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  157. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  158. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  159. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  160. // This should disable writes of leases to disk.
  161. pmap["persist"] = "no";
  162. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  163. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  164. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  165. }
  166. // Checks that adding/getting/deleting a Lease6 object works.
  167. TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
  168. testAddGetDelete6(true); // true - check T1,T2 values
  169. // memfile is able to preserve those values, but some other
  170. // backends can't do that.
  171. }
  172. /// @brief Basic Lease4 Checks
  173. ///
  174. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  175. /// IPv4 address) works.
  176. TEST_F(MemfileLeaseMgrTest, basicLease4) {
  177. testBasicLease4();
  178. }
  179. /// @todo Write more memfile tests
  180. // Simple test about lease4 retrieval through client id method
  181. TEST_F(MemfileLeaseMgrTest, getLease4ClientId) {
  182. testGetLease4ClientId();
  183. }
  184. // Checks that lease4 retrieval client id is null is working
  185. TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) {
  186. testGetLease4NullClientId();
  187. }
  188. // Checks lease4 retrieval through HWAddr
  189. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr1) {
  190. testGetLease4HWAddr1();
  191. }
  192. /// @brief Check GetLease4 methods - access by Hardware Address
  193. ///
  194. /// Adds leases to the database and checks that they can be accessed via
  195. /// a combination of DUID and IAID.
  196. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr2) {
  197. testGetLease4HWAddr2();
  198. }
  199. // Checks lease4 retrieval with clientId, HWAddr and subnet_id
  200. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) {
  201. testGetLease4ClientIdHWAddrSubnetId();
  202. }
  203. /// @brief Basic Lease4 Checks
  204. ///
  205. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  206. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  207. /// (client-id is optional and may not be present)
  208. TEST_F(MemfileLeaseMgrTest, lease4NullClientId) {
  209. testLease4NullClientId();
  210. }
  211. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  212. ///
  213. /// Adds leases to the database and checks that they can be accessed via
  214. /// a combination of hardware address and subnet ID
  215. TEST_F(MemfileLeaseMgrTest, DISABLED_getLease4HwaddrSubnetId) {
  216. /// @todo: fails on memfile. It's probably a memfile bug.
  217. testGetLease4HWAddrSubnetId();
  218. }
  219. /// @brief Check GetLease4 methods - access by Client ID
  220. ///
  221. /// Adds leases to the database and checks that they can be accessed via
  222. /// the Client ID.
  223. TEST_F(MemfileLeaseMgrTest, getLease4ClientId2) {
  224. testGetLease4ClientId2();
  225. }
  226. // @brief Get Lease4 by client ID
  227. //
  228. // Check that the system can cope with a client ID of any size.
  229. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSize) {
  230. testGetLease4ClientIdSize();
  231. }
  232. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  233. ///
  234. /// Adds leases to the database and checks that they can be accessed via
  235. /// a combination of client and subnet IDs.
  236. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSubnetId) {
  237. testGetLease4ClientIdSubnetId();
  238. }
  239. /// @brief Basic Lease6 Checks
  240. ///
  241. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  242. /// IPv6 address) works.
  243. TEST_F(MemfileLeaseMgrTest, basicLease6) {
  244. testBasicLease6();
  245. }
  246. /// @brief Check GetLease6 methods - access by DUID/IAID
  247. ///
  248. /// Adds leases to the database and checks that they can be accessed via
  249. /// a combination of DUID and IAID.
  250. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  251. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  252. TEST_F(MemfileLeaseMgrTest, DISABLED_getLeases6DuidIaid) {
  253. testGetLeases6DuidIaid();
  254. }
  255. // Check that the system can cope with a DUID of allowed size.
  256. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  257. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  258. TEST_F(MemfileLeaseMgrTest, DISABLED_getLeases6DuidSize) {
  259. testGetLeases6DuidSize();
  260. }
  261. /// @brief Check that getLease6 methods discriminate by lease type.
  262. ///
  263. /// Adds six leases, two per lease type all with the same duid and iad but
  264. /// with alternating subnet_ids.
  265. /// It then verifies that all of getLeases6() method variants correctly
  266. /// discriminate between the leases based on lease type alone.
  267. /// @todo: Disabled, because type parameter in Memfile_LeaseMgr::getLease6
  268. /// (Lease::Type, const isc::asiolink::IOAddress& addr) const is not used.
  269. TEST_F(MemfileLeaseMgrTest, DISABLED_lease6LeaseTypeCheck) {
  270. testLease6LeaseTypeCheck();
  271. }
  272. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  273. ///
  274. /// Adds leases to the database and checks that they can be accessed via
  275. /// a combination of DIUID and IAID.
  276. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetId) {
  277. testGetLease6DuidIaidSubnetId();
  278. }
  279. /// Checks that getLease6(type, duid, iaid, subnet-id) works with different
  280. /// DUID sizes
  281. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  282. testGetLease6DuidIaidSubnetIdSize();
  283. }
  284. /// @brief Lease4 update tests
  285. ///
  286. /// Checks that we are able to update a lease in the database.
  287. /// @todo: Disabled, because memfile does not throw when lease is updated.
  288. /// We should reconsider if lease{4,6} structures should have a limit
  289. /// implemented in them.
  290. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease4) {
  291. testUpdateLease4();
  292. }
  293. /// @brief Lease6 update tests
  294. ///
  295. /// Checks that we are able to update a lease in the database.
  296. /// @todo: Disabled, because memfile does not throw when lease is updated.
  297. /// We should reconsider if lease{4,6} structures should have a limit
  298. /// implemented in them.
  299. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease6) {
  300. testUpdateLease6();
  301. }
  302. // The following tests are not applicable for memfile. When adding
  303. // new tests to the list here, make sure to provide brief explanation
  304. // why they are not applicable:
  305. //
  306. // testGetLease4HWAddrSubnetIdSize() - memfile just keeps Lease structure
  307. // and does not do any checks of HWAddr content
  308. }; // end of anonymous namespace