memfile_lease_mgr_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/memfile_lease_mgr.h>
  20. #include <dhcpsrv/tests/test_utils.h>
  21. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  22. #include <gtest/gtest.h>
  23. #include <iostream>
  24. #include <sstream>
  25. using namespace std;
  26. using namespace isc;
  27. using namespace isc::asiolink;
  28. using namespace isc::dhcp;
  29. using namespace isc::dhcp::test;
  30. namespace {
  31. // empty class for now, but may be extended once Addr6 becomes bigger
  32. class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
  33. public:
  34. /// @brief memfile lease mgr test constructor
  35. ///
  36. /// Creates memfile and stores it in lmptr_ pointer
  37. MemfileLeaseMgrTest() {
  38. const LeaseMgr::ParameterMap pmap;
  39. lmptr_ = new Memfile_LeaseMgr(pmap);
  40. }
  41. virtual void reopen() {
  42. /// @todo: write lease to disk, flush, read file from disk
  43. }
  44. /// @brief destructor
  45. ///
  46. /// destroys lease manager backend.
  47. virtual ~MemfileLeaseMgrTest() {
  48. delete lmptr_;
  49. lmptr_ = 0;
  50. }
  51. /// @brief Return path to the lease file used by unit tests.
  52. ///
  53. /// @param filename Name of the lease file appended to the path to the
  54. /// directory where test data is held.
  55. ///
  56. /// @return Full path to the lease file.
  57. std::string getLeaseFilePath(const std::string& filename) const {
  58. std::ostringstream s;
  59. s << TEST_DATA_BUILDDIR << "/" << filename;
  60. return (s.str());
  61. }
  62. };
  63. // This test checks if the LeaseMgr can be instantiated and that it
  64. // parses parameters string properly.
  65. TEST_F(MemfileLeaseMgrTest, constructor) {
  66. const LeaseMgr::ParameterMap pmap; // Empty parameter map
  67. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
  68. ASSERT_NO_THROW(lease_mgr.reset(new Memfile_LeaseMgr(pmap)));
  69. }
  70. // Checks if the getType() and getName() methods both return "memfile".
  71. TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
  72. EXPECT_EQ(std::string("memfile"), lmptr_->getType());
  73. EXPECT_EQ(std::string("memory"), lmptr_->getName());
  74. }
  75. // Checks if the path to the lease files is initialized correctly.
  76. TEST_F(MemfileLeaseMgrTest, getLeaseFilePath) {
  77. LeaseMgr::ParameterMap pmap;
  78. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  79. std::ostringstream s4;
  80. s4 << CfgMgr::instance().getDataDir() << "/" << "kea-leases4.csv";
  81. std::ostringstream s6;
  82. s6 << CfgMgr::instance().getDataDir() << "/" << "kea-leases6.csv";
  83. EXPECT_EQ(s4.str(),
  84. lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V4));
  85. EXPECT_EQ(s6.str(),
  86. lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6));
  87. EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V4),
  88. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  89. EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6),
  90. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
  91. pmap["leasefile4"] = getLeaseFilePath("leasefile4.csv");
  92. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  93. EXPECT_EQ(pmap["leasefile4"],
  94. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  95. EXPECT_EQ(lease_mgr->getDefaultLeaseFilePath(Memfile_LeaseMgr::V6),
  96. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
  97. pmap["leasefile6"] = getLeaseFilePath("kea-leases6.csv");
  98. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  99. EXPECT_EQ(pmap["leasefile4"],
  100. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V4));
  101. EXPECT_EQ(pmap["leasefile6"],
  102. lease_mgr->getLeaseFilePath(Memfile_LeaseMgr::V6));
  103. }
  104. // Check if the persitLeases correctly checks that leases should not be written
  105. // to disk when lease file is set to empty value.
  106. TEST_F(MemfileLeaseMgrTest, persistLeases) {
  107. LeaseMgr::ParameterMap pmap;
  108. boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
  109. // If the leasefile4 and leasefile6 are not specified, the default
  110. // file names will be used. The leases will be written to these files.
  111. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  112. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  113. // Specify the names of the lease files. Leases will be written.
  114. pmap["leasefile4"] = "leases4.csv";
  115. pmap["leasefile6"] = "leases6.csv";
  116. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  117. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  118. EXPECT_TRUE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  119. // Specify empty names of the lease files. This should disable writes
  120. // of leases to disk.
  121. pmap["leasefile4"] = "";
  122. pmap["leasefile6"] = "";
  123. lease_mgr.reset(new Memfile_LeaseMgr(pmap));
  124. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V4));
  125. EXPECT_FALSE(lease_mgr->persistLeases(Memfile_LeaseMgr::V6));
  126. }
  127. // Checks that adding/getting/deleting a Lease6 object works.
  128. TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
  129. testAddGetDelete6(true); // true - check T1,T2 values
  130. // memfile is able to preserve those values, but some other
  131. // backends can't do that.
  132. }
  133. /// @brief Basic Lease4 Checks
  134. ///
  135. /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
  136. /// IPv4 address) works.
  137. TEST_F(MemfileLeaseMgrTest, basicLease4) {
  138. testBasicLease4();
  139. }
  140. /// @todo Write more memfile tests
  141. // Simple test about lease4 retrieval through client id method
  142. TEST_F(MemfileLeaseMgrTest, getLease4ClientId) {
  143. testGetLease4ClientId();
  144. }
  145. // Checks that lease4 retrieval client id is null is working
  146. TEST_F(MemfileLeaseMgrTest, getLease4NullClientId) {
  147. testGetLease4NullClientId();
  148. }
  149. // Checks lease4 retrieval through HWAddr
  150. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr1) {
  151. testGetLease4HWAddr1();
  152. }
  153. /// @brief Check GetLease4 methods - access by Hardware Address
  154. ///
  155. /// Adds leases to the database and checks that they can be accessed via
  156. /// a combination of DUID and IAID.
  157. TEST_F(MemfileLeaseMgrTest, getLease4HWAddr2) {
  158. testGetLease4HWAddr2();
  159. }
  160. // Checks lease4 retrieval with clientId, HWAddr and subnet_id
  161. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdHWAddrSubnetId) {
  162. testGetLease4ClientIdHWAddrSubnetId();
  163. }
  164. /// @brief Basic Lease4 Checks
  165. ///
  166. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  167. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  168. /// (client-id is optional and may not be present)
  169. TEST_F(MemfileLeaseMgrTest, DISABLED_lease4NullClientId) {
  170. /// @todo Test is disabled, because memfile does not support disk storage, so
  171. /// all leases are lost after reopen()
  172. testLease4NullClientId();
  173. }
  174. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  175. ///
  176. /// Adds leases to the database and checks that they can be accessed via
  177. /// a combination of hardware address and subnet ID
  178. TEST_F(MemfileLeaseMgrTest, DISABLED_getLease4HwaddrSubnetId) {
  179. /// @todo: fails on memfile. It's probably a memfile bug.
  180. testGetLease4HWAddrSubnetId();
  181. }
  182. /// @brief Check GetLease4 methods - access by Client ID
  183. ///
  184. /// Adds leases to the database and checks that they can be accessed via
  185. /// the Client ID.
  186. TEST_F(MemfileLeaseMgrTest, getLease4ClientId2) {
  187. testGetLease4ClientId2();
  188. }
  189. // @brief Get Lease4 by client ID
  190. //
  191. // Check that the system can cope with a client ID of any size.
  192. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSize) {
  193. testGetLease4ClientIdSize();
  194. }
  195. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  196. ///
  197. /// Adds leases to the database and checks that they can be accessed via
  198. /// a combination of client and subnet IDs.
  199. TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSubnetId) {
  200. testGetLease4ClientIdSubnetId();
  201. }
  202. /// @brief Check GetLease6 methods - access by DUID/IAID
  203. ///
  204. /// Adds leases to the database and checks that they can be accessed via
  205. /// a combination of DUID and IAID.
  206. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  207. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  208. TEST_F(MemfileLeaseMgrTest, DISABLED_getLeases6DuidIaid) {
  209. testGetLeases6DuidIaid();
  210. }
  211. // Check that the system can cope with a DUID of allowed size.
  212. /// @todo: test disabled, because Memfile_LeaseMgr::getLeases6(Lease::Type,
  213. /// const DUID& duid, uint32_t iaid) const is not implemented yet.
  214. TEST_F(MemfileLeaseMgrTest, DISABLED_getLeases6DuidSize) {
  215. testGetLeases6DuidSize();
  216. }
  217. /// @brief Check that getLease6 methods discriminate by lease type.
  218. ///
  219. /// Adds six leases, two per lease type all with the same duid and iad but
  220. /// with alternating subnet_ids.
  221. /// It then verifies that all of getLeases6() method variants correctly
  222. /// discriminate between the leases based on lease type alone.
  223. /// @todo: Disabled, because type parameter in Memfile_LeaseMgr::getLease6
  224. /// (Lease::Type, const isc::asiolink::IOAddress& addr) const is not used.
  225. TEST_F(MemfileLeaseMgrTest, DISABLED_lease6LeaseTypeCheck) {
  226. testLease6LeaseTypeCheck();
  227. }
  228. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  229. ///
  230. /// Adds leases to the database and checks that they can be accessed via
  231. /// a combination of DIUID and IAID.
  232. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetId) {
  233. testGetLease6DuidIaidSubnetId();
  234. }
  235. /// Checks that getLease6(type, duid, iaid, subnet-id) works with different
  236. /// DUID sizes
  237. TEST_F(MemfileLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
  238. testGetLease6DuidIaidSubnetIdSize();
  239. }
  240. /// @brief Lease4 update tests
  241. ///
  242. /// Checks that we are able to update a lease in the database.
  243. /// @todo: Disabled, because memfile does not throw when lease is updated.
  244. /// We should reconsider if lease{4,6} structures should have a limit
  245. /// implemented in them.
  246. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease4) {
  247. testUpdateLease4();
  248. }
  249. /// @brief Lease6 update tests
  250. ///
  251. /// Checks that we are able to update a lease in the database.
  252. /// @todo: Disabled, because memfile does not throw when lease is updated.
  253. /// We should reconsider if lease{4,6} structures should have a limit
  254. /// implemented in them.
  255. TEST_F(MemfileLeaseMgrTest, DISABLED_updateLease6) {
  256. testUpdateLease6();
  257. }
  258. // The following tests are not applicable for memfile. When adding
  259. // new tests to the list here, make sure to provide brief explanation
  260. // why they are not applicable:
  261. //
  262. // testGetLease4HWAddrSubnetIdSize() - memfile just keeps Lease structure
  263. // and does not do any checks of HWAddr content
  264. }; // end of anonymous namespace