123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958 |
- // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <config.h>
- #include <asiolink/io_address.h>
- #include <dhcp/option.h>
- #include <dhcp/dhcp6.h>
- #include <dhcpsrv/subnet.h>
- #include <exceptions/exceptions.h>
- #include <boost/scoped_ptr.hpp>
- #include <gtest/gtest.h>
- // don't import the entire boost namespace. It will unexpectedly hide uint8_t
- // for some systems.
- using boost::scoped_ptr;
- using namespace isc;
- using namespace isc::dhcp;
- using namespace isc::asiolink;
- namespace {
- TEST(Subnet4Test, constructor) {
- EXPECT_NO_THROW(Subnet4 subnet1(IOAddress("192.0.2.2"), 16,
- 1, 2, 3, 10));
- EXPECT_THROW(Subnet4 subnet2(IOAddress("192.0.2.0"), 33, 1, 2, 3),
- BadValue); // invalid prefix length
- EXPECT_THROW(Subnet4 subnet3(IOAddress("2001:db8::1"), 24, 1, 2, 3),
- BadValue); // IPv6 addresses are not allowed in Subnet4
- }
- // Checks that the subnet id can be either autogenerated or set to an
- // arbitrary value through the constructor.
- TEST(Subnet4Test, subnetID) {
- // Create subnet and don't specify id, so as it is autogenerated.
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1000, 2000,
- 3000));
- SubnetID id0 = subnet->getID();
- // Create another subnet and let id be autogenerated.
- subnet.reset(new Subnet4(IOAddress("192.0.3.0"), 24, 1000, 2000,
- 3000));
- SubnetID id1 = subnet->getID();
- // The autogenerated ids must not be equal.
- EXPECT_NE(id0, id1);
- // Create third subnet but this time select an arbitrary id. The id
- // we use the one of the second subnet. That way we ensure that the
- // subnet id we provide via constructor is used and it is not
- // autogenerated - if it was autogenerated we would get id other
- // than id1 because id1 has already been used.
- subnet.reset(new Subnet4(IOAddress("192.0.4.0"), 24, 1000, 2000,
- 3000, id1));
- EXPECT_EQ(id1, subnet->getID());
- }
- TEST(Subnet4Test, inRange) {
- Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
- EXPECT_EQ(1000, subnet.getT1());
- EXPECT_EQ(2000, subnet.getT2());
- EXPECT_EQ(3000, subnet.getValid());
- EXPECT_EQ("0.0.0.0", subnet.getRelayInfo().addr_.toText());
- EXPECT_FALSE(subnet.inRange(IOAddress("192.0.0.0")));
- EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.0")));
- EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.1")));
- EXPECT_TRUE(subnet.inRange(IOAddress("192.0.2.255")));
- EXPECT_FALSE(subnet.inRange(IOAddress("192.0.3.0")));
- EXPECT_FALSE(subnet.inRange(IOAddress("0.0.0.0")));
- EXPECT_FALSE(subnet.inRange(IOAddress("255.255.255.255")));
- }
- // Checks whether the relay field has sane default and if it can
- // be changed, stored and retrieved
- TEST(Subnet4Test, relay) {
- Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
- EXPECT_EQ("0.0.0.0", subnet.getRelayInfo().addr_.toText());
- subnet.setRelayInfo(IOAddress("192.0.123.45"));
- EXPECT_EQ("192.0.123.45", subnet.getRelayInfo().addr_.toText());
- }
- // Checks whether siaddr field can be set and retrieved correctly.
- TEST(Subnet4Test, siaddr) {
- Subnet4 subnet(IOAddress("192.0.2.1"), 24, 1000, 2000, 3000);
- // Check if the default is 0.0.0.0
- EXPECT_EQ("0.0.0.0", subnet.getSiaddr().toText());
- // Check that we can set it up
- EXPECT_NO_THROW(subnet.setSiaddr(IOAddress("1.2.3.4")));
- // Check that we can get it back
- EXPECT_EQ("1.2.3.4", subnet.getSiaddr().toText());
- // Check that only v4 addresses are supported
- EXPECT_THROW(subnet.setSiaddr(IOAddress("2001:db8::1")),
- BadValue);
- }
- TEST(Subnet4Test, Pool4InSubnet4) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.1.2.0"), 24, 1, 2, 3));
- PoolPtr pool1(new Pool4(IOAddress("192.1.2.0"), 25));
- PoolPtr pool2(new Pool4(IOAddress("192.1.2.128"), 26));
- PoolPtr pool3(new Pool4(IOAddress("192.1.2.192"), 30));
- EXPECT_NO_THROW(subnet->addPool(pool1));
- // If there's only one pool, get that pool
- PoolPtr mypool = subnet->getAnyPool(Lease::TYPE_V4);
- EXPECT_EQ(mypool, pool1);
- EXPECT_NO_THROW(subnet->addPool(pool2));
- EXPECT_NO_THROW(subnet->addPool(pool3));
- // If there are more than one pool and we didn't provide hint, we
- // should get the first pool
- EXPECT_NO_THROW(mypool = subnet->getAnyPool(Lease::TYPE_V4));
- EXPECT_EQ(mypool, pool1);
- // If we provide a hint, we should get a pool that this hint belongs to
- EXPECT_NO_THROW(mypool = subnet->getPool(Lease::TYPE_V4,
- IOAddress("192.1.2.195")));
- EXPECT_EQ(mypool, pool3);
- }
- TEST(Subnet4Test, Subnet4_Pool4_checks) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
- // this one is in subnet
- Pool4Ptr pool1(new Pool4(IOAddress("192.255.0.0"), 16));
- subnet->addPool(pool1);
- // this one is larger than the subnet!
- Pool4Ptr pool2(new Pool4(IOAddress("193.0.0.0"), 24));
- EXPECT_THROW(subnet->addPool(pool2), BadValue);
- // this one is totally out of blue
- Pool4Ptr pool3(new Pool4(IOAddress("1.2.3.4"), 16));
- EXPECT_THROW(subnet->addPool(pool3), BadValue);
- }
- // Tests whether Subnet4 object is able to store and process properly
- // information about allowed client class (a single class).
- TEST(Subnet4Test, clientClasses) {
- // Create the V4 subnet.
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
- // This client does not belong to any class.
- isc::dhcp::ClientClasses no_class;
- // This client belongs to foo only.
- isc::dhcp::ClientClasses foo_class;
- foo_class.insert("foo");
- // This client belongs to bar only. I like that client.
- isc::dhcp::ClientClasses bar_class;
- bar_class.insert("bar");
- // This client belongs to foo, bar and baz classes.
- isc::dhcp::ClientClasses three_classes;
- three_classes.insert("foo");
- three_classes.insert("bar");
- three_classes.insert("baz");
- // No class restrictions defined, any client should be supported
- EXPECT_TRUE(subnet->clientSupported(no_class));
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- EXPECT_TRUE(subnet->clientSupported(three_classes));
- // Let's allow only clients belongning to "bar" class.
- subnet->allowClientClass("bar");
- EXPECT_FALSE(subnet->clientSupported(no_class));
- EXPECT_FALSE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- EXPECT_TRUE(subnet->clientSupported(three_classes));
- }
- // Tests whether Subnet4 object is able to store and process properly
- // information about allowed client classes (multiple classes allowed).
- TEST(Subnet4Test, clientClassesMultiple) {
- // Create the V4 subnet.
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
- // This client does not belong to any class.
- isc::dhcp::ClientClasses no_class;
- // This client belongs to foo only.
- isc::dhcp::ClientClasses foo_class;
- foo_class.insert("foo");
- // This client belongs to bar only. I like that client.
- isc::dhcp::ClientClasses bar_class;
- bar_class.insert("bar");
- // No class restrictions defined, any client should be supported
- EXPECT_TRUE(subnet->clientSupported(no_class));
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- // Let's allow clients belongning to "bar" or "foo" class.
- subnet->allowClientClass("bar");
- subnet->allowClientClass("foo");
- // Class-less clients are to be rejected.
- EXPECT_FALSE(subnet->clientSupported(no_class));
- // Clients in foo class should be accepted.
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- // Clients in bar class should be accepted as well.
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- }
- TEST(Subnet4Test, addInvalidOption) {
- // Create the V4 subnet.
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 8, 1, 2, 3));
- // Create NULL pointer option. Attempt to add NULL option
- // should result in exception.
- OptionPtr option2;
- ASSERT_FALSE(option2);
- EXPECT_THROW(subnet->getCfgOption()->add(option2, false, "dhcp4"),
- isc::BadValue);
- }
- // This test verifies that inRange() and inPool() methods work properly.
- TEST(Subnet4Test, inRangeinPool) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.0.0"), 8, 1, 2, 3));
- // this one is in subnet
- Pool4Ptr pool1(new Pool4(IOAddress("192.2.0.0"), 16));
- subnet->addPool(pool1);
- // 192.1.1.1 belongs to the subnet...
- EXPECT_TRUE(subnet->inRange(IOAddress("192.1.1.1")));
- // ... but it does not belong to any pool within
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.1.1.1")));
- // the last address that is in range, but out of pool
- EXPECT_TRUE(subnet->inRange(IOAddress("192.1.255.255")));
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.1.255.255")));
- // the first address that is in range, in pool
- EXPECT_TRUE(subnet->inRange(IOAddress("192.2.0.0")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.0.0")));
- // let's try something in the middle as well
- EXPECT_TRUE(subnet->inRange(IOAddress("192.2.3.4")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.3.4")));
- // the last address that is in range, in pool
- EXPECT_TRUE(subnet->inRange(IOAddress("192.2.255.255")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_V4, IOAddress("192.2.255.255")));
- // the first address that is in range, but out of pool
- EXPECT_TRUE(subnet->inRange(IOAddress("192.3.0.0")));
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_V4, IOAddress("192.3.0.0")));
- }
- // This test checks if the toText() method returns text representation
- TEST(Subnet4Test, toText) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
- EXPECT_EQ("192.0.2.0/24", subnet->toText());
- }
- // This test checks if the get() method returns proper parameters
- TEST(Subnet4Test, get) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 28, 1, 2, 3));
- EXPECT_EQ("192.0.2.0", subnet->get().first.toText());
- EXPECT_EQ(28, subnet->get().second);
- }
- // Checks if last allocated address/prefix is stored/retrieved properly
- TEST(Subnet4Test, lastAllocated) {
- IOAddress addr("192.0.2.17");
- IOAddress last("192.0.2.255");
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3));
- // Check initial conditions (all should be set to the last address in range)
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_V4).toText());
- // Now set last allocated for IA
- EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_V4, addr));
- EXPECT_EQ(addr.toText(), subnet->getLastAllocated(Lease::TYPE_V4).toText());
- // No, you can't set the last allocated IPv6 address in IPv4 subnet
- EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_TA, addr), BadValue);
- EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_TA, addr), BadValue);
- EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_PD, addr), BadValue);
- }
- // Checks if the V4 is the only allowed type for Pool4 and if getPool()
- // is working properly.
- TEST(Subnet4Test, PoolType) {
- Subnet4Ptr subnet(new Subnet4(IOAddress("192.2.0.0"), 16, 1, 2, 3));
- PoolPtr pool1(new Pool4(IOAddress("192.2.1.0"), 24));
- PoolPtr pool2(new Pool4(IOAddress("192.2.2.0"), 24));
- PoolPtr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:3::"), 64));
- PoolPtr pool4(new Pool6(Lease::TYPE_TA, IOAddress("2001:db8:1:4::"), 64));
- PoolPtr pool5(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1:1::"), 64));
- // There should be no pools of any type by default
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_V4));
- // It should not be possible to ask for V6 pools in Subnet4
- EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_NA), BadValue);
- EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_TA), BadValue);
- EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_PD), BadValue);
- // Let's add a single V4 pool and check that it can be retrieved
- EXPECT_NO_THROW(subnet->addPool(pool1));
- // If there's only one IA pool, get that pool (without and with hint)
- EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_V4));
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("192.0.1.167")));
- // Let's add additional V4 pool
- EXPECT_NO_THROW(subnet->addPool(pool2));
- // Try without hints
- EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_V4));
- // Try with valid hints
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("192.2.1.5")));
- EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_V4, IOAddress("192.2.2.254")));
- // Try with bogus hints (hints should be ingored)
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_V4, IOAddress("10.1.1.1")));
- // Trying to add Pool6 to Subnet4 is a big no,no!
- EXPECT_THROW(subnet->addPool(pool3), BadValue);
- EXPECT_THROW(subnet->addPool(pool4), BadValue);
- EXPECT_THROW(subnet->addPool(pool5), BadValue);
- }
- // Tests for Subnet6
- TEST(Subnet6Test, constructor) {
- EXPECT_NO_THROW(Subnet6 subnet1(IOAddress("2001:db8:1::"), 64,
- 1, 2, 3, 4));
- EXPECT_THROW(Subnet6 subnet2(IOAddress("2001:db8:1::"), 129, 1, 2, 3, 4),
- BadValue); // invalid prefix length
- EXPECT_THROW(Subnet6 subnet3(IOAddress("192.168.0.0"), 32, 1, 2, 3, 4),
- BadValue); // IPv4 addresses are not allowed in Subnet6
- }
- // Checks that the subnet id can be either autogenerated or set to an
- // arbitrary value through the constructor.
- TEST(Subnet6Test, subnetID) {
- // Create subnet and don't specify id, so as it is autogenerated.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 1000, 2000,
- 3000, 4000));
- SubnetID id0 = subnet->getID();
- // Create another subnet and let id be autogenerated.
- subnet.reset(new Subnet6(IOAddress("2001:db8:2::"), 64, 1000, 2000,
- 3000, 4000));
- SubnetID id1 = subnet->getID();
- // The autogenerated ids must not be equal.
- EXPECT_NE(id0, id1);
- // Create third subnet but this time select an arbitrary id. The id
- // we use us the one of second subnet. That way we ensure that the
- // subnet id we provide via constructor is used and it is not
- // autogenerated - if it was autogenerated we would get id other
- // than id1 because id1 has already been used.
- subnet.reset(new Subnet6(IOAddress("2001:db8:3::"), 64, 1000, 2000,
- 3000, 4000, id1));
- EXPECT_EQ(id1, subnet->getID());
- }
- TEST(Subnet6Test, inRange) {
- Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
- EXPECT_EQ(1000, subnet.getT1());
- EXPECT_EQ(2000, subnet.getT2());
- EXPECT_EQ(3000, subnet.getPreferred());
- EXPECT_EQ(4000, subnet.getValid());
- EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:0:ffff:ffff:ffff:ffff:ffff")));
- EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::0")));
- EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::1")));
- EXPECT_TRUE(subnet.inRange(IOAddress("2001:db8:1::ffff:ffff:ffff:ffff")));
- EXPECT_FALSE(subnet.inRange(IOAddress("2001:db8:1:1::")));
- EXPECT_FALSE(subnet.inRange(IOAddress("::")));
- }
- // Checks whether the relay field has sane default and if it can
- // be changed, stored and retrieved
- TEST(Subnet6Test, relay) {
- Subnet6 subnet(IOAddress("2001:db8:1::"), 64, 1000, 2000, 3000, 4000);
- EXPECT_EQ("::", subnet.getRelayInfo().addr_.toText());
- subnet.setRelayInfo(IOAddress("2001:ffff::1"));
- EXPECT_EQ("2001:ffff::1", subnet.getRelayInfo().addr_.toText());
- }
- TEST(Subnet6Test, Pool6InSubnet6) {
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- PoolPtr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
- PoolPtr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:2::"), 64));
- PoolPtr pool3(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:3::"), 64));
- subnet->addPool(pool1);
- // If there's only one pool, get that pool
- PoolPtr mypool = subnet->getAnyPool(Lease::TYPE_NA);
- EXPECT_EQ(mypool, pool1);
- subnet->addPool(pool2);
- subnet->addPool(pool3);
- // If there are more than one pool and we didn't provide hint, we
- // should get the first pool
- mypool = subnet->getAnyPool(Lease::TYPE_NA);
- EXPECT_EQ(mypool, pool1);
- // If we provide a hint, we should get a pool that this hint belongs to
- mypool = subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:3::dead:beef"));
- EXPECT_EQ(mypool, pool3);
- }
- // Check if Subnet6 supports different types of pools properly.
- TEST(Subnet6Test, poolTypes) {
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- PoolPtr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
- PoolPtr pool2(new Pool6(Lease::TYPE_TA, IOAddress("2001:db8:1:2::"), 64));
- PoolPtr pool3(new Pool6(Lease::TYPE_PD, IOAddress("2001:db8:1:3::"), 64));
- PoolPtr pool4(new Pool6(Lease::TYPE_PD, IOAddress("3000:1::"), 64));
- PoolPtr pool5(new Pool4(IOAddress("192.0.2.0"), 24));
- // There should be no pools of any type by default
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_NA));
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_TA));
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_PD));
- // Trying to get IPv4 pool from Subnet6 is not allowed
- EXPECT_THROW(subnet->getAnyPool(Lease::TYPE_V4), BadValue);
- // Let's add a single IA pool and check that it can be retrieved
- EXPECT_NO_THROW(subnet->addPool(pool1));
- // If there's only one IA pool, get that pool
- EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_NA));
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:1::1")));
- // Check if pools of different type are not returned
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_TA));
- EXPECT_EQ(PoolPtr(), subnet->getAnyPool(Lease::TYPE_PD));
- // We ask with good hints, but wrong types, should return nothing
- EXPECT_EQ(PoolPtr(), subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:2::1")));
- EXPECT_EQ(PoolPtr(), subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:3::1")));
- // Let's add TA and PD pools
- EXPECT_NO_THROW(subnet->addPool(pool2));
- EXPECT_NO_THROW(subnet->addPool(pool3));
- // Try without hints
- EXPECT_EQ(pool1, subnet->getAnyPool(Lease::TYPE_NA));
- EXPECT_EQ(pool2, subnet->getAnyPool(Lease::TYPE_TA));
- EXPECT_EQ(pool3, subnet->getAnyPool(Lease::TYPE_PD));
- // Try with valid hints
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:1::1")));
- EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:2::1")));
- EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:3::1")));
- // Try with bogus hints (hints should be ingored)
- EXPECT_EQ(pool1, subnet->getPool(Lease::TYPE_NA, IOAddress("2001:db8:1:7::1")));
- EXPECT_EQ(pool2, subnet->getPool(Lease::TYPE_TA, IOAddress("2001:db8:1:7::1")));
- EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:7::1")));
- // Let's add a second PD pool
- EXPECT_NO_THROW(subnet->addPool(pool4));
- // Without hints, it should return the first pool
- EXPECT_EQ(pool3, subnet->getAnyPool(Lease::TYPE_PD));
- // With valid hint, it should return that hint
- EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8:1:3::1")));
- EXPECT_EQ(pool4, subnet->getPool(Lease::TYPE_PD, IOAddress("3000:1::")));
- // With invalid hint, it should return the first pool
- EXPECT_EQ(pool3, subnet->getPool(Lease::TYPE_PD, IOAddress("2001:db8::123")));
- // Adding Pool4 to Subnet6 is a big no, no!
- EXPECT_THROW(subnet->addPool(pool5), BadValue);
- }
- // Tests whether Subnet6 object is able to store and process properly
- // information about allowed client class (a single class).
- TEST(Subnet6Test, clientClasses) {
- // Create the V6 subnet.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // This client does not belong to any class.
- isc::dhcp::ClientClasses no_class;
- // This client belongs to foo only.
- isc::dhcp::ClientClasses foo_class;
- foo_class.insert("foo");
- // This client belongs to bar only. I like that client.
- isc::dhcp::ClientClasses bar_class;
- bar_class.insert("bar");
- // This client belongs to foo, bar and baz classes.
- isc::dhcp::ClientClasses three_classes;
- three_classes.insert("foo");
- three_classes.insert("bar");
- three_classes.insert("baz");
- // No class restrictions defined, any client should be supported
- EXPECT_TRUE(subnet->clientSupported(no_class));
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- EXPECT_TRUE(subnet->clientSupported(three_classes));
- // Let's allow only clients belongning to "bar" class.
- subnet->allowClientClass("bar");
- EXPECT_FALSE(subnet->clientSupported(no_class));
- EXPECT_FALSE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- EXPECT_TRUE(subnet->clientSupported(three_classes));
- }
- // Tests whether Subnet6 object is able to store and process properly
- // information about allowed client class (multiple classes allowed).
- TEST(Subnet6Test, clientClassesMultiple) {
- // Create the V6 subnet.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // This client does not belong to any class.
- isc::dhcp::ClientClasses no_class;
- // This client belongs to foo only.
- isc::dhcp::ClientClasses foo_class;
- foo_class.insert("foo");
- // This client belongs to bar only. I like that client.
- isc::dhcp::ClientClasses bar_class;
- bar_class.insert("bar");
- // No class restrictions defined, any client should be supported
- EXPECT_TRUE(subnet->clientSupported(no_class));
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- // Let's allow only clients belongning to "foo" or "bar" class.
- subnet->allowClientClass("foo");
- subnet->allowClientClass("bar");
- // Class-less clients are to be rejected.
- EXPECT_FALSE(subnet->clientSupported(no_class));
- // Clients in foo class should be accepted.
- EXPECT_TRUE(subnet->clientSupported(foo_class));
- // Clients in bar class should be accepted as well.
- EXPECT_TRUE(subnet->clientSupported(bar_class));
- }
- TEST(Subnet6Test, Subnet6_Pool6_checks) {
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // this one is in subnet
- Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8:1:1::"), 64));
- subnet->addPool(pool1);
- // this one is larger than the subnet!
- Pool6Ptr pool2(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::"), 48));
- EXPECT_THROW(subnet->addPool(pool2), BadValue);
- // this one is totally out of blue
- Pool6Ptr pool3(new Pool6(Lease::TYPE_NA, IOAddress("3000::"), 16));
- EXPECT_THROW(subnet->addPool(pool3), BadValue);
- Pool6Ptr pool4(new Pool6(Lease::TYPE_NA, IOAddress("4001:db8:1::"), 80));
- EXPECT_THROW(subnet->addPool(pool4), BadValue);
- }
- TEST(Subnet6Test, addOptions) {
- // Create as subnet to add options to it.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // Differentiate options by their codes (100-109)
- for (uint16_t code = 100; code < 110; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "dhcp6"));
- }
- // Add 7 options to another option space. The option codes partially overlap
- // with option codes that we have added to dhcp6 option space.
- for (uint16_t code = 105; code < 112; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "isc"));
- }
- // Get options from the Subnet and check if all 10 are there.
- OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
- ASSERT_TRUE(options);
- ASSERT_EQ(10, options->size());
- // Validate codes of options added to dhcp6 option space.
- uint16_t expected_code = 100;
- for (OptionContainer::const_iterator option_desc = options->begin();
- option_desc != options->end(); ++option_desc) {
- ASSERT_TRUE(option_desc->option_);
- EXPECT_EQ(expected_code, option_desc->option_->getType());
- ++expected_code;
- }
- options = subnet->getCfgOption()->getAll("isc");
- ASSERT_TRUE(options);
- ASSERT_EQ(7, options->size());
- // Validate codes of options added to isc option space.
- expected_code = 105;
- for (OptionContainer::const_iterator option_desc = options->begin();
- option_desc != options->end(); ++option_desc) {
- ASSERT_TRUE(option_desc->option_);
- EXPECT_EQ(expected_code, option_desc->option_->getType());
- ++expected_code;
- }
- // Try to get options from a non-existing option space.
- options = subnet->getCfgOption()->getAll("abcd");
- ASSERT_TRUE(options);
- EXPECT_TRUE(options->empty());
- }
- TEST(Subnet6Test, addNonUniqueOptions) {
- // Create as subnet to add options to it.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // Create a set of options with non-unique codes.
- for (int i = 0; i < 2; ++i) {
- // In the inner loop we create options with unique codes (100-109).
- for (uint16_t code = 100; code < 110; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "dhcp6"));
- }
- }
- // Sanity check that all options are there.
- OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
- ASSERT_EQ(20, options->size());
- // Use container index #1 to get the options by their codes.
- OptionContainerTypeIndex& idx = options->get<1>();
- // Look for the codes 100-109.
- for (uint16_t code = 100; code < 110; ++ code) {
- // For each code we should get two instances of options->
- std::pair<OptionContainerTypeIndex::const_iterator,
- OptionContainerTypeIndex::const_iterator> range =
- idx.equal_range(code);
- // Distance between iterators indicates how many options
- // have been retured for the particular code.
- ASSERT_EQ(2, distance(range.first, range.second));
- // Check that returned options actually have the expected option code.
- for (OptionContainerTypeIndex::const_iterator option_desc = range.first;
- option_desc != range.second; ++option_desc) {
- ASSERT_TRUE(option_desc->option_);
- EXPECT_EQ(code, option_desc->option_->getType());
- }
- }
- // Let's try to find some non-exiting option.
- const uint16_t non_existing_code = 150;
- std::pair<OptionContainerTypeIndex::const_iterator,
- OptionContainerTypeIndex::const_iterator> range =
- idx.equal_range(non_existing_code);
- // Empty set is expected.
- EXPECT_EQ(0, distance(range.first, range.second));
- }
- TEST(Subnet6Test, addPersistentOption) {
- // Create as subnet to add options to it.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // Add 10 options to the subnet with option codes 100 - 109.
- for (uint16_t code = 100; code < 110; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- // We create 10 options and want some of them to be flagged
- // persistent and some non-persistent. Persistent options are
- // those that server sends to clients regardless if they ask
- // for them or not. We pick 3 out of 10 options and mark them
- // non-persistent and 7 other options persistent.
- // Code values: 102, 105 and 108 are divisible by 3
- // and options with these codes will be flagged non-persistent.
- // Options with other codes will be flagged persistent.
- bool persistent = (code % 3) ? true : false;
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, persistent, "dhcp6"));
- }
- // Get added options from the subnet.
- OptionContainerPtr options = subnet->getCfgOption()->getAll("dhcp6");
- // options->get<2> returns reference to container index #2. This
- // index is used to access options by the 'persistent' flag.
- OptionContainerPersistIndex& idx = options->get<2>();
- // Get all persistent options->
- std::pair<OptionContainerPersistIndex::const_iterator,
- OptionContainerPersistIndex::const_iterator> range_persistent =
- idx.equal_range(true);
- // 3 out of 10 options have been flagged persistent.
- ASSERT_EQ(7, distance(range_persistent.first, range_persistent.second));
- // Get all non-persistent options->
- std::pair<OptionContainerPersistIndex::const_iterator,
- OptionContainerPersistIndex::const_iterator> range_non_persistent =
- idx.equal_range(false);
- // 7 out of 10 options have been flagged persistent.
- ASSERT_EQ(3, distance(range_non_persistent.first, range_non_persistent.second));
- }
- TEST(Subnet6Test, getOptions) {
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 56, 1, 2, 3, 4));
- // Add 10 options to a "dhcp6" option space in the subnet.
- for (uint16_t code = 100; code < 110; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "dhcp6"));
- }
- // Check that we can get each added option descriptor using
- // individually.
- for (uint16_t code = 100; code < 110; ++code) {
- std::ostringstream stream;
- // First, try the invalid option space name.
- OptionDescriptor desc = subnet->getCfgOption()->get("isc", code);
- // Returned descriptor should contain NULL option ptr.
- EXPECT_FALSE(desc.option_);
- // Now, try the valid option space.
- desc = subnet->getCfgOption()->get("dhcp6", code);
- // Test that the option code matches the expected code.
- ASSERT_TRUE(desc.option_);
- EXPECT_EQ(code, desc.option_->getType());
- }
- }
- TEST(Subnet6Test, addVendorOption) {
- // Create as subnet to add options to it.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- // Differentiate options by their codes (100-109)
- for (uint16_t code = 100; code < 110; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "vendor-12345678"));
- }
- // Add 7 options to another option space. The option codes partially overlap
- // with option codes that we have added to dhcp6 option space.
- for (uint16_t code = 105; code < 112; ++code) {
- OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
- ASSERT_NO_THROW(subnet->getCfgOption()->add(option, false, "vendor-87654321"));
- }
- // Get options from the Subnet and check if all 10 are there.
- OptionContainerPtr options = subnet->getCfgOption()->getAll(12345678);
- ASSERT_TRUE(options);
- ASSERT_EQ(10, options->size());
- // Validate codes of options added to dhcp6 option space.
- uint16_t expected_code = 100;
- for (OptionContainer::const_iterator option_desc = options->begin();
- option_desc != options->end(); ++option_desc) {
- ASSERT_TRUE(option_desc->option_);
- EXPECT_EQ(expected_code, option_desc->option_->getType());
- ++expected_code;
- }
- options = subnet->getCfgOption()->getAll(87654321);
- ASSERT_TRUE(options);
- ASSERT_EQ(7, options->size());
- // Validate codes of options added to isc option space.
- expected_code = 105;
- for (OptionContainer::const_iterator option_desc = options->begin();
- option_desc != options->end(); ++option_desc) {
- ASSERT_TRUE(option_desc->option_);
- EXPECT_EQ(expected_code, option_desc->option_->getType());
- ++expected_code;
- }
- // Try to get options from a non-existing option space.
- options = subnet->getCfgOption()->getAll(1111111);
- ASSERT_TRUE(options);
- EXPECT_TRUE(options->empty());
- }
- // This test verifies that inRange() and inPool() methods work properly.
- TEST(Subnet6Test, inRangeinPool) {
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));
- // this one is in subnet
- Pool6Ptr pool1(new Pool6(Lease::TYPE_NA, IOAddress("2001:db8::10"),
- IOAddress("2001:db8::20")));
- subnet->addPool(pool1);
- // 192.1.1.1 belongs to the subnet...
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::1")));
- // ... but it does not belong to any pool within
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::1")));
- // the last address that is in range, but out of pool
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::f")));
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::f")));
- // the first address that is in range, in pool
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::10")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::10")));
- // let's try something in the middle as well
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::18")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::18")));
- // the last address that is in range, in pool
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::20")));
- EXPECT_TRUE (subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::20")));
- // the first address that is in range, but out of pool
- EXPECT_TRUE(subnet->inRange(IOAddress("2001:db8::21")));
- EXPECT_FALSE(subnet->inPool(Lease::TYPE_NA, IOAddress("2001:db8::21")));
- }
- // This test checks if the toText() method returns text representation
- TEST(Subnet6Test, toText) {
- Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
- EXPECT_EQ("2001:db8::/32", subnet.toText());
- }
- // This test checks if the get() method returns proper parameters
- TEST(Subnet6Test, get) {
- Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
- EXPECT_EQ("2001:db8::", subnet.get().first.toText());
- EXPECT_EQ(32, subnet.get().second);
- }
- // This trivial test checks if interface name is stored properly
- // in Subnet6 objects.
- TEST(Subnet6Test, iface) {
- Subnet6 subnet(IOAddress("2001:db8::"), 32, 1, 2, 3, 4);
- EXPECT_TRUE(subnet.getIface().empty());
- subnet.setIface("en1");
- EXPECT_EQ("en1", subnet.getIface());
- }
- // This trivial test checks if the interface-id option can be set and
- // later retrieved for a subnet6 object.
- TEST(Subnet6Test, interfaceId) {
- // Create as subnet to add options to it.
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 56, 1, 2, 3, 4));
- EXPECT_FALSE(subnet->getInterfaceId());
- OptionPtr option(new Option(Option::V6, D6O_INTERFACE_ID, OptionBuffer(10, 0xFF)));
- subnet->setInterfaceId(option);
- EXPECT_EQ(option, subnet->getInterfaceId());
- }
- // Checks if last allocated address/prefix is stored/retrieved properly
- TEST(Subnet6Test, lastAllocated) {
- IOAddress ia("2001:db8:1::1");
- IOAddress ta("2001:db8:1::abcd");
- IOAddress pd("2001:db8:1::1234:5678");
- IOAddress last("2001:db8:1::ffff:ffff:ffff:ffff");
- Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8:1::"), 64, 1, 2, 3, 4));
- // Check initial conditions (all should be set to the last address in range)
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
- // Now set last allocated for IA
- EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_NA, ia));
- EXPECT_EQ(ia.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
- // TA and PD should be unchanged
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
- EXPECT_EQ(last.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
- // Now set TA and PD
- EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_TA, ta));
- EXPECT_NO_THROW(subnet->setLastAllocated(Lease::TYPE_PD, pd));
- EXPECT_EQ(ia.toText(), subnet->getLastAllocated(Lease::TYPE_NA).toText());
- EXPECT_EQ(ta.toText(), subnet->getLastAllocated(Lease::TYPE_TA).toText());
- EXPECT_EQ(pd.toText(), subnet->getLastAllocated(Lease::TYPE_PD).toText());
- // No, you can't set the last allocated IPv4 address in IPv6 subnet
- EXPECT_THROW(subnet->setLastAllocated(Lease::TYPE_V4, ia), BadValue);
- }
- };
|