Skip to content

Commit cdb16bd

Browse files
tmarkwalderRazvan Becheriu
authored and
Razvan Becheriu
committed
[#3576] Addressed review comments
Moved UT, cleaned up test JSON
1 parent 80cd3eb commit cdb16bd

File tree

3 files changed

+104
-119
lines changed

3 files changed

+104
-119
lines changed

src/bin/dhcp4/tests/classify_unittest.cc

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <config.h>
88
#include <asiolink/io_address.h>
99
#include <dhcp/dhcp4.h>
10+
#include <dhcp/option_int_array.h>
1011
#include <dhcp/testutils/iface_mgr_test_config.h>
1112
#include <dhcp4/tests/dhcp4_client.h>
1213
#include <dhcp/option_int.h>
@@ -1431,4 +1432,98 @@ TEST_F(ClassifyTest, earlyDrop) {
14311432
EXPECT_TRUE(client2.getContext().response_);
14321433
}
14331434

1435+
// Checks that sub-class options have precedence of template class options
1436+
TEST_F(ClassifyTest, subClassPrecedence) {
1437+
IfaceMgrTestConfig test_config(true);
1438+
IfaceMgr::instance().openSockets4();
1439+
1440+
NakedDhcpv4Srv srv(0);
1441+
1442+
string config = R"^(
1443+
{
1444+
"interfaces-config": {
1445+
"interfaces": [ "*" ]
1446+
},
1447+
"rebind-timer": 2000,
1448+
"renew-timer": 1000,
1449+
"valid-lifetime": 4000,
1450+
"subnet4": [{
1451+
"id": 1,
1452+
"subnet": "192.0.2.0/24",
1453+
"pools": [{
1454+
"pool": "192.0.2.1 - 192.0.2.100"
1455+
}]
1456+
}],
1457+
"option-def": [{
1458+
"name": "opt1",
1459+
"code": 249,
1460+
"type": "string"
1461+
},{
1462+
"name": "opt2",
1463+
"code": 250,
1464+
"type": "string"
1465+
}],
1466+
"client-classes": [{
1467+
"name": "template-client-id",
1468+
"template-test": "substring(option[61].hex,0,3)",
1469+
"option-data": [{
1470+
"name": "opt1",
1471+
"data": "template one"
1472+
},{
1473+
"name": "opt2",
1474+
"data": "template two"
1475+
}]
1476+
},{
1477+
"name": "SPAWN_template-client-id_111",
1478+
"option-data": [{
1479+
"name": "opt2",
1480+
"data": "spawn two"
1481+
}]
1482+
}]
1483+
}
1484+
)^";
1485+
1486+
// Configure DHCP server.
1487+
configure(config, srv);
1488+
1489+
// Create packets with enough to select the subnet
1490+
auto id = ClientId::fromText("31:31:31");
1491+
OptionPtr clientid = (OptionPtr(new Option(Option::V4,
1492+
DHO_DHCP_CLIENT_IDENTIFIER,
1493+
id->getClientId())));
1494+
1495+
Pkt4Ptr query1(new Pkt4(DHCPDISCOVER, 1234));
1496+
query1->setRemoteAddr(IOAddress("192.0.2.1"));
1497+
query1->addOption(clientid);
1498+
query1->setIface("eth1");
1499+
query1->setIndex(ETH1_INDEX);
1500+
1501+
// Create and add a PRL option to the first 2 queries
1502+
OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
1503+
DHO_DHCP_PARAMETER_REQUEST_LIST));
1504+
prl->addValue(249);
1505+
prl->addValue(250);
1506+
query1->addOption(prl);
1507+
1508+
// Classify packets
1509+
srv.classifyPacket(query1);
1510+
1511+
// Verify class membership is as expected.
1512+
EXPECT_TRUE(query1->inClass("template-client-id"));
1513+
EXPECT_TRUE(query1->inClass("SPAWN_template-client-id_111"));
1514+
1515+
// Process query
1516+
Pkt4Ptr response1 = srv.processDiscover(query1);
1517+
1518+
// Verify that opt1 is inherited from the template.
1519+
OptionPtr opt = response1->getOption(249);
1520+
ASSERT_TRUE(opt);
1521+
EXPECT_EQ(opt->toString(), "template one");
1522+
1523+
// Verify that for opt2 subclass overrides the template.
1524+
opt = response1->getOption(250);
1525+
ASSERT_TRUE(opt);
1526+
EXPECT_EQ(opt->toString(), "spawn two");
1527+
}
1528+
14341529
} // end of anonymous namespace

src/bin/dhcp4/tests/dhcp4_srv_unittest.cc

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -6848,113 +6848,6 @@ TEST_F(StashAgentOptionTest, badHwareAddress) {
68486848
EXPECT_FALSE(query_->inClass("STASH_AGENT_OPTIONS"));
68496849
}
68506850

6851-
// Checks that sub-class options have precedence of template class options
6852-
TEST_F(Dhcpv4SrvTest, subClassPrecedence) {
6853-
IfaceMgrTestConfig test_config(true);
6854-
IfaceMgr::instance().openSockets4();
6855-
6856-
NakedDhcpv4Srv srv(0);
6857-
6858-
string config = R"^(
6859-
{
6860-
"interfaces-config": { "interfaces": [ "*" ] },
6861-
"rebind-timer": 2000,
6862-
"renew-timer": 1000,
6863-
"valid-lifetime": 4000,
6864-
"subnet4": [
6865-
{
6866-
"id": 1,
6867-
"subnet": "192.0.2.0/24",
6868-
"pools": [{ "pool": "192.0.2.1 - 192.0.2.100" }]
6869-
}],
6870-
"option-def": [{
6871-
"name": "opt1",
6872-
"code": 249,
6873-
"type": "string"
6874-
},
6875-
{
6876-
"name": "opt2",
6877-
"code": 250,
6878-
"type": "string"
6879-
6880-
}],
6881-
"client-classes": [
6882-
{
6883-
"name": "template-client-id",
6884-
"template-test": "substring(option[61].hex,0,3)",
6885-
"option-data": [
6886-
{
6887-
"name": "opt1",
6888-
"data": "template one"
6889-
},
6890-
{
6891-
"name": "opt2",
6892-
"data": "template two"
6893-
}]
6894-
},
6895-
{
6896-
"name": "SPAWN_template-client-id_111",
6897-
"option-data": [
6898-
{
6899-
"name": "opt2",
6900-
"data": "spawn two"
6901-
}]
6902-
}]
6903-
}
6904-
)^";
6905-
6906-
ConstElementPtr json;
6907-
ASSERT_NO_THROW(json = parseDHCP4(config));
6908-
ConstElementPtr status;
6909-
6910-
// Configure the server and make sure the config is accepted
6911-
EXPECT_NO_THROW(status = Dhcpv4SrvTest::configure(srv, json));
6912-
ASSERT_TRUE(status);
6913-
comment_ = config::parseAnswer(rcode_, status);
6914-
ASSERT_EQ(0, rcode_);
6915-
6916-
CfgMgr::instance().commit();
6917-
6918-
// Create packets with enough to select the subnet
6919-
auto id = ClientId::fromText("31:31:31");
6920-
OptionPtr clientid = (OptionPtr(new Option(Option::V4,
6921-
DHO_DHCP_CLIENT_IDENTIFIER,
6922-
id->getClientId())));
6923-
6924-
Pkt4Ptr query1(new Pkt4(DHCPDISCOVER, 1234));
6925-
query1->setRemoteAddr(IOAddress("192.0.2.1"));
6926-
query1->addOption(clientid);
6927-
query1->setIface("eth1");
6928-
query1->setIndex(ETH1_INDEX);
6929-
6930-
// Create and add a PRL option to the first 2 queries
6931-
OptionUint8ArrayPtr prl(new OptionUint8Array(Option::V4,
6932-
DHO_DHCP_PARAMETER_REQUEST_LIST));
6933-
prl->addValue(249);
6934-
prl->addValue(250);
6935-
query1->addOption(prl);
6936-
6937-
// Classify packets
6938-
srv.classifyPacket(query1);
6939-
6940-
// Verify class membership is as expected.
6941-
EXPECT_TRUE(query1->inClass("template-client-id"));
6942-
EXPECT_TRUE(query1->inClass("SPAWN_template-client-id_111"));
6943-
6944-
// Process query
6945-
Pkt4Ptr response1 = srv.processDiscover(query1);
6946-
6947-
// Verify that opt1 is inherited from the template.
6948-
OptionPtr opt = response1->getOption(249);
6949-
ASSERT_TRUE(opt);
6950-
EXPECT_EQ(opt->toString(), "template one");
6951-
6952-
// Verify that for opt2 subclass overrides the template.
6953-
opt = response1->getOption(250);
6954-
ASSERT_TRUE(opt);
6955-
EXPECT_EQ(opt->toString(), "spawn two");
6956-
}
6957-
69586851
/// @todo: lease ownership
69596852

69606853
/// @todo: Implement proper tests for MySQL lease/host database,

src/bin/dhcp6/tests/classify_unittests.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,8 +2645,10 @@ TEST_F(ClassifyTest, subClassPrecedence) {
26452645
"rebind-timer": 2000,
26462646
"renew-timer": 1000,
26472647
"valid-lifetime": 4000,
2648-
"subnet6": [
2649-
{ "pools": [ { "pool": "2001:db8:1::/64" } ],
2648+
"subnet6": [{
2649+
"pools": [{
2650+
"pool": "2001:db8:1::/64"
2651+
}],
26502652
"subnet": "2001:db8:1::/48",
26512653
"id": 1,
26522654
"interface": "eth1",
@@ -2655,31 +2657,26 @@ TEST_F(ClassifyTest, subClassPrecedence) {
26552657
"name": "opt1",
26562658
"code": 1249,
26572659
"type": "string"
2658-
},
2659-
{
2660+
},{
26602661
"name": "opt2",
26612662
"code": 1250,
26622663
"type": "string"
26632664
26642665
}],
2665-
"client-classes": [
2666-
{
2666+
"client-classes": [{
26672667
"name": "template-client-id",
26682668
"template-test": "substring(option[1].hex,0,3)",
2669-
"option-data": [
2670-
{
2669+
"option-data": [{
26712670
"name": "opt1",
26722671
"data": "template one"
26732672
},
26742673
{
26752674
"name": "opt2",
26762675
"data": "template two"
26772676
}]
2678-
},
2679-
{
2677+
},{
26802678
"name": "SPAWN_template-client-id_def",
2681-
"option-data": [
2682-
{
2679+
"option-data": [{
26832680
"name": "opt2",
26842681
"data": "spawn two"
26852682
}]

0 commit comments

Comments
 (0)