alloc_engine_utils.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 Test that statistic manager holds a given value.
  38. ///
  39. /// This function may be used in many allocation tests and there's no
  40. /// single base class for it. @todo consider moving it src/lib/util.
  41. ///
  42. /// @param stat_name Statistic name.
  43. /// @param exp_value Expected value.
  44. ///
  45. /// @return true if the statistic manager holds a particular value,
  46. /// false otherwise.
  47. bool testStatistics(const std::string& stat_name, const int64_t exp_value);
  48. /// @brief Allocation engine with some internal methods exposed
  49. class NakedAllocEngine : public AllocEngine {
  50. public:
  51. /// @brief the sole constructor
  52. /// @param engine_type specifies engine type (e.g. iterative)
  53. /// @param attempts number of lease selection attempts before giving up
  54. /// @param ipv6 specifies if the engine is IPv6 or IPv4
  55. NakedAllocEngine(AllocEngine::AllocType engine_type,
  56. unsigned int attempts, bool ipv6 = true)
  57. :AllocEngine(engine_type, attempts, ipv6) {
  58. }
  59. // Expose internal classes for testing purposes
  60. using AllocEngine::Allocator;
  61. using AllocEngine::IterativeAllocator;
  62. using AllocEngine::getAllocator;
  63. /// @brief IterativeAllocator with internal methods exposed
  64. class NakedIterativeAllocator: public AllocEngine::IterativeAllocator {
  65. public:
  66. /// @brief constructor
  67. /// @param type pool types that will be interated
  68. NakedIterativeAllocator(Lease::Type type)
  69. :IterativeAllocator(type) {
  70. }
  71. using AllocEngine::IterativeAllocator::increasePrefix;
  72. };
  73. };
  74. /// @brief Used in Allocation Engine tests for IPv6
  75. class AllocEngine6Test : public ::testing::Test {
  76. public:
  77. /// @brief Default constructor
  78. ///
  79. /// Sets duid_, iaid_, subnet_, pool_ fields to example values used
  80. /// in many tests, initializes cfg_mgr configuration and creates
  81. /// lease database.
  82. AllocEngine6Test();
  83. /// @brief Configures a subnet and adds one pool to it.
  84. ///
  85. /// This function removes existing v6 subnets before configuring
  86. /// a new one.
  87. ///
  88. /// @param subnet Address of a subnet to be configured.
  89. /// @param pool_start First address in the address pool.
  90. /// @param pool_end Last address in the address pool.
  91. void initSubnet(const asiolink::IOAddress& subnet,
  92. const asiolink::IOAddress& pool_start,
  93. const asiolink::IOAddress& pool_end);
  94. /// @brief Initializes FQDN data for a test.
  95. ///
  96. /// The initialized values are used by the test fixture class members to
  97. /// verify the correctness of a lease.
  98. ///
  99. /// @param hostname Hostname to be assigned to a lease.
  100. /// @param fqdn_fwd Indicates whether or not to perform forward DNS update
  101. /// for a lease.
  102. /// @param fqdn_fwd Indicates whether or not to perform reverse DNS update
  103. /// for a lease.
  104. void initFqdn(const std::string& hostname, const bool fqdn_fwd,
  105. const bool fqdn_rev) {
  106. hostname_ = hostname;
  107. fqdn_fwd_ = fqdn_fwd;
  108. fqdn_rev_ = fqdn_rev;
  109. }
  110. /// @brief Wrapper around call to AllocEngine6::findRervation
  111. ///
  112. /// If a reservation is found by the engine, the function sets
  113. /// ctx.hostname_ accordingly.
  114. ///
  115. /// @param engine allocation engine to use
  116. /// @param ctx client context to pass into engine's findReservation method
  117. void findReservation(AllocEngine& engine, AllocEngine::ClientContext6& ctx);
  118. /// @brief attempts to convert leases collection to a single lease
  119. ///
  120. /// This operation makes sense if there is at most one lease in the
  121. /// collection. Otherwise it will throw.
  122. ///
  123. /// @param col collection of leases (zero or one leases allowed)
  124. /// @throw MultipleRecords if there is more than one lease
  125. /// @return Lease6 pointer (or NULL if collection was empty)
  126. Lease6Ptr expectOneLease(const Lease6Collection& col) {
  127. if (col.size() > 1) {
  128. isc_throw(MultipleRecords, "More than one lease found in collection");
  129. }
  130. if (col.empty()) {
  131. return (Lease6Ptr());
  132. }
  133. return (*col.begin());
  134. }
  135. /// @brief checks if Lease6 matches expected configuration
  136. ///
  137. /// @param lease lease to be checked
  138. /// @param exp_type expected lease type
  139. /// @param exp_pd_len expected prefix length
  140. /// @param expected_in_subnet whether the lease is expected to be in subnet
  141. /// @param expected_in_pool whether the lease is expected to be in dynamic
  142. void checkLease6(const Lease6Ptr& lease, Lease::Type exp_type,
  143. uint8_t exp_pd_len = 128, bool expected_in_subnet = true,
  144. bool expected_in_pool = true) {
  145. // that is belongs to the right subnet
  146. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  147. if (expected_in_subnet) {
  148. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  149. } else {
  150. EXPECT_FALSE(subnet_->inRange(lease->addr_));
  151. }
  152. if (expected_in_pool) {
  153. EXPECT_TRUE(subnet_->inPool(exp_type, lease->addr_));
  154. } else {
  155. EXPECT_FALSE(subnet_->inPool(exp_type, lease->addr_));
  156. }
  157. // that it have proper parameters
  158. EXPECT_EQ(exp_type, lease->type_);
  159. EXPECT_EQ(iaid_, lease->iaid_);
  160. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  161. EXPECT_EQ(subnet_->getPreferred(), lease->preferred_lft_);
  162. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  163. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  164. EXPECT_EQ(exp_pd_len, lease->prefixlen_);
  165. EXPECT_EQ(fqdn_fwd_, lease->fqdn_fwd_);
  166. EXPECT_EQ(fqdn_rev_, lease->fqdn_rev_);
  167. EXPECT_EQ(hostname_, lease->hostname_);
  168. EXPECT_TRUE(*lease->duid_ == *duid_);
  169. /// @todo: check cltt
  170. }
  171. /// @brief Checks if specified address is increased properly
  172. ///
  173. /// Method uses gtest macros to mark check failure. This is a proxy
  174. /// method, since increaseAddress was moved to IOAddress class.
  175. ///
  176. /// @param alloc IterativeAllocator that is tested
  177. /// @param input address to be increased
  178. /// @param exp_output expected address after increase
  179. void
  180. checkAddrIncrease(NakedAllocEngine::NakedIterativeAllocator&,
  181. std::string input, std::string exp_output) {
  182. EXPECT_EQ(exp_output, asiolink::IOAddress::increase(
  183. asiolink::IOAddress(input)).toText());
  184. }
  185. /// @brief Checks if increasePrefix() works as expected
  186. ///
  187. /// Method uses gtest macros to mark check failure.
  188. ///
  189. /// @param alloc allocator to be tested
  190. /// @param input IPv6 prefix (as a string)
  191. /// @param prefix_len prefix len
  192. /// @param exp_output expected output (string)
  193. void
  194. checkPrefixIncrease(NakedAllocEngine::NakedIterativeAllocator& alloc,
  195. std::string input, uint8_t prefix_len,
  196. std::string exp_output) {
  197. EXPECT_EQ(exp_output, alloc.increasePrefix(asiolink::IOAddress(input),
  198. prefix_len).toText());
  199. }
  200. /// @brief Checks if the simple allocation can succeed
  201. ///
  202. /// The type of lease is determined by pool type (pool->getType()
  203. ///
  204. /// @param pool pool from which the lease will be allocated from
  205. /// @param hint address to be used as a hint
  206. /// @param fake true - this is fake allocation (SOLICIT)
  207. /// @param in_pool specifies whether the lease is expected to be in pool
  208. /// @return allocated lease (or NULL)
  209. Lease6Ptr simpleAlloc6Test(const Pool6Ptr& pool,
  210. const asiolink::IOAddress& hint,
  211. bool fake, bool in_pool = true);
  212. /// @brief Checks if the allocation can succeed.
  213. ///
  214. /// The type of lease is determined by pool type (pool->getType()).
  215. /// This test is particularly useful in connection with @ref renewTest.
  216. ///
  217. /// @param engine a reference to Allocation Engine
  218. /// @param pool pool from which the lease will be allocated from
  219. /// @param hint address to be used as a hint
  220. /// @param fake true - this is fake allocation (SOLICIT)
  221. /// @param in_pool specifies whether the lease is expected to be in pool
  222. /// @return allocated lease(s) (may be empty)
  223. Lease6Collection allocateTest(AllocEngine& engine, const Pool6Ptr& pool,
  224. const asiolink::IOAddress& hint, bool fake,
  225. bool in_pool = true);
  226. /// @brief Checks if the allocation can be renewed.
  227. ///
  228. /// The type of lease is determined by pool type (pool->getType()).
  229. /// This test is particularly useful as a follow up to @ref allocateTest.
  230. ///
  231. /// @param engine a reference to Allocation Engine
  232. /// @param pool pool from which the lease will be allocated from
  233. /// @param hints address to be used as a hint
  234. /// @param in_pool specifies whether the lease is expected to be in pool
  235. /// @return allocated lease(s) (may be empty)
  236. Lease6Collection renewTest(AllocEngine& engine, const Pool6Ptr& pool,
  237. AllocEngine::HintContainer& hints,
  238. bool in_pool = true);
  239. /// @brief Checks if the address allocation with a hint that is in range,
  240. /// in pool, but is currently used, can succeed
  241. ///
  242. /// Method uses gtest macros to mark check failure.
  243. ///
  244. /// @param type lease type
  245. /// @param used_addr address should be preallocated (simulates prior
  246. /// allocation by some other user)
  247. /// @param requested address requested by the client
  248. /// @param expected_pd_len expected PD len (128 for addresses)
  249. void allocWithUsedHintTest(Lease::Type type, asiolink::IOAddress used_addr,
  250. asiolink::IOAddress requested,
  251. uint8_t expected_pd_len);
  252. /// @brief checks if bogus hint can be ignored and the allocation succeeds
  253. ///
  254. /// This test checks if the allocation with a hing that is out of the blue
  255. /// can succeed. The invalid hint should be ignored completely.
  256. ///
  257. /// @param type Lease type
  258. /// @param hint hint (as send by a client)
  259. /// @param expected_pd_len (used in validation)
  260. void allocBogusHint6(Lease::Type type, asiolink::IOAddress hint,
  261. uint8_t expected_pd_len);
  262. /// @brief Utility function that creates a host reservation (duid)
  263. ///
  264. /// @param add_to_host_mgr true if the reservation should be added
  265. /// @param type specifies reservation type
  266. /// @param addr specifies reserved address or prefix
  267. /// @param prefix_len prefix length (should be 128 for addresses)
  268. /// @return created Host object.
  269. HostPtr
  270. createHost6(bool add_to_host_mgr, IPv6Resrv::Type type,
  271. const asiolink::IOAddress& addr, uint8_t prefix_len) {
  272. HostPtr host(new Host(&duid_->getDuid()[0], duid_->getDuid().size(),
  273. Host::IDENT_DUID, SubnetID(0), subnet_->getID(),
  274. asiolink::IOAddress("0.0.0.0")));
  275. IPv6Resrv resv(type, addr, prefix_len);
  276. host->addReservation(resv);
  277. if (add_to_host_mgr) {
  278. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  279. CfgMgr::instance().commit();
  280. }
  281. return (host);
  282. }
  283. /// @brief Utility function that creates a host reservation (hwaddr)
  284. ///
  285. /// @param add_to_host_mgr true if the reservation should be added
  286. /// @param type specifies reservation type
  287. /// @param hwaddr hardware address to be reserved to
  288. /// @param addr specifies reserved address or prefix
  289. /// @param prefix_len prefix length (should be 128 for addresses)
  290. /// @return created Host object.
  291. HostPtr
  292. createHost6HWAddr(bool add_to_host_mgr, IPv6Resrv::Type type,
  293. HWAddrPtr& hwaddr, const asiolink::IOAddress& addr,
  294. uint8_t prefix_len);
  295. virtual ~AllocEngine6Test() {
  296. factory_.destroy();
  297. }
  298. DuidPtr duid_; ///< client-identifier (value used in tests)
  299. HWAddrPtr hwaddr_; ///< client's hardware address
  300. uint32_t iaid_; ///< IA identifier (value used in tests)
  301. Subnet6Ptr subnet_; ///< subnet6 (used in tests)
  302. Pool6Ptr pool_; ///< NA pool belonging to subnet_
  303. Pool6Ptr pd_pool_; ///< PD pool belonging to subnet_
  304. std::string hostname_; ///< Hostname
  305. bool fqdn_fwd_; ///< Perform forward update for a lease.
  306. bool fqdn_rev_; ///< Perform reverse update for a lease.
  307. LeaseMgrFactory factory_; ///< pointer to LeaseMgr factory
  308. };
  309. /// @brief Used in Allocation Engine tests for IPv4
  310. class AllocEngine4Test : public ::testing::Test {
  311. public:
  312. /// @brief Specified expected result of a given operation
  313. enum ExpectedResult {
  314. SHOULD_PASS,
  315. SHOULD_FAIL
  316. };
  317. /// @brief Default constructor
  318. ///
  319. /// Sets clientid_, hwaddr_, subnet_, pool_ fields to example values
  320. /// used in many tests, initializes cfg_mgr configuration and creates
  321. /// lease database.
  322. ///
  323. /// It also re-initializes the Host Manager.
  324. AllocEngine4Test();
  325. /// @brief checks if Lease4 matches expected configuration
  326. ///
  327. /// @param lease lease to be checked
  328. void checkLease4(const Lease4Ptr& lease) {
  329. // Check that is belongs to the right subnet
  330. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  331. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  332. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, lease->addr_));
  333. // Check that it has proper parameters
  334. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  335. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  336. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  337. if (lease->client_id_ && !clientid_) {
  338. ADD_FAILURE() << "Lease4 has a client-id, while it should have none.";
  339. } else
  340. if (!lease->client_id_ && clientid_) {
  341. ADD_FAILURE() << "Lease4 has no client-id, but it was expected to have one.";
  342. } else
  343. if (lease->client_id_ && clientid_) {
  344. EXPECT_TRUE(*lease->client_id_ == *clientid_);
  345. }
  346. EXPECT_TRUE(*lease->hwaddr_ == *hwaddr_);
  347. /// @todo: check cltt
  348. }
  349. /// @brief Generic test used for lease allocation and reuse
  350. ///
  351. /// This test inserts old_lease (if specified, may be null) into the
  352. /// LeaseMgr, then conducts lease allocation (pretends that client
  353. /// sent either Discover or Request, depending on fake_allocation).
  354. /// Allocated lease is then returned (using result) for further inspection.
  355. ///
  356. /// @param alloc_engine allocation engine
  357. /// @param existing_lease optional lease to be inserted in the database
  358. /// @param addr address to be requested by client
  359. /// @param exp_result expected result
  360. /// @param result [out] allocated lease
  361. void testReuseLease4(const AllocEnginePtr& alloc_engine,
  362. Lease4Ptr& existing_lease,
  363. const std::string& addr,
  364. const bool fake_allocation,
  365. ExpectedResult exp_result,
  366. Lease4Ptr& result);
  367. /// @brief Creates a declined lease with specified expiration time
  368. ///
  369. /// expired parameter controls probation period. Positive value
  370. /// means that the lease will expire in X seconds. Negative means
  371. /// that the lease had expired X seconds ago. 0 means it expires now.
  372. /// Probation period is a parameter that specifies for how long
  373. /// a lease will become unavailable after decline.
  374. ///
  375. /// @param addr address of the lease
  376. /// @param probation_period expressed in seconds
  377. /// @param expired number of seconds where it will expire
  378. Lease4Ptr generateDeclinedLease(const std::string& addr,
  379. time_t probation_period,
  380. int32_t expired);
  381. /// @brief Create a subnet with a specified pool of addresses.
  382. ///
  383. /// @param pool_start First address in the pool.
  384. /// @param pool_end Last address in the pool.
  385. void initSubnet(const asiolink::IOAddress& pool_start,
  386. const asiolink::IOAddress& pool_end);
  387. virtual ~AllocEngine4Test() {
  388. factory_.destroy();
  389. }
  390. ClientIdPtr clientid_; ///< Client-identifier (value used in tests)
  391. ClientIdPtr clientid2_; ///< Alternative client-identifier.
  392. HWAddrPtr hwaddr_; ///< Hardware address (value used in tests)
  393. HWAddrPtr hwaddr2_; ///< Alternative hardware address.
  394. Subnet4Ptr subnet_; ///< Subnet4 (used in tests)
  395. Pool4Ptr pool_; ///< Pool belonging to subnet_
  396. LeaseMgrFactory factory_; ///< Pointer to LeaseMgr factory
  397. AllocEngine::ClientContext4 ctx_; ///< Context information passed to various
  398. ///< allocation engine functions.
  399. };
  400. }; // namespace test
  401. }; // namespace dhcp
  402. }; // namespace isc
  403. #endif