alloc_engine_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // Copyright (C) 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 <asiolink/io_address.h>
  16. #include <dhcp/duid.h>
  17. #include <dhcpsrv/alloc_engine.h>
  18. #include <dhcpsrv/cfgmgr.h>
  19. #include <dhcpsrv/lease_mgr.h>
  20. #include <dhcpsrv/lease_mgr_factory.h>
  21. #include <dhcpsrv/memfile_lease_mgr.h>
  22. #include <dhcpsrv/tests/test_utils.h>
  23. #include <boost/shared_ptr.hpp>
  24. #include <boost/scoped_ptr.hpp>
  25. #include <gtest/gtest.h>
  26. #include <iostream>
  27. #include <sstream>
  28. #include <map>
  29. #include <time.h>
  30. using namespace std;
  31. using namespace isc;
  32. using namespace isc::asiolink;
  33. using namespace isc::dhcp;
  34. using namespace isc::dhcp::test;
  35. namespace {
  36. class NakedAllocEngine : public AllocEngine {
  37. public:
  38. NakedAllocEngine(AllocEngine::AllocType engine_type, unsigned int attempts)
  39. :AllocEngine(engine_type, attempts) {
  40. }
  41. using AllocEngine::Allocator;
  42. using AllocEngine::IterativeAllocator;
  43. };
  44. // empty class for now, but may be extended once Addr6 becomes bigger
  45. class AllocEngineTest : public ::testing::Test {
  46. public:
  47. AllocEngineTest() {
  48. duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
  49. iaid_ = 42;
  50. // instantiate cfg_mgr
  51. CfgMgr& cfg_mgr = CfgMgr::instance();
  52. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  53. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, IOAddress("2001:db8:1::10"),
  54. IOAddress("2001:db8:1::20")));
  55. subnet_->addPool6(pool_);
  56. cfg_mgr.addSubnet6(subnet_);
  57. factory_.create("type=memfile");
  58. }
  59. void checkLease6(const Lease6Ptr& lease) {
  60. // that is belongs to the right subnet
  61. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  62. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  63. EXPECT_TRUE(subnet_->inPool(lease->addr_));
  64. // that it have proper parameters
  65. EXPECT_EQ(iaid_, lease->iaid_);
  66. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  67. EXPECT_EQ(subnet_->getPreferred(), lease->preferred_lft_);
  68. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  69. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  70. EXPECT_EQ(0, lease->prefixlen_); // this is IA_NA, not IA_PD
  71. EXPECT_TRUE(false == lease->fqdn_fwd_);
  72. EXPECT_TRUE(false == lease->fqdn_rev_);
  73. EXPECT_TRUE(*lease->duid_ == *duid_);
  74. // @todo: check cltt
  75. }
  76. ~AllocEngineTest() {
  77. factory_.destroy();
  78. }
  79. DuidPtr duid_;
  80. uint32_t iaid_;
  81. Subnet6Ptr subnet_;
  82. Pool6Ptr pool_;
  83. LeaseMgrFactory factory_;
  84. };
  85. // This test checks if the Allocation Engine can be instantiated and that it
  86. // parses parameters string properly.
  87. TEST_F(AllocEngineTest, constructor) {
  88. boost::scoped_ptr<AllocEngine> x;
  89. // Hashed and random allocators are not supported yet
  90. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_HASHED, 5)), NotImplemented);
  91. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_RANDOM, 5)), NotImplemented);
  92. ASSERT_NO_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  93. }
  94. // This test checks if the simple allocation can succeed
  95. TEST_F(AllocEngineTest, simpleAlloc) {
  96. boost::scoped_ptr<AllocEngine> engine;
  97. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  98. ASSERT_TRUE(engine);
  99. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
  100. false);
  101. // check that we got a lease
  102. ASSERT_TRUE(lease);
  103. // do all checks on the lease
  104. checkLease6(lease);
  105. // Check that the lease is indeed in LeaseMgr
  106. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  107. ASSERT_TRUE(from_mgr);
  108. // Now check that the lease in LeaseMgr has the same parameters
  109. detailCompareLease(lease, from_mgr);
  110. }
  111. // This test checks if the fake allocation (for SOLICIT) can succeed
  112. TEST_F(AllocEngineTest, fakeAlloc) {
  113. boost::scoped_ptr<AllocEngine> engine;
  114. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  115. ASSERT_TRUE(engine);
  116. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
  117. true);
  118. // check that we got a lease
  119. ASSERT_TRUE(lease);
  120. // do all checks on the lease
  121. checkLease6(lease);
  122. // Check that the lease is NOT in LeaseMgr
  123. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  124. ASSERT_FALSE(from_mgr);
  125. }
  126. // This test checks if the allocation with a hint that is valid (in range,
  127. // in pool and free) can succeed
  128. TEST_F(AllocEngineTest, allocWithValidHint) {
  129. boost::scoped_ptr<AllocEngine> engine;
  130. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  131. ASSERT_TRUE(engine);
  132. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
  133. IOAddress("2001:db8:1::15"),
  134. false);
  135. // check that we got a lease
  136. ASSERT_TRUE(lease);
  137. // we should get what we asked for
  138. EXPECT_EQ(lease->addr_.toText(), "2001:db8:1::15");
  139. // do all checks on the lease
  140. checkLease6(lease);
  141. // Check that the lease is indeed in LeaseMgr
  142. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  143. ASSERT_TRUE(from_mgr);
  144. // Now check that the lease in LeaseMgr has the same parameters
  145. detailCompareLease(lease, from_mgr);
  146. }
  147. // This test checks if the allocation with a hint that is in range,
  148. // in pool, but is currently used) can succeed
  149. TEST_F(AllocEngineTest, allocWithUsedHint) {
  150. boost::scoped_ptr<AllocEngine> engine;
  151. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  152. ASSERT_TRUE(engine);
  153. // let's create a lease and put it in the LeaseMgr
  154. DuidPtr duid2 = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0xff)));
  155. Lease6Ptr used(new Lease6(Lease6::LEASE_IA_NA, IOAddress("2001:db8:1::1f"),
  156. duid2, 1, 2, 3, 4, 5, subnet_->getID()));
  157. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  158. // another client comes in and request an address that is in pool, but
  159. // unfortunately it is used already. The same address must not be allocated
  160. // twice.
  161. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
  162. IOAddress("2001:db8:1::1f"),
  163. false);
  164. // check that we got a lease
  165. ASSERT_TRUE(lease);
  166. // allocated address must be different
  167. EXPECT_TRUE(used->addr_.toText() != lease->addr_.toText());
  168. // we should NOT get what we asked for, because it is used already
  169. EXPECT_TRUE(lease->addr_.toText() != "2001:db8:1::1f");
  170. // do all checks on the lease
  171. checkLease6(lease);
  172. // Check that the lease is indeed in LeaseMgr
  173. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  174. ASSERT_TRUE(from_mgr);
  175. // Now check that the lease in LeaseMgr has the same parameters
  176. detailCompareLease(lease, from_mgr);
  177. }
  178. // This test checks if the allocation with a hint that is out the blue
  179. // can succeed. The invalid hint should be ignored completely.
  180. TEST_F(AllocEngineTest, allocBogusHint) {
  181. boost::scoped_ptr<AllocEngine> engine;
  182. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  183. ASSERT_TRUE(engine);
  184. // Client would like to get a 3000::abc lease, which does not belong to any
  185. // supported lease. Allocation engine should ignore it and carry on
  186. // with the normal allocation
  187. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_,
  188. IOAddress("3000::abc"),
  189. false);
  190. // check that we got a lease
  191. ASSERT_TRUE(lease);
  192. // we should NOT get what we asked for, because it is used already
  193. EXPECT_TRUE(lease->addr_.toText() != "3000::abc");
  194. // do all checks on the lease
  195. checkLease6(lease);
  196. // Check that the lease is indeed in LeaseMgr
  197. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  198. ASSERT_TRUE(from_mgr);
  199. // Now check that the lease in LeaseMgr has the same parameters
  200. detailCompareLease(lease, from_mgr);
  201. }
  202. // This test verifies that the allocator picks addresses that belong to the
  203. // pool
  204. TEST_F(AllocEngineTest, IterativeAllocator) {
  205. boost::scoped_ptr<NakedAllocEngine::Allocator>
  206. alloc(new NakedAllocEngine::IterativeAllocator());
  207. for (int i = 0; i < 1000; ++i) {
  208. IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
  209. EXPECT_TRUE(subnet_->inPool(candidate));
  210. }
  211. }
  212. // This test verifies that the iterative allocator really walks over all addresses
  213. // in all pools in specified subnet. It also must not pick the same address twice
  214. // unless it runs out of pool space and must start over.
  215. TEST_F(AllocEngineTest, IterativeAllocator_manyPools) {
  216. NakedAllocEngine::IterativeAllocator* alloc = new NakedAllocEngine::IterativeAllocator();
  217. // let's start from 2, as there is 2001:db8:1::10 - 2001:db8:1::20 pool already.
  218. for (int i = 2; i < 10; ++i) {
  219. stringstream min, max;
  220. min << "2001:db8:1::" << hex << i*16 + 1;
  221. max << "2001:db8:1::" << hex << i*16 + 9;
  222. Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, IOAddress(min.str()),
  223. IOAddress(max.str())));
  224. // cout << "Adding pool: " << min.str() << "-" << max.str() << endl;
  225. subnet_->addPool6(pool);
  226. }
  227. int total = 17 + 8*9; // first pool (::10 - ::20) has 17 addresses in it,
  228. // there are 8 extra pools with 9 addresses in each.
  229. // Let's keep picked addresses here and check their uniqueness.
  230. std::map<IOAddress, int> generated_addrs;
  231. int cnt = 0;
  232. while (++cnt) {
  233. IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
  234. EXPECT_TRUE(subnet_->inPool(candidate));
  235. // One way to easily verify that the iterative allocator really works is
  236. // to uncomment the following line and observe its output that it
  237. // covers all defined subnets.
  238. // cout << candidate.toText() << endl;
  239. if (generated_addrs.find(candidate) == generated_addrs.end()) {
  240. // we haven't had this
  241. generated_addrs[candidate] = 0;
  242. } else {
  243. // we have seen this address before. That should mean that we
  244. // iterated over all addresses.
  245. if (generated_addrs.size() == total) {
  246. // we have exactly the number of address in all pools
  247. break;
  248. }
  249. ADD_FAILURE() << "Too many or not enough unique addresses generated.";
  250. break;
  251. }
  252. if ( cnt>total ) {
  253. ADD_FAILURE() << "Too many unique addresses generated.";
  254. break;
  255. }
  256. }
  257. delete alloc;
  258. }
  259. // This test checks if really small pools are working
  260. TEST_F(AllocEngineTest, smallPool) {
  261. boost::scoped_ptr<AllocEngine> engine;
  262. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  263. ASSERT_TRUE(engine);
  264. IOAddress addr("2001:db8:1::ad");
  265. CfgMgr& cfg_mgr = CfgMgr::instance();
  266. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  267. // Create configuration similar to other tests, but with a single address pool
  268. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  269. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
  270. subnet_->addPool6(pool_);
  271. cfg_mgr.addSubnet6(subnet_);
  272. Lease6Ptr lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
  273. false);
  274. // Check that we got that single lease
  275. ASSERT_TRUE(lease);
  276. EXPECT_EQ("2001:db8:1::ad", lease->addr_.toText());
  277. // do all checks on the lease
  278. checkLease6(lease);
  279. // Check that the lease is indeed in LeaseMgr
  280. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->addr_);
  281. ASSERT_TRUE(from_mgr);
  282. // Now check that the lease in LeaseMgr has the same parameters
  283. detailCompareLease(lease, from_mgr);
  284. }
  285. // This test checks if all addresses in a pool are currently used, the attempt
  286. // to find out a new lease fails.
  287. TEST_F(AllocEngineTest, outOfAddresses) {
  288. boost::scoped_ptr<AllocEngine> engine;
  289. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  290. ASSERT_TRUE(engine);
  291. IOAddress addr("2001:db8:1::ad");
  292. CfgMgr& cfg_mgr = CfgMgr::instance();
  293. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  294. // Create configuration similar to other tests, but with a single address pool
  295. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  296. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
  297. subnet_->addPool6(pool_);
  298. cfg_mgr.addSubnet6(subnet_);
  299. // Just a different duid
  300. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  301. const uint32_t other_iaid = 3568;
  302. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
  303. 501, 502, 503, 504, subnet_->getID(), 0));
  304. lease->cltt_ = time(NULL) - 10; // Allocated 10 seconds ago
  305. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  306. // There is just a single address in the pool and allocated it to someone
  307. // else, so the allocation should fail
  308. EXPECT_THROW(engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),false),
  309. AllocFailed);
  310. }
  311. // This test checks if an expired lease can be reused in SOLICIT (fake allocation)
  312. TEST_F(AllocEngineTest, solicitReuseExpiredLease) {
  313. boost::scoped_ptr<AllocEngine> engine;
  314. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  315. ASSERT_TRUE(engine);
  316. IOAddress addr("2001:db8:1::ad");
  317. CfgMgr& cfg_mgr = CfgMgr::instance();
  318. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  319. // Create configuration similar to other tests, but with a single address pool
  320. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  321. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
  322. subnet_->addPool6(pool_);
  323. cfg_mgr.addSubnet6(subnet_);
  324. // Just a different duid
  325. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  326. const uint32_t other_iaid = 3568;
  327. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
  328. 501, 502, 503, 504, subnet_->getID(), 0));
  329. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  330. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  331. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  332. // CASE 1: Asking for any address
  333. lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress("::"),
  334. true);
  335. // Check that we got that single lease
  336. ASSERT_TRUE(lease);
  337. EXPECT_EQ(addr.toText(), lease->addr_.toText());
  338. // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
  339. checkLease6(lease);
  340. // CASE 2: Asking specifically for this address
  341. lease = engine->allocateAddress6(subnet_, duid_, iaid_, IOAddress(addr.toText()),
  342. true);
  343. // Check that we got that single lease
  344. ASSERT_TRUE(lease);
  345. EXPECT_EQ(addr.toText(), lease->addr_.toText());
  346. }
  347. // This test checks if an expired lease can be reused in REQUEST (actual allocation)
  348. TEST_F(AllocEngineTest, requestReuseExpiredLease) {
  349. boost::scoped_ptr<AllocEngine> engine;
  350. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  351. ASSERT_TRUE(engine);
  352. IOAddress addr("2001:db8:1::ad");
  353. CfgMgr& cfg_mgr = CfgMgr::instance();
  354. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  355. // Create configuration similar to other tests, but with a single address pool
  356. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  357. pool_ = Pool6Ptr(new Pool6(Pool6::TYPE_IA, addr, addr)); // just a single address
  358. subnet_->addPool6(pool_);
  359. cfg_mgr.addSubnet6(subnet_);
  360. // Let's create an expired lease
  361. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  362. const uint32_t other_iaid = 3568;
  363. const SubnetID other_subnetid = 999;
  364. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, other_duid, other_iaid,
  365. 501, 502, 503, 504, other_subnetid, 0));
  366. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  367. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  368. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  369. // A client comes along, asking specifically for this address
  370. lease = engine->allocateAddress6(subnet_, duid_, iaid_,
  371. IOAddress(addr.toText()), false);
  372. // Check that he got that single lease
  373. ASSERT_TRUE(lease);
  374. EXPECT_EQ(addr.toText(), lease->addr_.toText());
  375. // Check that the lease is indeed updated in LeaseMgr
  376. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(addr);
  377. ASSERT_TRUE(from_mgr);
  378. // Now check that the lease in LeaseMgr has the same parameters
  379. detailCompareLease(lease, from_mgr);
  380. }
  381. }; // end of anonymous namespace