lease_mgr_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <dhcpsrv/lease_mgr.h>
  17. #include <dhcpsrv/memfile_lease_mgr.h>
  18. #include <dhcpsrv/tests/test_utils.h>
  19. #include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
  20. #include <gtest/gtest.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <time.h>
  24. using namespace std;
  25. using namespace isc;
  26. using namespace isc::asiolink;
  27. using namespace isc::dhcp;
  28. using namespace isc::dhcp::test;
  29. // This is a concrete implementation of a Lease database. It does not do
  30. // anything useful and is used for abstract LeaseMgr class testing.
  31. class ConcreteLeaseMgr : public LeaseMgr {
  32. public:
  33. /// @brief The sole lease manager constructor
  34. ///
  35. /// dbconfig is a generic way of passing parameters. Parameters
  36. /// are passed in the "name=value" format, separated by spaces.
  37. /// Values may be enclosed in double quotes, if needed.
  38. ///
  39. /// @param parameters A data structure relating keywords and values
  40. /// concerned with the database.
  41. ConcreteLeaseMgr(const LeaseMgr::ParameterMap& parameters)
  42. : LeaseMgr(parameters)
  43. {}
  44. /// @brief Destructor
  45. virtual ~ConcreteLeaseMgr()
  46. {}
  47. /// @brief Adds an IPv4 lease.
  48. ///
  49. /// @param lease lease to be added
  50. virtual bool addLease(const Lease4Ptr&) {
  51. return (false);
  52. }
  53. /// @brief Adds an IPv6 lease.
  54. ///
  55. /// @param lease lease to be added
  56. virtual bool addLease(const Lease6Ptr&) {
  57. return (false);
  58. }
  59. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  60. ///
  61. /// @param addr address of the searched lease
  62. ///
  63. /// @return smart pointer to the lease (or NULL if a lease is not found)
  64. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress&) const {
  65. return (Lease4Ptr());
  66. }
  67. /// @brief Returns existing IPv4 leases for specified hardware address.
  68. ///
  69. /// Although in the usual case there will be only one lease, for mobile
  70. /// clients or clients with multiple static/fixed/reserved leases there
  71. /// can be more than one. Thus return type is a container, not a single
  72. /// pointer.
  73. ///
  74. /// @param hwaddr hardware address of the client
  75. ///
  76. /// @return lease collection
  77. virtual Lease4Collection getLease4(const HWAddr&) const {
  78. return (Lease4Collection());
  79. }
  80. /// @brief Returns existing IPv4 leases for specified hardware address
  81. /// and a subnet
  82. ///
  83. /// There can be at most one lease for a given HW address in a single
  84. /// pool, so this method with either return a single lease or NULL.
  85. ///
  86. /// @param hwaddr hardware address of the client
  87. /// @param subnet_id identifier of the subnet that lease must belong to
  88. ///
  89. /// @return a pointer to the lease (or NULL if a lease is not found)
  90. virtual Lease4Ptr getLease4(const HWAddr&, SubnetID) const {
  91. return (Lease4Ptr());
  92. }
  93. /// @brief Returns existing IPv4 lease for specified client-id
  94. ///
  95. /// @param clientid client identifier
  96. ///
  97. /// @return lease collection
  98. virtual Lease4Collection getLease4(const ClientId&) const {
  99. return (Lease4Collection());
  100. }
  101. /// @brief Returns existing IPv4 lease for specified client identifier,
  102. /// HW address and subnet identifier.
  103. ///
  104. /// @param client_id Aclient identifier
  105. /// @param hwaddr A HW address.
  106. /// @param subnet_id A subnet identifier.
  107. ///
  108. /// @return A pointer to an existing lease or NULL if lease not found.
  109. virtual Lease4Ptr
  110. getLease4(const ClientId&, const HWAddr&, SubnetID) const {
  111. return (Lease4Ptr());
  112. }
  113. /// @brief Returns existing IPv4 lease for specified client-id
  114. ///
  115. /// There can be at most one lease for a given HW address in a single
  116. /// pool, so this method with either return a single lease or NULL.
  117. ///
  118. /// @param clientid client identifier
  119. /// @param subnet_id identifier of the subnet that lease must belong to
  120. ///
  121. /// @return a pointer to the lease (or NULL if a lease is not found)
  122. virtual Lease4Ptr getLease4(const ClientId&, SubnetID) const {
  123. return (Lease4Ptr());
  124. }
  125. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  126. ///
  127. /// @param addr address of the searched lease
  128. ///
  129. /// @return smart pointer to the lease (or NULL if a lease is not found)
  130. virtual Lease6Ptr getLease6(Lease::Type /* not used yet */,
  131. const isc::asiolink::IOAddress&) const {
  132. return (Lease6Ptr());
  133. }
  134. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  135. ///
  136. /// @param duid ignored
  137. /// @param iaid ignored
  138. ///
  139. /// @return whatever is set in leases6_ field
  140. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  141. const DUID&, uint32_t) const {
  142. return (leases6_);
  143. }
  144. /// @brief Returns existing IPv6 lease for a given DUID+IA+subnet-id combination
  145. ///
  146. /// @param duid ignored
  147. /// @param iaid ignored
  148. /// @param subnet_id ignored
  149. ///
  150. /// @return whatever is set in leases6_ field
  151. virtual Lease6Collection getLeases6(Lease::Type /* not used yet */,
  152. const DUID&, uint32_t, SubnetID) const {
  153. return (leases6_);
  154. }
  155. /// @brief Updates IPv4 lease.
  156. ///
  157. /// @param lease4 The lease to be updated.
  158. ///
  159. /// If no such lease is present, an exception will be thrown.
  160. virtual void updateLease4(const Lease4Ptr&) {}
  161. /// @brief Updates IPv4 lease.
  162. ///
  163. /// @param lease4 The lease to be updated.
  164. ///
  165. /// If no such lease is present, an exception will be thrown.
  166. virtual void updateLease6(const Lease6Ptr&) {}
  167. /// @brief Deletes a lease.
  168. ///
  169. /// @param addr Address of the lease to be deleted. (This can be either
  170. /// a V4 address or a V6 address.)
  171. ///
  172. /// @return true if deletion was successful, false if no such lease exists
  173. virtual bool deleteLease(const isc::asiolink::IOAddress&) {
  174. return (false);
  175. }
  176. /// @brief Returns backend type.
  177. ///
  178. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  179. ///
  180. /// @return Type of the backend.
  181. virtual std::string getType() const {
  182. return (std::string("concrete"));
  183. }
  184. /// @brief Returns backend name.
  185. ///
  186. /// If the backend is a database, this is the name of the database or the
  187. /// file. Otherwise it is just the same as the type.
  188. ///
  189. /// @return Name of the backend.
  190. virtual std::string getName() const {
  191. return (std::string("concrete"));
  192. }
  193. /// @brief Returns description of the backend.
  194. ///
  195. /// This description may be multiline text that describes the backend.
  196. ///
  197. /// @return Description of the backend.
  198. virtual std::string getDescription() const {
  199. return (std::string("This is a dummy concrete backend implementation."));
  200. }
  201. /// @brief Returns backend version.
  202. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  203. return (make_pair(uint32_t(0), uint32_t(0)));
  204. }
  205. /// @brief Commit transactions
  206. virtual void commit() {
  207. }
  208. /// @brief Rollback transactions
  209. virtual void rollback() {
  210. }
  211. // We need to use it in ConcreteLeaseMgr
  212. using LeaseMgr::getLease6;
  213. Lease6Collection leases6_; ///< getLease6 methods return this as is
  214. };
  215. class LeaseMgrTest : public GenericLeaseMgrTest {
  216. public:
  217. LeaseMgrTest() {
  218. }
  219. /// @brief Reopen the database
  220. ///
  221. /// No-op implementation. We need to provide concrete implementation,
  222. /// as this is a pure virtual method in GenericLeaseMgrTest.
  223. virtual void reopen(Universe) {
  224. }
  225. };
  226. namespace {
  227. /// @brief getParameter test
  228. ///
  229. /// This test checks if the LeaseMgr can be instantiated and that it
  230. /// parses parameters string properly.
  231. TEST_F(LeaseMgrTest, getParameter) {
  232. LeaseMgr::ParameterMap pmap;
  233. pmap[std::string("param1")] = std::string("value1");
  234. pmap[std::string("param2")] = std::string("value2");
  235. ConcreteLeaseMgr leasemgr(pmap);
  236. EXPECT_EQ("value1", leasemgr.getParameter("param1"));
  237. EXPECT_EQ("value2", leasemgr.getParameter("param2"));
  238. EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
  239. }
  240. // This test checks if getLease6() method is working properly for 0 (NULL),
  241. // 1 (return the lease) and more than 1 leases (throw).
  242. TEST_F(LeaseMgrTest, getLease6) {
  243. LeaseMgr::ParameterMap pmap;
  244. boost::scoped_ptr<ConcreteLeaseMgr> mgr(new ConcreteLeaseMgr(pmap));
  245. vector<Lease6Ptr> leases = createLeases6();
  246. mgr->leases6_.clear();
  247. // For no leases, the function should return NULL pointer
  248. Lease6Ptr lease;
  249. // the getLease6() is calling getLeases6(), which is a dummy. It returns
  250. // whatever is there in leases6_ field.
  251. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  252. leases[1]->iaid_,
  253. leases[1]->subnet_id_));
  254. EXPECT_TRUE(Lease6Ptr() == lease);
  255. // For a single lease, the function should return that lease
  256. mgr->leases6_.push_back(leases[1]);
  257. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  258. leases[1]->iaid_,
  259. leases[1]->subnet_id_));
  260. EXPECT_TRUE(lease);
  261. EXPECT_NO_THROW(detailCompareLease(lease, leases[1]));
  262. // Add one more lease. There are 2 now. It should throw
  263. mgr->leases6_.push_back(leases[2]);
  264. EXPECT_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  265. leases[1]->iaid_,
  266. leases[1]->subnet_id_),
  267. MultipleRecords);
  268. }
  269. // There's no point in calling any other methods in LeaseMgr, as they
  270. // are purely virtual, so we would only call ConcreteLeaseMgr methods.
  271. // Those methods are just stubs that do not return anything.
  272. }; // end of anonymous namespace