diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 14:53:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 14:53:22 +0000 |
commit | 52c021ee0b0c6ad2128ed550c694aad0d11d4c3f (patch) | |
tree | 83cf8627b94336cf4bee7479b9749263bbfd3a06 /src/lib/http/tests/basic_auth_unittests.cc | |
parent | Initial commit. (diff) | |
download | isc-kea-upstream.tar.xz isc-kea-upstream.zip |
Adding upstream version 2.5.7.upstream/2.5.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/http/tests/basic_auth_unittests.cc')
-rw-r--r-- | src/lib/http/tests/basic_auth_unittests.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/lib/http/tests/basic_auth_unittests.cc b/src/lib/http/tests/basic_auth_unittests.cc new file mode 100644 index 0000000..1c2693d --- /dev/null +++ b/src/lib/http/tests/basic_auth_unittests.cc @@ -0,0 +1,65 @@ +// Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include <config.h> +#include <http/basic_auth.h> +#include <gtest/gtest.h> + +using namespace isc; +using namespace isc::http; + +namespace { + +// Test that user name with a colon is rejected. +TEST(BasicHttpAuthTest, userColon) { + BasicHttpAuthPtr basic_auth; + EXPECT_THROW(basic_auth.reset(new BasicHttpAuth("foo:bar", "")), BadValue); +} + +// Test that secret without a colon is rejected. +TEST(BasicHttpAuthTest, secretNoColon) { + BasicHttpAuthPtr basic_auth; + EXPECT_THROW(basic_auth.reset(new BasicHttpAuth("foo-bar")), BadValue); +} + +// Test that valid user and password work. +TEST(BasicHttpAuthTest, user) { + BasicHttpAuthPtr basic_auth; + EXPECT_NO_THROW(basic_auth.reset(new BasicHttpAuth("foo", "bar"))); + ASSERT_TRUE(basic_auth); + EXPECT_EQ("foo:bar", basic_auth->getSecret()); + EXPECT_EQ("Zm9vOmJhcg==", basic_auth->getCredential()); +} + +// Test that valid secret work. +TEST(BasicHttpAuthTest, secret) { + BasicHttpAuthPtr basic_auth; + EXPECT_NO_THROW(basic_auth.reset(new BasicHttpAuth("foo:bar"))); + ASSERT_TRUE(basic_auth); + EXPECT_EQ("foo:bar", basic_auth->getSecret()); + EXPECT_EQ("Zm9vOmJhcg==", basic_auth->getCredential()); +} + +// Test that secret is encoded in UTF-8. +TEST(BasicHttpAuthTest, utf8) { + BasicHttpAuthPtr basic_auth; + EXPECT_NO_THROW(basic_auth.reset(new BasicHttpAuth("foo\n", "b\ar"))); + ASSERT_TRUE(basic_auth); + EXPECT_EQ("foo\n:b\ar", basic_auth->getSecret()); + EXPECT_EQ("Zm9vCjpiB3I=", basic_auth->getCredential()); +} + +// Test that a header context for basic HTTP authentication can be created. +TEST(BasicHttpAuthTest, headerContext) { + BasicHttpAuthPtr basic_auth; + EXPECT_NO_THROW(basic_auth.reset(new BasicHttpAuth("foo", "bar"))); + ASSERT_TRUE(basic_auth); + BasicAuthHttpHeaderContext ctx(*basic_auth); + EXPECT_EQ("Authorization", ctx.name_); + EXPECT_EQ("Basic Zm9vOmJhcg==", ctx.value_); +} + +} // end of anonymous namespace |