]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2006] Fixed agent unit tests
authorFrancis Dupont <fdupont@isc.org>
Tue, 7 Dec 2021 15:32:52 +0000 (16:32 +0100)
committerRazvan Becheriu <razvan@isc.org>
Fri, 21 Jan 2022 17:15:38 +0000 (17:15 +0000)
src/bin/agent/tests/Makefile.am
src/bin/agent/tests/ca_cfg_mgr_unittests.cc
src/bin/agent/tests/parser_unittests.cc
src/bin/agent/tests/testdata/hiddenp
src/bin/agent/tests/testdata/hiddens
src/bin/agent/tests/testdata/hiddenu

index 0a77ff8f27fe53e37335814212fa5c2f5acad9e6..7cfedfeb111d1e04bbce784125704a42f5b10ac6 100644 (file)
@@ -2,6 +2,9 @@ SUBDIRS = .
 
 # Add to the tarball:
 EXTRA_DIST = testdata/get_config.json
+EXTRA_DIST += testdata/hiddenp
+EXTRA_DIST += testdata/hiddens
+EXTRA_DIST += testdata/hiddenu
 
 TESTS_ENVIRONMENT = \
         $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND)
index 96f8dc10fb81b4fc7290cc0440ef07dfd765ee0c..dc41250ae33a682c999b7605d729890737f7f432 100644 (file)
@@ -12,6 +12,7 @@
 #include <process/d_cfg_mgr.h>
 #include <http/basic_auth_config.h>
 #include <agent/tests/test_callout_libraries.h>
+#include <agent/tests/test_data_files_config.h>
 #include <boost/pointer_cast.hpp>
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
@@ -225,6 +226,27 @@ TEST(CtrlAgentCfgMgr, contextAuthConfig) {
     EXPECT_EQ(auth->toElement()->str(), stored_auth->toElement()->str());
 }
 
+// Test if the context can store and retrieve basic HTTP authentication
+// configuration using files.
+TEST(CtrlAgentCfgMgr, contextAuthConfigFile) {
+    CtrlAgentCfgContext ctx;
+
+    // By default there should be no authentication.
+    EXPECT_FALSE(ctx.getAuthConfig());
+    BasicHttpAuthConfigPtr auth(new BasicHttpAuthConfig());
+    EXPECT_NO_THROW(ctx.setAuthConfig(auth));
+
+    auth->setRealm("foobar");
+    auth->setDirectory("/tmp");
+    auth->add("", "/tmp/foo", "", "/tmp/bar");
+    auth->add("", "/tmp/test", "", "/tmp/pwd");
+
+    const HttpAuthConfigPtr& stored_auth = ctx.getAuthConfig();
+    ASSERT_TRUE(stored_auth);
+    EXPECT_FALSE(stored_auth->empty());
+    EXPECT_EQ(auth->toElement()->str(), stored_auth->toElement()->str());
+}
+
 /// Control Agent configurations used in tests.
 const char* AGENT_CONFIGS[] = {
 
@@ -371,6 +393,30 @@ const char* AGENT_CONFIGS[] = {
     "    \"cert-file\": \"my-cert\",\n"
     "    \"key-file\": \"my-key\",\n"
     "    \"cert-required\": false\n"
+    "}",
+
+    // Configuration 10: http, 1 socket and authentication using files
+    "{\n"
+    "    \"http-host\": \"betelgeuse\",\n"
+    "    \"http-port\": 8001,\n"
+    "    \"authentication\": {\n"
+    "        \"type\": \"basic\",\n"
+    "        \"realm\": \"foobar\",\n"
+    "        \"directory\": \"" CA_TEST_DATA_DIR "\",\n"
+    "        \"clients\": ["
+    "            {"
+    "              \"user-file\": \"hiddenu\",\n"
+    "              \"password-file\": \"hiddenp\"\n"
+    "            },{\n"
+    "              \"password-file\": \"hiddens\"\n"
+    "            }\n"
+    "         ]\n"
+    "    },\n"
+    "    \"control-sockets\": {\n"
+    "        \"dhcp4\": {\n"
+    "            \"socket-name\": \"/tmp/socket-v4\"\n"
+    "        }\n"
+    "    }\n"
     "}"
 };
 
@@ -619,4 +665,39 @@ TEST_F(AgentParserTest, configParseTls) {
     EXPECT_FALSE(ctx->getCertRequired());
 }
 
+// This test checks that the config file with basic HTTP authentication
+// using files can be loaded.
+TEST_F(AgentParserTest, configParseAuthFile) {
+    configParse(AGENT_CONFIGS[10], 0);
+    CtrlAgentCfgContextPtr ctx = cfg_mgr_.getCtrlAgentCfgContext();
+    const HttpAuthConfigPtr& auth = ctx->getAuthConfig();
+    ASSERT_TRUE(auth);
+    const BasicHttpAuthConfigPtr& basic_auth =
+        boost::dynamic_pointer_cast<BasicHttpAuthConfig>(auth);
+    ASSERT_TRUE(basic_auth);
+
+    // Check realm
+    EXPECT_EQ("foobar", basic_auth->getRealm());
+
+    // Check directory
+    EXPECT_EQ(std::string(CA_TEST_DATA_DIR), basic_auth->getDirectory());
+
+    // Check credentials
+    auto credentials = basic_auth->getCredentialMap();
+    EXPECT_EQ(2, credentials.size());
+    std::string user;
+    EXPECT_NO_THROW(user = credentials.at("a2VhdGVzdDpLZWFUZXN0"));
+    EXPECT_EQ("keatest", user);
+    EXPECT_NO_THROW(user = credentials.at("a2VhOnRlc3Q="));
+    EXPECT_EQ("kea", user);
+
+    // Check clients.
+    BasicHttpAuthConfig expected;
+    expected.setRealm("foobar");
+    expected.setDirectory(std::string(CA_TEST_DATA_DIR));
+    expected.add("", "hiddenu", "", "hiddenp");
+    expected.add("", "", "", "hiddens", true);
+    EXPECT_EQ(expected.toElement()->str(), basic_auth->toElement()->str());
+}
+
 } // end of anonymous namespace
index 5eac95bb7c8953cec59b7f59c58b0d66fc4aa5ec..3a0bab632fa09ebfa035f1537fe402313779c942 100644 (file)
@@ -764,8 +764,7 @@ TEST(ParserTest, mapEntries) {
     ElementPtr sample_json = Element::createList();
     loadFile(sample_dir + "https.json", sample_json);
     loadFile(sample_dir + "simple.json", sample_json);
-    //// KeywordSet sample_keys;
-    KeywordSet sample_keys = { "password-file", "user-file" };
+    KeywordSet sample_keys;
     // Recursively extract keywords.
     static void (*extract)(ConstElementPtr, KeywordSet&) =
         [] (ConstElementPtr json, KeywordSet& set) {
index beb2d2fb11ab78e6c35b328df901b05a0ddbc48b..e8b2395155b8a72f4eb94d3d9f8b7c8d4bc4ec1b 100644 (file)
@@ -1 +1 @@
-KeaTest
+KeaTest
\ No newline at end of file
index 2c2e06d8c04e8665af5096619389dc1424edd879..f52fb831e36838cdc7f89526cd22f3a87ab7a0a5 100644 (file)
@@ -1 +1 @@
-kea:test
+kea:test
\ No newline at end of file
index eadbb15e846d2a7d3cefb7893a58860732b045b1..801489abbef0a7b41e094694c9225c53c1ad3bed 100644 (file)
@@ -1 +1 @@
-keatest
+keatest
\ No newline at end of file