diff options
author | Francis Dupont <fdupont@isc.org> | 2018-06-22 11:37:22 +0200 |
---|---|---|
committer | Francis Dupont <fdupont@isc.org> | 2018-06-22 11:37:22 +0200 |
commit | b14d388e5477322cb7597ab64dd8ffa0bd4a931e (patch) | |
tree | c5bf447e5414a5a827eb10f443efcbe615c0db9c /src/hooks | |
parent | [5584] Checkpoint: core code and test done (diff) | |
download | kea-b14d388e5477322cb7597ab64dd8ffa0bd4a931e.tar.xz kea-b14d388e5477322cb7597ab64dd8ffa0bd4a931e.zip |
[5584] Checkpoint: more updates
Diffstat (limited to 'src/hooks')
-rw-r--r-- | src/hooks/dhcp/lease_cmds/lease_parser.cc | 16 | ||||
-rw-r--r-- | src/hooks/dhcp/lease_cmds/lease_parser.h | 6 | ||||
-rw-r--r-- | src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc | 99 |
3 files changed, 115 insertions, 6 deletions
diff --git a/src/hooks/dhcp/lease_cmds/lease_parser.cc b/src/hooks/dhcp/lease_cmds/lease_parser.cc index 4a457d8a77..6198205c62 100644 --- a/src/hooks/dhcp/lease_cmds/lease_parser.cc +++ b/src/hooks/dhcp/lease_cmds/lease_parser.cc @@ -110,6 +110,13 @@ Lease4Parser::parse(ConstSrvConfigPtr& cfg, "values are: 0 (default), 1 (declined) and 2 (expired-reclaimed)"); } + // Handle user context. + ConstElementPtr ctx = lease_info->get("user-context"); + if (ctx && (ctx->getType() != Element::map)) { + isc_throw(BadValue, "Invalid user context '" << ctx->str() + << "' is not a JSON map."); + } + // Let's fabricate some data and we're ready to go. uint32_t t1 = subnet->getT1(); uint32_t t2 = subnet->getT2(); @@ -118,6 +125,7 @@ Lease4Parser::parse(ConstSrvConfigPtr& cfg, cltt, subnet_id, fqdn_fwd, fqdn_rev, hostname)); l->state_ = state; + l->setContext(ctx); // Retrieve the optional flag indicating if the lease must be created when it // doesn't exist during the update. @@ -251,6 +259,13 @@ Lease6Parser::parse(ConstSrvConfigPtr& cfg, "values are: 0 (default), 1 (declined) and 2 (expired-reclaimed)"); } + // Handle user context. + ConstElementPtr ctx = lease_info->get("user-context"); + if (ctx && (ctx->getType() != Element::map)) { + isc_throw(BadValue, "Invalid user context '" << ctx->str() + << "' is not a JSON map."); + } + // Let's fabricate some data and we're ready to go. uint32_t t1 = subnet->getT1(); uint32_t t2 = subnet->getT2(); @@ -260,6 +275,7 @@ Lease6Parser::parse(ConstSrvConfigPtr& cfg, hwaddr_ptr, prefix_len)); l->cltt_ = cltt; l->state_ = state; + l->setContext(ctx); // Retrieve the optional flag indicating if the lease must be created when it // doesn't exist during the update. diff --git a/src/hooks/dhcp/lease_cmds/lease_parser.h b/src/hooks/dhcp/lease_cmds/lease_parser.h index 8b72c0018a..2220d49933 100644 --- a/src/hooks/dhcp/lease_cmds/lease_parser.h +++ b/src/hooks/dhcp/lease_cmds/lease_parser.h @@ -29,7 +29,8 @@ namespace lease_cmds { /// "fqdn-fwd": true, /// "fqdn-rev": true, /// "hostname": "myhost.example.org", -/// "state": 0 +/// "state": 0, +/// "user-context": { \"version\": 1 } /// } class Lease4Parser : public isc::data::SimpleParser { public: @@ -67,7 +68,8 @@ public: /// "fqdn-fwd": true, /// "fqdn-rev": true, /// "hostname": "myhost.example.org", -/// "state": 0 +/// "state": 0, +/// "user-context": { \"version\": 1 } /// } /// It expects the input data to use the following format: diff --git a/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc b/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc index 04b3252695..a43d4e8cc5 100644 --- a/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc +++ b/src/hooks/dhcp/lease_cmds/tests/lease_cmds_unittest.cc @@ -14,8 +14,8 @@ #include <dhcpsrv/cfgmgr.h> #include <cc/command_interpreter.h> #include <cc/data.h> +#include <testutils/user_context_utils.h> #include <gtest/gtest.h> -#include <cc/data.h> #include <errno.h> using namespace std; @@ -25,6 +25,7 @@ using namespace isc::config; using namespace isc::data; using namespace isc::dhcp; using namespace isc::asiolink; +using namespace isc::test; namespace { @@ -472,6 +473,22 @@ public: // Check that there are no v4 specific fields. EXPECT_FALSE(l->contains("client-id")); } + + /// @brief Checks if specified response contains user context + /// + /// @param lease Element tree that represents a lease + /// @param expected expected user context in textual form + void checkContext(ConstElementPtr lease, std::string expected) { + ASSERT_TRUE(lease); + ConstElementPtr moved = moveComments(lease); + ConstElementPtr ctx = moved->get("user-context"); + if (!expected.empty()) { + ASSERT_TRUE(ctx); + EXPECT_EQ(expected, ctx->str()); + } else { + EXPECT_FALSE(ctx); + } + } }; // Simple test that checks the library really registers the commands. @@ -611,6 +628,20 @@ TEST_F(LeaseCmdsTest, Lease4AddBadParams) { exp_rsp = "Invalid state value: 123, supported values are: 0 (default), 1 " "(declined) and 2 (expired-reclaimed)"; testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); + + // Bad user context: not a map. + txt = + "{\n" + " \"command\": \"lease4-add\",\n" + " \"arguments\": {" + " \"subnet-id\": 44,\n" + " \"ip-address\": \"192.0.2.1\",\n" + " \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n" + " \"user-context\": \"bad value\"\n" + " }\n" + "}"; + exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); } // Check that a simple, well formed lease4 can be added. @@ -646,6 +677,7 @@ TEST_F(LeaseCmdsTest, Lease4Add) { EXPECT_FALSE(l->fqdn_fwd_); EXPECT_FALSE(l->fqdn_rev_); EXPECT_EQ("", l->hostname_); + EXPECT_FALSE(l->getContext()); // Test execution is fast. The cltt should be set to now. In some rare // cases we could have the seconds counter to tick, so having a value off @@ -677,7 +709,8 @@ TEST_F(LeaseCmdsTest, Lease4AddFull) { " \"expire\": 12345678,\n" " \"fqdn-fwd\": true,\n" " \"fqdn-rev\": true,\n" - " \"hostname\": \"urania.example.org\"" + " \"hostname\": \"urania.example.org\",\n" + " \"user-context\": { \"foobar\": true }\n" " }\n" "}"; string exp_rsp = "Lease added."; @@ -695,6 +728,8 @@ TEST_F(LeaseCmdsTest, Lease4AddFull) { EXPECT_EQ(true, l->fqdn_fwd_); EXPECT_EQ(true, l->fqdn_rev_); EXPECT_EQ("urania.example.org", l->hostname_); + ASSERT_TRUE(l->getContext()); + EXPECT_EQ("{ \"foobar\": true }", l->getContext()->str()); } // Check that lease6-add with missing parameters will fail. @@ -845,6 +880,21 @@ TEST_F(LeaseCmdsTest, Lease6AddBadParams) { exp_rsp = "Invalid state value: 123, supported values are: 0 (default), 1 " "(declined) and 2 (expired-reclaimed)"; testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); + + // Bad user context: not a map. + txt = + "{\n" + " \"command\": \"lease6-add\",\n" + " \"arguments\": {" + " \"subnet-id\": 66,\n" + " \"ip-address\": \"2001:db8:1::1\",\n" + " \"duid\": \"1a:1b:1c:1d:1e:1f\",\n" + " \"iaid\": 1234\n," + " \"user-context\": \"bad value\"\n" + " }\n" + "}"; + exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); } // Check that a simple, well formed lease6 can be added. @@ -871,7 +921,10 @@ TEST_F(LeaseCmdsTest, Lease6Add) { testCommand(txt, CONTROL_RESULT_SUCCESS, exp_rsp); // Now check that the lease is really there. - EXPECT_TRUE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"))); + Lease6Ptr l = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3")); + ASSERT_TRUE(l); + EXPECT_EQ("", l->hostname_); + EXPECT_FALSE(l->getContext()); } // Check that a simple, well formed prefix lease can be added. @@ -904,6 +957,8 @@ TEST_F(LeaseCmdsTest, Lease6AddPrefix) { ASSERT_TRUE(l); EXPECT_EQ(Lease::TYPE_PD, l->type_); EXPECT_EQ(48, l->prefixlen_); + EXPECT_EQ("", l->hostname_); + EXPECT_FALSE(l->getContext()); } // Check that a well formed lease6 with tons of parameters can be added. @@ -930,7 +985,8 @@ TEST_F(LeaseCmdsTest, Lease6AddFullAddr) { " \"expire\": 12345678,\n" " \"fqdn-fwd\": true,\n" " \"fqdn-rev\": true,\n" - " \"hostname\": \"urania.example.org\"" + " \"hostname\": \"urania.example.org\",\n" + " \"user-context\": { \"foobar\": true }\n" " }\n" "}"; string exp_rsp = "Lease added."; @@ -949,6 +1005,8 @@ TEST_F(LeaseCmdsTest, Lease6AddFullAddr) { EXPECT_EQ(true, l->fqdn_fwd_); EXPECT_EQ(true, l->fqdn_rev_); EXPECT_EQ("urania.example.org", l->hostname_); + ASSERT_TRUE(l->getContext()); + EXPECT_EQ("{ \"foobar\": true }", l->getContext()->str()); } // Checks that lease6-get can handle a situation when the query is @@ -1894,6 +1952,20 @@ TEST_F(LeaseCmdsTest, Lease4UpdateBadParams) { "}"; exp_rsp = "Non-IPv4 address specified: 2001:db8:1::1"; testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); + + // Bad user context: not a map. + txt = + "{\n" + " \"command\": \"lease4-update\",\n" + " \"arguments\": {" + " \"subnet-id\": 44,\n" + " \"ip-address\": \"192.0.2.1\",\n" + " \"hw-address\": \"1a:1b:1c:1d:1e:1f\",\n" + " \"user-context\": \"bad value\"\n" + " }\n" + "}"; + exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); } // Check that lease4-update correctly handles case when there is @@ -1953,6 +2025,7 @@ TEST_F(LeaseCmdsTest, Lease4Update) { ASSERT_TRUE(l->hwaddr_); EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false)); EXPECT_EQ("newhostname.example.org", l->hostname_); + EXPECT_FALSE(l->getContext()); } // Check that a lease4 is created if it doesn't exist during the update. @@ -1988,6 +2061,7 @@ TEST_F(LeaseCmdsTest, Lease4UpdateForceCreate) { ASSERT_TRUE(l->hwaddr_); EXPECT_EQ("1a:1b:1c:1d:1e:1f", l->hwaddr_->toText(false)); EXPECT_EQ("newhostname.example.org", l->hostname_); + EXPECT_FALSE(l->getContext()); } // Check that lease4-update correctly handles case when the 'force-create' @@ -2119,6 +2193,21 @@ TEST_F(LeaseCmdsTest, Lease6UpdateBadParams) { "}"; exp_rsp = "Non-IPv6 address specified: 192.0.2.1"; testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); + + // Bad user context: not a map. + txt = + "{\n" + " \"command\": \"lease6-update\",\n" + " \"arguments\": {" + " \"subnet-id\": 66,\n" + " \"ip-address\": \"2001:db8:1::1\",\n" + " \"duid\": \"1a:1b:1c:1d:1e:1f\",\n" + " \"iaid\": 1234\n," + " \"user-context\": \"bad value\"\n" + " }\n" + "}"; + exp_rsp = "Invalid user context '\"bad value\"' is not a JSON map."; + testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp); } // Check that a lease6 can be updated. We're changing hw-address @@ -2155,6 +2244,7 @@ TEST_F(LeaseCmdsTest, Lease6Update) { EXPECT_EQ("88:88:88:88:88:88:88:88", l->duid_->toText()); EXPECT_EQ("newhostname.example.org", l->hostname_); EXPECT_EQ(7654321, l->iaid_); + EXPECT_FALSE(l->getContext()); } @@ -2220,6 +2310,7 @@ TEST_F(LeaseCmdsTest, Lease6UpdateForceCreate) { EXPECT_EQ("88:88:88:88:88:88:88:88", l->duid_->toText()); EXPECT_EQ("newhostname.example.org", l->hostname_); EXPECT_EQ(7654321, l->iaid_); + EXPECT_FALSE(l->getContext()); } // Check that lease6-update correctly handles case when the 'force-create' |