alloc_engine6_unittest.cc 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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, 100);
  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, 100);
  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. }; // namespace test
  1313. }; // namespace dhcp
  1314. }; // namespace isc