alloc_engine6_unittest.cc 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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. #include <config.h>
  15. #include <dhcp/pkt4.h>
  16. #include <dhcp/pkt6.h>
  17. #include <dhcpsrv/tests/alloc_engine_utils.h>
  18. #include <dhcpsrv/tests/test_utils.h>
  19. #include <stats/stats_mgr.h>
  20. using namespace std;
  21. using namespace isc::hooks;
  22. using namespace isc::asiolink;
  23. using namespace isc::stats;
  24. namespace isc {
  25. namespace dhcp {
  26. namespace test {
  27. // This test checks if the v6 Allocation Engine can be instantiated, parses
  28. // parameters string and allocators are created.
  29. TEST_F(AllocEngine6Test, constructor) {
  30. boost::scoped_ptr<AllocEngine> x;
  31. // Hashed and random allocators are not supported yet
  32. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_HASHED, 5)), NotImplemented);
  33. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_RANDOM, 5)), NotImplemented);
  34. ASSERT_NO_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100, true)));
  35. // Check that allocator for normal addresses is created
  36. ASSERT_TRUE(x->getAllocator(Lease::TYPE_NA));
  37. // Check that allocator for temporary address is created
  38. ASSERT_TRUE(x->getAllocator(Lease::TYPE_TA));
  39. // Check that allocator for prefixes is created
  40. ASSERT_TRUE(x->getAllocator(Lease::TYPE_PD));
  41. // There should be no V4 allocator
  42. EXPECT_THROW(x->getAllocator(Lease::TYPE_V4), BadValue);
  43. }
  44. // This test checks if the simple allocation (REQUEST) can succeed
  45. // and the stats counter is properly bumped by 1
  46. TEST_F(AllocEngine6Test, simpleAlloc6) {
  47. simpleAlloc6Test(pool_, IOAddress("::"), false);
  48. // We should have bumped the address counter by 1
  49. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  50. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  51. ASSERT_TRUE(stat);
  52. EXPECT_EQ(101, stat->getInteger().first);
  53. }
  54. // This test checks if the simple PD allocation (REQUEST) can succeed
  55. // and the stats counter is properly bumped by 1
  56. TEST_F(AllocEngine6Test, pdSimpleAlloc6) {
  57. simpleAlloc6Test(pd_pool_, IOAddress("::"), false);
  58. // We should have bumped the address counter by 1
  59. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-pds");
  60. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  61. ASSERT_TRUE(stat);
  62. EXPECT_EQ(101, stat->getInteger().first);
  63. }
  64. // This test checks if the fake allocation (for SOLICIT) can succeed
  65. // and the stats counter isn't bumped
  66. TEST_F(AllocEngine6Test, fakeAlloc6) {
  67. simpleAlloc6Test(pool_, IOAddress("::"), true);
  68. // We should not have bumped the address counter
  69. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  70. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  71. ASSERT_TRUE(stat);
  72. EXPECT_EQ(100, stat->getInteger().first);
  73. }
  74. // This test checks if the fake PD allocation (for SOLICIT) can succeed
  75. // and the stats counter isn't bumped
  76. TEST_F(AllocEngine6Test, pdFakeAlloc6) {
  77. simpleAlloc6Test(pd_pool_, IOAddress("::"), true);
  78. // We should not have bumped the address counter
  79. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-pds");
  80. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  81. ASSERT_TRUE(stat);
  82. EXPECT_EQ(100, stat->getInteger().first);
  83. };
  84. // This test checks if the allocation with a hint that is valid (in range,
  85. // in pool and free) can succeed
  86. TEST_F(AllocEngine6Test, allocWithValidHint6) {
  87. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::15"),
  88. false);
  89. ASSERT_TRUE(lease);
  90. // We should get what we asked for
  91. EXPECT_EQ("2001:db8:1::15", lease->addr_.toText());
  92. }
  93. // This test checks if the address allocation with a hint that is in range,
  94. // in pool, but is currently used, can succeed
  95. TEST_F(AllocEngine6Test, allocWithUsedHint6) {
  96. allocWithUsedHintTest(Lease::TYPE_NA,
  97. IOAddress("2001:db8:1::1f"), // allocate this as used
  98. IOAddress("2001:db8:1::1f"), // request this addr
  99. 128);
  100. }
  101. // This test checks if the PD allocation with a hint that is in range,
  102. // in pool, but is currently used, can succeed
  103. TEST_F(AllocEngine6Test, pdAllocWithUsedHint6) {
  104. allocWithUsedHintTest(Lease::TYPE_PD,
  105. IOAddress("2001:db8:1::"), // allocate this prefix as used
  106. IOAddress("2001:db8:1::"), // request this prefix
  107. 64);
  108. }
  109. // This test checks if the allocation with a hint that is out the blue
  110. // can succeed. The invalid hint should be ignored completely.
  111. TEST_F(AllocEngine6Test, allocBogusHint6) {
  112. allocBogusHint6(Lease::TYPE_NA, IOAddress("3000::abc"), 128);
  113. }
  114. // This test checks if the allocation with a hint that is out the blue
  115. // can succeed. The invalid hint should be ignored completely.
  116. TEST_F(AllocEngine6Test, pdAllocBogusHint6) {
  117. allocBogusHint6(Lease::TYPE_PD, IOAddress("3000::abc"), 64);
  118. }
  119. // This test checks that NULL values are handled properly
  120. TEST_F(AllocEngine6Test, allocateAddress6Nulls) {
  121. boost::scoped_ptr<AllocEngine> engine;
  122. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  123. ASSERT_TRUE(engine);
  124. // Allocations without subnet are not allowed
  125. Lease6Ptr lease;
  126. AllocEngine::ClientContext6 ctx1(Subnet6Ptr(), duid_, iaid_, IOAddress("::"),
  127. Lease::TYPE_NA, false, false, "", false);
  128. ctx1.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  129. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx1)));
  130. ASSERT_FALSE(lease);
  131. // Allocations without DUID are not allowed either
  132. AllocEngine::ClientContext6 ctx2(subnet_, DuidPtr(), iaid_, IOAddress("::"),
  133. Lease::TYPE_NA, false, false, "", false);
  134. ctx2.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  135. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx2)));
  136. ASSERT_FALSE(lease);
  137. }
  138. // This test verifies that the allocator picks addresses that belong to the
  139. // pool
  140. TEST_F(AllocEngine6Test, IterativeAllocator) {
  141. boost::scoped_ptr<NakedAllocEngine::Allocator>
  142. alloc(new NakedAllocEngine::IterativeAllocator(Lease::TYPE_NA));
  143. for (int i = 0; i < 1000; ++i) {
  144. IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
  145. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
  146. }
  147. }
  148. TEST_F(AllocEngine6Test, IterativeAllocatorAddrStep) {
  149. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_NA);
  150. subnet_->delPools(Lease::TYPE_NA); // Get rid of default pool
  151. Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
  152. IOAddress("2001:db8:1::5")));
  153. Pool6Ptr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::100"),
  154. IOAddress("2001:db8:1::100")));
  155. Pool6Ptr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::105"),
  156. IOAddress("2001:db8:1::106")));
  157. subnet_->addPool(pool1);
  158. subnet_->addPool(pool2);
  159. subnet_->addPool(pool3);
  160. // Let's check the first pool (5 addresses here)
  161. EXPECT_EQ("2001:db8:1::1", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  162. EXPECT_EQ("2001:db8:1::2", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  163. EXPECT_EQ("2001:db8:1::3", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  164. EXPECT_EQ("2001:db8:1::4", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  165. EXPECT_EQ("2001:db8:1::5", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  166. // The second pool is easy - only one address here
  167. EXPECT_EQ("2001:db8:1::100", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  168. // This is the third and last pool, with 2 addresses in it
  169. EXPECT_EQ("2001:db8:1::105", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  170. EXPECT_EQ("2001:db8:1::106", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  171. // We iterated over all addresses and reached to the end of the last pool.
  172. // Let's wrap around and start from the beginning
  173. EXPECT_EQ("2001:db8:1::1", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  174. EXPECT_EQ("2001:db8:1::2", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  175. }
  176. TEST_F(AllocEngine6Test, IterativeAllocatorPrefixStep) {
  177. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_PD);
  178. subnet_.reset(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  179. Pool6Ptr pool1(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8::"), 56, 60));
  180. Pool6Ptr pool2(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1::"), 48, 48));
  181. Pool6Ptr pool3(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:2::"), 56, 64));
  182. subnet_->addPool(pool1);
  183. subnet_->addPool(pool2);
  184. subnet_->addPool(pool3);
  185. // We have a 2001:db8::/48 subnet that has 3 pools defined in it:
  186. // 2001:db8::/56 split into /60 prefixes (16 leases) (or 2001:db8:0:X0::)
  187. // 2001:db8:1::/48 split into a single /48 prefix (just 1 lease)
  188. // 2001:db8:2::/56 split into /64 prefixes (256 leases) (or 2001:db8:2:XX::)
  189. // First pool check (Let's check over all 16 leases)
  190. EXPECT_EQ("2001:db8::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  191. EXPECT_EQ("2001:db8:0:10::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  192. EXPECT_EQ("2001:db8:0:20::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  193. EXPECT_EQ("2001:db8:0:30::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  194. EXPECT_EQ("2001:db8:0:40::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  195. EXPECT_EQ("2001:db8:0:50::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  196. EXPECT_EQ("2001:db8:0:60::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  197. EXPECT_EQ("2001:db8:0:70::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  198. EXPECT_EQ("2001:db8:0:80::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  199. EXPECT_EQ("2001:db8:0:90::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  200. EXPECT_EQ("2001:db8:0:a0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  201. EXPECT_EQ("2001:db8:0:b0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  202. EXPECT_EQ("2001:db8:0:c0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  203. EXPECT_EQ("2001:db8:0:d0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  204. EXPECT_EQ("2001:db8:0:e0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  205. EXPECT_EQ("2001:db8:0:f0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  206. // Second pool (just one lease here)
  207. EXPECT_EQ("2001:db8:1::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  208. // Third pool (256 leases, let's check first and last explicitly and the
  209. // rest over in a pool
  210. EXPECT_EQ("2001:db8:2::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  211. for (int i = 1; i < 255; i++) {
  212. stringstream exp;
  213. exp << "2001:db8:2:" << hex << i << dec << "::";
  214. EXPECT_EQ(exp.str(), alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  215. }
  216. EXPECT_EQ("2001:db8:2:ff::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  217. // Ok, we've iterated over all prefixes in all pools. We now wrap around.
  218. // We're looping over now (iterating over first pool again)
  219. EXPECT_EQ("2001:db8::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  220. EXPECT_EQ("2001:db8:0:10::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  221. }
  222. // This test verifies that the iterative allocator can step over addresses
  223. TEST_F(AllocEngine6Test, IterativeAllocatorAddressIncrease) {
  224. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_NA);
  225. // Let's pick the first address
  226. IOAddress addr1 = alloc.pickAddress(subnet_, duid_, IOAddress("2001:db8:1::10"));
  227. // Check that we can indeed pick the first address from the pool
  228. EXPECT_EQ("2001:db8:1::10", addr1.toText());
  229. // Check that addresses can be increased properly
  230. checkAddrIncrease(alloc, "2001:db8::9", "2001:db8::a");
  231. checkAddrIncrease(alloc, "2001:db8::f", "2001:db8::10");
  232. checkAddrIncrease(alloc, "2001:db8::10", "2001:db8::11");
  233. checkAddrIncrease(alloc, "2001:db8::ff", "2001:db8::100");
  234. checkAddrIncrease(alloc, "2001:db8::ffff", "2001:db8::1:0");
  235. checkAddrIncrease(alloc, "::", "::1");
  236. checkAddrIncrease(alloc, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::");
  237. }
  238. // This test verifies that the allocator can step over prefixes
  239. TEST_F(AllocEngine6Test, IterativeAllocatorPrefixIncrease) {
  240. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_PD);
  241. // For /128 prefix, increasePrefix should work the same as addressIncrease
  242. checkPrefixIncrease(alloc, "2001:db8::9", 128, "2001:db8::a");
  243. checkPrefixIncrease(alloc, "2001:db8::f", 128, "2001:db8::10");
  244. checkPrefixIncrease(alloc, "2001:db8::10", 128, "2001:db8::11");
  245. checkPrefixIncrease(alloc, "2001:db8::ff", 128, "2001:db8::100");
  246. checkPrefixIncrease(alloc, "2001:db8::ffff", 128, "2001:db8::1:0");
  247. checkPrefixIncrease(alloc, "::", 128, "::1");
  248. checkPrefixIncrease(alloc, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, "::");
  249. // Check that /64 prefixes can be generated
  250. checkPrefixIncrease(alloc, "2001:db8::", 64, "2001:db8:0:1::");
  251. // Check that prefix length not divisible by 8 are working
  252. checkPrefixIncrease(alloc, "2001:db8::", 128, "2001:db8::1");
  253. checkPrefixIncrease(alloc, "2001:db8::", 127, "2001:db8::2");
  254. checkPrefixIncrease(alloc, "2001:db8::", 126, "2001:db8::4");
  255. checkPrefixIncrease(alloc, "2001:db8::", 125, "2001:db8::8");
  256. checkPrefixIncrease(alloc, "2001:db8::", 124, "2001:db8::10");
  257. checkPrefixIncrease(alloc, "2001:db8::", 123, "2001:db8::20");
  258. checkPrefixIncrease(alloc, "2001:db8::", 122, "2001:db8::40");
  259. checkPrefixIncrease(alloc, "2001:db8::", 121, "2001:db8::80");
  260. checkPrefixIncrease(alloc, "2001:db8::", 120, "2001:db8::100");
  261. // These are not really useful cases, because there are bits set
  262. // int the last (128 - prefix_len) bits. Nevertheless, it shows
  263. // that the algorithm is working even in such cases
  264. checkPrefixIncrease(alloc, "2001:db8::1", 128, "2001:db8::2");
  265. checkPrefixIncrease(alloc, "2001:db8::1", 127, "2001:db8::3");
  266. checkPrefixIncrease(alloc, "2001:db8::1", 126, "2001:db8::5");
  267. checkPrefixIncrease(alloc, "2001:db8::1", 125, "2001:db8::9");
  268. checkPrefixIncrease(alloc, "2001:db8::1", 124, "2001:db8::11");
  269. checkPrefixIncrease(alloc, "2001:db8::1", 123, "2001:db8::21");
  270. checkPrefixIncrease(alloc, "2001:db8::1", 122, "2001:db8::41");
  271. checkPrefixIncrease(alloc, "2001:db8::1", 121, "2001:db8::81");
  272. checkPrefixIncrease(alloc, "2001:db8::1", 120, "2001:db8::101");
  273. // Let's try out couple real life scenarios
  274. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 64, "2001:db8:1:abce::");
  275. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 60, "2001:db8:1:abdd::");
  276. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 56, "2001:db8:1:accd::");
  277. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 52, "2001:db8:1:bbcd::");
  278. // And now let's try something over the top
  279. checkPrefixIncrease(alloc, "::", 1, "8000::");
  280. }
  281. // This test verifies that the iterative allocator really walks over all addresses
  282. // in all pools in specified subnet. It also must not pick the same address twice
  283. // unless it runs out of pool space and must start over.
  284. TEST_F(AllocEngine6Test, IterativeAllocator_manyPools6) {
  285. NakedAllocEngine::IterativeAllocator alloc(Lease::TYPE_NA);
  286. // let's start from 2, as there is 2001:db8:1::10 - 2001:db8:1::20 pool already.
  287. for (int i = 2; i < 10; ++i) {
  288. stringstream min, max;
  289. min << "2001:db8:1::" << hex << i*16 + 1;
  290. max << "2001:db8:1::" << hex << i*16 + 9;
  291. Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress(min.str()),
  292. IOAddress(max.str())));
  293. subnet_->addPool(pool);
  294. }
  295. int total = 17 + 8 * 9; // First pool (::10 - ::20) has 17 addresses in it,
  296. // there are 8 extra pools with 9 addresses in each.
  297. // Let's keep picked addresses here and check their uniqueness.
  298. std::set<IOAddress> generated_addrs;
  299. int cnt = 0;
  300. while (++cnt) {
  301. IOAddress candidate = alloc.pickAddress(subnet_, duid_, IOAddress("::"));
  302. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
  303. // One way to easily verify that the iterative allocator really works is
  304. // to uncomment the following line and observe its output that it
  305. // covers all defined pools.
  306. // cout << candidate.toText() << endl;
  307. if (generated_addrs.find(candidate) == generated_addrs.end()) {
  308. // We haven't had this.
  309. generated_addrs.insert(candidate);
  310. } else {
  311. // We have seen this address before. That should mean that we
  312. // iterated over all addresses.
  313. if (generated_addrs.size() == total) {
  314. // We have exactly the number of address in all pools.
  315. break;
  316. }
  317. ADD_FAILURE() << "Too many or not enough unique addresses generated.";
  318. break;
  319. }
  320. if ( cnt>total ) {
  321. ADD_FAILURE() << "Too many unique addresses generated.";
  322. break;
  323. }
  324. }
  325. }
  326. // This test checks if really small pools are working
  327. TEST_F(AllocEngine6Test, smallPool6) {
  328. boost::scoped_ptr<AllocEngine> engine;
  329. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  330. ASSERT_TRUE(engine);
  331. IOAddress addr("2001:db8:1::ad");
  332. // Create a subnet with a pool that has one address.
  333. initSubnet(IOAddress("2001:db8:1::"), addr, addr);
  334. // Initialize FQDN for a lease.
  335. initFqdn("myhost.example.com", true, true);
  336. Lease6Ptr lease;
  337. AllocEngine::ClientContext6 ctx(subnet_, duid_, iaid_, IOAddress("::"),
  338. Lease::TYPE_NA, fqdn_fwd_, fqdn_rev_,
  339. hostname_, false);
  340. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  341. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx)));
  342. // Check that we got that single lease
  343. ASSERT_TRUE(lease);
  344. EXPECT_EQ("2001:db8:1::ad", lease->addr_.toText());
  345. // Do all checks on the lease
  346. checkLease6(lease, Lease::TYPE_NA, 128);
  347. // Check that the lease is indeed in LeaseMgr
  348. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  349. lease->addr_);
  350. ASSERT_TRUE(from_mgr);
  351. // Now check that the lease in LeaseMgr has the same parameters
  352. detailCompareLease(lease, from_mgr);
  353. // This is a new lease allocation. The old lease corresponding to a newly
  354. // allocated lease should be NULL.
  355. ASSERT_TRUE(ctx.old_leases_.empty());
  356. }
  357. // This test checks if all addresses in a pool are currently used, the attempt
  358. // to find out a new lease fails.
  359. TEST_F(AllocEngine6Test, outOfAddresses6) {
  360. boost::scoped_ptr<AllocEngine> engine;
  361. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  362. ASSERT_TRUE(engine);
  363. IOAddress addr("2001:db8:1::ad");
  364. CfgMgr& cfg_mgr = CfgMgr::instance();
  365. cfg_mgr.clear(); // Get rid of the default test configuration
  366. // Create configuration similar to other tests, but with a single address pool
  367. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  368. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  369. subnet_->addPool(pool_);
  370. cfg_mgr.getStagingCfg()->getCfgSubnets6()->add(subnet_);
  371. // Just a different duid
  372. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  373. const uint32_t other_iaid = 3568;
  374. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  375. 501, 502, 503, 504, subnet_->getID(),
  376. HWAddrPtr(), 0));
  377. lease->cltt_ = time(NULL) - 10; // Allocated 10 seconds ago
  378. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  379. // There is just a single address in the pool and allocated it to someone
  380. // else, so the allocation should fail
  381. Lease6Ptr lease2;
  382. AllocEngine::ClientContext6 ctx(subnet_, duid_, iaid_, IOAddress("::"),
  383. Lease::TYPE_NA, false, false, "", false);
  384. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  385. EXPECT_NO_THROW(lease2 = expectOneLease(engine->allocateLeases6(ctx)));
  386. EXPECT_FALSE(lease2);
  387. }
  388. // This test checks if an expired lease can be reused in SOLICIT (fake allocation)
  389. TEST_F(AllocEngine6Test, solicitReuseExpiredLease6) {
  390. boost::scoped_ptr<AllocEngine> engine;
  391. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  392. ASSERT_TRUE(engine);
  393. IOAddress addr("2001:db8:1::ad");
  394. // Create one subnet with a pool holding one address.
  395. initSubnet(IOAddress("2001:db8:1::"), addr, addr);
  396. // Initialize FQDN data for the lease.
  397. initFqdn("myhost.example.com", true, true);
  398. // Just a different duid
  399. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  400. const uint32_t other_iaid = 3568;
  401. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  402. 501, 502, 503, 504, subnet_->getID(),
  403. HWAddrPtr(), 0));
  404. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  405. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  406. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  407. // Make sure that we really created expired lease
  408. ASSERT_TRUE(lease->expired());
  409. // CASE 1: Asking for any address
  410. AllocEngine::ClientContext6 ctx1(subnet_, duid_, iaid_, IOAddress("::"),
  411. Lease::TYPE_NA, fqdn_fwd_, fqdn_rev_, hostname_, true);
  412. ctx1.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  413. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx1)));
  414. // Check that we got that single lease
  415. ASSERT_TRUE(lease);
  416. EXPECT_EQ(addr, lease->addr_);
  417. // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
  418. checkLease6(lease, Lease::TYPE_NA, 128);
  419. // CASE 2: Asking specifically for this address
  420. AllocEngine::ClientContext6 ctx2(subnet_, duid_, iaid_, addr, Lease::TYPE_NA,
  421. false, false, "", true);
  422. ctx2.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  423. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx2)));
  424. // Check that we got that single lease
  425. ASSERT_TRUE(lease);
  426. EXPECT_EQ(addr, lease->addr_);
  427. }
  428. // This test checks if an expired lease can be reused in REQUEST (actual allocation)
  429. TEST_F(AllocEngine6Test, requestReuseExpiredLease6) {
  430. boost::scoped_ptr<AllocEngine> engine;
  431. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  432. ASSERT_TRUE(engine);
  433. IOAddress addr("2001:db8:1::ad");
  434. CfgMgr& cfg_mgr = CfgMgr::instance();
  435. cfg_mgr.clear(); // Get rid of the default test configuration
  436. // Create configuration similar to other tests, but with a single address pool
  437. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  438. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  439. subnet_->addPool(pool_);
  440. cfg_mgr.getStagingCfg()->getCfgSubnets6()->add(subnet_);
  441. cfg_mgr.commit();
  442. // Let's create an expired lease
  443. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  444. const uint32_t other_iaid = 3568;
  445. const SubnetID other_subnetid = 999;
  446. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  447. 501, 502, 503, 504, other_subnetid, HWAddrPtr(),
  448. 0));
  449. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  450. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  451. lease->fqdn_fwd_ = true;
  452. lease->fqdn_rev_ = true;
  453. lease->hostname_ = "myhost.example.com.";
  454. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  455. // By default we pretend our subnet has 100 addresses
  456. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  457. StatsMgr::instance().setValue(name, static_cast<int64_t>(100));
  458. // A client comes along, asking specifically for this address
  459. AllocEngine::ClientContext6 ctx(subnet_, duid_, iaid_, addr, Lease::TYPE_NA,
  460. false, false, "", false);
  461. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  462. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(ctx)));
  463. // Check that he got that single lease
  464. ASSERT_TRUE(lease);
  465. EXPECT_EQ(addr, lease->addr_);
  466. // This reactivated lease should have updated FQDN data.
  467. EXPECT_TRUE(lease->hostname_.empty());
  468. EXPECT_FALSE(lease->fqdn_fwd_);
  469. EXPECT_FALSE(lease->fqdn_rev_);
  470. // Check that the old lease has been returned.
  471. Lease6Ptr old_lease = expectOneLease(ctx.old_leases_);
  472. // It should at least have the same IPv6 address.
  473. EXPECT_EQ(lease->addr_, old_lease->addr_);
  474. // Check that it carries not updated FQDN data.
  475. EXPECT_EQ("myhost.example.com.", old_lease->hostname_);
  476. EXPECT_TRUE(old_lease->fqdn_fwd_);
  477. EXPECT_TRUE(old_lease->fqdn_rev_);
  478. // Check that the lease is indeed updated in LeaseMgr
  479. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  480. addr);
  481. ASSERT_TRUE(from_mgr);
  482. // Now check that the lease in LeaseMgr has the same parameters
  483. detailCompareLease(lease, from_mgr);
  484. // We should not have bumped the address counter
  485. // NOTE: when we start expiring addresses and removing them from
  486. // the stats this will no longer be true.
  487. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  488. ASSERT_TRUE(stat);
  489. EXPECT_EQ(100, stat->getInteger().first);
  490. }
  491. // Checks if the lease lifetime is extended when the client sends the
  492. // Request.
  493. TEST_F(AllocEngine6Test, requestExtendLeaseLifetime) {
  494. // Create a lease for the client.
  495. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::15"),
  496. duid_, iaid_, 300, 400, 100, 200,
  497. subnet_->getID(), HWAddrPtr(), 128));
  498. // Allocated 200 seconds ago - half of the lifetime.
  499. time_t lease_cltt = time(NULL) - 200;
  500. lease->cltt_ = lease_cltt;
  501. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  502. // Client should receive a lease.
  503. Lease6Ptr new_lease = simpleAlloc6Test(pool_, IOAddress("::"), false);
  504. ASSERT_TRUE(new_lease);
  505. // And the lease lifetime should be extended.
  506. EXPECT_GT(new_lease->cltt_, lease_cltt)
  507. << "Lease lifetime was not extended, but it should";
  508. }
  509. // Checks if the lease lifetime is extended when the client sends the
  510. // Request and the client has a reservation for the lease.
  511. TEST_F(AllocEngine6Test, requestExtendLeaseLifetimeForReservation) {
  512. // Create reservation for the client. This is in-pool reservation,
  513. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  514. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  515. // Create a lease for the client.
  516. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::1c"),
  517. duid_, iaid_, 300, 400, 100, 200,
  518. subnet_->getID(), HWAddrPtr(), 128));
  519. // Allocated 200 seconds ago - half of the lifetime.
  520. time_t lease_cltt = time(NULL) - 200;
  521. lease->cltt_ = lease_cltt;
  522. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  523. // Client should receive a lease.
  524. Lease6Ptr new_lease = simpleAlloc6Test(pool_, IOAddress("::"), false);
  525. ASSERT_TRUE(new_lease);
  526. // And the lease lifetime should be extended.
  527. EXPECT_GT(new_lease->cltt_, lease_cltt)
  528. << "Lease lifetime was not extended, but it should";
  529. }
  530. // Checks if the lease lifetime is extended when the client sends the
  531. // Renew.
  532. TEST_F(AllocEngine6Test, renewExtendLeaseLifetime) {
  533. // Create a lease for the client.
  534. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::15"),
  535. duid_, iaid_, 300, 400, 100, 200,
  536. subnet_->getID(), HWAddrPtr(), 128));
  537. // Allocated 200 seconds ago - half of the lifetime.
  538. time_t lease_cltt = time(NULL) - 200;
  539. lease->cltt_ = lease_cltt;
  540. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  541. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  542. // This is what the client will send in his renew message.
  543. AllocEngine::HintContainer hints;
  544. hints.push_back(make_pair(IOAddress("2001:db8:1::15"), 128));
  545. // Client should receive a lease.
  546. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  547. ASSERT_EQ(1, renewed.size());
  548. // And the lease lifetime should be extended.
  549. EXPECT_GT(renewed[0]->cltt_, lease_cltt)
  550. << "Lease lifetime was not extended, but it should";
  551. }
  552. // Checks if the lease lifetime is extended when the client sends the
  553. // Renew and the client has a reservation for the lease.
  554. TEST_F(AllocEngine6Test, renewExtendLeaseLifetimeForReservation) {
  555. // Create reservation for the client. This is in-pool reservation,
  556. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  557. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::15"), 128);
  558. // Create a lease for the client.
  559. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress("2001:db8:1::15"),
  560. duid_, iaid_, 300, 400, 100, 200,
  561. subnet_->getID(), HWAddrPtr(), 128));
  562. // Allocated 200 seconds ago - half of the lifetime.
  563. time_t lease_cltt = time(NULL) - 200;
  564. lease->cltt_ = lease_cltt;
  565. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  566. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  567. // This is what the client will send in his renew message.
  568. AllocEngine::HintContainer hints;
  569. hints.push_back(make_pair(IOAddress("2001:db8:1::15"), 128));
  570. // Client should receive a lease.
  571. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  572. ASSERT_EQ(1, renewed.size());
  573. // And the lease lifetime should be extended.
  574. EXPECT_GT(renewed[0]->cltt_, lease_cltt)
  575. << "Lease lifetime was not extended, but it should";
  576. }
  577. // --- v6 host reservation ---
  578. // Checks that a client gets the address reserved (in-pool case)
  579. // This test checks the behavior of the allocation engine in the following
  580. // scenario:
  581. // - Client has no lease in the database.
  582. // - Client has an in-pool reservation.
  583. // - Client sends SOLICIT without any hints.
  584. // - Client is allocated a reserved address.
  585. //
  586. // Note that a DHCPv6 client can, but doesn't have to send any hints in its
  587. // Solicit message.
  588. TEST_F(AllocEngine6Test, reservedAddressInPoolSolicitNoHint) {
  589. // Create reservation for the client. This is in-pool reservation,
  590. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  591. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  592. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  593. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), true);
  594. ASSERT_TRUE(lease);
  595. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  596. }
  597. // Checks that a client gets the address reserved (in-pool case)
  598. // This test checks the behavior of the allocation engine in the following
  599. // scenario:
  600. // - Client has no lease in the database.
  601. // - Client has an in-pool reservation.
  602. // - Client sends REQUEST without any hints.
  603. // - Client is allocated a reserved address.
  604. //
  605. // Note that DHCPv6 client must send an address in REQUEST that the server
  606. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  607. TEST_F(AllocEngine6Test, reservedAddressInPoolRequestNoHint) {
  608. // Create reservation for the client. This is in-pool reservation,
  609. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  610. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  611. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  612. // By default we pretend our subnet has 100 addresses
  613. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  614. StatsMgr::instance().setValue(name, static_cast<int64_t>(100));
  615. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), false);
  616. ASSERT_TRUE(lease);
  617. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  618. // We should have bumped the address counter
  619. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  620. ASSERT_TRUE(stat);
  621. EXPECT_EQ(101, stat->getInteger().first);
  622. }
  623. // Checks that a client gets the address reserved (in-pool case)
  624. // This test checks the behavior of the allocation engine in the following
  625. // scenario:
  626. // - Client has no lease in the database.
  627. // - Client has an in-pool reservation.
  628. // - Client sends SOLICIT with a hint that does not match reservation
  629. // - Client is allocated a reserved address, not the hint.
  630. //
  631. // Note that DHCPv6 client can, but don't have to send any hints in its
  632. // Solicit message.
  633. TEST_F(AllocEngine6Test, reservedAddressInPoolSolicitValidHint) {
  634. // Create reservation for the client. This is in-pool reservation,
  635. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  636. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  637. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  638. // Let's pretend the client sends hint 2001:db8:1::10.
  639. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), true);
  640. ASSERT_TRUE(lease);
  641. // The hint should be ignored and the reserved address should be assigned
  642. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  643. }
  644. // Checks that a client gets the address reserved (in-pool case)
  645. // This test checks the behavior of the allocation engine in the following
  646. // scenario:
  647. // - Client has no lease in the database.
  648. // - Client has an in-pool reservation.
  649. // - Client sends REQUEST with a hint that does not match reservation
  650. // - Client is allocated a reserved address, not the hint.
  651. //
  652. // Note that DHCPv6 client must send an address in REQUEST that the server
  653. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  654. TEST_F(AllocEngine6Test, reservedAddressInPoolRequestValidHint) {
  655. // Create reservation for the client This is in-pool reservation,
  656. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  657. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  658. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  659. // Let's pretend the client sends hint 2001:db8:1::10.
  660. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), false);
  661. ASSERT_TRUE(lease);
  662. // The hint should be ignored and the reserved address should be assigned
  663. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  664. }
  665. // Checks that a client gets the address reserved (in-pool case)
  666. // This test checks the behavior of the allocation engine in the following
  667. // scenario:
  668. // - Client has no lease in the database.
  669. // - Client has an in-pool reservation.
  670. // - Client sends SOLICIT with a hint that does matches reservation
  671. // - Client is allocated a reserved address, not the hint.
  672. //
  673. // Note that DHCPv6 client can, but don't have to send any hints in its
  674. // Solicit message.
  675. TEST_F(AllocEngine6Test, reservedAddressInPoolSolicitMatchingHint) {
  676. // Create reservation for the client. This is in-pool reservation,
  677. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  678. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  679. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  680. // Let's pretend the client sends hint 2001:db8:1::10.
  681. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::1c"), true);
  682. ASSERT_TRUE(lease);
  683. // The hint should be ignored and the reserved address should be assigned
  684. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  685. }
  686. // Checks that a client gets the address reserved (in-pool case)
  687. // This test checks the behavior of the allocation engine in the following
  688. // scenario:
  689. // - Client has no lease in the database.
  690. // - Client has an in-pool reservation.
  691. // - Client sends REQUEST with a hint that does not match reservation
  692. // - Client is allocated a reserved address, not the hint.
  693. //
  694. // Note that DHCPv6 client must send an address in REQUEST that the server
  695. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  696. TEST_F(AllocEngine6Test, reservedAddressInPoolRequestMatchingHint) {
  697. // Create reservation for the client. This is in-pool reservation,
  698. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  699. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  700. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  701. // Let's pretend the client sends hint 2001:db8:1::10.
  702. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::1c"), false);
  703. ASSERT_TRUE(lease);
  704. // The hint should be ignored and the reserved address should be assigned
  705. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  706. }
  707. // Checks that a client gets the address reserved (out-of-pool case)
  708. // This test checks the behavior of the allocation engine in the following
  709. // scenario:
  710. // - Client has no lease in the database.
  711. // - Client has an out-of-pool reservation.
  712. // - Client sends SOLICIT without any hints.
  713. // - Client is allocated a reserved address.
  714. //
  715. // Note that DHCPv6 client can, but don't have to send any hints in its
  716. // Solicit message.
  717. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolSolicitNoHint) {
  718. // Create reservation for the client. This is out-of-pool reservation,
  719. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  720. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  721. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  722. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), true, false);
  723. ASSERT_TRUE(lease);
  724. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  725. }
  726. // Checks that a client gets the address reserved (out-of-pool case)
  727. // This test checks the behavior of the allocation engine in the following
  728. // scenario:
  729. // - Client has no lease in the database.
  730. // - Client has an out-of-pool reservation.
  731. // - Client sends REQUEST without any hints.
  732. // - Client is allocated a reserved address.
  733. //
  734. // Note that DHCPv6 client must send an address in REQUEST that the server
  735. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  736. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolRequestNoHint) {
  737. // Create reservation for the client. This is out-of-pool reservation,
  738. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  739. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  740. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  741. // By default we pretend our subnet has 100 addresses
  742. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  743. StatsMgr::instance().setValue(name, static_cast<int64_t>(100));
  744. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), false, false);
  745. ASSERT_TRUE(lease);
  746. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  747. // We should not have bumped the address counter
  748. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  749. ASSERT_TRUE(stat);
  750. EXPECT_EQ(100, stat->getInteger().first);
  751. }
  752. // Checks that a client gets the address reserved (in-pool case)
  753. // This test checks the behavior of the allocation engine in the following
  754. // scenario:
  755. // - Client has no lease in the database.
  756. // - Client has an in-pool reservation.
  757. // - Client sends SOLICIT with a hint that does not match reservation
  758. // - Client is allocated a reserved address, not the hint.
  759. //
  760. // Note that DHCPv6 client can, but don't have to send any hints in its
  761. // Solicit message.
  762. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolSolicitValidHint) {
  763. // Create reservation for the client. This is out-of-pool reservation,
  764. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  765. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  766. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  767. // Let's pretend the client sends hint 2001:db8:1::10.
  768. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), true, false);
  769. ASSERT_TRUE(lease);
  770. // The hint should be ignored and the reserved address should be assigned
  771. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  772. }
  773. // Checks that a client gets the address reserved (out-of-pool case)
  774. // This test checks the behavior of the allocation engine in the following
  775. // scenario:
  776. // - Client has no lease in the database.
  777. // - Client has an out-of-pool reservation.
  778. // - Client sends REQUEST with a hint that does not match reservation
  779. // - Client is allocated a reserved address, not the hint.
  780. //
  781. // Note that DHCPv6 client must send an address in REQUEST that the server
  782. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  783. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolRequestValidHint) {
  784. // Create reservation for the client. This is out-of-pool reservation,
  785. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  786. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  787. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  788. // Let's pretend the client sends hint 2001:db8:1::10.
  789. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), false, false);
  790. ASSERT_TRUE(lease);
  791. // The hint should be ignored and the reserved address should be assigned
  792. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  793. }
  794. // Checks that a client gets the address reserved (out-of-pool case)
  795. // This test checks the behavior of the allocation engine in the following
  796. // scenario:
  797. // - Client has no lease in the database.
  798. // - Client has an out-of-pool reservation.
  799. // - Client sends SOLICIT with a hint that does matches reservation
  800. // - Client is allocated a reserved address, not the hint.
  801. //
  802. // Note that DHCPv6 client can, but don't have to send any hints in its
  803. // Solicit message.
  804. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolSolicitMatchingHint) {
  805. // Create reservation for the client. This is out-of-pool reservation,
  806. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  807. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  808. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  809. // Let's pretend the client sends hint 2001:db8:1::10.
  810. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::1c"), true, false);
  811. ASSERT_TRUE(lease);
  812. // The hint should be ignored and the reserved address should be assigned
  813. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  814. }
  815. // Checks that a client gets the address reserved (out-of-pool case)
  816. // This test checks the behavior of the allocation engine in the following
  817. // scenario:
  818. // - Client has no lease in the database.
  819. // - Client has an out-of-pool reservation.
  820. // - Client sends REQUEST with a hint that does not match reservation
  821. // - Client is allocated a reserved address, not the hint.
  822. //
  823. // Note that DHCPv6 client must send an address in REQUEST that the server
  824. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  825. TEST_F(AllocEngine6Test, reservedAddressOutOfPoolRequestMatchingHint) {
  826. // Create reservation for the client. This is out-of-pool reservation,
  827. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  828. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8::abcd"), 128);
  829. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  830. // Let's pretend the client sends hint 2001:db8:1::10.
  831. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::1c"), false, false);
  832. ASSERT_TRUE(lease);
  833. // The hint should be ignored and the reserved address should be assigned
  834. EXPECT_EQ("2001:db8::abcd", lease->addr_.toText());
  835. }
  836. // In the following situation:
  837. // - client is assigned an address A
  838. // - HR is made for *this* client to get B
  839. // - client tries to get address A:
  840. // Check that his existing lease for lease A is removed
  841. // Check that he is assigned a new lease for B
  842. // - verify that the number of assigned address behaves as expected
  843. TEST_F(AllocEngine6Test, reservedAddressInPoolReassignedThis) {
  844. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  845. // Client gets an address
  846. Lease6Ptr lease1 = simpleAlloc6Test(pool_, IOAddress("::"), false);
  847. ASSERT_TRUE(lease1);
  848. // We should have bumped the address counter
  849. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  850. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  851. ASSERT_TRUE(stat);
  852. EXPECT_EQ(101, stat->getInteger().first);
  853. // Just check that if the client requests again, it will get the same
  854. // address.
  855. Lease6Ptr lease2 = simpleAlloc6Test(pool_, lease1->addr_, false);
  856. ASSERT_TRUE(lease2);
  857. detailCompareLease(lease1, lease2);
  858. // We should not have bumped the address counter again
  859. EXPECT_EQ(101, stat->getInteger().first);
  860. // Now admin creates a reservation for this client. This is in-pool
  861. // reservation, as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  862. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  863. // Just check that this time the client will get.
  864. Lease6Ptr lease3 = simpleAlloc6Test(pool_, lease1->addr_, false);
  865. ASSERT_TRUE(lease3);
  866. // Check that previous lease was not used anymore.
  867. EXPECT_NE(lease1->addr_.toText(), lease3->addr_.toText());
  868. // Check that the reserved address was indeed assigned.
  869. EXPECT_EQ("2001:db8:1::1c", lease3->addr_.toText());
  870. // Check that the old lease is gone.
  871. Lease6Ptr old = LeaseMgrFactory::instance().getLease6(lease1->type_,
  872. lease1->addr_);
  873. EXPECT_FALSE(old);
  874. // Check that the reserved lease is in the database.
  875. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease1->type_,
  876. IOAddress("2001:db8:1::1c"));
  877. ASSERT_TRUE(from_mgr);
  878. // Now check that the lease in LeaseMgr has the same parameters
  879. detailCompareLease(lease3, from_mgr);
  880. // Lastly check to see that the address counter is still 101 we should have
  881. // have decremented it on the implied release and incremented it on the reserved
  882. EXPECT_EQ(101, stat->getInteger().first);
  883. }
  884. // In the following situation:
  885. // - client X is assigned an address A
  886. // - HR is made for client Y (*other* client) to get A
  887. // - client X tries to get address A:
  888. // Check that his existing lease for lease A is removed
  889. // Check that he is assigned a new lease
  890. TEST_F(AllocEngine6Test, reservedAddressInPoolReassignedOther) {
  891. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  892. // Client gets an address
  893. Lease6Ptr lease1 = simpleAlloc6Test(pool_, IOAddress("::"), false);
  894. ASSERT_TRUE(lease1);
  895. // We should have bumped the address counter
  896. string name = StatsMgr::generateName("subnet", subnet_->getID(), "assigned-nas");
  897. ObservationPtr stat = StatsMgr::instance().getObservation(name);
  898. ASSERT_TRUE(stat);
  899. EXPECT_EQ(101, stat->getInteger().first);
  900. // Just check that if the client requests again, it will get the same
  901. // address.
  902. Lease6Ptr lease2 = simpleAlloc6Test(pool_, lease1->addr_, false);
  903. ASSERT_TRUE(lease2);
  904. detailCompareLease(lease1, lease2);
  905. // We should not have bumped the address counter again
  906. EXPECT_EQ(101, stat->getInteger().first);
  907. // Now admin creates a reservation for this client. Let's use the
  908. // address client X just received. Let's generate a host, but don't add it
  909. // to the HostMgr yet.
  910. HostPtr host = createHost6(false, IPv6Resrv::TYPE_NA, lease1->addr_, 128);
  911. // We need to tweak reservation id: use a different DUID for client Y
  912. vector<uint8_t> other_duid(8, 0x45);
  913. host->setIdentifier(&other_duid[0], other_duid.size(), Host::IDENT_DUID);
  914. // Ok, now add it to the HostMgr
  915. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  916. CfgMgr::instance().commit();
  917. // Just check that this time the client will get a different lease.
  918. Lease6Ptr lease3 = simpleAlloc6Test(pool_, lease1->addr_, false);
  919. ASSERT_TRUE(lease3);
  920. // Check that previous lease was not used anymore.
  921. EXPECT_NE(lease1->addr_.toText(), lease3->addr_.toText());
  922. // Check that the old lease is gone.
  923. Lease6Ptr old = LeaseMgrFactory::instance().getLease6(lease1->type_,
  924. lease1->addr_);
  925. EXPECT_FALSE(old);
  926. // Check that the reserved lease is in the database.
  927. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease1->type_,
  928. lease3->addr_);
  929. ASSERT_TRUE(from_mgr);
  930. // Now check that the lease in LeaseMgr has the same parameters
  931. detailCompareLease(lease3, from_mgr);
  932. // Lastly check to see that the address counter is still 101 we should have
  933. // have decremented it on the implied release and incremented it on the reserved
  934. EXPECT_EQ(101, stat->getInteger().first);
  935. }
  936. // Checks that a reserved address for client A is not assigned when
  937. // other clients are requesting addresses. The scenario is as follows:
  938. // we use a regular pool with 17 addresses in it. One of them is
  939. // reserved for client A. Now we try to allocate addresses for 30 clients
  940. // (A is not one of them). The first 16 attempts should succeed. Then
  941. // we run out of addresses and remaining 14 clients will get nothing.
  942. // Finally, we check that client A still can get his reserved address.
  943. TEST_F(AllocEngine6Test, reservedAddress) {
  944. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, true);
  945. // Create reservation for the client. This is in-pool reservation,
  946. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  947. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::12"), 128);
  948. // Let's generate 30 DUIDs, each of them 16 bytes long
  949. vector<DuidPtr> clients;
  950. for (int i = 0; i < 30; i++) {
  951. vector<uint8_t> data(16, i);
  952. clients.push_back(DuidPtr(new DUID(data)));
  953. }
  954. // The default pool is 2001:db8:1::10 to 2001:db8:1::20. There's 17
  955. // addresses in it. One of them is reserved, so this means that we should
  956. // get 16 successes and 14 (20-16) failures.
  957. int success = 0;
  958. int failure = 0;
  959. for (int i = 0; i < 30; i++) {
  960. AllocEngine::ClientContext6 ctx(subnet_, clients[i], iaid_, IOAddress("::"),
  961. Lease::TYPE_NA, false, false, "", false);
  962. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  963. findReservation(engine, ctx);
  964. Lease6Collection leases = engine.allocateLeases6(ctx);
  965. if (leases.empty()) {
  966. failure++;
  967. std::cout << "Alloc for client " << (int)i << " failed." << std::endl;
  968. } else {
  969. success++;
  970. std::cout << "Alloc for client " << (int)i << " succeeded:"
  971. << leases[0]->addr_.toText() << std::endl;
  972. // The assigned addresses must not match the one reserved.
  973. EXPECT_NE("2001:db8:1::12", leases[0]->addr_.toText());
  974. }
  975. }
  976. EXPECT_EQ(16, success);
  977. EXPECT_EQ(14, failure);
  978. // We're now pretty sure that any clients other than the reserved address
  979. // will not get any service. Now let's check if the client that has the
  980. // address reserved, will get it (despite the pool being depleted).
  981. AllocEngine::ClientContext6 ctx(subnet_, duid_, iaid_, IOAddress("::"),
  982. Lease::TYPE_NA, false, false, "", false);
  983. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  984. findReservation(engine, ctx);
  985. Lease6Collection leases = engine.allocateLeases6(ctx);
  986. ASSERT_EQ(1, leases.size());
  987. EXPECT_EQ("2001:db8:1::12", leases[0]->addr_.toText());
  988. }
  989. // Checks if the allocateLeases throws exceptions for invalid input data.
  990. TEST_F(AllocEngine6Test, allocateLeasesInvalidData) {
  991. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, true);
  992. // That looks like a valid context.
  993. AllocEngine::ClientContext6 ctx(subnet_, duid_, iaid_, IOAddress("::"),
  994. Lease::TYPE_NA, false, false, "", false);
  995. ctx.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  996. Lease6Collection leases;
  997. // Let's break it!
  998. ctx.subnet_.reset();
  999. // Subnet is required for allocation, so we should get no leases.
  1000. EXPECT_NO_THROW(leases = engine.allocateLeases6(ctx));
  1001. EXPECT_TRUE(leases.empty());
  1002. // Let's fix this and break it in a different way.
  1003. ctx.subnet_ = subnet_;
  1004. ctx.duid_.reset();
  1005. // We must know who we're allocating for. No duid = no service.
  1006. EXPECT_NO_THROW(leases = engine.allocateLeases6(ctx));
  1007. EXPECT_TRUE(leases.empty());
  1008. }
  1009. // Checks whether an address can be renewed (simple case, no reservation tricks)
  1010. TEST_F(AllocEngine6Test, addressRenewal) {
  1011. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1012. Lease6Collection leases;
  1013. leases = allocateTest(engine, pool_, IOAddress("::"), false, true);
  1014. ASSERT_EQ(1, leases.size());
  1015. // This is what the client will send in his renew message.
  1016. AllocEngine::HintContainer hints;
  1017. hints.push_back(make_pair(leases[0]->addr_, 128));
  1018. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  1019. ASSERT_EQ(1, renewed.size());
  1020. // Check that the lease was indeed renewed and hasn't changed
  1021. // (i.e. the same address, preferred and valid lifetimes)
  1022. /// @todo: use leaseCompare, but ignore cltt_
  1023. EXPECT_EQ(leases[0]->addr_, renewed[0]->addr_);
  1024. EXPECT_EQ(leases[0]->type_, renewed[0]->type_);
  1025. EXPECT_EQ(leases[0]->preferred_lft_, renewed[0]->preferred_lft_);
  1026. EXPECT_EQ(leases[0]->valid_lft_, renewed[0]->valid_lft_);
  1027. }
  1028. // Checks whether an address can be renewed (in-pool reservation)
  1029. TEST_F(AllocEngine6Test, reservedAddressRenewal) {
  1030. // Create reservation for the client. This is in-pool reservation,
  1031. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1032. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  1033. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1034. Lease6Collection leases;
  1035. leases = allocateTest(engine, pool_, IOAddress("::"), false, true);
  1036. ASSERT_EQ(1, leases.size());
  1037. ASSERT_EQ("2001:db8:1::1c", leases[0]->addr_.toText());
  1038. // This is what the client will send in his renew message.
  1039. AllocEngine::HintContainer hints;
  1040. hints.push_back(make_pair(leases[0]->addr_, 128));
  1041. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  1042. ASSERT_EQ(1, renewed.size());
  1043. ASSERT_EQ("2001:db8:1::1c", leases[0]->addr_.toText());
  1044. }
  1045. // Checks whether a single host can have more than one reservation.
  1046. //
  1047. /// @todo: as of #3677, this does not work. When processing solicit with two
  1048. /// IA_NAs and two reservations, there currently no way to indicate that
  1049. /// the first reservation should be used for the first IA and the second
  1050. /// reservation for the second IA. This works for Requests and Renews, though.
  1051. /// In both of those messages, when processing of the first IA is complete,
  1052. /// we have a lease in the database. Based on that, when processing the second
  1053. /// IA we can detect that the first reservated address is in use already and
  1054. /// use the second reservation.
  1055. TEST_F(AllocEngine6Test, DISABLED_reserved2AddressesSolicit) {
  1056. // Create reservation for the client. This is in-pool reservation,
  1057. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1058. // Two addresses are reserved: 2001:db8:1::babe and 2001:db8:1::cafe
  1059. HostPtr host = createHost6(true, IPv6Resrv::TYPE_NA,
  1060. IOAddress("2001:db8:1::babe"), 128);
  1061. IPv6Resrv resv2(IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::cafe"), 128);
  1062. host->addReservation(resv2);
  1063. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  1064. CfgMgr::instance().commit();
  1065. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1066. AllocEngine::ClientContext6 ctx1(subnet_, duid_, iaid_, IOAddress("::"),
  1067. pool_->getType(), false, false, "", true);
  1068. ctx1.query_.reset(new Pkt6(DHCPV6_SOLICIT, 1234));
  1069. Lease6Collection leases1;
  1070. findReservation(engine, ctx1);
  1071. EXPECT_NO_THROW(leases1 = engine.allocateLeases6(ctx1));
  1072. ASSERT_EQ(1, leases1.size());
  1073. EXPECT_EQ("2001:db8:1::babe", leases1[0]->addr_.toText());
  1074. // Double check that repeating the same duid/type/iaid will end up with
  1075. // the same address.
  1076. AllocEngine::ClientContext6 ctx2(subnet_, duid_, iaid_, IOAddress("::"),
  1077. pool_->getType(), false, false, "", true);
  1078. ctx2.query_.reset(new Pkt6(DHCPV6_SOLICIT, 1234));
  1079. Lease6Collection leases2;
  1080. findReservation(engine, ctx2);
  1081. EXPECT_NO_THROW(leases2 = engine.allocateLeases6(ctx2));
  1082. EXPECT_EQ(1, leases2.size());
  1083. EXPECT_EQ("2001:db8:1::babe", leases2[0]->addr_.toText());
  1084. // Ok, now the tricky part. Request allocation for the same duid and type, but
  1085. // different iaid. The second address should be assigned.
  1086. AllocEngine::ClientContext6 ctx3(subnet_, duid_, iaid_ + 1, IOAddress("::"),
  1087. pool_->getType(), false, false, "", true);
  1088. ctx3.query_.reset(new Pkt6(DHCPV6_SOLICIT, 1234));
  1089. Lease6Collection leases3;
  1090. findReservation(engine, ctx3);
  1091. EXPECT_NO_THROW(leases3 = engine.allocateLeases6(ctx3));
  1092. ASSERT_EQ(1, leases3.size());
  1093. EXPECT_EQ("2001:db8:1::cafe", leases3[0]->addr_.toText());
  1094. }
  1095. // Checks whether a single host can have more than one reservation.
  1096. TEST_F(AllocEngine6Test, reserved2Addresses) {
  1097. // Create reservation for the client. This is in-pool reservation,
  1098. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1099. // Two addresses are reserved: 2001:db8:1::babe and 2001:db8:1::cafe
  1100. HostPtr host = createHost6(true, IPv6Resrv::TYPE_NA,
  1101. IOAddress("2001:db8:1::babe"), 128);
  1102. IPv6Resrv resv2(IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::cafe"), 128);
  1103. host->addReservation(resv2);
  1104. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  1105. CfgMgr::instance().commit();
  1106. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1107. AllocEngine::ClientContext6 ctx1(subnet_, duid_, iaid_, IOAddress("::"),
  1108. pool_->getType(), false, false, "", false);
  1109. ctx1.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  1110. Lease6Collection leases1;
  1111. findReservation(engine, ctx1);
  1112. EXPECT_NO_THROW(leases1 = engine.allocateLeases6(ctx1));
  1113. ASSERT_EQ(1, leases1.size());
  1114. EXPECT_EQ("2001:db8:1::babe", leases1[0]->addr_.toText());
  1115. // Double check that repeating the same duid/type/iaid will end up with
  1116. // the same address.
  1117. AllocEngine::ClientContext6 ctx2(subnet_, duid_, iaid_, IOAddress("::"),
  1118. pool_->getType(), false, false, "", false);
  1119. ctx2.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  1120. Lease6Collection leases2;
  1121. findReservation(engine, ctx2);
  1122. EXPECT_NO_THROW(leases2 = engine.allocateLeases6(ctx2));
  1123. EXPECT_EQ(1, leases2.size());
  1124. EXPECT_EQ("2001:db8:1::babe", leases2[0]->addr_.toText());
  1125. // Ok, now the tricky part. Request allocation for the same duid and type, but
  1126. // different iaid. The second address should be assigned.
  1127. AllocEngine::ClientContext6 ctx3(subnet_, duid_, iaid_ + 1, IOAddress("::"),
  1128. pool_->getType(), false, false, "", false);
  1129. ctx3.query_.reset(new Pkt6(DHCPV6_REQUEST, 1234));
  1130. Lease6Collection leases3;
  1131. findReservation(engine, ctx3);
  1132. EXPECT_NO_THROW(leases3 = engine.allocateLeases6(ctx3));
  1133. ASSERT_EQ(1, leases3.size());
  1134. EXPECT_EQ("2001:db8:1::cafe", leases3[0]->addr_.toText());
  1135. }
  1136. // Checks whether address can change during renew (if there is a new
  1137. // reservation for this client)
  1138. TEST_F(AllocEngine6Test, reservedAddressRenewChange) {
  1139. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1140. Lease6Collection leases;
  1141. leases = allocateTest(engine, pool_, IOAddress("::"), false, true);
  1142. ASSERT_EQ(1, leases.size());
  1143. ASSERT_NE("2001:db8:1::1c", leases[0]->addr_.toText());
  1144. // This is what the client will send in his renew message.
  1145. AllocEngine::HintContainer hints;
  1146. hints.push_back(make_pair(leases[0]->addr_, 128));
  1147. // Create reservation for the client. This is in-pool reservation,
  1148. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1149. createHost6(true, IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::1c"), 128);
  1150. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  1151. ASSERT_EQ(1, renewed.size());
  1152. ASSERT_EQ("2001:db8:1::1c", renewed[0]->addr_.toText());
  1153. }
  1154. // Checks whether address can change during renew (if there is a new
  1155. // reservation for this address for another client)
  1156. TEST_F(AllocEngine6Test, reservedAddressRenewReserved) {
  1157. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100);
  1158. Lease6Collection leases;
  1159. leases = allocateTest(engine, pool_, IOAddress("::"), false, true);
  1160. ASSERT_EQ(1, leases.size());
  1161. // This is what the client will send in his renew message.
  1162. AllocEngine::HintContainer hints;
  1163. hints.push_back(make_pair(leases[0]->addr_, 128));
  1164. // Create reservation for this address, but for another client.
  1165. // This is in-pool reservation, as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1166. HostPtr host = createHost6(false, IPv6Resrv::TYPE_NA, leases[0]->addr_, 128);
  1167. // We need to tweak reservation id: use a different DUID for client Y
  1168. vector<uint8_t> other_duid(8, 0x45);
  1169. host->setIdentifier(&other_duid[0], other_duid.size(), Host::IDENT_DUID);
  1170. // Ok, now add it to the HostMgr
  1171. CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
  1172. CfgMgr::instance().commit();
  1173. Lease6Collection renewed = renewTest(engine, pool_, hints, true);
  1174. ASSERT_EQ(1, renewed.size());
  1175. // Check that we no longer have the reserved address.
  1176. ASSERT_NE(leases[0]->addr_.toText(), renewed[0]->addr_.toText());
  1177. // Check that the lease for the now reserved address is no longer in
  1178. // the lease database.
  1179. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1180. leases[0]->addr_);
  1181. EXPECT_FALSE(from_mgr);
  1182. }
  1183. /// @todo: The following methods are tested indirectly by allocateLeases6()
  1184. /// tests, but could use more direct testing:
  1185. /// - AllocEngine::allocateUnreservedLeases6
  1186. /// - AllocEngine::allocateReservedLeases6
  1187. /// - AllocEngine::removeNonmatchingReservedLeases6
  1188. /// - AllocEngine::removeLeases
  1189. /// - AllocEngine::removeNonreservedLeases6
  1190. // Checks that a client gets the address reserved (in-pool case)
  1191. // This test checks the behavior of the allocation engine in the following
  1192. // scenario:
  1193. // - Client has no lease in the database.
  1194. // - Client has an in-pool reservation.
  1195. // - Client sends SOLICIT without any hints.
  1196. // - Client is allocated a reserved address.
  1197. //
  1198. // Note that DHCPv6 client can, but don't have to send any hints in its
  1199. // Solicit message.
  1200. TEST_F(AllocEngine6Test, reservedAddressByMacInPoolSolicitNoHint) {
  1201. // Create reservation for the client. This is in-pool reservation,
  1202. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1203. createHost6HWAddr(true, IPv6Resrv::TYPE_NA, hwaddr_,
  1204. IOAddress("2001:db8:1::1c"), 128);
  1205. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  1206. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), true);
  1207. ASSERT_TRUE(lease);
  1208. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  1209. }
  1210. // Checks that a client gets the address reserved (in-pool case)
  1211. // This test checks the behavior of the allocation engine in the following
  1212. // scenario:
  1213. // - Client has no lease in the database.
  1214. // - Client has an in-pool reservation.
  1215. // - Client sends REQUEST without any hints.
  1216. // - Client is allocated a reserved address.
  1217. //
  1218. // Note that DHCPv6 client must send an address in REQUEST that the server
  1219. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  1220. TEST_F(AllocEngine6Test, reservedAddressByMacInPoolRequestNoHint) {
  1221. // Create reservation for the client. This is in-pool reservation,
  1222. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1223. createHost6HWAddr(true, IPv6Resrv::TYPE_NA, hwaddr_,
  1224. IOAddress("2001:db8:1::1c"), 128);
  1225. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  1226. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("::"), false);
  1227. ASSERT_TRUE(lease);
  1228. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  1229. }
  1230. // Checks that a client gets the address reserved (in-pool case)
  1231. // This test checks the behavior of the allocation engine in the following
  1232. // scenario:
  1233. // - Client has no lease in the database.
  1234. // - Client has an in-pool reservation.
  1235. // - Client sends SOLICIT with a hint that does not match reservation
  1236. // - Client is allocated a reserved address, not the hint.
  1237. //
  1238. // Note that DHCPv6 client can, but don't have to send any hints in its
  1239. // Solicit message.
  1240. TEST_F(AllocEngine6Test, reservedAddressByMacInPoolSolicitValidHint) {
  1241. // Create reservation for the client. This is in-pool reservation,
  1242. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1243. createHost6HWAddr(true, IPv6Resrv::TYPE_NA, hwaddr_,
  1244. IOAddress("2001:db8:1::1c"), 128);
  1245. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  1246. // Let's pretend the client sends hint 2001:db8:1::10.
  1247. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), true);
  1248. ASSERT_TRUE(lease);
  1249. // The hint should be ignored and the reserved address should be assigned
  1250. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  1251. }
  1252. // Checks that a client gets the address reserved (in-pool case)
  1253. // This test checks the behavior of the allocation engine in the following
  1254. // scenario:
  1255. // - Client has no lease in the database.
  1256. // - Client has an in-pool reservation.
  1257. // - Client sends REQUEST with a hint that does not match reservation
  1258. // - Client is allocated a reserved address, not the hint.
  1259. //
  1260. // Note that DHCPv6 client must send an address in REQUEST that the server
  1261. // offered in Advertise. Nevertheless, the client may ignore this requirement.
  1262. TEST_F(AllocEngine6Test, reservedAddressByMacInPoolRequestValidHint) {
  1263. // Create reservation for the client This is in-pool reservation,
  1264. // as the pool is 2001:db8:1::10 - 2001:db8:1::20.
  1265. createHost6HWAddr(true, IPv6Resrv::TYPE_NA, hwaddr_,
  1266. IOAddress("2001:db8:1::1c"), 128);
  1267. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
  1268. // Let's pretend the client sends hint 2001:db8:1::10.
  1269. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::10"), false);
  1270. ASSERT_TRUE(lease);
  1271. // The hint should be ignored and the reserved address should be assigned
  1272. EXPECT_EQ("2001:db8:1::1c", lease->addr_.toText());
  1273. }
  1274. // This test checks that the allocation engine can delegate the long prefix.
  1275. // The pool with prefix of 64 and with long delegated prefix has a very
  1276. // high capacity. The number of attempts that the allocation engine makes
  1277. // to allocate the prefix for high capacity pools is equal to the capacity
  1278. // value. This test verifies that the prefix can be allocated in that
  1279. // case.
  1280. TEST_F(AllocEngine6Test, largePDPool) {
  1281. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 0);
  1282. // Remove the default PD pool.
  1283. subnet_->delPools(Lease::TYPE_PD);
  1284. // Configure the PD pool with the prefix length of /64 and the delegated
  1285. // length /96.
  1286. Pool6Ptr pool(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1::"), 64, 96));
  1287. subnet_->addPool(pool);
  1288. // We should have got exactly one lease.
  1289. Lease6Collection leases = allocateTest(engine, pool, IOAddress("::"),
  1290. false, true);
  1291. ASSERT_EQ(1, leases.size());
  1292. }
  1293. // This test checks that the allocation engine can delegate addresses
  1294. // from ridiculously large pool. The configuration provides 2^80 or
  1295. // 1208925819614629174706176 addresses. We used to have a bug that would
  1296. // confuse the allocation engine if the number of available addresses
  1297. // was larger than 2^32.
  1298. TEST_F(AllocEngine6Test, largePoolOver32bits) {
  1299. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 0);
  1300. // Configure 2001:db8::/32 subnet
  1301. subnet_.reset(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  1302. // Configure the NA pool of /48. So there are 2^80 addresses there. Make
  1303. // sure that we still can handle cases where number of available addresses
  1304. // is over max_uint64
  1305. Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::"), 48));
  1306. subnet_->addPool(pool);
  1307. // We should have got exactly one lease.
  1308. Lease6Collection leases = allocateTest(engine, pool, IOAddress("::"),
  1309. false, true);
  1310. ASSERT_EQ(1, leases.size());
  1311. }
  1312. // This test verifies that it is possible to override the number of allocation
  1313. // attempts made by the allocation engine for a single lease.
  1314. TEST_F(AllocEngine6Test, largeAllocationAttemptsOverride) {
  1315. // Remove the default NA pools.
  1316. subnet_->delPools(Lease::TYPE_NA);
  1317. // Add exactly one pool with many addresses.
  1318. Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::"), 56));
  1319. subnet_->addPool(pool);
  1320. // Allocate 5 addresses from the pool configured.
  1321. for (int i = 0; i < 5; ++i) {
  1322. DuidPtr duid = DuidPtr(new DUID(vector<uint8_t>(12,
  1323. static_cast<uint8_t>(i))));
  1324. // Get the unique IAID.
  1325. const uint32_t iaid = 3568 + i;
  1326. // Construct the unique address from the pool.
  1327. std::ostringstream address;
  1328. address << "2001:db8:1::";
  1329. address << i;
  1330. // Allocate the leease.
  1331. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, IOAddress(address.str()),
  1332. duid, iaid, 501, 502, 503, 504, subnet_->getID(),
  1333. HWAddrPtr(), 0));
  1334. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1335. }
  1336. // Try to use the allocation engine to allocate the lease. The iterative
  1337. // allocator will pick the addresses already allocated until it finds the
  1338. // available address. Since, we have restricted the number of attempts the
  1339. // allocation should fail.
  1340. AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 3);
  1341. Lease6Collection leases = allocateTest(engine, pool_, IOAddress("::"),
  1342. false, true);
  1343. ASSERT_EQ(0, leases.size());
  1344. // This time, lets allow more attempts, and expect that the allocation will
  1345. // be successful.
  1346. AllocEngine engine2(AllocEngine::ALLOC_ITERATIVE, 6);
  1347. leases = allocateTest(engine2, pool_, IOAddress("::"), false, true);
  1348. ASSERT_EQ(1, leases.size());
  1349. }
  1350. // This test checks if an expired declined lease can be reused in SOLICIT (fake allocation)
  1351. TEST_F(AllocEngine6Test, solicitReuseDeclinedLease6) {
  1352. AllocEnginePtr engine(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100));
  1353. ASSERT_TRUE(engine);
  1354. // Now prepare a configuration with single address pool.
  1355. // Create one subnet with a pool holding one address.
  1356. string addr_txt("2001:db8:1::ad");
  1357. IOAddress addr(addr_txt);
  1358. initSubnet(IOAddress("2001:db8:1::"), addr, addr);
  1359. // Use information that is different than what we'll request
  1360. Lease6Ptr declined = generateDeclinedLease(addr_txt, 100, -10);
  1361. ASSERT_TRUE(declined->expired());
  1362. // CASE 1: Asking for any address
  1363. Lease6Ptr assigned;
  1364. testReuseLease6(engine, declined, "::", true, SHOULD_PASS, assigned);
  1365. // Check that we got that single lease
  1366. ASSERT_TRUE(assigned);
  1367. EXPECT_EQ(addr, assigned->addr_);
  1368. // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
  1369. checkLease6(assigned, Lease::TYPE_NA, 128);
  1370. // CASE 2: Asking specifically for this address
  1371. testReuseLease6(engine, declined, addr_txt, true, SHOULD_PASS, assigned);
  1372. // Check that we got that single lease
  1373. ASSERT_TRUE(assigned);
  1374. EXPECT_EQ(addr, assigned->addr_);
  1375. }
  1376. // This test checks if an expired declined lease can be reused when responding
  1377. // to REQUEST (actual allocation)
  1378. TEST_F(AllocEngine6Test, requestReuseDeclinedLease6) {
  1379. AllocEnginePtr engine(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100, true));
  1380. ASSERT_TRUE(engine);
  1381. // Now prepare a configuration with single address pool.
  1382. string addr_txt("2001:db8::7");
  1383. IOAddress addr(addr_txt);
  1384. initSubnet(IOAddress("2001:db8::"), addr, addr);
  1385. // Now create a declined lease, decline it and rewind its cltt, so it
  1386. // is expired.
  1387. Lease6Ptr declined = generateDeclinedLease(addr_txt, 100, -10);
  1388. // Asking specifically for this address
  1389. Lease6Ptr assigned;
  1390. testReuseLease6(engine, declined, addr_txt, false, SHOULD_PASS, assigned);
  1391. // Check that we got it.
  1392. ASSERT_TRUE(assigned);
  1393. EXPECT_EQ(addr, assigned->addr_);
  1394. // Check that the lease is indeed updated in LeaseMgr
  1395. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  1396. addr);
  1397. ASSERT_TRUE(from_mgr);
  1398. // Now check that the lease in LeaseMgr has the same parameters
  1399. detailCompareLease(assigned, from_mgr);
  1400. }
  1401. // This test checks if statistics are not updated when expired declined lease
  1402. // is reused when responding to SOLICIT (fake allocation)
  1403. TEST_F(AllocEngine6Test, solicitReuseDeclinedLease6Stats) {
  1404. // Now prepare for SOLICIT processing
  1405. AllocEnginePtr engine(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1406. 100, true));
  1407. ASSERT_TRUE(engine);
  1408. // Now prepare a configuration with single address pool.
  1409. string addr_txt("2001:db8:1::1");
  1410. IOAddress addr(addr_txt);
  1411. initSubnet(IOAddress("2001:db8:1::"), addr, addr);
  1412. // Now create a declined lease, decline it and rewind its cltt, so it
  1413. // is expired.
  1414. Lease6Ptr declined = generateDeclinedLease(addr_txt, 100, -10);
  1415. // Let's fix some global stats...
  1416. StatsMgr& stats_mgr = StatsMgr::instance();
  1417. stats_mgr.setValue("declined-addresses", static_cast<int64_t>(1000));
  1418. stats_mgr.setValue("reclaimed-declined-addresses", static_cast<int64_t>(1000));
  1419. // ...and subnet specific stats as well.
  1420. string stat1 = StatsMgr::generateName("subnet", subnet_->getID(),
  1421. "declined-addresses");
  1422. string stat2 = StatsMgr::generateName("subnet", subnet_->getID(),
  1423. "reclaimed-declined-addresses");
  1424. stats_mgr.setValue(stat1, static_cast<int64_t>(1000));
  1425. stats_mgr.setValue(stat2, static_cast<int64_t>(1000));
  1426. // Ask for any address. There's only one address in the pool, so it doesn't
  1427. // matter much.
  1428. Lease6Ptr assigned;
  1429. testReuseLease6(engine, declined, "::", true, SHOULD_PASS, assigned);
  1430. // Check that the stats were not modified
  1431. testStatistics("declined-addresses", 1000);
  1432. testStatistics("reclaimed-declined-addresses", 1000);
  1433. testStatistics(stat1, 1000);
  1434. testStatistics(stat2, 1000);
  1435. }
  1436. // This test checks if statistics are not updated when expired declined lease
  1437. // is reused when responding to REQUEST (actual allocation)
  1438. TEST_F(AllocEngine6Test, requestReuseDeclinedLease6Stats) {
  1439. // Prepare for REQUEST processing.
  1440. AllocEnginePtr engine(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1441. 100, true));
  1442. ASSERT_TRUE(engine);
  1443. // Now prepare a configuration with single address pool.
  1444. string addr_txt("2001:db8::1");
  1445. IOAddress addr(addr_txt);
  1446. initSubnet(IOAddress("2001:db8::"), addr, addr);
  1447. // Now create a declined lease, decline it and rewind its cltt, so it
  1448. // is expired.
  1449. Lease6Ptr declined = generateDeclinedLease(addr_txt, 100, -10);
  1450. // Let's fix some global stats...
  1451. StatsMgr& stats_mgr = StatsMgr::instance();
  1452. stats_mgr.setValue("declined-addresses", static_cast<int64_t>(1000));
  1453. stats_mgr.setValue("reclaimed-declined-addresses", static_cast<int64_t>(1000));
  1454. // ...and subnet specific stats as well.
  1455. string stat1 = StatsMgr::generateName("subnet", subnet_->getID(),
  1456. "declined-addresses");
  1457. string stat2 = StatsMgr::generateName("subnet", subnet_->getID(),
  1458. "reclaimed-declined-addresses");
  1459. stats_mgr.setValue(stat1, static_cast<int64_t>(1000));
  1460. stats_mgr.setValue(stat2, static_cast<int64_t>(1000));
  1461. // Ask for any address. There's only one address in the pool, so it doesn't
  1462. // matter much.
  1463. Lease6Ptr assigned;
  1464. testReuseLease6(engine, declined, "::", false, SHOULD_PASS, assigned);
  1465. // Check that the stats were not modified
  1466. testStatistics("declined-addresses", 999);
  1467. testStatistics("reclaimed-declined-addresses", 1001);
  1468. testStatistics(stat1, 999);
  1469. testStatistics(stat2, 1001);
  1470. }
  1471. }; // namespace test
  1472. }; // namespace dhcp
  1473. }; // namespace isc