From: Francis Dupont Date: Tue, 7 Dec 2021 15:32:52 +0000 (+0100) Subject: [#2006] Fixed agent unit tests X-Git-Tag: Kea-2.1.2~50 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e149b75981ebc6214b293f8f6fa82a15e7d452a2;p=thirdparty%2Fkea.git [#2006] Fixed agent unit tests --- diff --git a/src/bin/agent/tests/Makefile.am b/src/bin/agent/tests/Makefile.am index 0a77ff8f27..7cfedfeb11 100644 --- a/src/bin/agent/tests/Makefile.am +++ b/src/bin/agent/tests/Makefile.am @@ -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) diff --git a/src/bin/agent/tests/ca_cfg_mgr_unittests.cc b/src/bin/agent/tests/ca_cfg_mgr_unittests.cc index 96f8dc10fb..dc41250ae3 100644 --- a/src/bin/agent/tests/ca_cfg_mgr_unittests.cc +++ b/src/bin/agent/tests/ca_cfg_mgr_unittests.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -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(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 diff --git a/src/bin/agent/tests/parser_unittests.cc b/src/bin/agent/tests/parser_unittests.cc index 5eac95bb7c..3a0bab632f 100644 --- a/src/bin/agent/tests/parser_unittests.cc +++ b/src/bin/agent/tests/parser_unittests.cc @@ -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) { diff --git a/src/bin/agent/tests/testdata/hiddenp b/src/bin/agent/tests/testdata/hiddenp index beb2d2fb11..e8b2395155 100644 --- a/src/bin/agent/tests/testdata/hiddenp +++ b/src/bin/agent/tests/testdata/hiddenp @@ -1 +1 @@ -KeaTest +KeaTest \ No newline at end of file diff --git a/src/bin/agent/tests/testdata/hiddens b/src/bin/agent/tests/testdata/hiddens index 2c2e06d8c0..f52fb831e3 100644 --- a/src/bin/agent/tests/testdata/hiddens +++ b/src/bin/agent/tests/testdata/hiddens @@ -1 +1 @@ -kea:test +kea:test \ No newline at end of file diff --git a/src/bin/agent/tests/testdata/hiddenu b/src/bin/agent/tests/testdata/hiddenu index eadbb15e84..801489abbe 100644 --- a/src/bin/agent/tests/testdata/hiddenu +++ b/src/bin/agent/tests/testdata/hiddenu @@ -1 +1 @@ -keatest +keatest \ No newline at end of file