Commit 95a7b0d1 authored by Marcin Siodelski's avatar Marcin Siodelski

[#253,!147] Command interpreter now rejects unknown command parameters.

parent c8d37863
......@@ -179,6 +179,17 @@ parseCommand(ConstElementPtr& arg, ConstElementPtr command) {
"Invalid answer specified, does not contain mandatory 'command'");
}
// Make sure that all specified parameters are supported.
auto command_params = command->mapValue();
for (auto param : command_params) {
if ((param.first != CONTROL_COMMAND) &&
(param.first != CONTROL_ARGUMENTS) &&
(param.first != CONTROL_SERVICE)) {
isc_throw(CtrlChannelError, "Received command contains unsupported "
"parameter '" << param.first << "'");
}
}
ConstElementPtr cmd = command->get(CONTROL_COMMAND);
if (cmd->getType() != Element::string) {
isc_throw(CtrlChannelError,
......
......@@ -165,12 +165,16 @@ TEST(CommandInterpreterTest, parseCommand) {
EXPECT_THROW(parseCommand(arg, el("{ \"command\": 1 }")), CtrlChannelError);
EXPECT_THROW(parseCommand(arg, el("{ \"command\": [] }")), CtrlChannelError);
EXPECT_THROW(parseCommand(arg, el("{ \"command\": [ 1 ] }")), CtrlChannelError);
EXPECT_THROW(parseCommand(arg, el("{ \"command\": \"my_command\", "
"\"unknown\": \"xyz\" }")), CtrlChannelError);
cmd = parseCommand(arg, el("{ \"command\": \"my_command\" }"));
EXPECT_EQ("my_command", cmd);
EXPECT_FALSE(arg);
cmd = parseCommand(arg, el("{ \"command\": \"my_command\", \"arguments\": 1 }"));
// Include "service" to verify that it is not rejected.
cmd = parseCommand(arg, el("{ \"command\": \"my_command\", \"arguments\": 1, "
" \"service\": [ \"dhcp4\" ] }"));
ASSERT_TRUE(arg);
EXPECT_EQ("my_command", cmd);
EXPECT_EQ("1", arg->str());
......@@ -203,10 +207,17 @@ TEST(CommandInterpreterTest, parseCommandWithArgs) {
"\"arguments\": { } }")),
CtrlChannelError);
// Command with unsupported parameter is rejected.
EXPECT_THROW(parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
" \"arguments\": { \"arg1\": \"value1\" }, "
" \"unsupported\": 1 }")),
CtrlChannelError);
// Specifying arguments in non empty map should be successful.
EXPECT_NO_THROW(
cmd = parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
"\"arguments\": { \"arg1\": \"value1\" } }"))
" \"arguments\": { \"arg1\": \"value1\" } }"))
);
ASSERT_TRUE(arg);
ASSERT_EQ(Element::map, arg->getType());
......@@ -215,6 +226,13 @@ TEST(CommandInterpreterTest, parseCommandWithArgs) {
ASSERT_EQ(Element::string, arg1->getType());
EXPECT_EQ("value1", arg1->stringValue());
EXPECT_EQ("my_command", cmd);
// The "service" parameter should be allowed.
EXPECT_NO_THROW(parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
" \"service\": [ \"dhcp4\" ], "
" \"arguments\": { \"arg1\": \"value1\" } }"))
);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment