|
@@ -226,7 +226,7 @@ TEST_F(AllocEngine4Test, allocateLease4Nulls) {
|
|
|
ASSERT_TRUE(engine);
|
|
|
|
|
|
// Allocations without subnet are not allowed
|
|
|
- AllocEngine::ClientContext4 ctx1(SubnetPtr(), clientid_, hwaddr_,
|
|
|
+ AllocEngine::ClientContext4 ctx1(Subnet4Ptr(), clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
Lease4Ptr lease = engine->allocateLease4(ctx1);
|
|
@@ -598,6 +598,7 @@ TEST_F(AllocEngine4Test, reservedAddressNoHint) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
ASSERT_TRUE(lease);
|
|
@@ -634,6 +635,7 @@ TEST_F(AllocEngine4Test,reservedAddressNoHintFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
ASSERT_TRUE(lease);
|
|
@@ -671,6 +673,7 @@ TEST_F(AllocEngine4Test, reservedAddressHint) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.234"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr lease = engine.allocateLease4(ctx1);
|
|
|
|
|
|
// The client requested a different address than reserved, so
|
|
@@ -683,6 +686,7 @@ TEST_F(AllocEngine4Test, reservedAddressHint) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.123"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
lease = engine.allocateLease4(ctx2);
|
|
|
ASSERT_TRUE(lease);
|
|
|
EXPECT_EQ("192.0.2.123", lease->addr_.toText());
|
|
@@ -718,6 +722,7 @@ TEST_F(AllocEngine4Test, reservedAddressHintFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.234"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
ASSERT_TRUE(lease);
|
|
@@ -761,6 +766,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLease) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.123"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
@@ -808,6 +814,7 @@ TEST_F(AllocEngine4Test, reservedAddressHijacked) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.123"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
// The lease is allocated to someone else, so the allocation should not
|
|
|
// succeed.
|
|
@@ -825,6 +832,7 @@ TEST_F(AllocEngine4Test, reservedAddressHijacked) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
allocated_lease = engine.allocateLease4(ctx2);
|
|
|
ASSERT_FALSE(allocated_lease);
|
|
|
EXPECT_FALSE(ctx2.old_lease_);
|
|
@@ -863,6 +871,7 @@ TEST_F(AllocEngine4Test, reservedAddressHijackedFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.123"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
|
|
|
// The allocation engine should return a lease but for a different address
|
|
@@ -878,6 +887,7 @@ TEST_F(AllocEngine4Test, reservedAddressHijackedFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
allocated_lease = engine.allocateLease4(ctx2);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
@@ -918,6 +928,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLeaseInvalidHint) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.102"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
ASSERT_FALSE(allocated_lease);
|
|
|
ASSERT_FALSE(ctx1.old_lease_);
|
|
@@ -968,6 +979,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLeaseFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.102"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
|
|
|
// Server should offer a lease for a reserved address.
|
|
@@ -986,6 +998,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLeaseFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.101"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
allocated_lease = engine.allocateLease4(ctx2);
|
|
|
|
|
|
// The server should offer the lease, but not for the address that
|
|
@@ -1026,6 +1039,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLeaseNoHint) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
// The reserved address should be allocated.
|
|
@@ -1075,6 +1089,7 @@ TEST_F(AllocEngine4Test, reservedAddressExistingLeaseNoHintFakeAllocation) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
// The server should offer the reserved address.
|
|
@@ -1137,6 +1152,7 @@ TEST_F(AllocEngine4Test, reservedAddressConflictResolution) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, ClientIdPtr(), hwaddr2_,
|
|
|
IOAddress("192.0.2.101"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr offered_lease = engine.allocateLease4(ctx1);
|
|
|
ASSERT_FALSE(offered_lease);
|
|
|
|
|
@@ -1146,6 +1162,7 @@ TEST_F(AllocEngine4Test, reservedAddressConflictResolution) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.101"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
ASSERT_FALSE(engine.allocateLease4(ctx2));
|
|
|
|
|
|
ASSERT_FALSE(ctx2.old_lease_);
|
|
@@ -1156,6 +1173,7 @@ TEST_F(AllocEngine4Test, reservedAddressConflictResolution) {
|
|
|
AllocEngine::ClientContext4 ctx3(subnet_, clientid_, hwaddr_,
|
|
|
IOAddress("192.0.2.101"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx3);
|
|
|
offered_lease = engine.allocateLease4(ctx3);
|
|
|
ASSERT_TRUE(offered_lease);
|
|
|
EXPECT_NE(offered_lease->addr_.toText(), "192.0.2.101");
|
|
@@ -1166,6 +1184,7 @@ TEST_F(AllocEngine4Test, reservedAddressConflictResolution) {
|
|
|
AllocEngine::ClientContext4 ctx4(subnet_, clientid_, hwaddr_,
|
|
|
offered_lease->addr_, false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx4);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx4);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
@@ -1176,6 +1195,7 @@ TEST_F(AllocEngine4Test, reservedAddressConflictResolution) {
|
|
|
AllocEngine::ClientContext4 ctx5(subnet_, ClientIdPtr(), hwaddr2_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", true);
|
|
|
+ AllocEngine::findReservation(ctx5);
|
|
|
offered_lease = engine.allocateLease4(ctx5);
|
|
|
|
|
|
ASSERT_TRUE(offered_lease);
|
|
@@ -1210,6 +1230,7 @@ TEST_F(AllocEngine4Test, reservedAddressVsDynamicPool) {
|
|
|
AllocEngine::ClientContext4 ctx(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
@@ -1236,7 +1257,8 @@ TEST_F(AllocEngine4Test, reservedAddressHintUsedByOtherClient) {
|
|
|
// Different client is requesting this address.
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
IOAddress("192.0.2.100"), false, false,
|
|
|
- "", false);
|
|
|
+ "", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
|
|
|
// The client should get no lease (DHCPNAK).
|
|
@@ -1246,7 +1268,8 @@ TEST_F(AllocEngine4Test, reservedAddressHintUsedByOtherClient) {
|
|
|
// if is sending a DHCPDISCOVER (fake allocation is true).
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
IOAddress("192.0.2.100"), false, false,
|
|
|
- "", true);
|
|
|
+ "", true);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
allocated_lease = engine.allocateLease4(ctx2);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
@@ -1263,7 +1286,7 @@ TEST_F(AllocEngine4Test, reservedAddressShortPool) {
|
|
|
// Create short pool with only one address.
|
|
|
initSubnet(IOAddress("192.0.2.100"), IOAddress("192.0.2.100"));
|
|
|
// Reserve the address for a different client.
|
|
|
- HostPtr host(new Host(&hwaddr2_->hwaddr_[0], hwaddr_->hwaddr_.size(),
|
|
|
+ HostPtr host(new Host(&hwaddr2_->hwaddr_[0], hwaddr2_->hwaddr_.size(),
|
|
|
Host::IDENT_HWADDR, subnet_->getID(),
|
|
|
SubnetID(0), IOAddress("192.0.2.100")));
|
|
|
CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
|
|
@@ -1274,6 +1297,7 @@ TEST_F(AllocEngine4Test, reservedAddressShortPool) {
|
|
|
AllocEngine::ClientContext4 ctx1(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx1);
|
|
|
Lease4Ptr allocated_lease = engine.allocateLease4(ctx1);
|
|
|
|
|
|
EXPECT_FALSE(allocated_lease);
|
|
@@ -1286,12 +1310,119 @@ TEST_F(AllocEngine4Test, reservedAddressShortPool) {
|
|
|
AllocEngine::ClientContext4 ctx2(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
IOAddress("0.0.0.0"), false, false,
|
|
|
"", false);
|
|
|
+ AllocEngine::findReservation(ctx2);
|
|
|
allocated_lease = engine.allocateLease4(ctx2);
|
|
|
|
|
|
ASSERT_TRUE(allocated_lease);
|
|
|
EXPECT_EQ("192.0.2.100", allocated_lease->addr_.toText());
|
|
|
}
|
|
|
|
|
|
+// This test checks that the AllocEngine allocates an address from the
|
|
|
+// dynamic pool if the client's reservation is made for a hostname but
|
|
|
+// not for an address.
|
|
|
+TEST_F(AllocEngine4Test, reservedHostname) {
|
|
|
+ AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
|
|
|
+
|
|
|
+ // Create a reservation for a hostname. Address is set to 0 which
|
|
|
+ // indicates that there is no reservation.
|
|
|
+ HostPtr host(new Host(&hwaddr_->hwaddr_[0], hwaddr_->hwaddr_.size(),
|
|
|
+ Host::IDENT_HWADDR, subnet_->getID(),
|
|
|
+ SubnetID(0), IOAddress::IPV4_ZERO_ADDRESS(),
|
|
|
+ "foo.example.org"));
|
|
|
+ CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
|
|
|
+ CfgMgr::instance().commit();
|
|
|
+
|
|
|
+ // Try to allocate a lease.
|
|
|
+ AllocEngine::ClientContext4 ctx(subnet_, ClientIdPtr(), hwaddr_,
|
|
|
+ IOAddress::IOAddress("192.0.2.109"), false, false,
|
|
|
+ "foo.example.org", true);
|
|
|
+ AllocEngine::findReservation(ctx);
|
|
|
+ Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
|
|
|
+ ASSERT_TRUE(allocated_lease);
|
|
|
+ ASSERT_FALSE(allocated_lease->addr_.isV4Zero());
|
|
|
+ ASSERT_EQ("192.0.2.109", allocated_lease->addr_.toText());
|
|
|
+
|
|
|
+ ctx.requested_address_ = allocated_lease->addr_;
|
|
|
+ ctx.fake_allocation_ = false;
|
|
|
+ allocated_lease = engine.allocateLease4(ctx);
|
|
|
+ ASSERT_TRUE(allocated_lease);
|
|
|
+ EXPECT_EQ("192.0.2.109", allocated_lease->addr_.toText());
|
|
|
+}
|
|
|
+
|
|
|
+// This test checks that the AllocEngine::findReservation method finds
|
|
|
+// and returns host reservation for the DHCPv4 client using the data from
|
|
|
+// the client context. If the host reservation can't be found, it sets
|
|
|
+// the value of NULL in the host_ field of the client context.
|
|
|
+TEST_F(AllocEngine4Test, findReservation) {
|
|
|
+ // Create the instance of the allocation engine.
|
|
|
+ AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
|
|
|
+
|
|
|
+ // Context is required to call the AllocEngine::findReservation.
|
|
|
+ AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
|
|
|
+ IOAddress("0.0.0.0"), false, false,
|
|
|
+ "", false);
|
|
|
+
|
|
|
+ // There is no reservation in the database so no host should be
|
|
|
+ // retruned.
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_FALSE(ctx.host_);
|
|
|
+
|
|
|
+ // Create a reservation for the client.
|
|
|
+ HostPtr host(new Host(&hwaddr_->hwaddr_[0], hwaddr_->hwaddr_.size(),
|
|
|
+ Host::IDENT_HWADDR, subnet_->getID(),
|
|
|
+ SubnetID(0), IOAddress("192.0.2.100")));
|
|
|
+ CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
|
|
|
+ CfgMgr::instance().commit();
|
|
|
+
|
|
|
+ // This time the reservation should be returned.
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_TRUE(ctx.host_);
|
|
|
+ EXPECT_EQ(ctx.host_->getIPv4Reservation(), host->getIPv4Reservation());
|
|
|
+
|
|
|
+ // If the host reservation mode for the subnet is disabled, the
|
|
|
+ // host should not be returned, even though it exists in the
|
|
|
+ // host database.
|
|
|
+ subnet_->setHostReservationMode(Subnet::HR_DISABLED);
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_FALSE(ctx.host_);
|
|
|
+
|
|
|
+ // Check the third possible reservation mode.
|
|
|
+ subnet_->setHostReservationMode(Subnet::HR_OUT_OF_POOL);
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_TRUE(ctx.host_);
|
|
|
+ EXPECT_EQ(ctx.host_->getIPv4Reservation(), host->getIPv4Reservation());
|
|
|
+
|
|
|
+ // This time use the client identifier to search for the host.
|
|
|
+ host.reset(new Host(&clientid_->getClientId()[0],
|
|
|
+ clientid_->getClientId().size(),
|
|
|
+ Host::IDENT_DUID, subnet_->getID(),
|
|
|
+ SubnetID(0), IOAddress("192.0.2.101")));
|
|
|
+ CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
|
|
|
+ CfgMgr::instance().commit();
|
|
|
+
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_TRUE(ctx.host_);
|
|
|
+ EXPECT_EQ(ctx.host_->getIPv4Reservation(), host->getIPv4Reservation());
|
|
|
+
|
|
|
+ // Remove the subnet. Subnet id is required to find host reservations, so
|
|
|
+ // if it is set to NULL, no reservation should be returned
|
|
|
+ ctx.subnet_.reset();
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_FALSE(ctx.host_);
|
|
|
+
|
|
|
+ // The same if there is a mismatch of the subnet id between the reservation
|
|
|
+ // and the context.
|
|
|
+ ctx.subnet_ = subnet_;
|
|
|
+ host.reset(new Host(&hwaddr_->hwaddr_[0], hwaddr_->hwaddr_.size(),
|
|
|
+ Host::IDENT_HWADDR, subnet_->getID() + 1,
|
|
|
+ SubnetID(0), IOAddress("192.0.2.100")));
|
|
|
+ CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
|
|
|
+ CfgMgr::instance().commit();
|
|
|
+
|
|
|
+ ASSERT_NO_THROW(engine.findReservation(ctx));
|
|
|
+ EXPECT_FALSE(ctx.host_);
|
|
|
+}
|
|
|
+
|
|
|
}; // namespace test
|
|
|
}; // namespace dhcp
|
|
|
}; // namespace isc
|