alloc_engine_utils.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright (C) 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 LIBDHCPSRV_ALLOC_ENGINE_UTILS_H
  15. #define LIBDHCPSRV_ALLOC_ENGINE_UTILS_H
  16. #include <dhcpsrv/lease_mgr.h>
  17. #include <dhcpsrv/lease_mgr_factory.h>
  18. #include <dhcpsrv/alloc_engine.h>
  19. #include <dhcpsrv/cfgmgr.h>
  20. #include <asiolink/io_address.h>
  21. #include <gtest/gtest.h>
  22. #include <vector>
  23. namespace isc {
  24. namespace dhcp {
  25. namespace test {
  26. /// @file alloc_engine_utils.h
  27. ///
  28. /// @brief This is a header file for all Allocation Engine tests.
  29. ///
  30. /// There used to be one, huge (over 3kloc) alloc_engine_unittest.cc. It is now
  31. /// split into serveral smaller files:
  32. /// alloc_engine_utils.h - contains test class definitions (this file)
  33. /// alloc_engine_utils.cc - contains test class implementation
  34. /// alloc_engine4_unittest.cc - all unit-tests dedicated to IPv4
  35. /// alloc_engine6_unittest.cc - all unit-tests dedicated to IPv6
  36. /// alloc_engine_hooks_unittest.cc - all unit-tests dedicated to hooks
  37. /// @brief Allocation engine with some internal methods exposed
  38. class NakedAllocEngine : public AllocEngine {
  39. public:
  40. /// @brief the sole constructor
  41. /// @param engine_type specifies engine type (e.g. iterative)
  42. /// @param attempts number of lease selection attempts before giving up
  43. /// @param ipv6 specifies if the engine is IPv6 or IPv4
  44. NakedAllocEngine(AllocEngine::AllocType engine_type,
  45. unsigned int attempts, bool ipv6 = true)
  46. :AllocEngine(engine_type, attempts, ipv6) {
  47. }
  48. // Expose internal classes for testing purposes
  49. using AllocEngine::Allocator;
  50. using AllocEngine::IterativeAllocator;
  51. using AllocEngine::getAllocator;
  52. /// @brief IterativeAllocator with internal methods exposed
  53. class NakedIterativeAllocator: public AllocEngine::IterativeAllocator {
  54. public:
  55. /// @brief constructor
  56. /// @param type pool types that will be interated
  57. NakedIterativeAllocator(Lease::Type type)
  58. :IterativeAllocator(type) {
  59. }
  60. using AllocEngine::IterativeAllocator::increasePrefix;
  61. };
  62. };
  63. /// @brief Used in Allocation Engine tests for IPv6
  64. class AllocEngine6Test : public ::testing::Test {
  65. public:
  66. /// @brief Default constructor
  67. ///
  68. /// Sets duid_, iaid_, subnet_, pool_ fields to example values used
  69. /// in many tests, initializes cfg_mgr configuration and creates
  70. /// lease database.
  71. AllocEngine6Test();
  72. /// @brief Configures a subnet and adds one pool to it.
  73. ///
  74. /// This function removes existing v6 subnets before configuring
  75. /// a new one.
  76. ///
  77. /// @param subnet Address of a subnet to be configured.
  78. /// @param pool_start First address in the address pool.
  79. /// @param pool_end Last address in the address pool.
  80. void initSubnet(const asiolink::IOAddress& subnet,
  81. const asiolink::IOAddress& pool_start,
  82. const asiolink::IOAddress& pool_end);
  83. /// @brief Initializes FQDN data for a test.
  84. ///
  85. /// The initialized values are used by the test fixture class members to
  86. /// verify the correctness of a lease.
  87. ///
  88. /// @param hostname Hostname to be assigned to a lease.
  89. /// @param fqdn_fwd Indicates whether or not to perform forward DNS update
  90. /// for a lease.
  91. /// @param fqdn_fwd Indicates whether or not to perform reverse DNS update
  92. /// for a lease.
  93. void initFqdn(const std::string& hostname, const bool fqdn_fwd,
  94. const bool fqdn_rev) {
  95. hostname_ = hostname;
  96. fqdn_fwd_ = fqdn_fwd;
  97. fqdn_rev_ = fqdn_rev;
  98. }
  99. /// @brief Wrapper around call to AllocEngine6::findRervation
  100. ///
  101. /// If a reservation is found by the engine, the function sets
  102. /// ctx.hostname_ accordingly.
  103. ///
  104. /// @param engine allocation engine to use
  105. /// @param ctx client context to pass into engine's findReservation method
  106. void findReservation(AllocEngine& engine, AllocEngine::ClientContext6& ctx);
  107. /// @brief attempts to convert leases collection to a single lease
  108. ///
  109. /// This operation makes sense if there is at most one lease in the
  110. /// collection. Otherwise it will throw.
  111. ///
  112. /// @param col collection of leases (zero or one leases allowed)
  113. /// @throw MultipleRecords if there is more than one lease
  114. /// @return Lease6 pointer (or NULL if collection was empty)
  115. Lease6Ptr expectOneLease(const Lease6Collection& col) {
  116. if (col.size() > 1) {
  117. isc_throw(MultipleRecords, "More than one lease found in collection");
  118. }
  119. if (col.empty()) {
  120. return (Lease6Ptr());
  121. }
  122. return (*col.begin());
  123. }
  124. /// @brief checks if Lease6 matches expected configuration
  125. ///
  126. /// @param lease lease to be checked
  127. /// @param exp_type expected lease type
  128. /// @param exp_pd_len expected prefix length
  129. /// @param expected_in_subnet whether the lease is expected to be in subnet
  130. /// @param expected_in_pool whether the lease is expected to be in dynamic
  131. void checkLease6(const Lease6Ptr& lease, Lease::Type exp_type,
  132. uint8_t exp_pd_len = 128, bool expected_in_subnet = true,
  133. bool expected_in_pool = true) {
  134. // that is belongs to the right subnet
  135. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  136. if (expected_in_subnet) {
  137. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  138. } else {
  139. EXPECT_FALSE(subnet_->inRange(lease->addr_));
  140. }
  141. if (expected_in_pool) {
  142. EXPECT_TRUE(subnet_->inPool(exp_type, lease->addr_));
  143. } else {
  144. EXPECT_FALSE(subnet_->inPool(exp_type, lease->addr_));
  145. }
  146. // that it have proper parameters
  147. EXPECT_EQ(exp_type, lease->type_);
  148. EXPECT_EQ(iaid_, lease->iaid_);
  149. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  150. EXPECT_EQ(subnet_->getPreferred(), lease->preferred_lft_);
  151. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  152. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  153. EXPECT_EQ(exp_pd_len, lease->prefixlen_);
  154. EXPECT_EQ(fqdn_fwd_, lease->fqdn_fwd_);
  155. EXPECT_EQ(fqdn_rev_, lease->fqdn_rev_);
  156. EXPECT_EQ(hostname_, lease->hostname_);
  157. EXPECT_TRUE(*lease->duid_ == *duid_);
  158. /// @todo: check cltt
  159. }
  160. /// @brief Checks if specified address is increased properly
  161. ///
  162. /// Method uses gtest macros to mark check failure. This is a proxy
  163. /// method, since increaseAddress was moved to IOAddress class.
  164. ///
  165. /// @param alloc IterativeAllocator that is tested
  166. /// @param input address to be increased
  167. /// @param exp_output expected address after increase
  168. void
  169. checkAddrIncrease(NakedAllocEngine::NakedIterativeAllocator&,
  170. std::string input, std::string exp_output) {
  171. EXPECT_EQ(exp_output, asiolink::IOAddress::increase(
  172. asiolink::IOAddress(input)).toText());
  173. }
  174. /// @brief Checks if increasePrefix() works as expected
  175. ///
  176. /// Method uses gtest macros to mark check failure.
  177. ///
  178. /// @param alloc allocator to be tested
  179. /// @param input IPv6 prefix (as a string)
  180. /// @param prefix_len prefix len
  181. /// @param exp_output expected output (string)
  182. void
  183. checkPrefixIncrease(NakedAllocEngine::NakedIterativeAllocator& alloc,
  184. std::string input, uint8_t prefix_len,
  185. std::string exp_output) {
  186. EXPECT_EQ(exp_output, alloc.increasePrefix(asiolink::IOAddress(input),
  187. prefix_len).toText());
  188. }
  189. /// @brief Checks if the simple allocation can succeed
  190. ///
  191. /// The type of lease is determined by pool type (pool->getType()
  192. ///
  193. /// @param pool pool from which the lease will be allocated from
  194. /// @param hint address to be used as a hint
  195. /// @param fake true - this is fake allocation (SOLICIT)
  196. /// @param in_pool specifies whether the lease is expected to be in pool
  197. /// @return allocated lease (or NULL)
  198. Lease6Ptr simpleAlloc6Test(const Pool6Ptr& pool,
  199. const asiolink::IOAddress& hint,
  200. bool fake, bool in_pool = true);
  201. /// @brief Checks if the allocation can succeed.
  202. ///
  203. /// The type of lease is determined by pool type (pool->getType()).
  204. /// This test is particularly useful in connection with @ref renewTest.
  205. ///
  206. /// @param engine a reference to Allocation Engine
  207. /// @param pool pool from which the lease will be allocated from
  208. /// @param hint address to be used as a hint
  209. /// @param fake true - this is fake allocation (SOLICIT)
  210. /// @param in_pool specifies whether the lease is expected to be in pool
  211. /// @return allocated lease(s) (may be empty)
  212. Lease6Collection allocateTest(AllocEngine& engine, const Pool6Ptr& pool,
  213. const asiolink::IOAddress& hint, bool fake,
  214. bool in_pool = true);
  215. /// @brief Checks if the allocation can be renewed.
  216. ///
  217. /// The type of lease is determined by pool type (pool->getType()).
  218. /// This test is particularly useful as a follow up to @ref allocateTest.
  219. ///
  220. /// @param engine a reference to Allocation Engine
  221. /// @param pool pool from which the lease will be allocated from
  222. /// @param hints address to be used as a hint
  223. /// @param allow_new_leases_in_renewal - specifies how to set the
  224. /// allow_new_leases_in_renewal flag in ClientContext6
  225. /// @param in_pool specifies whether the lease is expected to be in pool
  226. /// @return allocated lease(s) (may be empty)
  227. Lease6Collection renewTest(AllocEngine& engine, const Pool6Ptr& pool,
  228. AllocEngine::HintContainer& hints,
  229. bool allow_new_leases_in_renewal,
  230. bool in_pool = true);
  231. /// @brief Checks if the address allocation with a hint that is in range,
  232. /// in pool, but is currently used, can succeed
  233. ///
  234. /// Method uses gtest macros to mark check failure.
  235. ///
  236. /// @param type lease type
  237. /// @param used_addr address should be preallocated (simulates prior
  238. /// allocation by some other user)
  239. /// @param requested address requested by the client
  240. /// @param expected_pd_len expected PD len (128 for addresses)
  241. void allocWithUsedHintTest(Lease::Type type, asiolink::IOAddress used_addr,
  242. asiolink::IOAddress requested,
  243. uint8_t expected_pd_len);
  244. /// @brief checks if bogus hint can be ignored and the allocation succeeds
  245. ///
  246. /// This test checks if the allocation with a hing that is out of the blue
  247. /// can succeed. The invalid hint should be ignored completely.
  248. ///
  249. /// @param type Lease type
  250. /// @param hint hint (as send by a client)
  251. /// @param expected_pd_len (used in validation)
  252. void allocBogusHint6(Lease::Type type, asiolink::IOAddress hint,
  253. uint8_t expected_pd_len);
  254. /// @brief Utility function that creates a host reservation (duid)
  255. ///
  256. /// @param add_to_host_mgr true if the reservation should be added
  257. /// @param type specifies reservation type
  258. /// @param addr specifies reserved address or prefix
  259. /// @param prefix_len prefix length (should be 128 for addresses)
  260. /// @return created Host object.
  261. HostPtr
  262. createHost6(bool add_to_host_mgr, IPv6Resrv::Type type,
  263. const asiolink::IOAddress& addr, uint8_t prefix_len) {
  264. HostPtr host(new Host(&duid_->getDuid()[0], duid_->getDuid().size(),
  265. Host::IDENT_DUID, SubnetID(0), subnet_->getID(),
  266. asiolink::IOAddress("0.0.0.0")));
  267. IPv6Resrv resv(type, addr, prefix_len);
  268. host->addReservation(resv);
  269. if (add_to_host_mgr) {
  270. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  271. CfgMgr::instance().commit();
  272. }
  273. return (host);
  274. }
  275. /// @brief Utility function that creates a host reservation (hwaddr)
  276. ///
  277. /// @param add_to_host_mgr true if the reservation should be added
  278. /// @param type specifies reservation type
  279. /// @param hwaddr hardware address to be reserved to
  280. /// @param addr specifies reserved address or prefix
  281. /// @param prefix_len prefix length (should be 128 for addresses)
  282. /// @return created Host object.
  283. HostPtr
  284. createHost6HWAddr(bool add_to_host_mgr, IPv6Resrv::Type type,
  285. HWAddrPtr& hwaddr, const asiolink::IOAddress& addr,
  286. uint8_t prefix_len);
  287. virtual ~AllocEngine6Test() {
  288. factory_.destroy();
  289. }
  290. DuidPtr duid_; ///< client-identifier (value used in tests)
  291. HWAddrPtr hwaddr_; ///< client's hardware address
  292. uint32_t iaid_; ///< IA identifier (value used in tests)
  293. Subnet6Ptr subnet_; ///< subnet6 (used in tests)
  294. Pool6Ptr pool_; ///< NA pool belonging to subnet_
  295. Pool6Ptr pd_pool_; ///< PD pool belonging to subnet_
  296. std::string hostname_; ///< Hostname
  297. bool fqdn_fwd_; ///< Perform forward update for a lease.
  298. bool fqdn_rev_; ///< Perform reverse update for a lease.
  299. LeaseMgrFactory factory_; ///< pointer to LeaseMgr factory
  300. };
  301. /// @brief Used in Allocation Engine tests for IPv4
  302. class AllocEngine4Test : public ::testing::Test {
  303. public:
  304. /// @brief Default constructor
  305. ///
  306. /// Sets clientid_, hwaddr_, subnet_, pool_ fields to example values
  307. /// used in many tests, initializes cfg_mgr configuration and creates
  308. /// lease database.
  309. ///
  310. /// It also re-initializes the Host Manager.
  311. AllocEngine4Test();
  312. /// @brief checks if Lease4 matches expected configuration
  313. ///
  314. /// @param lease lease to be checked
  315. void checkLease4(const Lease4Ptr& lease) {
  316. // Check that is belongs to the right subnet
  317. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  318. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  319. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, lease->addr_));
  320. // Check that it has proper parameters
  321. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  322. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  323. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  324. if (lease->client_id_ && !clientid_) {
  325. ADD_FAILURE() << "Lease4 has a client-id, while it should have none.";
  326. } else
  327. if (!lease->client_id_ && clientid_) {
  328. ADD_FAILURE() << "Lease4 has no client-id, but it was expected to have one.";
  329. } else
  330. if (lease->client_id_ && clientid_) {
  331. EXPECT_TRUE(*lease->client_id_ == *clientid_);
  332. }
  333. EXPECT_TRUE(*lease->hwaddr_ == *hwaddr_);
  334. /// @todo: check cltt
  335. }
  336. /// @brief Create a subnet with a specified pool of addresses.
  337. ///
  338. /// @param pool_start First address in the pool.
  339. /// @param pool_end Last address in the pool.
  340. void initSubnet(const asiolink::IOAddress& pool_start,
  341. const asiolink::IOAddress& pool_end);
  342. virtual ~AllocEngine4Test() {
  343. factory_.destroy();
  344. }
  345. ClientIdPtr clientid_; ///< Client-identifier (value used in tests)
  346. HWAddrPtr hwaddr_; ///< Hardware address (value used in tests)
  347. HWAddrPtr hwaddr2_; ///< Alternative hardware address.
  348. Subnet4Ptr subnet_; ///< Subnet4 (used in tests)
  349. Pool4Ptr pool_; ///< Pool belonging to subnet_
  350. LeaseMgrFactory factory_; ///< Pointer to LeaseMgr factory
  351. AllocEngine::ClientContext4 ctx_; ///< Context information passed to various
  352. ///< allocation engine functions.
  353. };
  354. }; // namespace test
  355. }; // namespace dhcp
  356. }; // namespace isc
  357. #endif