subnet_unittest.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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/option.h>
  17. #include <dhcp/dhcp6.h>
  18. #include <dhcpsrv/subnet.h>
  19. #include <exceptions/exceptions.h>
  20. #include <boost/scoped_ptr.hpp>
  21. #include <gtest/gtest.h>
  22. // don't import the entire boost namespace. It will unexpectedly hide uint8_t
  23. // for some systems.
  24. using boost::scoped_ptr;
  25. using namespace isc;
  26. using namespace isc::dhcp;
  27. using namespace isc::asiolink;
  28. namespace {
  29. TEST(Subnet4Test, constructor) {
  30. EXPECT_NO_THROW(Subnet4 subnet1(IOAddress("192.0.2.2"), 16,
  31. 1, 2, 3));
  32. EXPECT_THROW(Subnet4 subnet2(IOAddress("192.0.2.0"), 33, 1, 2, 3),
  33. BadValue); // invalid prefix length
  34. EXPECT_THROW(Subnet4 subnet3(IOAddress("2001:db8::1"), 24, 1, 2, 3),
  35. BadValue); // IPv6 addresses are not allowed in Subnet4
  36. }
  37. TEST(Subnet4Test, in_range) {
  38. Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
  39. EXPECT_EQ(1000, subnet.getT1());
  40. EXPECT_EQ(2000, subnet.getT2());
  41. EXPECT_EQ(3000, subnet.getValid());
  42. EXPECT_EQ("0.0.0.0", subnet.getRelayInfo().addr_.toText());
  43. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.0.0")));
  44. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.0")));
  45. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.1")));
  46. EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.255")));
  47. EXPECT_FALSE(subnet.inRange(IOAddress("192.0.3.0")));
  48. EXPECT_FALSE(subnet.inRange(IOAddress("0.0.0.0")));
  49. EXPECT_FALSE(subnet.inRange(IOAddress("255.255.255.255")));
  50. }
  51. // Checks whether the relay field has sane default and if it can
  52. // be changed, stored and retrieved
  53. TEST(Subnet4Test, relay) {
  54. Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
  55. EXPECT_EQ("0.0.0.0", subnet.getRelayInfo().addr_.toText());
  56. subnet.setRelayInfo(IOAddress("192.0.123.45"));
  57. EXPECT_EQ("192.0.123.45", subnet.getRelayInfo().addr_.toText());
  58. }
  59. // Checks whether siaddr field can be set and retrieved correctly.
  60. TEST(Subnet4Test, siaddr) {
  61. Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
  62. // Check if the default is 0.0.0.0
  63. EXPECT_EQ("0.0.0.0", subnet.getSiaddr().toText());
  64. // Check that we can set it up
  65. EXPECT_NO_THROW(subnet.setSiaddr(IOAddress("1.2.3.4")));
  66. // Check that we can get it back
  67. EXPECT_EQ("1.2.3.4", subnet.getSiaddr().toText());
  68. // Check that only v4 addresses are supported
  69. EXPECT_THROW(subnet.setSiaddr(IOAddress("2001:db8::1")),
  70. BadValue);
  71. }
  72. TEST(Subnet4Test, Pool4InSubnet4) {
  73. Subnet4Ptr subnet(new Subnet4(IOAddress("192.1.2.0"), 24, 1, 2, 3));
  74. PoolPtr pool1(new Pool4(IOAddress("192.1.2.0"), 25));
  75. PoolPtr pool2(new Pool4(IOAddress("192.1.2.128"), 26));
  76. PoolPtr pool3(new Pool4(IOAddress("192.1.2.192"), 30));
  77. EXPECT_NO_THROW(subnet->addPool(pool1));
  78. // If there's only one pool, get that pool
  79. PoolPtr mypool = subnet->getAnyPool(Lease::TYPE_V4);
  80. EXPECT_EQ(mypool, pool1);
  81. EXPECT_NO_THROW(subnet->addPool(pool2));
  82. EXPECT_NO_THROW(subnet->addPool(pool3));
  83. // If there are more than one pool and we didn't provide hint, we
  84. // should get the first pool
  85. EXPECT_NO_THROW(mypool = subnet->getAnyPool(Lease::TYPE_V4));
  86. EXPECT_EQ(mypool, pool1);
  87. // If we provide a hint, we should get a pool that this hint belongs to
  88. EXPECT_NO_THROW(mypool = subnet->getPool(Lease::TYPE_V4,
  89. IOAddress("192.1.2.195")));
  90. EXPECT_EQ(mypool, pool3);
  91. }
  92. TEST(Subnet4Test, Subnet4_Pool4_checks) {
  93. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  94. // this one is in subnet
  95. Pool4Ptr pool1(new Pool4(IOAddress("192.255.0.0"), 16));
  96. subnet->addPool(pool1);
  97. // this one is larger than the subnet!
  98. Pool4Ptr pool2(new Pool4(IOAddress("193.0.0.0"), 24));
  99. EXPECT_THROW(subnet->addPool(pool2), BadValue);
  100. // this one is totally out of blue
  101. Pool4Ptr pool3(new Pool4(IOAddress("1.2.3.4"), 16));
  102. EXPECT_THROW(subnet->addPool(pool3), BadValue);
  103. }
  104. // Tests whether Subnet4 object is able to store and process properly
  105. // information about allowed client class (a single class).
  106. TEST(Subnet4Test, clientClasses) {
  107. // Create the V4 subnet.
  108. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  109. // This client does not belong to any class.
  110. isc::dhcp::ClientClasses no_class;
  111. // This client belongs to foo only.
  112. isc::dhcp::ClientClasses foo_class;
  113. foo_class.insert("foo");
  114. // This client belongs to bar only. I like that client.
  115. isc::dhcp::ClientClasses bar_class;
  116. bar_class.insert("bar");
  117. // This client belongs to foo, bar and baz classes.
  118. isc::dhcp::ClientClasses three_classes;
  119. three_classes.insert("foo");
  120. three_classes.insert("bar");
  121. three_classes.insert("baz");
  122. // No class restrictions defined, any client should be supported
  123. EXPECT_TRUE(subnet->clientSupported(no_class));
  124. EXPECT_TRUE(subnet->clientSupported(foo_class));
  125. EXPECT_TRUE(subnet->clientSupported(bar_class));
  126. EXPECT_TRUE(subnet->clientSupported(three_classes));
  127. // Let's allow only clients belongning to "bar" class.
  128. subnet->allowClientClass("bar");
  129. EXPECT_FALSE(subnet->clientSupported(no_class));
  130. EXPECT_FALSE(subnet->clientSupported(foo_class));
  131. EXPECT_TRUE(subnet->clientSupported(bar_class));
  132. EXPECT_TRUE(subnet->clientSupported(three_classes));
  133. }
  134. // Tests whether Subnet4 object is able to store and process properly
  135. // information about allowed client classes (multiple classes allowed).
  136. TEST(Subnet4Test, clientClassesMultiple) {
  137. // Create the V4 subnet.
  138. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  139. // This client does not belong to any class.
  140. isc::dhcp::ClientClasses no_class;
  141. // This client belongs to foo only.
  142. isc::dhcp::ClientClasses foo_class;
  143. foo_class.insert("foo");
  144. // This client belongs to bar only. I like that client.
  145. isc::dhcp::ClientClasses bar_class;
  146. bar_class.insert("bar");
  147. // No class restrictions defined, any client should be supported
  148. EXPECT_TRUE(subnet->clientSupported(no_class));
  149. EXPECT_TRUE(subnet->clientSupported(foo_class));
  150. EXPECT_TRUE(subnet->clientSupported(bar_class));
  151. // Let's allow clients belongning to "bar" or "foo" class.
  152. subnet->allowClientClass("bar");
  153. subnet->allowClientClass("foo");
  154. // Class-less clients are to be rejected.
  155. EXPECT_FALSE(subnet->clientSupported(no_class));
  156. // Clients in foo class should be accepted.
  157. EXPECT_TRUE(subnet->clientSupported(foo_class));
  158. // Clients in bar class should be accepted as well.
  159. EXPECT_TRUE(subnet->clientSupported(bar_class));
  160. }
  161. TEST(Subnet4Test, addInvalidOption) {
  162. // Create the V4 subnet.
  163. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
  164. // Some dummy option code.
  165. uint16_t code = 100;
  166. // Create option with invalid universe (V6 instead of V4).
  167. // Attempt to add this option should result in exception.
  168. OptionPtr option1(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  169. EXPECT_THROW(subnet->addOption(option1, false, "dhcp4"),
  170. isc::BadValue);
  171. // Create NULL pointer option. Attempt to add NULL option
  172. // should result in exception.
  173. OptionPtr option2;
  174. ASSERT_FALSE(option2);
  175. EXPECT_THROW(subnet->addOption(option2, false, "dhcp4"),
  176. isc::BadValue);
  177. }
  178. // This test verifies that inRange() and inPool() methods work properly.
  179. TEST(Subnet4Test, inRangeinPool) {
  180. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.0.0"), 8, 1, 2, 3));
  181. // this one is in subnet
  182. Pool4Ptr pool1(new Pool4(IOAddress("192.2.0.0"), 16));
  183. subnet->addPool(pool1);
  184. // 192.1.1.1 belongs to the subnet...
  185. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.1.1")));
  186. // ... but it does not belong to any pool within
  187. EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.1.1.1")));
  188. // the last address that is in range, but out of pool
  189. EXPECT_TRUE(subnet->inRange(IOAddress("192.1.255.255")));
  190. EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.1.255.255")));
  191. // the first address that is in range, in pool
  192. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.0.0")));
  193. EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.0.0")));
  194. // let's try something in the middle as well
  195. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.3.4")));
  196. EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.3.4")));
  197. // the last address that is in range, in pool
  198. EXPECT_TRUE(subnet->inRange(IOAddress("192.2.255.255")));
  199. EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.255.255")));
  200. // the first address that is in range, but out of pool
  201. EXPECT_TRUE(subnet->inRange(IOAddress("192.3.0.0")));
  202. EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.3.0.0")));
  203. }
  204. // This test checks if the toText() method returns text representation
  205. TEST(Subnet4Test, toText) {
  206. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  207. EXPECT_EQ("192.0.2.0/24", subnet->toText());
  208. }
  209. // This test checks if the get() method returns proper parameters
  210. TEST(Subnet4Test, get) {
  211. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 28, 1, 2, 3));
  212. EXPECT_EQ("192.0.2.0", subnet->get().first.toText());
  213. EXPECT_EQ(28, subnet->get().second);
  214. }
  215. // Checks if last allocated address/prefix is stored/retrieved properly
  216. TEST(Subnet4Test, lastAllocated) {
  217. IOAddress addr("192.0.2.17");
  218. IOAddress last("192.0.2.255");
  219. Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
  220. // Check initial conditions (all should be set to the last address in range)
  221. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_V4).toText());
  222. // Now set last allocated for IA
  223. EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_V4, addr));
  224. EXPECT_EQ(addr.toText(), subnet->getLastAllocated(Lease::TYPE_V4).toText());
  225. // No, you can't set the last allocated IPv6 address in IPv4 subnet
  226. EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_TA, addr), BadValue);
  227. EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_TA, addr), BadValue);
  228. EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_PD, addr), BadValue);
  229. }
  230. // Checks if the V4 is the only allowed type for Pool4 and if getPool()
  231. // is working properly.
  232. TEST(Subnet4Test, PoolType) {
  233. Subnet4Ptr subnet(new Subnet4(IOAddress("192.2.0.0"), 16, 1, 2, 3));
  234. PoolPtr pool1(new Pool4(IOAddress("192.2.1.0"), 24));
  235. PoolPtr pool2(new Pool4(IOAddress("192.2.2.0"), 24));
  236. PoolPtr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:3::"), 64));
  237. PoolPtr pool4(new Pool6(Lease::TYPE_TA, IOAddress("2001:db8:1:4::"), 64));
  238. PoolPtr pool5(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1:1::"), 64));
  239. // There should be no pools of any type by default
  240. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_V4));
  241. // It should not be possible to ask for V6 pools in Subnet4
  242. EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_NA), BadValue);
  243. EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_TA), BadValue);
  244. EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_PD), BadValue);
  245. // Let's add a single V4 pool and check that it can be retrieved
  246. EXPECT_NO_THROW(subnet->addPool(pool1));
  247. // If there's only one IA pool, get that pool (without and with hint)
  248. EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_V4));
  249. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.1.167")));
  250. // Let's add additional V4 pool
  251. EXPECT_NO_THROW(subnet->addPool(pool2));
  252. // Try without hints
  253. EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_V4));
  254. // Try with valid hints
  255. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("192.2.1.5")));
  256. EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_V4, IOAddress("192.2.2.254")));
  257. // Try with bogus hints (hints should be ingored)
  258. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("10.1.1.1")));
  259. // Trying to add Pool6 to Subnet4 is a big no,no!
  260. EXPECT_THROW(subnet->addPool(pool3), BadValue);
  261. EXPECT_THROW(subnet->addPool(pool4), BadValue);
  262. EXPECT_THROW(subnet->addPool(pool5), BadValue);
  263. }
  264. // Tests for Subnet6
  265. TEST(Subnet6Test, constructor) {
  266. EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
  267. 1, 2, 3, 4));
  268. EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
  269. BadValue); // invalid prefix length
  270. EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
  271. BadValue); // IPv4 addresses are not allowed in Subnet6
  272. }
  273. TEST(Subnet6Test, in_range) {
  274. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  275. EXPECT_EQ(1000, subnet.getT1());
  276. EXPECT_EQ(2000, subnet.getT2());
  277. EXPECT_EQ(3000, subnet.getPreferred());
  278. EXPECT_EQ(4000, subnet.getValid());
  279. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
  280. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
  281. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
  282. EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
  283. EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
  284. EXPECT_FALSE(subnet.inRange(IOAddress("::")));
  285. }
  286. // Checks whether the relay field has sane default and if it can
  287. // be changed, stored and retrieved
  288. TEST(Subnet6Test, relay) {
  289. Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
  290. EXPECT_EQ("::", subnet.getRelayInfo().addr_.toText());
  291. subnet.setRelayInfo(IOAddress("2001:ffff::1"));
  292. EXPECT_EQ("2001:ffff::1", subnet.getRelayInfo().addr_.toText());
  293. }
  294. TEST(Subnet6Test, Pool6InSubnet6) {
  295. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  296. PoolPtr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
  297. PoolPtr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:2::"), 64));
  298. PoolPtr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:3::"), 64));
  299. subnet->addPool(pool1);
  300. // If there's only one pool, get that pool
  301. PoolPtr mypool = subnet->getAnyPool(Lease::TYPE_NA);
  302. EXPECT_EQ(mypool, pool1);
  303. subnet->addPool(pool2);
  304. subnet->addPool(pool3);
  305. // If there are more than one pool and we didn't provide hint, we
  306. // should get the first pool
  307. mypool = subnet->getAnyPool(Lease::TYPE_NA);
  308. EXPECT_EQ(mypool, pool1);
  309. // If we provide a hint, we should get a pool that this hint belongs to
  310. mypool = subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:3::dead:beef"));
  311. EXPECT_EQ(mypool, pool3);
  312. }
  313. // Check if Subnet6 supports different types of pools properly.
  314. TEST(Subnet6Test, PoolTypes) {
  315. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  316. PoolPtr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
  317. PoolPtr pool2(new Pool6(Lease::TYPE_TA, IOAddress("2001:db8:1:2::"), 64));
  318. PoolPtr pool3(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1:3::"), 64));
  319. PoolPtr pool4(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1:4::"), 64));
  320. PoolPtr pool5(new Pool4(IOAddress("192.0.2.0"), 24));
  321. // There should be no pools of any type by default
  322. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_NA));
  323. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_TA));
  324. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_PD));
  325. // Trying to get IPv4 pool from Subnet6 is not allowed
  326. EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_V4), BadValue);
  327. // Let's add a single IA pool and check that it can be retrieved
  328. EXPECT_NO_THROW(subnet->addPool(pool1));
  329. // If there's only one IA pool, get that pool
  330. EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_NA));
  331. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:1::1")));
  332. // Check if pools of different type are not returned
  333. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_TA));
  334. EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_PD));
  335. // We ask with good hints, but wrong types, should return nothing
  336. EXPECT_EQ(PoolPtr(), subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:2::1")));
  337. EXPECT_EQ(PoolPtr(), subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:3::1")));
  338. // Let's add TA and PD pools
  339. EXPECT_NO_THROW(subnet->addPool(pool2));
  340. EXPECT_NO_THROW(subnet->addPool(pool3));
  341. // Try without hints
  342. EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_NA));
  343. EXPECT_EQ(pool2, subnet->getAnyPool(Lease::TYPE_TA));
  344. EXPECT_EQ(pool3, subnet->getAnyPool(Lease::TYPE_PD));
  345. // Try with valid hints
  346. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:1::1")));
  347. EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:2::1")));
  348. EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:3::1")));
  349. // Try with bogus hints (hints should be ingored)
  350. EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:7::1")));
  351. EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:7::1")));
  352. EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:7::1")));
  353. // Let's add a second PD pool
  354. EXPECT_NO_THROW(subnet->addPool(pool4));
  355. // Without hints, it should return the first pool
  356. EXPECT_EQ(pool3, subnet->getAnyPool(Lease::TYPE_PD));
  357. // With valid hint, it should return that hint
  358. EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:3::1")));
  359. EXPECT_EQ(pool4, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:4::1")));
  360. // With invalid hint, it should return the first pool
  361. EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8::123")));
  362. // Adding Pool4 to Subnet6 is a big no, no!
  363. EXPECT_THROW(subnet->addPool(pool5), BadValue);
  364. }
  365. // Tests whether Subnet6 object is able to store and process properly
  366. // information about allowed client class (a single class).
  367. TEST(Subnet6Test, clientClasses) {
  368. // Create the V6 subnet.
  369. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  370. // This client does not belong to any class.
  371. isc::dhcp::ClientClasses no_class;
  372. // This client belongs to foo only.
  373. isc::dhcp::ClientClasses foo_class;
  374. foo_class.insert("foo");
  375. // This client belongs to bar only. I like that client.
  376. isc::dhcp::ClientClasses bar_class;
  377. bar_class.insert("bar");
  378. // This client belongs to foo, bar and baz classes.
  379. isc::dhcp::ClientClasses three_classes;
  380. three_classes.insert("foo");
  381. three_classes.insert("bar");
  382. three_classes.insert("baz");
  383. // No class restrictions defined, any client should be supported
  384. EXPECT_TRUE(subnet->clientSupported(no_class));
  385. EXPECT_TRUE(subnet->clientSupported(foo_class));
  386. EXPECT_TRUE(subnet->clientSupported(bar_class));
  387. EXPECT_TRUE(subnet->clientSupported(three_classes));
  388. // Let's allow only clients belongning to "bar" class.
  389. subnet->allowClientClass("bar");
  390. EXPECT_FALSE(subnet->clientSupported(no_class));
  391. EXPECT_FALSE(subnet->clientSupported(foo_class));
  392. EXPECT_TRUE(subnet->clientSupported(bar_class));
  393. EXPECT_TRUE(subnet->clientSupported(three_classes));
  394. }
  395. // Tests whether Subnet6 object is able to store and process properly
  396. // information about allowed client class (multiple classes allowed).
  397. TEST(Subnet6Test, clientClassesMultiple) {
  398. // Create the V6 subnet.
  399. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  400. // This client does not belong to any class.
  401. isc::dhcp::ClientClasses no_class;
  402. // This client belongs to foo only.
  403. isc::dhcp::ClientClasses foo_class;
  404. foo_class.insert("foo");
  405. // This client belongs to bar only. I like that client.
  406. isc::dhcp::ClientClasses bar_class;
  407. bar_class.insert("bar");
  408. // No class restrictions defined, any client should be supported
  409. EXPECT_TRUE(subnet->clientSupported(no_class));
  410. EXPECT_TRUE(subnet->clientSupported(foo_class));
  411. EXPECT_TRUE(subnet->clientSupported(bar_class));
  412. // Let's allow only clients belongning to "foo" or "bar" class.
  413. subnet->allowClientClass("foo");
  414. subnet->allowClientClass("bar");
  415. // Class-less clients are to be rejected.
  416. EXPECT_FALSE(subnet->clientSupported(no_class));
  417. // Clients in foo class should be accepted.
  418. EXPECT_TRUE(subnet->clientSupported(foo_class));
  419. // Clients in bar class should be accepted as well.
  420. EXPECT_TRUE(subnet->clientSupported(bar_class));
  421. }
  422. TEST(Subnet6Test, Subnet6_Pool6_checks) {
  423. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  424. // this one is in subnet
  425. Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
  426. subnet->addPool(pool1);
  427. // this one is larger than the subnet!
  428. Pool6Ptr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::"), 48));
  429. EXPECT_THROW(subnet->addPool(pool2), BadValue);
  430. // this one is totally out of blue
  431. Pool6Ptr pool3(new Pool6(Lease::TYPE_NA, IOAddress("3000::"), 16));
  432. EXPECT_THROW(subnet->addPool(pool3), BadValue);
  433. Pool6Ptr pool4(new Pool6(Lease::TYPE_NA, IOAddress("4001:db8:1::"), 80));
  434. EXPECT_THROW(subnet->addPool(pool4), BadValue);
  435. }
  436. TEST(Subnet6Test, addOptions) {
  437. // Create as subnet to add options to it.
  438. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  439. // Differentiate options by their codes (100-109)
  440. for (uint16_t code = 100; code < 110; ++code) {
  441. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  442. ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
  443. }
  444. // Add 7 options to another option space. The option codes partially overlap
  445. // with option codes that we have added to dhcp6 option space.
  446. for (uint16_t code = 105; code < 112; ++code) {
  447. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  448. ASSERT_NO_THROW(subnet->addOption(option, false, "isc"));
  449. }
  450. // Get options from the Subnet and check if all 10 are there.
  451. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  452. ASSERT_TRUE(options);
  453. ASSERT_EQ(10, options->size());
  454. // Validate codes of options added to dhcp6 option space.
  455. uint16_t expected_code = 100;
  456. for (Subnet::OptionContainer::const_iterator option_desc = options->begin();
  457. option_desc != options->end(); ++option_desc) {
  458. ASSERT_TRUE(option_desc->option);
  459. EXPECT_EQ(expected_code, option_desc->option->getType());
  460. ++expected_code;
  461. }
  462. options = subnet->getOptionDescriptors("isc");
  463. ASSERT_TRUE(options);
  464. ASSERT_EQ(7, options->size());
  465. // Validate codes of options added to isc option space.
  466. expected_code = 105;
  467. for (Subnet::OptionContainer::const_iterator option_desc = options->begin();
  468. option_desc != options->end(); ++option_desc) {
  469. ASSERT_TRUE(option_desc->option);
  470. EXPECT_EQ(expected_code, option_desc->option->getType());
  471. ++expected_code;
  472. }
  473. // Try to get options from a non-existing option space.
  474. options = subnet->getOptionDescriptors("abcd");
  475. ASSERT_TRUE(options);
  476. EXPECT_TRUE(options->empty());
  477. // Delete options from all spaces.
  478. subnet->delOptions();
  479. // Make sure that all options have been removed.
  480. options = subnet->getOptionDescriptors("dhcp6");
  481. ASSERT_TRUE(options);
  482. EXPECT_TRUE(options->empty());
  483. options = subnet->getOptionDescriptors("isc");
  484. ASSERT_TRUE(options);
  485. EXPECT_TRUE(options->empty());
  486. }
  487. TEST(Subnet6Test, addNonUniqueOptions) {
  488. // Create as subnet to add options to it.
  489. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  490. // Create a set of options with non-unique codes.
  491. for (int i = 0; i < 2; ++i) {
  492. // In the inner loop we create options with unique codes (100-109).
  493. for (uint16_t code = 100; code < 110; ++code) {
  494. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  495. ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
  496. }
  497. }
  498. // Sanity check that all options are there.
  499. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  500. ASSERT_EQ(20, options->size());
  501. // Use container index #1 to get the options by their codes.
  502. Subnet::OptionContainerTypeIndex& idx = options->get<1>();
  503. // Look for the codes 100-109.
  504. for (uint16_t code = 100; code < 110; ++ code) {
  505. // For each code we should get two instances of options->
  506. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  507. Subnet::OptionContainerTypeIndex::const_iterator> range =
  508. idx.equal_range(code);
  509. // Distance between iterators indicates how many options
  510. // have been retured for the particular code.
  511. ASSERT_EQ(2, distance(range.first, range.second));
  512. // Check that returned options actually have the expected option code.
  513. for (Subnet::OptionContainerTypeIndex::const_iterator option_desc = range.first;
  514. option_desc != range.second; ++option_desc) {
  515. ASSERT_TRUE(option_desc->option);
  516. EXPECT_EQ(code, option_desc->option->getType());
  517. }
  518. }
  519. // Let's try to find some non-exiting option.
  520. const uint16_t non_existing_code = 150;
  521. std::pair<Subnet::OptionContainerTypeIndex::const_iterator,
  522. Subnet::OptionContainerTypeIndex::const_iterator> range =
  523. idx.equal_range(non_existing_code);
  524. // Empty set is expected.
  525. EXPECT_EQ(0, distance(range.first, range.second));
  526. subnet->delOptions();
  527. options = subnet->getOptionDescriptors("dhcp6");
  528. EXPECT_EQ(0, options->size());
  529. }
  530. TEST(Subnet6Test, addInvalidOption) {
  531. // Create as subnet to add options to it.
  532. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  533. // Some dummy option code.
  534. uint16_t code = 100;
  535. // Create option with invalid universe (V4 instead of V6).
  536. // Attempt to add this option should result in exception.
  537. OptionPtr option1(new Option(Option::V4, code, OptionBuffer(10, 0xFF)));
  538. EXPECT_THROW(subnet->addOption(option1, false, "dhcp6"), isc::BadValue);
  539. // Create NULL pointer option. Attempt to add NULL option
  540. // should result in exception.
  541. OptionPtr option2;
  542. ASSERT_FALSE(option2);
  543. EXPECT_THROW(subnet->addOption(option2, false, "dhcp6"), isc::BadValue);
  544. }
  545. TEST(Subnet6Test, addPersistentOption) {
  546. // Create as subnet to add options to it.
  547. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  548. // Add 10 options to the subnet with option codes 100 - 109.
  549. for (uint16_t code = 100; code < 110; ++code) {
  550. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  551. // We create 10 options and want some of them to be flagged
  552. // persistent and some non-persistent. Persistent options are
  553. // those that server sends to clients regardless if they ask
  554. // for them or not. We pick 3 out of 10 options and mark them
  555. // non-persistent and 7 other options persistent.
  556. // Code values: 102, 105 and 108 are divisible by 3
  557. // and options with these codes will be flagged non-persistent.
  558. // Options with other codes will be flagged persistent.
  559. bool persistent = (code % 3) ? true : false;
  560. ASSERT_NO_THROW(subnet->addOption(option, persistent, "dhcp6"));
  561. }
  562. // Get added options from the subnet.
  563. Subnet::OptionContainerPtr options = subnet->getOptionDescriptors("dhcp6");
  564. // options->get<2> returns reference to container index #2. This
  565. // index is used to access options by the 'persistent' flag.
  566. Subnet::OptionContainerPersistIndex& idx = options->get<2>();
  567. // Get all persistent options->
  568. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  569. Subnet::OptionContainerPersistIndex::const_iterator> range_persistent =
  570. idx.equal_range(true);
  571. // 3 out of 10 options have been flagged persistent.
  572. ASSERT_EQ(7, distance(range_persistent.first, range_persistent.second));
  573. // Get all non-persistent options->
  574. std::pair<Subnet::OptionContainerPersistIndex::const_iterator,
  575. Subnet::OptionContainerPersistIndex::const_iterator> range_non_persistent =
  576. idx.equal_range(false);
  577. // 7 out of 10 options have been flagged persistent.
  578. ASSERT_EQ(3, distance(range_non_persistent.first, range_non_persistent.second));
  579. subnet->delOptions();
  580. options = subnet->getOptionDescriptors("dhcp6");
  581. EXPECT_EQ(0, options->size());
  582. }
  583. TEST(Subnet6Test, getOptionDescriptor) {
  584. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 56, 1, 2, 3, 4));
  585. // Add 10 options to a "dhcp6" option space in the subnet.
  586. for (uint16_t code = 100; code < 110; ++code) {
  587. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  588. ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
  589. }
  590. // Check that we can get each added option descriptor using
  591. // individually.
  592. for (uint16_t code = 100; code < 110; ++code) {
  593. std::ostringstream stream;
  594. // First, try the invalid option space name.
  595. Subnet::OptionDescriptor desc = subnet->getOptionDescriptor("isc", code);
  596. // Returned descriptor should contain NULL option ptr.
  597. EXPECT_FALSE(desc.option);
  598. // Now, try the valid option space.
  599. desc = subnet->getOptionDescriptor("dhcp6", code);
  600. // Test that the option code matches the expected code.
  601. ASSERT_TRUE(desc.option);
  602. EXPECT_EQ(code, desc.option->getType());
  603. }
  604. }
  605. TEST(Subnet6Test, addVendorOptions) {
  606. uint32_t vendor_id1 = 12345678;
  607. uint32_t vendor_id2 = 87654321;
  608. uint32_t vendor_id_bogus = 1111111;
  609. // Create as subnet to add options to it.
  610. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  611. // Differentiate options by their codes (100-109)
  612. for (uint16_t code = 100; code < 110; ++code) {
  613. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  614. ASSERT_NO_THROW(subnet->addVendorOption(option, false, vendor_id1));
  615. }
  616. // Add 7 options to another option space. The option codes partially overlap
  617. // with option codes that we have added to dhcp6 option space.
  618. for (uint16_t code = 105; code < 112; ++code) {
  619. OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
  620. ASSERT_NO_THROW(subnet->addVendorOption(option, false, vendor_id2));
  621. }
  622. // Get options from the Subnet and check if all 10 are there.
  623. Subnet::OptionContainerPtr options = subnet->getVendorOptionDescriptors(vendor_id1);
  624. ASSERT_TRUE(options);
  625. ASSERT_EQ(10, options->size());
  626. // Validate codes of options added to dhcp6 option space.
  627. uint16_t expected_code = 100;
  628. for (Subnet::OptionContainer::const_iterator option_desc = options->begin();
  629. option_desc != options->end(); ++option_desc) {
  630. ASSERT_TRUE(option_desc->option);
  631. EXPECT_EQ(expected_code, option_desc->option->getType());
  632. ++expected_code;
  633. }
  634. options = subnet->getVendorOptionDescriptors(vendor_id2);
  635. ASSERT_TRUE(options);
  636. ASSERT_EQ(7, options->size());
  637. // Validate codes of options added to isc option space.
  638. expected_code = 105;
  639. for (Subnet::OptionContainer::const_iterator option_desc = options->begin();
  640. option_desc != options->end(); ++option_desc) {
  641. ASSERT_TRUE(option_desc->option);
  642. EXPECT_EQ(expected_code, option_desc->option->getType());
  643. ++expected_code;
  644. }
  645. // Try to get options from a non-existing option space.
  646. options = subnet->getVendorOptionDescriptors(vendor_id_bogus);
  647. ASSERT_TRUE(options);
  648. EXPECT_TRUE(options->empty());
  649. // Delete options from all spaces.
  650. subnet->delVendorOptions();
  651. // Make sure that all options have been removed.
  652. options = subnet->getVendorOptionDescriptors(vendor_id1);
  653. ASSERT_TRUE(options);
  654. EXPECT_TRUE(options->empty());
  655. options = subnet->getVendorOptionDescriptors(vendor_id2);
  656. ASSERT_TRUE(options);
  657. EXPECT_TRUE(options->empty());
  658. }
  659. // This test verifies that inRange() and inPool() methods work properly.
  660. TEST(Subnet6Test, inRangeinPool) {
  661. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
  662. // this one is in subnet
  663. Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::10"),
  664. IOAddress("2001:db8::20")));
  665. subnet->addPool(pool1);
  666. // 192.1.1.1 belongs to the subnet...
  667. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::1")));
  668. // ... but it does not belong to any pool within
  669. EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::1")));
  670. // the last address that is in range, but out of pool
  671. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::f")));
  672. EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::f")));
  673. // the first address that is in range, in pool
  674. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::10")));
  675. EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::10")));
  676. // let's try something in the middle as well
  677. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::18")));
  678. EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::18")));
  679. // the last address that is in range, in pool
  680. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::20")));
  681. EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::20")));
  682. // the first address that is in range, but out of pool
  683. EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::21")));
  684. EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::21")));
  685. }
  686. // This test checks if the toText() method returns text representation
  687. TEST(Subnet6Test, toText) {
  688. Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
  689. EXPECT_EQ("2001:db8::/32", subnet.toText());
  690. }
  691. // This test checks if the get() method returns proper parameters
  692. TEST(Subnet6Test, get) {
  693. Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
  694. EXPECT_EQ("2001:db8::", subnet.get().first.toText());
  695. EXPECT_EQ(32, subnet.get().second);
  696. }
  697. // This trivial test checks if interface name is stored properly
  698. // in Subnet6 objects.
  699. TEST(Subnet6Test, iface) {
  700. Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
  701. EXPECT_TRUE(subnet.getIface().empty());
  702. subnet.setIface("en1");
  703. EXPECT_EQ("en1", subnet.getIface());
  704. }
  705. // This trivial test checks if the interface-id option can be set and
  706. // later retrieved for a subnet6 object.
  707. TEST(Subnet6Test, interfaceId) {
  708. // Create as subnet to add options to it.
  709. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
  710. EXPECT_FALSE(subnet->getInterfaceId());
  711. OptionPtr option(new Option(Option::V6, D6O_INTERFACE_ID, OptionBuffer(10, 0xFF)));
  712. subnet->setInterfaceId(option);
  713. EXPECT_EQ(option, subnet->getInterfaceId());
  714. }
  715. // Checks if last allocated address/prefix is stored/retrieved properly
  716. TEST(Subnet6Test, lastAllocated) {
  717. IOAddress ia("2001:db8:1::1");
  718. IOAddress ta("2001:db8:1::abcd");
  719. IOAddress pd("2001:db8:1::1234:5678");
  720. IOAddress last("2001:db8:1::ffff:ffff:ffff:ffff");
  721. Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 1, 2, 3, 4));
  722. // Check initial conditions (all should be set to the last address in range)
  723. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
  724. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
  725. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
  726. // Now set last allocated for IA
  727. EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_NA, ia));
  728. EXPECT_EQ(ia.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
  729. // TA and PD should be unchanged
  730. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
  731. EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
  732. // Now set TA and PD
  733. EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_TA, ta));
  734. EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_PD, pd));
  735. EXPECT_EQ(ia.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
  736. EXPECT_EQ(ta.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
  737. EXPECT_EQ(pd.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
  738. // No, you can't set the last allocated IPv4 address in IPv6 subnet
  739. EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_V4, ia), BadValue);
  740. }
  741. };