generic_lease_mgr_unittest.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright (C) 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. #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 Test lease retrieval using client id.
  94. void testGetLease4ClientId();
  95. /// @brief Test lease retrieval when leases with NULL client id are present.
  96. void testGetLease4NullClientId();
  97. /// @brief Test lease retrieval using HW address.
  98. void testGetLease4HWAddr1();
  99. /// @brief Check GetLease4 methods - access by Hardware Address
  100. ///
  101. /// Adds leases to the database and checks that they can be accessed using
  102. /// HWAddr information.
  103. void testGetLease4HWAddr2();
  104. /// @brief Test lease retrieval using client id, HW address and subnet id.
  105. void testGetLease4ClientIdHWAddrSubnetId();
  106. // @brief Get lease4 by hardware address (2)
  107. //
  108. // Check that the system can cope with getting a hardware address of
  109. // any size.
  110. void testGetLease4HWAddrSize();
  111. /// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
  112. ///
  113. /// Adds leases to the database and checks that they can be accessed via
  114. /// a combination of hardware address and subnet ID
  115. void testGetLease4HWAddrSubnetId();
  116. /// @brief Get lease4 by hardware address and subnet ID (2)
  117. ///
  118. /// Check that the system can cope with getting a hardware address of
  119. /// any size.
  120. void testGetLease4HWAddrSubnetIdSize();
  121. /// @brief Check GetLease4 methods - access by Client ID
  122. ///
  123. /// Adds leases to the database and checks that they can be accessed via
  124. /// the Client ID.
  125. void testGetLease4ClientId2();
  126. /// @brief Get Lease4 by client ID (2)
  127. ///
  128. /// Check that the system can cope with a client ID of any size.
  129. void testGetLease4ClientIdSize();
  130. /// @brief Check GetLease4 methods - access by Client ID & Subnet ID
  131. ///
  132. /// Adds leases to the database and checks that they can be accessed via
  133. /// a combination of client and subnet IDs.
  134. void testGetLease4ClientIdSubnetId();
  135. /// @brief Basic Lease4 Checks
  136. ///
  137. /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),
  138. /// updateLease4() and deleteLease (IPv4 address) can handle NULL client-id.
  139. /// (client-id is optional and may not be present)
  140. ///
  141. /// @todo: check if it does overlap with @ref testGetLease4NullClientId()
  142. void testLease4NullClientId();
  143. /// @brief Check that the DHCPv4 lease can be added, removed and recreated.
  144. ///
  145. /// This test creates a lease, removes it and then recreates it with some
  146. /// of the attributes changed. Next it verifies that the lease in the
  147. /// persistent storage has been updated as expected.
  148. void testRecreateLease4();
  149. /// @brief Basic Lease6 Checks
  150. ///
  151. /// Checks that the addLease, getLease6 (by address) and deleteLease (with an
  152. /// IPv6 address) works.
  153. void testBasicLease6();
  154. /// @brief Test that IPv6 lease can be added, retrieved and deleted.
  155. ///
  156. /// This method checks basic IPv6 lease operations. There's check_t1_t2
  157. /// parameter that controls whether the backend supports storing T1, T2
  158. /// parameters. memfile supports it, while MySQL doesn't. If T1,T2
  159. /// storage is not supported, the expected values are 0.
  160. ///
  161. /// @param check_t1_t2 controls whether T1,T2 timers should be checked
  162. void testAddGetDelete6(bool check_t1_t2);
  163. /// @brief Check GetLease6 methods - access by DUID/IAID
  164. ///
  165. /// Adds leases to the database and checks that they can be accessed via
  166. /// a combination of DUID and IAID.
  167. void testGetLeases6DuidIaid();
  168. /// @brief Check that the system can cope with a DUID of allowed size.
  169. void testGetLeases6DuidSize();
  170. /// @brief Check that getLease6 methods discriminate by lease type.
  171. ///
  172. /// Adds six leases, two per lease type all with the same duid and iad but
  173. /// with alternating subnet_ids.
  174. /// It then verifies that all of getLeases6() method variants correctly
  175. /// discriminate between the leases based on lease type alone.
  176. void testLease6LeaseTypeCheck();
  177. /// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
  178. ///
  179. /// Adds leases to the database and checks that they can be accessed via
  180. /// a combination of DIUID and IAID.
  181. void testGetLease6DuidIaidSubnetId();
  182. /// @brief Checks that getLease6() works with different DUID sizes
  183. void testGetLease6DuidIaidSubnetIdSize();
  184. /// @brief Verify that too long hostname for Lease4 is not accepted.
  185. ///
  186. /// Checks that the it is not possible to create a lease when the hostname
  187. /// length exceeds 255 characters.
  188. void testLease4InvalidHostname();
  189. /// @brief Verify that too long hostname for Lease6 is not accepted.
  190. ///
  191. /// Checks that the it is not possible to create a lease when the hostname
  192. /// length exceeds 255 characters.
  193. void testLease6InvalidHostname();
  194. /// @brief Lease4 update test
  195. ///
  196. /// Checks that the code is able to update an IPv4 lease in the database.
  197. void testUpdateLease4();
  198. /// @brief Lease6 update test
  199. ///
  200. /// Checks that the code is able to update an IPv6 lease in the database.
  201. void testUpdateLease6();
  202. /// @brief Check that the DHCPv6 lease can be added, removed and recreated.
  203. ///
  204. /// This test creates a lease, removes it and then recreates it with some
  205. /// of the attributes changed. Next it verifies that the lease in the
  206. /// persistent storage has been updated as expected.
  207. void testRecreateLease6();
  208. /// @brief String forms of IPv4 addresses
  209. std::vector<std::string> straddress4_;
  210. /// @brief IOAddress forms of IPv4 addresses
  211. std::vector<isc::asiolink::IOAddress> ioaddress4_;
  212. /// @brief String forms of IPv6 addresses
  213. std::vector<std::string> straddress6_;
  214. /// @brief Types of IPv6 Leases
  215. std::vector<Lease::Type> leasetype6_;
  216. /// @brief IOAddress forms of IPv6 addresses
  217. std::vector<isc::asiolink::IOAddress> ioaddress6_;
  218. /// @brief Pointer to the lease manager
  219. LeaseMgr* lmptr_;
  220. };
  221. }; // namespace test
  222. }; // namespace dhcp
  223. }; // namespace isc
  224. #endif