lease_mgr_unittest.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (C) 2011-2012 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 <iostream>
  16. #include <sstream>
  17. #include <gtest/gtest.h>
  18. #include <asiolink/io_address.h>
  19. #include <dhcp/lease_mgr.h>
  20. using namespace std;
  21. using namespace isc;
  22. using namespace isc::asiolink;
  23. using namespace isc::dhcp;
  24. // This is a concrete implementation of a Lease database.
  25. // It does not do anything useful now, and is used for abstract LeaseMgr
  26. // class testing. It may later evolve into more useful backend if the
  27. // need arises. We can reuse code from memfile benchmark. See code in
  28. // tests/tools/dhcp-ubench/memfile_bench.{cc|h}
  29. class Memfile_LeaseMgr : public LeaseMgr {
  30. public:
  31. /// @brief The sole lease manager constructor
  32. ///
  33. /// dbconfig is a generic way of passing parameters. Parameters
  34. /// are passed in the "name=value" format, separated by spaces.
  35. /// Values may be enclosed in double quotes, if needed.
  36. ///
  37. /// @param dbconfig database configuration
  38. Memfile_LeaseMgr(const std::string& dbconfig);
  39. /// @brief Destructor (closes file)
  40. virtual ~Memfile_LeaseMgr();
  41. /// @brief Adds an IPv4 lease.
  42. ///
  43. /// @param lease lease to be added
  44. virtual bool addLease(Lease4Ptr lease);
  45. /// @brief Adds an IPv6 lease.
  46. ///
  47. /// @param lease lease to be added
  48. virtual bool addLease(Lease6Ptr lease);
  49. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  50. ///
  51. /// @param addr address of the searched lease
  52. ///
  53. /// @return a collection of leases
  54. virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const;
  55. /// @brief Returns existing IPv4 lease for specific address and subnet
  56. /// @param addr address of the searched lease
  57. /// @param subnet_id ID of the subnet the lease must belong to
  58. ///
  59. /// @return smart pointer to the lease (or NULL if a lease is not found)
  60. virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr,
  61. SubnetID subnet_id) const;
  62. /// @brief Returns existing IPv4 leases for specified hardware address.
  63. ///
  64. /// Although in the usual case there will be only one lease, for mobile
  65. /// clients or clients with multiple static/fixed/reserved leases there
  66. /// can be more than one. Thus return type is a container, not a single
  67. /// pointer.
  68. ///
  69. /// @param hwaddr hardware address of the client
  70. ///
  71. /// @return lease collection
  72. virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;
  73. /// @brief Returns existing IPv4 leases for specified hardware address
  74. /// and a subnet
  75. ///
  76. /// There can be at most one lease for a given HW address in a single
  77. /// pool, so this method with either return a single lease or NULL.
  78. ///
  79. /// @param hwaddr hardware address of the client
  80. /// @param subnet_id identifier of the subnet that lease must belong to
  81. ///
  82. /// @return a pointer to the lease (or NULL if a lease is not found)
  83. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  84. SubnetID subnet_id) const;
  85. /// @brief Returns existing IPv4 lease for specified client-id
  86. ///
  87. /// @param clientid client identifier
  88. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  89. /// @brief Returns existing IPv4 lease for specified client-id
  90. ///
  91. /// There can be at most one lease for a given HW address in a single
  92. /// pool, so this method with either return a single lease or NULL.
  93. ///
  94. /// @param clientid client identifier
  95. /// @param subnet_id identifier of the subnet that lease must belong to
  96. ///
  97. /// @return a pointer to the lease (or NULL if a lease is not found)
  98. virtual Lease4Ptr getLease4(const ClientId& clientid,
  99. SubnetID subnet_id) const;
  100. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  101. ///
  102. /// @param addr address of the searched lease
  103. ///
  104. /// @return smart pointer to the lease (or NULL if a lease is not found)
  105. Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const;
  106. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  107. ///
  108. /// @param duid client DUID
  109. /// @param iaid IA identifier
  110. ///
  111. /// @return collection of IPv6 leases
  112. Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const;
  113. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  114. ///
  115. /// @param duid client DUID
  116. /// @param iaid IA identifier
  117. /// @param subnet_id identifier of the subnet the lease must belong to
  118. ///
  119. /// @return smart pointer to the lease (or NULL if a lease is not found)
  120. Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const;
  121. /// @brief Updates IPv4 lease.
  122. ///
  123. /// @param lease4 The lease to be updated.
  124. ///
  125. /// If no such lease is present, an exception will be thrown.
  126. void updateLease4(Lease4Ptr lease4);
  127. /// @brief Updates IPv4 lease.
  128. ///
  129. /// @param lease4 The lease to be updated.
  130. ///
  131. /// If no such lease is present, an exception will be thrown.
  132. void updateLease6(Lease6Ptr lease6);
  133. /// @brief Deletes a lease.
  134. ///
  135. /// @param addr IPv4 address of the lease to be deleted.
  136. ///
  137. /// @return true if deletion was successful, false if no such lease exists
  138. bool deleteLease4(uint32_t addr);
  139. /// @brief Deletes a lease.
  140. ///
  141. /// @param addr IPv4 address of the lease to be deleted.
  142. ///
  143. /// @return true if deletion was successful, false if no such lease exists
  144. bool deleteLease6(isc::asiolink::IOAddress addr);
  145. /// @brief Returns backend name.
  146. ///
  147. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  148. std::string getName() const { return "memfile"; }
  149. /// @brief Returns description of the backend.
  150. ///
  151. /// This description may be multiline text that describes the backend.
  152. std::string getDescription() const;
  153. /// @brief Returns backend version.
  154. std::string getVersion() const { return "test-version"; }
  155. using LeaseMgr::getParameter;
  156. protected:
  157. };
  158. Memfile_LeaseMgr::Memfile_LeaseMgr(const std::string& dbconfig)
  159. : LeaseMgr(dbconfig) {
  160. }
  161. Memfile_LeaseMgr::~Memfile_LeaseMgr() {
  162. }
  163. bool Memfile_LeaseMgr::addLease(boost::shared_ptr<isc::dhcp::Lease4>) {
  164. return (false);
  165. }
  166. bool Memfile_LeaseMgr::addLease(boost::shared_ptr<isc::dhcp::Lease6>) {
  167. return (false);
  168. }
  169. Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress) const {
  170. return (Lease4Ptr());
  171. }
  172. Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& ) const {
  173. return (Lease4Collection());
  174. }
  175. Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress ,
  176. SubnetID) const {
  177. return (Lease4Ptr());
  178. }
  179. Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr&,
  180. SubnetID) const {
  181. return (Lease4Ptr());
  182. }
  183. Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId&,
  184. SubnetID) const {
  185. return (Lease4Ptr());
  186. }
  187. Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& ) const {
  188. return (Lease4Collection());
  189. }
  190. Lease6Ptr Memfile_LeaseMgr::getLease6(isc::asiolink::IOAddress) const {
  191. return (Lease6Ptr());
  192. }
  193. Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& , uint32_t ) const {
  194. return (Lease6Collection());
  195. }
  196. Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID&, uint32_t,
  197. SubnetID) const {
  198. return (Lease6Ptr());
  199. }
  200. void Memfile_LeaseMgr::updateLease4(Lease4Ptr ) {
  201. }
  202. void Memfile_LeaseMgr::updateLease6(Lease6Ptr ) {
  203. }
  204. bool Memfile_LeaseMgr::deleteLease4(uint32_t ) {
  205. return (false);
  206. }
  207. bool Memfile_LeaseMgr::deleteLease6(isc::asiolink::IOAddress ) {
  208. return (false);
  209. }
  210. std::string Memfile_LeaseMgr::getDescription() const {
  211. return (string("This is a dummy memfile backend implementation.\n"
  212. "It does not offer any useful lease management and its only\n"
  213. "purpose is to test abstract lease manager API."));
  214. }
  215. namespace {
  216. // empty class for now, but may be extended once Addr6 becomes bigger
  217. class LeaseMgrTest : public ::testing::Test {
  218. public:
  219. LeaseMgrTest() {
  220. }
  221. };
  222. // This test checks if the LeaseMgr can be instantiated and that it
  223. // parses parameters string properly.
  224. TEST_F(LeaseMgrTest, constructor) {
  225. // should not throw any exceptions here
  226. Memfile_LeaseMgr * leaseMgr = new Memfile_LeaseMgr("");
  227. delete leaseMgr;
  228. leaseMgr = new Memfile_LeaseMgr("param1=value1 param2=value2");
  229. EXPECT_EQ("value1", leaseMgr->getParameter("param1"));
  230. EXPECT_EQ("value2", leaseMgr->getParameter("param2"));
  231. EXPECT_THROW(leaseMgr->getParameter("param3"), BadValue);
  232. delete leaseMgr;
  233. }
  234. // There's no point in calling any other methods in LeaseMgr, as they
  235. // are purely virtual, so we would only call Memfile_LeaseMgr methods.
  236. // Those methods are just stubs that does not return anything.
  237. // It seems likely that we will need to extend the memfile code for
  238. // allocation engine tests, so we may implement tests that call
  239. // Memfile_LeaseMgr methods then.
  240. }; // end of anonymous namespace