alloc_engine_unittest.cc 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <asiolink/io_address.h>
  16. #include <dhcp/duid.h>
  17. #include <dhcp/dhcp4.h>
  18. #include <dhcpsrv/alloc_engine.h>
  19. #include <dhcpsrv/cfgmgr.h>
  20. #include <dhcpsrv/lease_mgr.h>
  21. #include <dhcpsrv/lease_mgr_factory.h>
  22. #include <dhcpsrv/memfile_lease_mgr.h>
  23. #include <dhcpsrv/tests/test_utils.h>
  24. #include <hooks/server_hooks.h>
  25. #include <hooks/callout_manager.h>
  26. #include <hooks/hooks_manager.h>
  27. #include <boost/shared_ptr.hpp>
  28. #include <boost/scoped_ptr.hpp>
  29. #include <gtest/gtest.h>
  30. #include <iostream>
  31. #include <sstream>
  32. #include <algorithm>
  33. #include <set>
  34. #include <time.h>
  35. using namespace std;
  36. using namespace isc;
  37. using namespace isc::asiolink;
  38. using namespace isc::hooks;
  39. using namespace isc::dhcp;
  40. using namespace isc::dhcp::test;
  41. namespace {
  42. /// @brief Allocation engine with some internal methods exposed
  43. class NakedAllocEngine : public AllocEngine {
  44. public:
  45. /// @brief the sole constructor
  46. /// @param engine_type specifies engine type (e.g. iterative)
  47. /// @param attempts number of lease selection attempts before giving up
  48. /// @param ipv6 specifies if the engine is IPv6 or IPv4
  49. NakedAllocEngine(AllocEngine::AllocType engine_type,
  50. unsigned int attempts, bool ipv6 = true)
  51. :AllocEngine(engine_type, attempts, ipv6) {
  52. }
  53. // Expose internal classes for testing purposes
  54. using AllocEngine::Allocator;
  55. using AllocEngine::IterativeAllocator;
  56. using AllocEngine::getAllocator;
  57. /// @brief IterativeAllocator with internal methods exposed
  58. class NakedIterativeAllocator: public AllocEngine::IterativeAllocator {
  59. public:
  60. /// @brief constructor
  61. /// @param type pool types that will be interated
  62. NakedIterativeAllocator(Lease::Type type)
  63. :IterativeAllocator(type) {
  64. }
  65. using AllocEngine::IterativeAllocator::increaseAddress;
  66. using AllocEngine::IterativeAllocator::increasePrefix;
  67. };
  68. };
  69. /// @brief Used in Allocation Engine tests for IPv6
  70. class AllocEngine6Test : public ::testing::Test {
  71. public:
  72. /// @brief Default constructor
  73. ///
  74. /// Sets duid_, iaid_, subnet_, pool_ fields to example values used
  75. /// in many tests, initializes cfg_mgr configuration and creates
  76. /// lease database.
  77. AllocEngine6Test() {
  78. duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
  79. iaid_ = 42;
  80. // instantiate cfg_mgr
  81. CfgMgr& cfg_mgr = CfgMgr::instance();
  82. // Configure normal address pool
  83. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  84. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::10"),
  85. IOAddress("2001:db8:1::20")));
  86. subnet_->addPool(pool_);
  87. // Configure PD pool
  88. pd_pool_ = Pool6Ptr(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1::"), 56, 64));
  89. subnet_->addPool(pd_pool_);
  90. cfg_mgr.addSubnet6(subnet_);
  91. factory_.create("type=memfile");
  92. }
  93. /// @brief attempts to convert leases collection to a single lease
  94. ///
  95. /// This operation makes sense if there is at most one lease in the
  96. /// collection. Otherwise it will throw.
  97. ///
  98. /// @param col collection of leases (zero or one leases allowed)
  99. /// @throw MultipleRecords if there is more than one lease
  100. /// @return Lease6 pointer (or NULL if collection was empty)
  101. Lease6Ptr expectOneLease(const Lease6Collection& col) {
  102. if (col.size() > 1) {
  103. isc_throw(MultipleRecords, "More than one lease found in collection");
  104. }
  105. if (col.empty()) {
  106. return (Lease6Ptr());
  107. }
  108. return (*col.begin());
  109. }
  110. /// @brief checks if Lease6 matches expected configuration
  111. ///
  112. /// @param lease lease to be checked
  113. /// @param exp_type expected lease type
  114. /// @param exp_pd_len expected prefix length
  115. void checkLease6(const Lease6Ptr& lease, Lease::Type exp_type,
  116. uint8_t exp_pd_len = 128) {
  117. // that is belongs to the right subnet
  118. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  119. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  120. EXPECT_TRUE(subnet_->inPool(exp_type, lease->addr_));
  121. // that it have proper parameters
  122. EXPECT_EQ(exp_type, lease->type_);
  123. EXPECT_EQ(iaid_, lease->iaid_);
  124. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  125. EXPECT_EQ(subnet_->getPreferred(), lease->preferred_lft_);
  126. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  127. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  128. EXPECT_EQ(exp_pd_len, lease->prefixlen_);
  129. EXPECT_TRUE(false == lease->fqdn_fwd_);
  130. EXPECT_TRUE(false == lease->fqdn_rev_);
  131. EXPECT_TRUE(*lease->duid_ == *duid_);
  132. /// @todo: check cltt
  133. }
  134. /// @brief Checks if specified address is increased properly
  135. ///
  136. /// Method uses gtest macros to mark check failure.
  137. ///
  138. /// @param alloc IterativeAllocator that is tested
  139. /// @param input address to be increased
  140. /// @param exp_output expected address after increase
  141. void
  142. checkAddrIncrease(NakedAllocEngine::NakedIterativeAllocator& alloc,
  143. std::string input, std::string exp_output) {
  144. EXPECT_EQ(exp_output, alloc.increaseAddress(IOAddress(input)).toText());
  145. }
  146. /// @brief Checks if increasePrefix() works as expected
  147. ///
  148. /// Method uses gtest macros to mark check failure.
  149. ///
  150. /// @param alloc allocator to be tested
  151. /// @param input IPv6 prefix (as a string)
  152. /// @param prefix_len prefix len
  153. /// @param exp_output expected output (string)
  154. void
  155. checkPrefixIncrease(NakedAllocEngine::NakedIterativeAllocator& alloc,
  156. std::string input, uint8_t prefix_len,
  157. std::string exp_output) {
  158. EXPECT_EQ(exp_output, alloc.increasePrefix(IOAddress(input), prefix_len)
  159. .toText());
  160. }
  161. /// @brief Checks if the simple allocation can succeed
  162. ///
  163. /// The type of lease is determined by pool type (pool->getType()
  164. ///
  165. /// @param pool pool from which the lease will be allocated from
  166. /// @param hint address to be used as a hint
  167. /// @param fake true - this is fake allocation (SOLICIT)
  168. /// @return allocated lease (or NULL)
  169. Lease6Ptr simpleAlloc6Test(const Pool6Ptr& pool, const IOAddress& hint,
  170. bool fake) {
  171. Lease::Type type = pool->getType();
  172. uint8_t expected_len = pool->getLength();
  173. boost::scoped_ptr<AllocEngine> engine;
  174. EXPECT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  175. 100)));
  176. // We can't use ASSERT macros in non-void methods
  177. EXPECT_TRUE(engine);
  178. if (!engine) {
  179. return (Lease6Ptr());
  180. }
  181. Lease6Ptr lease;
  182. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  183. duid_, iaid_, hint, type, false, false,
  184. "", fake, CalloutHandlePtr())));
  185. // Check that we got a lease
  186. EXPECT_TRUE(lease);
  187. if (!lease) {
  188. return (Lease6Ptr());
  189. }
  190. // Do all checks on the lease
  191. checkLease6(lease, type, expected_len);
  192. // Check that the lease is indeed in LeaseMgr
  193. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(type,
  194. lease->addr_);
  195. if (!fake) {
  196. // This is a real (REQUEST) allocation, the lease must be in the DB
  197. EXPECT_TRUE(from_mgr);
  198. if (!from_mgr) {
  199. return (Lease6Ptr());
  200. }
  201. // Now check that the lease in LeaseMgr has the same parameters
  202. detailCompareLease(lease, from_mgr);
  203. } else {
  204. // This is a fake (SOLICIT) allocation, the lease must not be in DB
  205. EXPECT_FALSE(from_mgr);
  206. if (from_mgr) {
  207. return (Lease6Ptr());
  208. }
  209. }
  210. return (lease);
  211. }
  212. /// @brief Checks if the address allocation with a hint that is in range,
  213. /// in pool, but is currently used, can succeed
  214. ///
  215. /// Method uses gtest macros to mark check failure.
  216. ///
  217. /// @param type lease type
  218. /// @param used_addr address should be preallocated (simulates prior
  219. /// allocation by some other user)
  220. /// @param requested address requested by the client
  221. /// @param expected_pd_len expected PD len (128 for addresses)
  222. void allocWithUsedHintTest(Lease::Type type, IOAddress used_addr,
  223. IOAddress requested, uint8_t expected_pd_len) {
  224. boost::scoped_ptr<AllocEngine> engine;
  225. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  226. 100)));
  227. ASSERT_TRUE(engine);
  228. // Let's create a lease and put it in the LeaseMgr
  229. DuidPtr duid2 = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0xff)));
  230. time_t now = time(NULL);
  231. Lease6Ptr used(new Lease6(type, used_addr,
  232. duid2, 1, 2, 3, 4, now, subnet_->getID()));
  233. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  234. // Another client comes in and request an address that is in pool, but
  235. // unfortunately it is used already. The same address must not be allocated
  236. // twice.
  237. Lease6Ptr lease;
  238. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  239. duid_, iaid_, requested, type, false, false, "", false,
  240. CalloutHandlePtr())));
  241. // Check that we got a lease
  242. ASSERT_TRUE(lease);
  243. // Allocated address must be different
  244. EXPECT_NE(used_addr, lease->addr_);
  245. // We should NOT get what we asked for, because it is used already
  246. EXPECT_NE(requested, lease->addr_);
  247. // Do all checks on the lease
  248. checkLease6(lease, type, expected_pd_len);
  249. // Check that the lease is indeed in LeaseMgr
  250. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  251. lease->addr_);
  252. ASSERT_TRUE(from_mgr);
  253. // Now check that the lease in LeaseMgr has the same parameters
  254. detailCompareLease(lease, from_mgr);
  255. }
  256. /// @brief checks if bogus hint can be ignored and the allocation succeeds
  257. ///
  258. /// This test checks if the allocation with a hing that is out of the blue
  259. /// can succeed. The invalid hint should be ingored completely.
  260. ///
  261. /// @param type Lease type
  262. /// @param hint hint (as send by a client)
  263. /// @param expectd_pd_len (used in validation)
  264. void allocBogusHint6(Lease::Type type, IOAddress hint,
  265. uint8_t expected_pd_len) {
  266. boost::scoped_ptr<AllocEngine> engine;
  267. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  268. 100)));
  269. ASSERT_TRUE(engine);
  270. // Client would like to get a 3000::abc lease, which does not belong to any
  271. // supported lease. Allocation engine should ignore it and carry on
  272. // with the normal allocation
  273. Lease6Ptr lease;
  274. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  275. duid_, iaid_, hint, type, false,
  276. false, "", false, CalloutHandlePtr())));
  277. // Check that we got a lease
  278. ASSERT_TRUE(lease);
  279. // We should NOT get what we asked for, because it is used already
  280. EXPECT_NE(hint, lease->addr_);
  281. // Do all checks on the lease
  282. checkLease6(lease, type, expected_pd_len);
  283. // Check that the lease is indeed in LeaseMgr
  284. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  285. lease->addr_);
  286. ASSERT_TRUE(from_mgr);
  287. // Now check that the lease in LeaseMgr has the same parameters
  288. detailCompareLease(lease, from_mgr);
  289. }
  290. virtual ~AllocEngine6Test() {
  291. factory_.destroy();
  292. }
  293. DuidPtr duid_; ///< client-identifier (value used in tests)
  294. uint32_t iaid_; ///< IA identifier (value used in tests)
  295. Subnet6Ptr subnet_; ///< subnet6 (used in tests)
  296. Pool6Ptr pool_; ///< NA pool belonging to subnet_
  297. Pool6Ptr pd_pool_; ///< PD pool belonging to subnet_
  298. LeaseMgrFactory factory_; ///< pointer to LeaseMgr factory
  299. };
  300. /// @brief Used in Allocation Engine tests for IPv4
  301. class AllocEngine4Test : public ::testing::Test {
  302. public:
  303. /// @brief Default constructor
  304. ///
  305. /// Sets clientid_, hwaddr_, subnet_, pool_ fields to example values
  306. /// used in many tests, initializes cfg_mgr configuration and creates
  307. /// lease database.
  308. AllocEngine4Test() {
  309. clientid_ = ClientIdPtr(new ClientId(vector<uint8_t>(8, 0x44)));
  310. static uint8_t mac[] = { 0, 1, 22, 33, 44, 55};
  311. // Let's use odd hardware type to check if there is no Ethernet
  312. // hardcoded anywhere.
  313. hwaddr_ = HWAddrPtr(new HWAddr(mac, sizeof(mac), HTYPE_FDDI));
  314. // instantiate cfg_mgr
  315. CfgMgr& cfg_mgr = CfgMgr::instance();
  316. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  317. pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"),
  318. IOAddress("192.0.2.109")));
  319. subnet_->addPool(pool_);
  320. cfg_mgr.addSubnet4(subnet_);
  321. factory_.create("type=memfile");
  322. }
  323. /// @brief checks if Lease4 matches expected configuration
  324. ///
  325. /// @param lease lease to be checked
  326. void checkLease4(const Lease4Ptr& lease) {
  327. // Check that is belongs to the right subnet
  328. EXPECT_EQ(lease->subnet_id_, subnet_->getID());
  329. EXPECT_TRUE(subnet_->inRange(lease->addr_));
  330. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, lease->addr_));
  331. // Check that it has proper parameters
  332. EXPECT_EQ(subnet_->getValid(), lease->valid_lft_);
  333. EXPECT_EQ(subnet_->getT1(), lease->t1_);
  334. EXPECT_EQ(subnet_->getT2(), lease->t2_);
  335. if (lease->client_id_ && !clientid_) {
  336. ADD_FAILURE() << "Lease4 has a client-id, while it should have none.";
  337. } else
  338. if (!lease->client_id_ && clientid_) {
  339. ADD_FAILURE() << "Lease4 has no client-id, but it was expected to have one.";
  340. } else
  341. if (lease->client_id_ && clientid_) {
  342. EXPECT_TRUE(*lease->client_id_ == *clientid_);
  343. }
  344. EXPECT_TRUE(lease->hwaddr_ == hwaddr_->hwaddr_);
  345. /// @todo: check cltt
  346. }
  347. virtual ~AllocEngine4Test() {
  348. factory_.destroy();
  349. }
  350. ClientIdPtr clientid_; ///< Client-identifier (value used in tests)
  351. HWAddrPtr hwaddr_; ///< Hardware address (value used in tests)
  352. Subnet4Ptr subnet_; ///< Subnet4 (used in tests)
  353. Pool4Ptr pool_; ///< Pool belonging to subnet_
  354. LeaseMgrFactory factory_; ///< Pointer to LeaseMgr factory
  355. Lease4Ptr old_lease_; ///< Holds previous instance of the lease.
  356. };
  357. // This test checks if the v6 Allocation Engine can be instantiated, parses
  358. // parameters string and allocators are created.
  359. TEST_F(AllocEngine6Test, constructor) {
  360. boost::scoped_ptr<AllocEngine> x;
  361. // Hashed and random allocators are not supported yet
  362. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_HASHED, 5)), NotImplemented);
  363. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_RANDOM, 5)), NotImplemented);
  364. ASSERT_NO_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100, true)));
  365. // Check that allocator for normal addresses is created
  366. ASSERT_TRUE(x->getAllocator(Lease::TYPE_NA));
  367. // Check that allocator for temporary address is created
  368. ASSERT_TRUE(x->getAllocator(Lease::TYPE_TA));
  369. // Check that allocator for prefixes is created
  370. ASSERT_TRUE(x->getAllocator(Lease::TYPE_PD));
  371. // There should be no V4 allocator
  372. EXPECT_THROW(x->getAllocator(Lease::TYPE_V4), BadValue);
  373. }
  374. // This test checks if the simple allocation (REQUEST) can succeed
  375. TEST_F(AllocEngine6Test, simpleAlloc6) {
  376. simpleAlloc6Test(pool_, IOAddress("::"), false);
  377. }
  378. // This test checks if the simple PD allocation (REQUEST) can succeed
  379. TEST_F(AllocEngine6Test, pdSimpleAlloc6) {
  380. simpleAlloc6Test(pd_pool_, IOAddress("::"), false);
  381. }
  382. // This test checks if the fake allocation (for SOLICIT) can succeed
  383. TEST_F(AllocEngine6Test, fakeAlloc6) {
  384. simpleAlloc6Test(pool_, IOAddress("::"), true);
  385. }
  386. // This test checks if the fake PD allocation (for SOLICIT) can succeed
  387. TEST_F(AllocEngine6Test, pdFakeAlloc6) {
  388. simpleAlloc6Test(pd_pool_, IOAddress("::"), true);
  389. };
  390. // This test checks if the allocation with a hint that is valid (in range,
  391. // in pool and free) can succeed
  392. TEST_F(AllocEngine6Test, allocWithValidHint6) {
  393. Lease6Ptr lease = simpleAlloc6Test(pool_, IOAddress("2001:db8:1::15"),
  394. false);
  395. // We should get what we asked for
  396. EXPECT_EQ("2001:db8:1::15", lease->addr_.toText());
  397. }
  398. // This test checks if the address allocation with a hint that is in range,
  399. // in pool, but is currently used, can succeed
  400. TEST_F(AllocEngine6Test, allocWithUsedHint6) {
  401. allocWithUsedHintTest(Lease::TYPE_NA,
  402. IOAddress("2001:db8:1::1f"), // allocate this as used
  403. IOAddress("2001:db8:1::1f"), // request this addr
  404. 128);
  405. }
  406. // This test checks if the PD allocation with a hint that is in range,
  407. // in pool, but is currently used, can succeed
  408. TEST_F(AllocEngine6Test, pdAllocWithUsedHint6) {
  409. allocWithUsedHintTest(Lease::TYPE_PD,
  410. IOAddress("2001:db8:1::"), // allocate this prefix as used
  411. IOAddress("2001:db8:1::"), // request this prefix
  412. 64);
  413. }
  414. // This test checks if the allocation with a hint that is out the blue
  415. // can succeed. The invalid hint should be ignored completely.
  416. TEST_F(AllocEngine6Test, allocBogusHint6) {
  417. allocBogusHint6(Lease::TYPE_NA, IOAddress("3000::abc"), 128);
  418. }
  419. // This test checks if the allocation with a hint that is out the blue
  420. // can succeed. The invalid hint should be ignored completely.
  421. TEST_F(AllocEngine6Test, pdAllocBogusHint6) {
  422. allocBogusHint6(Lease::TYPE_PD, IOAddress("3000::abc"), 64);
  423. }
  424. // This test checks that NULL values are handled properly
  425. TEST_F(AllocEngine6Test, allocateAddress6Nulls) {
  426. boost::scoped_ptr<AllocEngine> engine;
  427. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  428. ASSERT_TRUE(engine);
  429. // Allocations without subnet are not allowed
  430. Lease6Ptr lease;
  431. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(
  432. Subnet6Ptr(), duid_, iaid_, IOAddress("::"), Lease::TYPE_NA,
  433. false, false, "", false, CalloutHandlePtr())));
  434. ASSERT_FALSE(lease);
  435. // Allocations without DUID are not allowed either
  436. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  437. DuidPtr(), iaid_, IOAddress("::"), Lease::TYPE_NA, false,
  438. false, "", false, CalloutHandlePtr())));
  439. ASSERT_FALSE(lease);
  440. }
  441. // This test verifies that the allocator picks addresses that belong to the
  442. // pool
  443. TEST_F(AllocEngine6Test, IterativeAllocator) {
  444. boost::scoped_ptr<NakedAllocEngine::Allocator>
  445. alloc(new NakedAllocEngine::IterativeAllocator(Lease::TYPE_NA));
  446. for (int i = 0; i < 1000; ++i) {
  447. IOAddress candidate = alloc->pickAddress(subnet_, duid_, IOAddress("::"));
  448. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
  449. }
  450. }
  451. TEST_F(AllocEngine6Test, IterativeAllocatorAddrStep) {
  452. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_NA);
  453. subnet_->delPools(Lease::TYPE_NA); // Get rid of default pool
  454. Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::1"),
  455. IOAddress("2001:db8:1::5")));
  456. Pool6Ptr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::100"),
  457. IOAddress("2001:db8:1::100")));
  458. Pool6Ptr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1::105"),
  459. IOAddress("2001:db8:1::106")));
  460. subnet_->addPool(pool1);
  461. subnet_->addPool(pool2);
  462. subnet_->addPool(pool3);
  463. // Let's check the first pool (5 addresses here)
  464. EXPECT_EQ("2001:db8:1::1", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  465. EXPECT_EQ("2001:db8:1::2", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  466. EXPECT_EQ("2001:db8:1::3", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  467. EXPECT_EQ("2001:db8:1::4", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  468. EXPECT_EQ("2001:db8:1::5", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  469. // The second pool is easy - only one address here
  470. EXPECT_EQ("2001:db8:1::100", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  471. // This is the third and last pool, with 2 addresses in it
  472. EXPECT_EQ("2001:db8:1::105", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  473. EXPECT_EQ("2001:db8:1::106", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  474. // We iterated over all addresses and reached to the end of the last pool.
  475. // Let's wrap around and start from the beginning
  476. EXPECT_EQ("2001:db8:1::1", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  477. EXPECT_EQ("2001:db8:1::2", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  478. }
  479. TEST_F(AllocEngine6Test, IterativeAllocatorPrefixStep) {
  480. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_PD);
  481. subnet_.reset(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  482. Pool6Ptr pool1(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8::"), 56, 60));
  483. Pool6Ptr pool2(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1::"), 48, 48));
  484. Pool6Ptr pool3(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:2::"), 56, 64));
  485. subnet_->addPool(pool1);
  486. subnet_->addPool(pool2);
  487. subnet_->addPool(pool3);
  488. // We have a 2001:db8::/48 subnet that has 3 pools defined in it:
  489. // 2001:db8::/56 split into /60 prefixes (16 leases) (or 2001:db8:0:X0::)
  490. // 2001:db8:1::/48 split into a single /48 prefix (just 1 lease)
  491. // 2001:db8:2::/56 split into /64 prefixes (256 leases) (or 2001:db8:2:XX::)
  492. // First pool check (Let's check over all 16 leases)
  493. EXPECT_EQ("2001:db8::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  494. EXPECT_EQ("2001:db8:0:10::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  495. EXPECT_EQ("2001:db8:0:20::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  496. EXPECT_EQ("2001:db8:0:30::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  497. EXPECT_EQ("2001:db8:0:40::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  498. EXPECT_EQ("2001:db8:0:50::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  499. EXPECT_EQ("2001:db8:0:60::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  500. EXPECT_EQ("2001:db8:0:70::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  501. EXPECT_EQ("2001:db8:0:80::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  502. EXPECT_EQ("2001:db8:0:90::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  503. EXPECT_EQ("2001:db8:0:a0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  504. EXPECT_EQ("2001:db8:0:b0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  505. EXPECT_EQ("2001:db8:0:c0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  506. EXPECT_EQ("2001:db8:0:d0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  507. EXPECT_EQ("2001:db8:0:e0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  508. EXPECT_EQ("2001:db8:0:f0::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  509. // Second pool (just one lease here)
  510. EXPECT_EQ("2001:db8:1::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  511. // Third pool (256 leases, let's check first and last explictly and the
  512. // rest over in a pool
  513. EXPECT_EQ("2001:db8:2::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  514. for (int i = 1; i < 255; i++) {
  515. stringstream exp;
  516. exp << "2001:db8:2:" << hex << i << dec << "::";
  517. EXPECT_EQ(exp.str(), alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  518. }
  519. EXPECT_EQ("2001:db8:2:ff::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  520. // Ok, we've iterated over all prefixes in all pools. We now wrap around.
  521. // We're looping over now (iterating over first pool again)
  522. EXPECT_EQ("2001:db8::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  523. EXPECT_EQ("2001:db8:0:10::", alloc.pickAddress(subnet_, duid_, IOAddress("::")).toText());
  524. }
  525. // This test verifies that the iterative allocator can step over addresses
  526. TEST_F(AllocEngine6Test, IterativeAllocatorAddressIncrease) {
  527. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_NA);
  528. // Let's pick the first address
  529. IOAddress addr1 = alloc.pickAddress(subnet_, duid_, IOAddress("2001:db8:1::10"));
  530. // Check that we can indeed pick the first address from the pool
  531. EXPECT_EQ("2001:db8:1::10", addr1.toText());
  532. // Check that addresses can be increased properly
  533. checkAddrIncrease(alloc, "2001:db8::9", "2001:db8::a");
  534. checkAddrIncrease(alloc, "2001:db8::f", "2001:db8::10");
  535. checkAddrIncrease(alloc, "2001:db8::10", "2001:db8::11");
  536. checkAddrIncrease(alloc, "2001:db8::ff", "2001:db8::100");
  537. checkAddrIncrease(alloc, "2001:db8::ffff", "2001:db8::1:0");
  538. checkAddrIncrease(alloc, "::", "::1");
  539. checkAddrIncrease(alloc, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::");
  540. }
  541. // This test verifies that the allocator can step over prefixes
  542. TEST_F(AllocEngine6Test, IterativeAllocatorPrefixIncrease) {
  543. NakedAllocEngine::NakedIterativeAllocator alloc(Lease::TYPE_PD);
  544. // For /128 prefix, increasePrefix should work the same as addressIncrease
  545. checkPrefixIncrease(alloc, "2001:db8::9", 128, "2001:db8::a");
  546. checkPrefixIncrease(alloc, "2001:db8::f", 128, "2001:db8::10");
  547. checkPrefixIncrease(alloc, "2001:db8::10", 128, "2001:db8::11");
  548. checkPrefixIncrease(alloc, "2001:db8::ff", 128, "2001:db8::100");
  549. checkPrefixIncrease(alloc, "2001:db8::ffff", 128, "2001:db8::1:0");
  550. checkPrefixIncrease(alloc, "::", 128, "::1");
  551. checkPrefixIncrease(alloc, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, "::");
  552. // Check that /64 prefixes can be generated
  553. checkPrefixIncrease(alloc, "2001:db8::", 64, "2001:db8:0:1::");
  554. // Check that prefix length not divisible by 8 are working
  555. checkPrefixIncrease(alloc, "2001:db8::", 128, "2001:db8::1");
  556. checkPrefixIncrease(alloc, "2001:db8::", 127, "2001:db8::2");
  557. checkPrefixIncrease(alloc, "2001:db8::", 126, "2001:db8::4");
  558. checkPrefixIncrease(alloc, "2001:db8::", 125, "2001:db8::8");
  559. checkPrefixIncrease(alloc, "2001:db8::", 124, "2001:db8::10");
  560. checkPrefixIncrease(alloc, "2001:db8::", 123, "2001:db8::20");
  561. checkPrefixIncrease(alloc, "2001:db8::", 122, "2001:db8::40");
  562. checkPrefixIncrease(alloc, "2001:db8::", 121, "2001:db8::80");
  563. checkPrefixIncrease(alloc, "2001:db8::", 120, "2001:db8::100");
  564. // These are not really useful cases, because there are bits set
  565. // int the last (128 - prefix_len) bits. Nevertheless, it shows
  566. // that the algorithm is working even in such cases
  567. checkPrefixIncrease(alloc, "2001:db8::1", 128, "2001:db8::2");
  568. checkPrefixIncrease(alloc, "2001:db8::1", 127, "2001:db8::3");
  569. checkPrefixIncrease(alloc, "2001:db8::1", 126, "2001:db8::5");
  570. checkPrefixIncrease(alloc, "2001:db8::1", 125, "2001:db8::9");
  571. checkPrefixIncrease(alloc, "2001:db8::1", 124, "2001:db8::11");
  572. checkPrefixIncrease(alloc, "2001:db8::1", 123, "2001:db8::21");
  573. checkPrefixIncrease(alloc, "2001:db8::1", 122, "2001:db8::41");
  574. checkPrefixIncrease(alloc, "2001:db8::1", 121, "2001:db8::81");
  575. checkPrefixIncrease(alloc, "2001:db8::1", 120, "2001:db8::101");
  576. // Let's try out couple real life scenarios
  577. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 64, "2001:db8:1:abce::");
  578. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 60, "2001:db8:1:abdd::");
  579. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 56, "2001:db8:1:accd::");
  580. checkPrefixIncrease(alloc, "2001:db8:1:abcd::", 52, "2001:db8:1:bbcd::");
  581. // And now let's try something over the top
  582. checkPrefixIncrease(alloc, "::", 1, "8000::");
  583. }
  584. // This test verifies that the iterative allocator really walks over all addresses
  585. // in all pools in specified subnet. It also must not pick the same address twice
  586. // unless it runs out of pool space and must start over.
  587. TEST_F(AllocEngine6Test, IterativeAllocator_manyPools6) {
  588. NakedAllocEngine::IterativeAllocator alloc(Lease::TYPE_NA);
  589. // let's start from 2, as there is 2001:db8:1::10 - 2001:db8:1::20 pool already.
  590. for (int i = 2; i < 10; ++i) {
  591. stringstream min, max;
  592. min << "2001:db8:1::" << hex << i*16 + 1;
  593. max << "2001:db8:1::" << hex << i*16 + 9;
  594. Pool6Ptr pool(new Pool6(Lease::TYPE_NA, IOAddress(min.str()),
  595. IOAddress(max.str())));
  596. subnet_->addPool(pool);
  597. }
  598. int total = 17 + 8 * 9; // First pool (::10 - ::20) has 17 addresses in it,
  599. // there are 8 extra pools with 9 addresses in each.
  600. // Let's keep picked addresses here and check their uniqueness.
  601. std::set<IOAddress> generated_addrs;
  602. int cnt = 0;
  603. while (++cnt) {
  604. IOAddress candidate = alloc.pickAddress(subnet_, duid_, IOAddress("::"));
  605. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_NA, candidate));
  606. // One way to easily verify that the iterative allocator really works is
  607. // to uncomment the following line and observe its output that it
  608. // covers all defined pools.
  609. // cout << candidate.toText() << endl;
  610. if (generated_addrs.find(candidate) == generated_addrs.end()) {
  611. // We haven't had this.
  612. generated_addrs.insert(candidate);
  613. } else {
  614. // We have seen this address before. That should mean that we
  615. // iterated over all addresses.
  616. if (generated_addrs.size() == total) {
  617. // We have exactly the number of address in all pools.
  618. break;
  619. }
  620. ADD_FAILURE() << "Too many or not enough unique addresses generated.";
  621. break;
  622. }
  623. if ( cnt>total ) {
  624. ADD_FAILURE() << "Too many unique addresses generated.";
  625. break;
  626. }
  627. }
  628. }
  629. // This test checks if really small pools are working
  630. TEST_F(AllocEngine6Test, smallPool6) {
  631. boost::scoped_ptr<AllocEngine> engine;
  632. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  633. ASSERT_TRUE(engine);
  634. IOAddress addr("2001:db8:1::ad");
  635. CfgMgr& cfg_mgr = CfgMgr::instance();
  636. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  637. // Create configuration similar to other tests, but with a single address pool
  638. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  639. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  640. subnet_->addPool(pool_);
  641. cfg_mgr.addSubnet6(subnet_);
  642. Lease6Ptr lease;
  643. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  644. duid_, iaid_, IOAddress("::"), Lease::TYPE_NA, false, false,
  645. "", false, CalloutHandlePtr())));
  646. // Check that we got that single lease
  647. ASSERT_TRUE(lease);
  648. EXPECT_EQ("2001:db8:1::ad", lease->addr_.toText());
  649. // Do all checks on the lease
  650. checkLease6(lease, Lease::TYPE_NA, 128);
  651. // Check that the lease is indeed in LeaseMgr
  652. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  653. lease->addr_);
  654. ASSERT_TRUE(from_mgr);
  655. // Now check that the lease in LeaseMgr has the same parameters
  656. detailCompareLease(lease, from_mgr);
  657. }
  658. // This test checks if all addresses in a pool are currently used, the attempt
  659. // to find out a new lease fails.
  660. TEST_F(AllocEngine6Test, outOfAddresses6) {
  661. boost::scoped_ptr<AllocEngine> engine;
  662. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  663. ASSERT_TRUE(engine);
  664. IOAddress addr("2001:db8:1::ad");
  665. CfgMgr& cfg_mgr = CfgMgr::instance();
  666. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  667. // Create configuration similar to other tests, but with a single address pool
  668. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  669. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  670. subnet_->addPool(pool_);
  671. cfg_mgr.addSubnet6(subnet_);
  672. // Just a different duid
  673. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  674. const uint32_t other_iaid = 3568;
  675. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  676. 501, 502, 503, 504, subnet_->getID(), 0));
  677. lease->cltt_ = time(NULL) - 10; // Allocated 10 seconds ago
  678. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  679. // There is just a single address in the pool and allocated it to someone
  680. // else, so the allocation should fail
  681. Lease6Ptr lease2;
  682. EXPECT_NO_THROW(lease2 = expectOneLease(engine->allocateLeases6(subnet_,
  683. duid_, iaid_, IOAddress("::"), Lease::TYPE_NA, false, false,
  684. "", false, CalloutHandlePtr())));
  685. EXPECT_FALSE(lease2);
  686. }
  687. // This test checks if an expired lease can be reused in SOLICIT (fake allocation)
  688. TEST_F(AllocEngine6Test, solicitReuseExpiredLease6) {
  689. boost::scoped_ptr<AllocEngine> engine;
  690. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  691. ASSERT_TRUE(engine);
  692. IOAddress addr("2001:db8:1::ad");
  693. CfgMgr& cfg_mgr = CfgMgr::instance();
  694. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  695. // Create configuration similar to other tests, but with a single address pool
  696. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  697. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  698. subnet_->addPool(pool_);
  699. cfg_mgr.addSubnet6(subnet_);
  700. // Just a different duid
  701. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  702. const uint32_t other_iaid = 3568;
  703. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  704. 501, 502, 503, 504, subnet_->getID(), 0));
  705. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  706. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  707. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  708. // Make sure that we really created expired lease
  709. ASSERT_TRUE(lease->expired());
  710. // CASE 1: Asking for any address
  711. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  712. duid_, iaid_, IOAddress("::"), Lease::TYPE_NA, false, false, "", true,
  713. CalloutHandlePtr())));
  714. // Check that we got that single lease
  715. ASSERT_TRUE(lease);
  716. EXPECT_EQ(addr, lease->addr_);
  717. // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
  718. checkLease6(lease, Lease::TYPE_NA, 128);
  719. // CASE 2: Asking specifically for this address
  720. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  721. duid_, iaid_, addr, Lease::TYPE_NA, false, false, "",
  722. true, CalloutHandlePtr())));
  723. // Check that we got that single lease
  724. ASSERT_TRUE(lease);
  725. EXPECT_EQ(addr, lease->addr_);
  726. }
  727. // This test checks if an expired lease can be reused in REQUEST (actual allocation)
  728. TEST_F(AllocEngine6Test, requestReuseExpiredLease6) {
  729. boost::scoped_ptr<AllocEngine> engine;
  730. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  731. ASSERT_TRUE(engine);
  732. IOAddress addr("2001:db8:1::ad");
  733. CfgMgr& cfg_mgr = CfgMgr::instance();
  734. cfg_mgr.deleteSubnets6(); // Get rid of the default test configuration
  735. // Create configuration similar to other tests, but with a single address pool
  736. subnet_ = Subnet6Ptr(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  737. pool_ = Pool6Ptr(new Pool6(Lease::TYPE_NA, addr, addr)); // just a single address
  738. subnet_->addPool(pool_);
  739. cfg_mgr.addSubnet6(subnet_);
  740. // Let's create an expired lease
  741. DuidPtr other_duid = DuidPtr(new DUID(vector<uint8_t>(12, 0xff)));
  742. const uint32_t other_iaid = 3568;
  743. const SubnetID other_subnetid = 999;
  744. Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, other_duid, other_iaid,
  745. 501, 502, 503, 504, other_subnetid, 0));
  746. lease->cltt_ = time(NULL) - 500; // Allocated 500 seconds ago
  747. lease->valid_lft_ = 495; // Lease was valid for 495 seconds
  748. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  749. // A client comes along, asking specifically for this address
  750. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  751. duid_, iaid_, addr, Lease::TYPE_NA, false, false, "",
  752. false, CalloutHandlePtr())));
  753. // Check that he got that single lease
  754. ASSERT_TRUE(lease);
  755. EXPECT_EQ(addr, lease->addr_);
  756. // Check that the lease is indeed updated in LeaseMgr
  757. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(Lease::TYPE_NA,
  758. addr);
  759. ASSERT_TRUE(from_mgr);
  760. // Now check that the lease in LeaseMgr has the same parameters
  761. detailCompareLease(lease, from_mgr);
  762. }
  763. // --- IPv4 ---
  764. // This test checks if the v4 Allocation Engine can be instantiated, parses
  765. // parameters string and allocators are created.
  766. TEST_F(AllocEngine4Test, constructor) {
  767. boost::scoped_ptr<AllocEngine> x;
  768. // Hashed and random allocators are not supported yet
  769. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_HASHED, 5, false)),
  770. NotImplemented);
  771. ASSERT_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_RANDOM, 5, false)),
  772. NotImplemented);
  773. // Create V4 (ipv6=false) Allocation Engine that will try at most
  774. // 100 attempts to pick up a lease
  775. ASSERT_NO_THROW(x.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100,
  776. false)));
  777. // There should be V4 allocator
  778. ASSERT_TRUE(x->getAllocator(Lease::TYPE_V4));
  779. // Check that allocators for V6 stuff are not created
  780. EXPECT_THROW(x->getAllocator(Lease::TYPE_NA), BadValue);
  781. EXPECT_THROW(x->getAllocator(Lease::TYPE_TA), BadValue);
  782. EXPECT_THROW(x->getAllocator(Lease::TYPE_PD), BadValue);
  783. }
  784. // This test checks if the simple IPv4 allocation can succeed
  785. TEST_F(AllocEngine4Test, simpleAlloc4) {
  786. boost::scoped_ptr<AllocEngine> engine;
  787. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  788. 100, false)));
  789. ASSERT_TRUE(engine);
  790. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  791. IOAddress("0.0.0.0"),
  792. false, true,
  793. "somehost.example.com.",
  794. false, CalloutHandlePtr(),
  795. old_lease_);
  796. // The new lease has been allocated, so the old lease should not exist.
  797. EXPECT_FALSE(old_lease_);
  798. // Check that we got a lease
  799. ASSERT_TRUE(lease);
  800. // Do all checks on the lease
  801. checkLease4(lease);
  802. // Check that the lease is indeed in LeaseMgr
  803. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  804. ASSERT_TRUE(from_mgr);
  805. // Now check that the lease in LeaseMgr has the same parameters
  806. detailCompareLease(lease, from_mgr);
  807. }
  808. // This test checks if the fake allocation (for DISCOVER) can succeed
  809. TEST_F(AllocEngine4Test, fakeAlloc4) {
  810. boost::scoped_ptr<AllocEngine> engine;
  811. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  812. 100, false)));
  813. ASSERT_TRUE(engine);
  814. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  815. IOAddress("0.0.0.0"),
  816. false, true, "host.example.com.",
  817. true, CalloutHandlePtr(),
  818. old_lease_);
  819. // The new lease has been allocated, so the old lease should not exist.
  820. EXPECT_FALSE(old_lease_);
  821. // Check that we got a lease
  822. ASSERT_TRUE(lease);
  823. // Do all checks on the lease
  824. checkLease4(lease);
  825. // Check that the lease is NOT in LeaseMgr
  826. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  827. ASSERT_FALSE(from_mgr);
  828. }
  829. // This test checks if the allocation with a hint that is valid (in range,
  830. // in pool and free) can succeed
  831. TEST_F(AllocEngine4Test, allocWithValidHint4) {
  832. boost::scoped_ptr<AllocEngine> engine;
  833. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  834. 100, false)));
  835. ASSERT_TRUE(engine);
  836. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  837. IOAddress("192.0.2.105"),
  838. true, true, "host.example.com.",
  839. false, CalloutHandlePtr(),
  840. old_lease_);
  841. // Check that we got a lease
  842. ASSERT_TRUE(lease);
  843. // We have allocated the new lease, so the old lease should not exist.
  844. EXPECT_FALSE(old_lease_);
  845. // We should get what we asked for
  846. EXPECT_EQ(lease->addr_.toText(), "192.0.2.105");
  847. // Do all checks on the lease
  848. checkLease4(lease);
  849. // Check that the lease is indeed in LeaseMgr
  850. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  851. ASSERT_TRUE(from_mgr);
  852. // Now check that the lease in LeaseMgr has the same parameters
  853. detailCompareLease(lease, from_mgr);
  854. }
  855. // This test checks if the allocation with a hint that is in range,
  856. // in pool, but is currently used) can succeed
  857. TEST_F(AllocEngine4Test, allocWithUsedHint4) {
  858. boost::scoped_ptr<AllocEngine> engine;
  859. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  860. 100, false)));
  861. ASSERT_TRUE(engine);
  862. // Let's create a lease and put it in the LeaseMgr
  863. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  864. uint8_t clientid2[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
  865. time_t now = time(NULL);
  866. Lease4Ptr used(new Lease4(IOAddress("192.0.2.106"), hwaddr2, sizeof(hwaddr2),
  867. clientid2, sizeof(clientid2), 1, 2, 3, now, subnet_->getID()));
  868. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(used));
  869. // Another client comes in and request an address that is in pool, but
  870. // unfortunately it is used already. The same address must not be allocated
  871. // twice.
  872. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  873. IOAddress("192.0.2.106"),
  874. false, false, "",
  875. false, CalloutHandlePtr(),
  876. old_lease_);
  877. // New lease has been allocated, so the old lease should not exist.
  878. EXPECT_FALSE(old_lease_);
  879. // Check that we got a lease
  880. ASSERT_TRUE(lease);
  881. // Allocated address must be different
  882. EXPECT_NE(used->addr_, lease->addr_);
  883. // We should NOT get what we asked for, because it is used already
  884. EXPECT_NE("192.0.2.106", lease->addr_.toText());
  885. // Do all checks on the lease
  886. checkLease4(lease);
  887. // Check that the lease is indeed in LeaseMgr
  888. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  889. ASSERT_TRUE(from_mgr);
  890. // Now check that the lease in LeaseMgr has the same parameters
  891. detailCompareLease(lease, from_mgr);
  892. }
  893. // This test checks if the allocation with a hint that is out the blue
  894. // can succeed. The invalid hint should be ignored completely.
  895. TEST_F(AllocEngine4Test, allocBogusHint4) {
  896. boost::scoped_ptr<AllocEngine> engine;
  897. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  898. 100, false)));
  899. ASSERT_TRUE(engine);
  900. // Client would like to get a 3000::abc lease, which does not belong to any
  901. // supported lease. Allocation engine should ignore it and carry on
  902. // with the normal allocation
  903. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  904. IOAddress("10.1.1.1"),
  905. false, false, "",
  906. false, CalloutHandlePtr(),
  907. old_lease_);
  908. // Check that we got a lease
  909. ASSERT_TRUE(lease);
  910. // We have allocated a new lease, so the old lease should not exist.
  911. EXPECT_FALSE(old_lease_);
  912. // We should NOT get what we asked for, because it is used already
  913. EXPECT_NE("10.1.1.1", lease->addr_.toText());
  914. // Do all checks on the lease
  915. checkLease4(lease);
  916. // Check that the lease is indeed in LeaseMgr
  917. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  918. ASSERT_TRUE(from_mgr);
  919. // Now check that the lease in LeaseMgr has the same parameters
  920. detailCompareLease(lease, from_mgr);
  921. }
  922. // This test checks that NULL values are handled properly
  923. TEST_F(AllocEngine4Test, allocateLease4Nulls) {
  924. boost::scoped_ptr<AllocEngine> engine;
  925. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  926. 100, false)));
  927. ASSERT_TRUE(engine);
  928. // Allocations without subnet are not allowed
  929. Lease4Ptr lease = engine->allocateLease4(SubnetPtr(), clientid_, hwaddr_,
  930. IOAddress("0.0.0.0"),
  931. false, false, "",
  932. false, CalloutHandlePtr(),
  933. old_lease_);
  934. EXPECT_FALSE(lease);
  935. // Allocations without HW address are not allowed
  936. lease = engine->allocateLease4(subnet_, clientid_, HWAddrPtr(),
  937. IOAddress("0.0.0.0"),
  938. false, false, "",
  939. false, CalloutHandlePtr(),
  940. old_lease_);
  941. EXPECT_FALSE(lease);
  942. EXPECT_FALSE(old_lease_);
  943. // Allocations without client-id are allowed
  944. clientid_ = ClientIdPtr();
  945. lease = engine->allocateLease4(subnet_, ClientIdPtr(), hwaddr_,
  946. IOAddress("0.0.0.0"),
  947. true, true, "myhost.example.com.",
  948. false, CalloutHandlePtr(),
  949. old_lease_);
  950. // Check that we got a lease
  951. ASSERT_TRUE(lease);
  952. // New lease has been allocated, so the old lease should not exist.
  953. EXPECT_FALSE(old_lease_);
  954. // Do all checks on the lease
  955. checkLease4(lease);
  956. // Check that the lease is indeed in LeaseMgr
  957. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  958. ASSERT_TRUE(from_mgr);
  959. // Now check that the lease in LeaseMgr has the same parameters
  960. detailCompareLease(lease, from_mgr);
  961. }
  962. // This test verifies that the allocator picks addresses that belong to the
  963. // pool
  964. TEST_F(AllocEngine4Test, IterativeAllocator) {
  965. boost::scoped_ptr<NakedAllocEngine::Allocator>
  966. alloc(new NakedAllocEngine::IterativeAllocator(Lease::TYPE_V4));
  967. for (int i = 0; i < 1000; ++i) {
  968. IOAddress candidate = alloc->pickAddress(subnet_, clientid_,
  969. IOAddress("0.0.0.0"));
  970. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate));
  971. }
  972. }
  973. // This test verifies that the iterative allocator really walks over all addresses
  974. // in all pools in specified subnet. It also must not pick the same address twice
  975. // unless it runs out of pool space and must start over.
  976. TEST_F(AllocEngine4Test, IterativeAllocator_manyPools4) {
  977. NakedAllocEngine::IterativeAllocator alloc(Lease::TYPE_V4);
  978. // Let's start from 2, as there is 2001:db8:1::10 - 2001:db8:1::20 pool already.
  979. for (int i = 2; i < 10; ++i) {
  980. stringstream min, max;
  981. min << "192.0.2." << i * 10 + 1;
  982. max << "192.0.2." << i * 10 + 9;
  983. Pool4Ptr pool(new Pool4(IOAddress(min.str()),
  984. IOAddress(max.str())));
  985. // cout << "Adding pool: " << min.str() << "-" << max.str() << endl;
  986. subnet_->addPool(pool);
  987. }
  988. int total = 10 + 8 * 9; // first pool (.100 - .109) has 10 addresses in it,
  989. // there are 8 extra pools with 9 addresses in each.
  990. // Let's keep picked addresses here and check their uniqueness.
  991. std::set<IOAddress> generated_addrs;
  992. int cnt = 0;
  993. while (++cnt) {
  994. IOAddress candidate = alloc.pickAddress(subnet_, clientid_, IOAddress("0.0.0.0"));
  995. EXPECT_TRUE(subnet_->inPool(Lease::TYPE_V4, candidate));
  996. // One way to easily verify that the iterative allocator really works is
  997. // to uncomment the following line and observe its output that it
  998. // covers all defined subnets.
  999. // cout << candidate.toText() << endl;
  1000. if (generated_addrs.find(candidate) == generated_addrs.end()) {
  1001. // We haven't had this
  1002. generated_addrs.insert(candidate);
  1003. } else {
  1004. // We have seen this address before. That should mean that we
  1005. // iterated over all addresses.
  1006. if (generated_addrs.size() == total) {
  1007. // We have exactly the number of address in all pools
  1008. break;
  1009. }
  1010. ADD_FAILURE() << "Too many or not enough unique addresses generated.";
  1011. break;
  1012. }
  1013. if ( cnt>total ) {
  1014. ADD_FAILURE() << "Too many unique addresses generated.";
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. // This test checks if really small pools are working
  1020. TEST_F(AllocEngine4Test, smallPool4) {
  1021. boost::scoped_ptr<AllocEngine> engine;
  1022. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1023. 100, false)));
  1024. ASSERT_TRUE(engine);
  1025. IOAddress addr("192.0.2.17");
  1026. CfgMgr& cfg_mgr = CfgMgr::instance();
  1027. cfg_mgr.deleteSubnets4(); // Get rid of the default test configuration
  1028. // Create configuration similar to other tests, but with a single address pool
  1029. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  1030. pool_ = Pool4Ptr(new Pool4(addr, addr)); // just a single address
  1031. subnet_->addPool(pool_);
  1032. cfg_mgr.addSubnet4(subnet_);
  1033. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1034. IOAddress("0.0.0.0"),
  1035. true, true, "host.example.com.",
  1036. false, CalloutHandlePtr(),
  1037. old_lease_);
  1038. // Check that we got that single lease
  1039. ASSERT_TRUE(lease);
  1040. // We have allocated new lease, so the old lease should not exist.
  1041. EXPECT_FALSE(old_lease_);
  1042. EXPECT_EQ("192.0.2.17", lease->addr_.toText());
  1043. // Do all checks on the lease
  1044. checkLease4(lease);
  1045. // Check that the lease is indeed in LeaseMgr
  1046. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  1047. ASSERT_TRUE(from_mgr);
  1048. // Now check that the lease in LeaseMgr has the same parameters
  1049. detailCompareLease(lease, from_mgr);
  1050. }
  1051. // This test checks if all addresses in a pool are currently used, the attempt
  1052. // to find out a new lease fails.
  1053. TEST_F(AllocEngine4Test, outOfAddresses4) {
  1054. boost::scoped_ptr<AllocEngine> engine;
  1055. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1056. 100, false)));
  1057. ASSERT_TRUE(engine);
  1058. IOAddress addr("192.0.2.17");
  1059. CfgMgr& cfg_mgr = CfgMgr::instance();
  1060. cfg_mgr.deleteSubnets4(); // Get rid of the default test configuration
  1061. // Create configuration similar to other tests, but with a single address pool
  1062. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  1063. pool_ = Pool4Ptr(new Pool4(addr, addr)); // just a single address
  1064. subnet_->addPool(pool_);
  1065. cfg_mgr.addSubnet4(subnet_);
  1066. // Just a different hw/client-id for the second client
  1067. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1068. uint8_t clientid2[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
  1069. time_t now = time(NULL);
  1070. Lease4Ptr lease(new Lease4(addr, hwaddr2, sizeof(hwaddr2), clientid2,
  1071. sizeof(clientid2), 501, 502, 503, now,
  1072. subnet_->getID()));
  1073. lease->cltt_ = time(NULL) - 10; // Allocated 10 seconds ago
  1074. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1075. // There is just a single address in the pool and allocated it to someone
  1076. // else, so the allocation should fail
  1077. Lease4Ptr lease2 = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1078. IOAddress("0.0.0.0"),
  1079. false, false, "",
  1080. false, CalloutHandlePtr(),
  1081. old_lease_);
  1082. EXPECT_FALSE(lease2);
  1083. EXPECT_FALSE(old_lease_);
  1084. }
  1085. // This test checks if an expired lease can be reused in DISCOVER (fake allocation)
  1086. TEST_F(AllocEngine4Test, discoverReuseExpiredLease4) {
  1087. boost::scoped_ptr<AllocEngine> engine;
  1088. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1089. 100, false)));
  1090. ASSERT_TRUE(engine);
  1091. IOAddress addr("192.0.2.15");
  1092. CfgMgr& cfg_mgr = CfgMgr::instance();
  1093. cfg_mgr.deleteSubnets4(); // Get rid of the default test configuration
  1094. // Create configuration similar to other tests, but with a single address pool
  1095. subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  1096. pool_ = Pool4Ptr(new Pool4(addr, addr)); // just a single address
  1097. subnet_->addPool(pool_);
  1098. cfg_mgr.addSubnet4(subnet_);
  1099. // Just a different hw/client-id for the second client
  1100. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1101. uint8_t clientid2[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
  1102. time_t now = time(NULL) - 500; // Allocated 500 seconds ago
  1103. Lease4Ptr lease(new Lease4(addr, clientid2, sizeof(clientid2),
  1104. hwaddr2, sizeof(hwaddr2),
  1105. 495, 100, 200, now, subnet_->getID()));
  1106. // Copy the lease, so as it can be compared with the old lease returned
  1107. // by the allocation engine.
  1108. Lease4 original_lease(*lease);
  1109. // Lease was assigned 500 seconds ago, but its valid lifetime is 495, so it
  1110. // is expired already
  1111. ASSERT_TRUE(lease->expired());
  1112. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1113. // CASE 1: Asking for any address
  1114. lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1115. IOAddress("0.0.0.0"),
  1116. false, false, "",
  1117. true, CalloutHandlePtr(),
  1118. old_lease_);
  1119. // Check that we got that single lease
  1120. ASSERT_TRUE(lease);
  1121. EXPECT_EQ(addr, lease->addr_);
  1122. // We are reusing expired lease, the old (expired) instance should be
  1123. // returned. The returned instance should be the same as the original
  1124. // lease.
  1125. ASSERT_TRUE(old_lease_);
  1126. EXPECT_TRUE(original_lease == *old_lease_);
  1127. // Do all checks on the lease (if subnet-id, preferred/valid times are ok etc.)
  1128. checkLease4(lease);
  1129. // CASE 2: Asking specifically for this address
  1130. lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1131. IOAddress(addr),
  1132. false, false, "",
  1133. true, CalloutHandlePtr(),
  1134. old_lease_);
  1135. // Check that we got that single lease
  1136. ASSERT_TRUE(lease);
  1137. EXPECT_EQ(addr, lease->addr_);
  1138. // We are updating expired lease. The copy of the old lease should be
  1139. // returned and it should be equal to the original lease.
  1140. ASSERT_TRUE(old_lease_);
  1141. EXPECT_TRUE(*old_lease_ == original_lease);
  1142. }
  1143. // This test checks if an expired lease can be reused in REQUEST (actual allocation)
  1144. TEST_F(AllocEngine4Test, requestReuseExpiredLease4) {
  1145. boost::scoped_ptr<AllocEngine> engine;
  1146. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1147. 100, false)));
  1148. ASSERT_TRUE(engine);
  1149. IOAddress addr("192.0.2.105");
  1150. // Just a different hw/client-id for the second client
  1151. uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1152. uint8_t clientid2[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
  1153. time_t now = time(NULL) - 500; // Allocated 500 seconds ago
  1154. Lease4Ptr lease(new Lease4(addr, clientid2, sizeof(clientid2), hwaddr2,
  1155. sizeof(hwaddr2), 495, 100, 200, now,
  1156. subnet_->getID()));
  1157. // Make a copy of the lease, so as we can comapre that with the old lease
  1158. // instance returned by the allocation engine.
  1159. Lease4 original_lease(*lease);
  1160. // Lease was assigned 500 seconds ago, but its valid lifetime is 495, so it
  1161. // is expired already
  1162. ASSERT_TRUE(lease->expired());
  1163. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1164. // A client comes along, asking specifically for this address
  1165. lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1166. IOAddress(addr),
  1167. false, true, "host.example.com.",
  1168. false, CalloutHandlePtr(),
  1169. old_lease_);
  1170. // Check that he got that single lease
  1171. ASSERT_TRUE(lease);
  1172. EXPECT_EQ(addr, lease->addr_);
  1173. // Check that the lease is indeed updated in LeaseMgr
  1174. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(addr);
  1175. ASSERT_TRUE(from_mgr);
  1176. // Now check that the lease in LeaseMgr has the same parameters
  1177. detailCompareLease(lease, from_mgr);
  1178. // The allocation engine should return a copy of the old lease. This
  1179. // lease should be equal to the original lease.
  1180. ASSERT_TRUE(old_lease_);
  1181. EXPECT_TRUE(*old_lease_ == original_lease);
  1182. }
  1183. /// @todo write renewLease6
  1184. // This test checks if a lease is really renewed when renewLease4 method is
  1185. // called
  1186. TEST_F(AllocEngine4Test, renewLease4) {
  1187. boost::scoped_ptr<AllocEngine> engine;
  1188. CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
  1189. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1190. 100, false)));
  1191. ASSERT_TRUE(engine);
  1192. IOAddress addr("192.0.2.102");
  1193. const uint32_t old_lifetime = 100;
  1194. const uint32_t old_t1 = 50;
  1195. const uint32_t old_t2 = 75;
  1196. const time_t old_timestamp = time(NULL) - 45; // Allocated 45 seconds ago
  1197. // Just a different hw/client-id for the second client
  1198. const uint8_t hwaddr2[] = { 0, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};
  1199. const uint8_t clientid2[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
  1200. Lease4Ptr lease(new Lease4(addr, clientid2, sizeof(clientid2), hwaddr2,
  1201. sizeof(hwaddr2), old_lifetime, old_t1, old_t2,
  1202. old_timestamp, subnet_->getID()));
  1203. ASSERT_TRUE(LeaseMgrFactory::instance().addLease(lease));
  1204. // Lease was assigned 45 seconds ago and is valid for 100 seconds. Let's
  1205. // renew it.
  1206. ASSERT_FALSE(lease->expired());
  1207. lease = engine->renewLease4(subnet_, clientid_, hwaddr_, true,
  1208. true, "host.example.com.", lease,
  1209. callout_handle, false);
  1210. // Check that he got that single lease
  1211. ASSERT_TRUE(lease);
  1212. EXPECT_EQ(addr, lease->addr_);
  1213. // Check that the lease matches subnet_, hwaddr_,clientid_ parameters
  1214. checkLease4(lease);
  1215. // Check that the lease is indeed updated in LeaseMgr
  1216. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(addr);
  1217. ASSERT_TRUE(from_mgr);
  1218. // Now check that the lease in LeaseMgr has the same parameters
  1219. detailCompareLease(lease, from_mgr);
  1220. }
  1221. /// @brief helper class used in Hooks testing in AllocEngine6
  1222. ///
  1223. /// It features a couple of callout functions and buffers to store
  1224. /// the data that is accessible via callouts.
  1225. class HookAllocEngine6Test : public AllocEngine6Test {
  1226. public:
  1227. HookAllocEngine6Test() {
  1228. resetCalloutBuffers();
  1229. }
  1230. virtual ~HookAllocEngine6Test() {
  1231. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts(
  1232. "lease6_select");
  1233. }
  1234. /// @brief clears out buffers, so callouts can store received arguments
  1235. void resetCalloutBuffers() {
  1236. callback_name_ = string("");
  1237. callback_subnet6_.reset();
  1238. callback_fake_allocation_ = false;
  1239. callback_lease6_.reset();
  1240. callback_argument_names_.clear();
  1241. callback_addr_original_ = IOAddress("::");
  1242. callback_addr_updated_ = IOAddress("::");
  1243. }
  1244. /// callback that stores received callout name and received values
  1245. static int
  1246. lease6_select_callout(CalloutHandle& callout_handle) {
  1247. callback_name_ = string("lease6_select");
  1248. callout_handle.getArgument("subnet6", callback_subnet6_);
  1249. callout_handle.getArgument("fake_allocation", callback_fake_allocation_);
  1250. callout_handle.getArgument("lease6", callback_lease6_);
  1251. callback_addr_original_ = callback_lease6_->addr_;
  1252. callback_argument_names_ = callout_handle.getArgumentNames();
  1253. return (0);
  1254. }
  1255. /// callback that overrides the lease with different values
  1256. static int
  1257. lease6_select_different_callout(CalloutHandle& callout_handle) {
  1258. // Let's call the basic callout, so it can record all parameters
  1259. lease6_select_callout(callout_handle);
  1260. // Now we need to tweak the least a bit
  1261. Lease6Ptr lease;
  1262. callout_handle.getArgument("lease6", lease);
  1263. callback_addr_updated_ = addr_override_;
  1264. lease->addr_ = callback_addr_updated_;
  1265. lease->t1_ = t1_override_;
  1266. lease->t2_ = t2_override_;
  1267. lease->preferred_lft_ = pref_override_;
  1268. lease->valid_lft_ = valid_override_;
  1269. return (0);
  1270. }
  1271. // Values to be used in callout to override lease6 content
  1272. static const IOAddress addr_override_;
  1273. static const uint32_t t1_override_;
  1274. static const uint32_t t2_override_;
  1275. static const uint32_t pref_override_;
  1276. static const uint32_t valid_override_;
  1277. // Callback will store original and overridden values here
  1278. static IOAddress callback_addr_original_;
  1279. static IOAddress callback_addr_updated_;
  1280. // Buffers (callback will store received values here)
  1281. static string callback_name_;
  1282. static Subnet6Ptr callback_subnet6_;
  1283. static Lease6Ptr callback_lease6_;
  1284. static bool callback_fake_allocation_;
  1285. static vector<string> callback_argument_names_;
  1286. };
  1287. // For some reason intialization within a class makes the linker confused.
  1288. // linker complains about undefined references if they are defined within
  1289. // the class declaration.
  1290. const IOAddress HookAllocEngine6Test::addr_override_("2001:db8::abcd");
  1291. const uint32_t HookAllocEngine6Test::t1_override_ = 6000;
  1292. const uint32_t HookAllocEngine6Test::t2_override_ = 7000;
  1293. const uint32_t HookAllocEngine6Test::pref_override_ = 8000;
  1294. const uint32_t HookAllocEngine6Test::valid_override_ = 9000;
  1295. IOAddress HookAllocEngine6Test::callback_addr_original_("::");
  1296. IOAddress HookAllocEngine6Test::callback_addr_updated_("::");
  1297. string HookAllocEngine6Test::callback_name_;
  1298. Subnet6Ptr HookAllocEngine6Test::callback_subnet6_;
  1299. Lease6Ptr HookAllocEngine6Test::callback_lease6_;
  1300. bool HookAllocEngine6Test::callback_fake_allocation_;
  1301. vector<string> HookAllocEngine6Test::callback_argument_names_;
  1302. // This test checks if the lease6_select callout is executed and expected
  1303. // parameters as passed.
  1304. TEST_F(HookAllocEngine6Test, lease6_select) {
  1305. // Note: The following order is working as expected:
  1306. // 1. create AllocEngine (that register hook points)
  1307. // 2. call loadLibraries()
  1308. //
  1309. // This order, however, causes segfault in HooksManager
  1310. // 1. call loadLibraries()
  1311. // 2. create AllocEngine (that register hook points)
  1312. // Create allocation engine (hook names are registered in its ctor)
  1313. boost::scoped_ptr<AllocEngine> engine;
  1314. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  1315. ASSERT_TRUE(engine);
  1316. // Initialize Hooks Manager
  1317. vector<string> libraries; // no libraries at this time
  1318. HooksManager::loadLibraries(libraries);
  1319. // Install pkt6_receive_callout
  1320. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1321. "lease6_select", lease6_select_callout));
  1322. CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
  1323. Lease6Ptr lease;
  1324. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  1325. duid_, iaid_, IOAddress("::"), Lease::TYPE_NA, false, false,
  1326. "", false, callout_handle)));
  1327. // Check that we got a lease
  1328. ASSERT_TRUE(lease);
  1329. // Do all checks on the lease
  1330. checkLease6(lease, Lease::TYPE_NA, 128);
  1331. // Check that the lease is indeed in LeaseMgr
  1332. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  1333. lease->addr_);
  1334. ASSERT_TRUE(from_mgr);
  1335. // Check that callouts were indeed called
  1336. EXPECT_EQ("lease6_select", callback_name_);
  1337. // Now check that the lease in LeaseMgr has the same parameters
  1338. ASSERT_TRUE(callback_lease6_);
  1339. detailCompareLease(callback_lease6_, from_mgr);
  1340. ASSERT_TRUE(callback_subnet6_);
  1341. EXPECT_EQ(subnet_->toText(), callback_subnet6_->toText());
  1342. EXPECT_FALSE(callback_fake_allocation_);
  1343. // Check if all expected parameters are reported. It's a bit tricky, because
  1344. // order may be different. If the test starts failing, because someone tweaked
  1345. // hooks engine, we'll have to implement proper vector matching (ignoring order)
  1346. vector<string> expected_argument_names;
  1347. expected_argument_names.push_back("fake_allocation");
  1348. expected_argument_names.push_back("lease6");
  1349. expected_argument_names.push_back("subnet6");
  1350. sort(callback_argument_names_.begin(), callback_argument_names_.end());
  1351. sort(expected_argument_names.begin(), expected_argument_names.end());
  1352. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1353. }
  1354. // This test checks if lease6_select callout is able to override the values
  1355. // in a lease6.
  1356. TEST_F(HookAllocEngine6Test, change_lease6_select) {
  1357. // Make sure that the overridden values are different than the ones from
  1358. // subnet originally used to create the lease
  1359. ASSERT_NE(t1_override_, subnet_->getT1());
  1360. ASSERT_NE(t2_override_, subnet_->getT2());
  1361. ASSERT_NE(pref_override_, subnet_->getPreferred());
  1362. ASSERT_NE(valid_override_, subnet_->getValid());
  1363. ASSERT_FALSE(subnet_->inRange(addr_override_));
  1364. // Create allocation engine (hook names are registered in its ctor)
  1365. boost::scoped_ptr<AllocEngine> engine;
  1366. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE, 100)));
  1367. ASSERT_TRUE(engine);
  1368. // Initialize Hooks Manager
  1369. vector<string> libraries; // no libraries at this time
  1370. HooksManager::loadLibraries(libraries);
  1371. // Install a callout
  1372. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1373. "lease6_select", lease6_select_different_callout));
  1374. // Normally, dhcpv6_srv would passed the handle when calling allocateLeases6,
  1375. // but in tests we need to create it on our own.
  1376. CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
  1377. // Call allocateLeases6. Callouts should be triggered here.
  1378. Lease6Ptr lease;
  1379. EXPECT_NO_THROW(lease = expectOneLease(engine->allocateLeases6(subnet_,
  1380. duid_, iaid_, IOAddress("::"), Lease::TYPE_NA, false, false,
  1381. "", false, callout_handle)));
  1382. // Check that we got a lease
  1383. ASSERT_TRUE(lease);
  1384. // See if the values overridden by callout are there
  1385. EXPECT_TRUE(lease->addr_.equals(addr_override_));
  1386. EXPECT_EQ(t1_override_, lease->t1_);
  1387. EXPECT_EQ(t2_override_, lease->t2_);
  1388. EXPECT_EQ(pref_override_, lease->preferred_lft_);
  1389. EXPECT_EQ(valid_override_, lease->valid_lft_);
  1390. // Now check if the lease is in the database
  1391. Lease6Ptr from_mgr = LeaseMgrFactory::instance().getLease6(lease->type_,
  1392. lease->addr_);
  1393. ASSERT_TRUE(from_mgr);
  1394. // Check if values in the database are overridden
  1395. EXPECT_TRUE(from_mgr->addr_.equals(addr_override_));
  1396. EXPECT_EQ(t1_override_, from_mgr->t1_);
  1397. EXPECT_EQ(t2_override_, from_mgr->t2_);
  1398. EXPECT_EQ(pref_override_, from_mgr->preferred_lft_);
  1399. EXPECT_EQ(valid_override_, from_mgr->valid_lft_);
  1400. }
  1401. /// @brief helper class used in Hooks testing in AllocEngine4
  1402. ///
  1403. /// It features a couple of callout functions and buffers to store
  1404. /// the data that is accessible via callouts.
  1405. ///
  1406. /// Note: lease4_renew callout is tested from DHCPv4 server.
  1407. /// See HooksDhcpv4SrvTest.basic_lease4_renew in
  1408. /// src/bin/dhcp4/tests/dhcp4_srv_unittest.cc
  1409. class HookAllocEngine4Test : public AllocEngine4Test {
  1410. public:
  1411. HookAllocEngine4Test() {
  1412. resetCalloutBuffers();
  1413. }
  1414. virtual ~HookAllocEngine4Test() {
  1415. HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts(
  1416. "lease4_select");
  1417. }
  1418. /// @brief clears out buffers, so callouts can store received arguments
  1419. void resetCalloutBuffers() {
  1420. callback_name_ = string("");
  1421. callback_subnet4_.reset();
  1422. callback_fake_allocation_ = false;
  1423. callback_lease4_.reset();
  1424. callback_argument_names_.clear();
  1425. callback_addr_original_ = IOAddress("::");
  1426. callback_addr_updated_ = IOAddress("::");
  1427. }
  1428. /// callback that stores received callout name and received values
  1429. static int
  1430. lease4_select_callout(CalloutHandle& callout_handle) {
  1431. callback_name_ = string("lease4_select");
  1432. callout_handle.getArgument("subnet4", callback_subnet4_);
  1433. callout_handle.getArgument("fake_allocation", callback_fake_allocation_);
  1434. callout_handle.getArgument("lease4", callback_lease4_);
  1435. callback_addr_original_ = callback_lease4_->addr_;
  1436. callback_argument_names_ = callout_handle.getArgumentNames();
  1437. return (0);
  1438. }
  1439. /// callback that overrides the lease with different values
  1440. static int
  1441. lease4_select_different_callout(CalloutHandle& callout_handle) {
  1442. // Let's call the basic callout, so it can record all parameters
  1443. lease4_select_callout(callout_handle);
  1444. // Now we need to tweak the least a bit
  1445. Lease4Ptr lease;
  1446. callout_handle.getArgument("lease4", lease);
  1447. callback_addr_updated_ = addr_override_;
  1448. lease->addr_ = callback_addr_updated_;
  1449. lease->t1_ = t1_override_;
  1450. lease->t2_ = t2_override_;
  1451. lease->valid_lft_ = valid_override_;
  1452. return (0);
  1453. }
  1454. // Values to be used in callout to override lease4 content
  1455. static const IOAddress addr_override_;
  1456. static const uint32_t t1_override_;
  1457. static const uint32_t t2_override_;
  1458. static const uint32_t valid_override_;
  1459. // Callback will store original and overridden values here
  1460. static IOAddress callback_addr_original_;
  1461. static IOAddress callback_addr_updated_;
  1462. // Buffers (callback will store received values here)
  1463. static string callback_name_;
  1464. static Subnet4Ptr callback_subnet4_;
  1465. static Lease4Ptr callback_lease4_;
  1466. static bool callback_fake_allocation_;
  1467. static vector<string> callback_argument_names_;
  1468. };
  1469. // For some reason intialization within a class makes the linker confused.
  1470. // linker complains about undefined references if they are defined within
  1471. // the class declaration.
  1472. const IOAddress HookAllocEngine4Test::addr_override_("192.0.3.1");
  1473. const uint32_t HookAllocEngine4Test::t1_override_ = 4000;
  1474. const uint32_t HookAllocEngine4Test::t2_override_ = 7000;
  1475. const uint32_t HookAllocEngine4Test::valid_override_ = 9000;
  1476. IOAddress HookAllocEngine4Test::callback_addr_original_("::");
  1477. IOAddress HookAllocEngine4Test::callback_addr_updated_("::");
  1478. string HookAllocEngine4Test::callback_name_;
  1479. Subnet4Ptr HookAllocEngine4Test::callback_subnet4_;
  1480. Lease4Ptr HookAllocEngine4Test::callback_lease4_;
  1481. bool HookAllocEngine4Test::callback_fake_allocation_;
  1482. vector<string> HookAllocEngine4Test::callback_argument_names_;
  1483. // This test checks if the lease4_select callout is executed and expected
  1484. // parameters as passed.
  1485. TEST_F(HookAllocEngine4Test, lease4_select) {
  1486. // Note: The following order is working as expected:
  1487. // 1. create AllocEngine (that register hook points)
  1488. // 2. call loadLibraries()
  1489. //
  1490. // This order, however, causes segfault in HooksManager
  1491. // 1. call loadLibraries()
  1492. // 2. create AllocEngine (that register hook points)
  1493. // Create allocation engine (hook names are registered in its ctor)
  1494. boost::scoped_ptr<AllocEngine> engine;
  1495. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1496. 100, false)));
  1497. ASSERT_TRUE(engine);
  1498. // Initialize Hooks Manager
  1499. vector<string> libraries; // no libraries at this time
  1500. HooksManager::loadLibraries(libraries);
  1501. // Install pkt4_receive_callout
  1502. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1503. "lease4_select", lease4_select_callout));
  1504. CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
  1505. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1506. IOAddress("0.0.0.0"),
  1507. false, false, "",
  1508. false, callout_handle,
  1509. old_lease_);
  1510. // Check that we got a lease
  1511. ASSERT_TRUE(lease);
  1512. // Do all checks on the lease
  1513. checkLease4(lease);
  1514. // Check that the lease is indeed in LeaseMgr
  1515. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  1516. ASSERT_TRUE(from_mgr);
  1517. // Check that callouts were indeed called
  1518. EXPECT_EQ("lease4_select", callback_name_);
  1519. // Now check that the lease in LeaseMgr has the same parameters
  1520. ASSERT_TRUE(callback_lease4_);
  1521. detailCompareLease(callback_lease4_, from_mgr);
  1522. ASSERT_TRUE(callback_subnet4_);
  1523. EXPECT_EQ(subnet_->toText(), callback_subnet4_->toText());
  1524. EXPECT_EQ(callback_fake_allocation_, false);
  1525. // Check if all expected parameters are reported. It's a bit tricky, because
  1526. // order may be different. If the test starts failing, because someone tweaked
  1527. // hooks engine, we'll have to implement proper vector matching (ignoring order)
  1528. vector<string> expected_argument_names;
  1529. expected_argument_names.push_back("fake_allocation");
  1530. expected_argument_names.push_back("lease4");
  1531. expected_argument_names.push_back("subnet4");
  1532. EXPECT_TRUE(callback_argument_names_ == expected_argument_names);
  1533. }
  1534. // This test checks if lease4_select callout is able to override the values
  1535. // in a lease4.
  1536. TEST_F(HookAllocEngine4Test, change_lease4_select) {
  1537. // Make sure that the overridden values are different than the ones from
  1538. // subnet originally used to create the lease
  1539. ASSERT_NE(t1_override_, subnet_->getT1());
  1540. ASSERT_NE(t2_override_, subnet_->getT2());
  1541. ASSERT_NE(valid_override_, subnet_->getValid());
  1542. ASSERT_FALSE(subnet_->inRange(addr_override_));
  1543. // Create allocation engine (hook names are registered in its ctor)
  1544. boost::scoped_ptr<AllocEngine> engine;
  1545. ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
  1546. 100, false)));
  1547. ASSERT_TRUE(engine);
  1548. // Initialize Hooks Manager
  1549. vector<string> libraries; // no libraries at this time
  1550. HooksManager::loadLibraries(libraries);
  1551. // Install a callout
  1552. EXPECT_NO_THROW(HooksManager::preCalloutsLibraryHandle().registerCallout(
  1553. "lease4_select", lease4_select_different_callout));
  1554. // Normally, dhcpv4_srv would passed the handle when calling allocateLease4,
  1555. // but in tests we need to create it on our own.
  1556. CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();
  1557. // Call allocateLease4. Callouts should be triggered here.
  1558. Lease4Ptr lease = engine->allocateLease4(subnet_, clientid_, hwaddr_,
  1559. IOAddress("0.0.0.0"),
  1560. false, false, "",
  1561. false, callout_handle,
  1562. old_lease_);
  1563. // Check that we got a lease
  1564. ASSERT_TRUE(lease);
  1565. // See if the values overridden by callout are there
  1566. EXPECT_TRUE(lease->addr_.equals(addr_override_));
  1567. EXPECT_EQ(t1_override_, lease->t1_);
  1568. EXPECT_EQ(t2_override_, lease->t2_);
  1569. EXPECT_EQ(valid_override_, lease->valid_lft_);
  1570. // Now check if the lease is in the database
  1571. Lease4Ptr from_mgr = LeaseMgrFactory::instance().getLease4(lease->addr_);
  1572. ASSERT_TRUE(from_mgr);
  1573. // Check if values in the database are overridden
  1574. EXPECT_TRUE(from_mgr->addr_.equals(addr_override_));
  1575. EXPECT_EQ(t1_override_, from_mgr->t1_);
  1576. EXPECT_EQ(t2_override_, from_mgr->t2_);
  1577. EXPECT_EQ(valid_override_, from_mgr->valid_lft_);
  1578. }
  1579. }; // End of anonymous namespace