generic_lease_mgr_unittest.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (C) 2014-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. #ifndef GENERIC_LEASE_MGR_UNITTEST_H
  15. #define GENERIC_LEASE_MGR_UNITTEST_H
  16. #include <dhcpsrv/lease_mgr.h>
  17. #include <gtest/gtest.h>
  18. #include <vector>
  19. namespace isc {
  20. namespace dhcp {
  21. namespace test {
  22. /// @brief Test Fixture class with utility functions for LeaseMgr backends
  23. ///
  24. /// It contains utility functions, like dummy lease creation.
  25. /// All concrete LeaseMgr test classes should be derived from it.
  26. class GenericLeaseMgrTest : public ::testing::Test {
  27. public:
  28. /// @brief Universe (V4 or V6).
  29. enum Universe {
  30. V4,
  31. V6
  32. };
  33. /// @brief Default constructor.
  34. GenericLeaseMgrTest();
  35. /// @brief Virtual destructor.
  36. virtual ~GenericLeaseMgrTest();
  37. /// @brief Reopen the database
  38. ///
  39. /// Closes the database and re-opens it. It must be implemented
  40. /// in derived classes.
  41. ///
  42. /// @param u Universe (V4 or V6), required by some backends.
  43. virtual void reopen(Universe u = V4) = 0;
  44. /// @brief Initialize Lease4 Fields
  45. ///
  46. /// Returns a pointer to a Lease4 structure. Different values are put into
  47. /// the lease according to the address passed.
  48. ///
  49. /// This is just a convenience function for the test methods.
  50. ///
  51. /// @param address Address to use for the initialization
  52. ///
  53. /// @return Lease4Ptr. This will not point to anything if the
  54. /// initialization failed (e.g. unknown address).
  55. Lease4Ptr initializeLease4(std::string address);
  56. /// @brief Initialize Lease6 Fields
  57. ///
  58. /// Returns a pointer to a Lease6 structure. Different values are put into
  59. /// the lease according to the address passed.
  60. ///
  61. /// This is just a convenience function for the test methods.
  62. ///
  63. /// @param address Address to use for the initialization
  64. ///
  65. /// @return Lease6Ptr. This will not point to anything if the initialization
  66. /// failed (e.g. unknown address).
  67. Lease6Ptr initializeLease6(std::string address);
  68. /// @brief Check Leases present and different
  69. ///
  70. /// Checks a vector of lease pointers and ensures that all the leases
  71. /// they point to are present and different. If not, a GTest assertion
  72. /// will fail.
  73. ///
  74. /// @param leases Vector of pointers to leases
  75. /// @tparam Type of the leases held in the vector: @c Lease4 or
  76. /// @c Lease6.
  77. template <typename T>
  78. void checkLeasesDifferent(const std::vector<T>& leases) const;
  79. /// @brief Creates leases for the test
  80. ///
  81. /// Creates all leases for the test and checks that they are different.
  82. ///
  83. /// @return vector<Lease4Ptr> Vector of pointers to leases
  84. std::vector<Lease4Ptr> createLeases4();
  85. /// @brief Creates leases for the test
  86. ///
  87. /// Creates all leases for the test and checks that they are different.
  88. ///
  89. /// @return vector<Lease6Ptr> Vector of pointers to leases
  90. std::vector<Lease6Ptr> createLeases6();
  91. /// @brief checks that addLease, getLease4(addr) and deleteLease() works
  92. void testBasicLease4();
  93. /// @brief checks that invalid dates are safely handled.
  94. void testMaxDate4();
  95. /// @brief Test lease retrieval using client id.
  96. void testGetLease4ClientId();
  97. /// @brief Test lease retrieval when leases with NULL client id are present.
  98. void testGetLease4NullClientId();
  99. /// @brief Test lease retrieval using HW address.
  100. void testGetLease4HWAddr1();
  101. /// @brief Check GetLease4 methods - access by Hardware Address
  102. ///
  103. /// Adds leases to the database and checks that they can be accessed using
  104. /// HWAddr information.
  105. void testGetLease4HWAddr2();
  106. /// @brief Test lease retrieval using client id, HW address and subnet id.
  107. void testGetLease4ClientIdHWAddrSubnetId();
  108. // @brief Get lease4 by hardware address (2)
  109. //
  110. // Check that the system can cope with getting a hardware address of
  111. // any size.
  112. void testGetLease4HWAddrSize();
  113. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  114. ///
  115. /// Adds leases to the database and checks that they can be accessed via
  116. /// a combination of hardware address and subnet ID
  117. void testGetLease4HWAddrSubnetId();
  118. /// @brief Get lease4 by hardware address and subnet ID (2)
  119. ///
  120. /// Check that the system can cope with getting a hardware address of
  121. /// any size.
  122. void testGetLease4HWAddrSubnetIdSize();
  123. /// @brief Check GetLease4 methods - access by Client ID
  124. ///
  125. /// Adds leases to the database and checks that they can be accessed via
  126. /// the Client ID.
  127. void testGetLease4ClientId2();
  128. /// @brief Get Lease4 by client ID (2)
  129. ///
  130. /// Check that the system can cope with a client ID of any size.
  131. void testGetLease4ClientIdSize();
  132. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  133. ///
  134. /// Adds leases to the database and checks that they can be accessed via
  135. /// a combination of client and subnet IDs.
  136. void testGetLease4ClientIdSubnetId();
  137. /// @brief Basic Lease4 Checks
  138. ///
  139. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  140. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  141. /// (client-id is optional and may not be present)
  142. ///
  143. /// @todo: check if it does overlap with @ref testGetLease4NullClientId()
  144. void testLease4NullClientId();
  145. /// @brief Check that the DHCPv4 lease can be added, removed and recreated.
  146. ///
  147. /// This test creates a lease, removes it and then recreates it with some
  148. /// of the attributes changed. Next it verifies that the lease in the
  149. /// persistent storage has been updated as expected.
  150. void testRecreateLease4();
  151. /// @brief Basic Lease6 Checks
  152. ///
  153. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  154. /// IPv6 address) works.
  155. void testBasicLease6();
  156. /// @brief Checks that invalid dates are safely handled.
  157. void testMaxDate6();
  158. /// @brief Checks that Lease6 can be stored with and without a hardware address.
  159. void testLease6MAC();
  160. /// @brief Checks that Lease6 stores hardware type and hardware source.
  161. void testLease6HWTypeAndSource();
  162. /// @brief Test that IPv6 lease can be added, retrieved and deleted.
  163. ///
  164. /// This method checks basic IPv6 lease operations. There's check_t1_t2
  165. /// parameter that controls whether the backend supports storing T1, T2
  166. /// parameters. memfile supports it, while MySQL doesn't. If T1,T2
  167. /// storage is not supported, the expected values are 0.
  168. ///
  169. /// @param check_t1_t2 controls whether T1,T2 timers should be checked
  170. void testAddGetDelete6(bool check_t1_t2);
  171. /// @brief Check GetLease6 methods - access by DUID/IAID
  172. ///
  173. /// Adds leases to the database and checks that they can be accessed via
  174. /// a combination of DUID and IAID.
  175. void testGetLeases6DuidIaid();
  176. /// @brief Check that the system can cope with a DUID of allowed size.
  177. void testGetLeases6DuidSize();
  178. /// @brief Check that getLease6 methods discriminate by lease type.
  179. ///
  180. /// Adds six leases, two per lease type all with the same duid and iad but
  181. /// with alternating subnet_ids.
  182. /// It then verifies that all of getLeases6() method variants correctly
  183. /// discriminate between the leases based on lease type alone.
  184. void testLease6LeaseTypeCheck();
  185. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  186. ///
  187. /// Adds leases to the database and checks that they can be accessed via
  188. /// a combination of DIUID and IAID.
  189. void testGetLease6DuidIaidSubnetId();
  190. /// @brief Checks that getLease6() works with different DUID sizes
  191. void testGetLease6DuidIaidSubnetIdSize();
  192. /// @brief Verify that too long hostname for Lease4 is not accepted.
  193. ///
  194. /// Checks that the it is not possible to create a lease when the hostname
  195. /// length exceeds 255 characters.
  196. void testLease4InvalidHostname();
  197. /// @brief Verify that too long hostname for Lease6 is not accepted.
  198. ///
  199. /// Checks that the it is not possible to create a lease when the hostname
  200. /// length exceeds 255 characters.
  201. void testLease6InvalidHostname();
  202. /// @brief Lease4 update test
  203. ///
  204. /// Checks that the code is able to update an IPv4 lease in the database.
  205. void testUpdateLease4();
  206. /// @brief Lease6 update test
  207. ///
  208. /// Checks that the code is able to update an IPv6 lease in the database.
  209. void testUpdateLease6();
  210. /// @brief Check that the DHCPv6 lease can be added, removed and recreated.
  211. ///
  212. /// This test creates a lease, removes it and then recreates it with some
  213. /// of the attributes changed. Next it verifies that the lease in the
  214. /// persistent storage has been updated as expected.
  215. void testRecreateLease6();
  216. /// @brief Verifies that a null DUID is not allowed.
  217. void testNullDuid();
  218. /// @brief Verifies that the backend reports expected version numbers.
  219. /// @param major Expected major version to be reported.
  220. /// @param minor Expected minor version to be reported.
  221. void testVersion(int major, int minor);
  222. /// @brief Checks that the expired DHCPv4 leases can be retrieved.
  223. ///
  224. /// This test checks the following:
  225. /// - all expired and not reclaimed leases are retured
  226. /// - number of leases returned can be limited
  227. /// - leases are returned in the order from the most expired to the
  228. /// least expired
  229. /// - reclaimed leases are not returned.
  230. void testGetExpiredLeases4();
  231. /// @brief Checks that the expired DHCPv6 leases can be retrieved.
  232. ///
  233. /// This test checks the following:
  234. /// - all expired and not reclaimed leases are retured
  235. /// - number of leases returned can be limited
  236. /// - leases are returned in the order from the most expired to the
  237. /// least expired
  238. /// - reclaimed leases are not returned.
  239. void testGetExpiredLeases6();
  240. /// @brief Checks that selected expired-reclaimed DHCPv6 leases
  241. /// are removed.
  242. ///
  243. /// This creates a number of DHCPv6 leases and marks some of them
  244. /// as expired-reclaimed. It later verifies that the expired-reclaimed
  245. /// leases can be removed.
  246. void testDeleteExpiredReclaimedLeases6();
  247. /// @brief Checks that selected expired-reclaimed DHCPv4 leases
  248. /// are removed.
  249. ///
  250. /// This creates a number of DHCPv4 leases and marks some of them
  251. /// as expired-reclaimed. It later verifies that the expired-reclaimed
  252. /// leases can be removed.
  253. void testDeleteExpiredReclaimedLeases4();
  254. /// @brief String forms of IPv4 addresses
  255. std::vector<std::string> straddress4_;
  256. /// @brief IOAddress forms of IPv4 addresses
  257. std::vector<isc::asiolink::IOAddress> ioaddress4_;
  258. /// @brief String forms of IPv6 addresses
  259. std::vector<std::string> straddress6_;
  260. /// @brief Types of IPv6 Leases
  261. std::vector<Lease::Type> leasetype6_;
  262. /// @brief IOAddress forms of IPv6 addresses
  263. std::vector<isc::asiolink::IOAddress> ioaddress6_;
  264. /// @brief Pointer to the lease manager
  265. LeaseMgr* lmptr_;
  266. };
  267. }; // namespace test
  268. }; // namespace dhcp
  269. }; // namespace isc
  270. #endif