lease_mgr_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <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 Returns expired DHCPv6 leases.
  156. ///
  157. /// This method is not implemented.
  158. virtual void getExpiredLeases6(Lease6Collection&, const size_t) const {
  159. isc_throw(NotImplemented, "ConcreteLeaseMgr::getExpiredLeases6 is not"
  160. " implemented");
  161. }
  162. /// @brief Returns expired DHCPv4 leases.
  163. ///
  164. /// This method is not implemented.
  165. virtual void getExpiredLeases4(Lease4Collection&, const size_t) const {
  166. isc_throw(NotImplemented, "ConcreteLeaseMgr::getExpiredLeases4 is not"
  167. " implemented");
  168. }
  169. /// @brief Updates IPv4 lease.
  170. ///
  171. /// @param lease4 The lease to be updated.
  172. ///
  173. /// If no such lease is present, an exception will be thrown.
  174. virtual void updateLease4(const Lease4Ptr&) {}
  175. /// @brief Updates IPv4 lease.
  176. ///
  177. /// @param lease4 The lease to be updated.
  178. ///
  179. /// If no such lease is present, an exception will be thrown.
  180. virtual void updateLease6(const Lease6Ptr&) {}
  181. /// @brief Deletes a lease.
  182. ///
  183. /// @param addr Address of the lease to be deleted. (This can be either
  184. /// a V4 address or a V6 address.)
  185. ///
  186. /// @return true if deletion was successful, false if no such lease exists
  187. virtual bool deleteLease(const isc::asiolink::IOAddress&) {
  188. return (false);
  189. }
  190. /// @brief Returns backend type.
  191. ///
  192. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  193. ///
  194. /// @return Type of the backend.
  195. virtual std::string getType() const {
  196. return (std::string("concrete"));
  197. }
  198. /// @brief Returns backend name.
  199. ///
  200. /// If the backend is a database, this is the name of the database or the
  201. /// file. Otherwise it is just the same as the type.
  202. ///
  203. /// @return Name of the backend.
  204. virtual std::string getName() const {
  205. return (std::string("concrete"));
  206. }
  207. /// @brief Returns description of the backend.
  208. ///
  209. /// This description may be multiline text that describes the backend.
  210. ///
  211. /// @return Description of the backend.
  212. virtual std::string getDescription() const {
  213. return (std::string("This is a dummy concrete backend implementation."));
  214. }
  215. /// @brief Returns backend version.
  216. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  217. return (make_pair(uint32_t(0), uint32_t(0)));
  218. }
  219. /// @brief Commit transactions
  220. virtual void commit() {
  221. }
  222. /// @brief Rollback transactions
  223. virtual void rollback() {
  224. }
  225. // We need to use it in ConcreteLeaseMgr
  226. using LeaseMgr::getLease6;
  227. Lease6Collection leases6_; ///< getLease6 methods return this as is
  228. };
  229. class LeaseMgrTest : public GenericLeaseMgrTest {
  230. public:
  231. LeaseMgrTest() {
  232. }
  233. /// @brief Reopen the database
  234. ///
  235. /// No-op implementation. We need to provide concrete implementation,
  236. /// as this is a pure virtual method in GenericLeaseMgrTest.
  237. virtual void reopen(Universe) {
  238. }
  239. };
  240. namespace {
  241. /// @brief getParameter test
  242. ///
  243. /// This test checks if the LeaseMgr can be instantiated and that it
  244. /// parses parameters string properly.
  245. TEST_F(LeaseMgrTest, getParameter) {
  246. LeaseMgr::ParameterMap pmap;
  247. pmap[std::string("param1")] = std::string("value1");
  248. pmap[std::string("param2")] = std::string("value2");
  249. ConcreteLeaseMgr leasemgr(pmap);
  250. EXPECT_EQ("value1", leasemgr.getParameter("param1"));
  251. EXPECT_EQ("value2", leasemgr.getParameter("param2"));
  252. EXPECT_THROW(leasemgr.getParameter("param3"), BadValue);
  253. }
  254. // This test checks if getLease6() method is working properly for 0 (NULL),
  255. // 1 (return the lease) and more than 1 leases (throw).
  256. TEST_F(LeaseMgrTest, getLease6) {
  257. LeaseMgr::ParameterMap pmap;
  258. boost::scoped_ptr<ConcreteLeaseMgr> mgr(new ConcreteLeaseMgr(pmap));
  259. vector<Lease6Ptr> leases = createLeases6();
  260. mgr->leases6_.clear();
  261. // For no leases, the function should return NULL pointer
  262. Lease6Ptr lease;
  263. // the getLease6() is calling getLeases6(), which is a dummy. It returns
  264. // whatever is there in leases6_ field.
  265. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  266. leases[1]->iaid_,
  267. leases[1]->subnet_id_));
  268. EXPECT_TRUE(Lease6Ptr() == lease);
  269. // For a single lease, the function should return that lease
  270. mgr->leases6_.push_back(leases[1]);
  271. EXPECT_NO_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  272. leases[1]->iaid_,
  273. leases[1]->subnet_id_));
  274. EXPECT_TRUE(lease);
  275. EXPECT_NO_THROW(detailCompareLease(lease, leases[1]));
  276. // Add one more lease. There are 2 now. It should throw
  277. mgr->leases6_.push_back(leases[2]);
  278. EXPECT_THROW(lease = mgr->getLease6(leasetype6_[1], *leases[1]->duid_,
  279. leases[1]->iaid_,
  280. leases[1]->subnet_id_),
  281. MultipleRecords);
  282. }
  283. // There's no point in calling any other methods in LeaseMgr, as they
  284. // are purely virtual, so we would only call ConcreteLeaseMgr methods.
  285. // Those methods are just stubs that do not return anything.
  286. }; // end of anonymous namespace