From: Francis Dupont Date: Sat, 7 Nov 2020 14:32:01 +0000 (+0100) Subject: [#1421] Added callout context X-Git-Tag: Kea-1.9.2~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66833378c080fd71a833ac8018a9611f4a2dbb6d;p=thirdparty%2Fkea.git [#1421] Added callout context --- diff --git a/src/bin/agent/tests/basic_auth_library.cc b/src/bin/agent/tests/basic_auth_library.cc index f3581b6e12..9731d557f0 100644 --- a/src/bin/agent/tests/basic_auth_library.cc +++ b/src/bin/agent/tests/basic_auth_library.cc @@ -201,11 +201,17 @@ auth(CalloutHandle& handle) { } // Modify request. + int extra = 0; + ConstElementPtr extra_elem = command->get("extra"); ElementPtr mutable_command = boost::const_pointer_cast(command); - if (command->contains("service")) { - mutable_command->remove("service"); + if (extra_elem) { + if (extra_elem->getType() == Element::integer) { + extra = extra_elem->intValue(); + } + mutable_command->remove("extra"); request_json->setBodyAsJson(command); } + handle.setContext("extra", extra); // Perform authentication. response = impl->config_->checkAuth(*impl->creator_, request); @@ -263,7 +269,15 @@ response(CalloutHandle& handle) { return (0); } ElementPtr mutable_answer = boost::const_pointer_cast(answer); - mutable_answer->set("comment", Element::create(string("got"))); + try { + int extra = 0; + handle.getContext("extra", extra); + mutable_answer->set("got", Element::create(extra)); + } catch (const NoSuchCalloutContext&) { + std::cerr << "can't find 'extra' context\n"; + } catch (...) { + std::cerr << "getContext('extra') failed\n"; + } response->setBodyAsJson(body); // Set parameters. diff --git a/src/bin/agent/tests/ca_response_creator_unittests.cc b/src/bin/agent/tests/ca_response_creator_unittests.cc index c49ab858a9..eb3658146a 100644 --- a/src/bin/agent/tests/ca_response_creator_unittests.cc +++ b/src/bin/agent/tests/ca_response_creator_unittests.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -326,8 +327,14 @@ TEST_F(CtrlAgentResponseCreatorTest, hookNoAuth) { setBasicContext(request_); // Body: "list-commands" is natively supported by the command manager. - request_->context()->body_ = "{ \"command\": \"list-commands\"," - " \"service\": [ ] }"; + auto r32 = isc::cryptolink::random(4); + ASSERT_EQ(4, r32.size()); + int extra = r32[0]; + extra = (extra << 8) | r32[1]; + extra = (extra << 8) | r32[2]; + extra = (extra << 8) | r32[3]; + request_->context()->body_ = "{ \"command\": \"list-commands\", "; + request_->context()->body_ += "\"extra\": " + std::to_string(extra) + " }"; // All requests must be finalized before they can be processed. ASSERT_NO_THROW(request_->finalize()); @@ -352,7 +359,7 @@ TEST_F(CtrlAgentResponseCreatorTest, hookNoAuth) { ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_)); ASSERT_TRUE(response); - // Request should have no service. + // Request should have no extra. EXPECT_EQ("{ \"command\": \"list-commands\" }", request_->context()->body_); @@ -374,7 +381,18 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) { setBasicContext(request_); // Body: "list-commands" is natively supported by the command manager. - request_->context()->body_ = "{ \"command\": \"list-commands\" }"; + auto r32 = isc::cryptolink::random(4); + ASSERT_EQ(4, r32.size()); + int extra = r32[0]; + extra = (extra << 8) | r32[1]; + extra = (extra << 8) | r32[2]; + extra = (extra << 8) | r32[3]; + if (extra == 0) { + extra = 1; + } + std::string extra_str = std::to_string(extra); + request_->context()->body_ = "{ \"command\": \"list-commands\", "; + request_->context()->body_ += "\"extra\": " + extra_str + " }"; // Add basic HTTP authentication header. const BasicHttpAuth& basic_auth = BasicHttpAuth("foo", "bar"); @@ -405,6 +423,10 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) { ASSERT_NO_THROW(response = response_creator_.createHttpResponse(request_)); ASSERT_TRUE(response); + // Request should have no extra. + EXPECT_EQ("{ \"command\": \"list-commands\" }", + request_->context()->body_); + // Response must be convertible to HttpResponseJsonPtr. HttpResponseJsonPtr response_json = boost::dynamic_pointer_cast< HttpResponseJson>(response); @@ -417,7 +439,8 @@ TEST_F(CtrlAgentResponseCreatorTest, hookBasicAuth) { EXPECT_TRUE(response_json->toString().find("\"result\": 0") != std::string::npos); // Response must contain JSON body with "comment": "got". - EXPECT_TRUE(response_json->toString().find("\"comment\": \"got\"") != + std::string expected = "\"got\": " + extra_str + ", "; + EXPECT_TRUE(response_json->toString().find(expected) != std::string::npos); }