#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include "base/base64.h"
#include "base/containers/span.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/test_completion_callback.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_auth_challenge_tokenizer.h"
#include "net/http/http_auth_handler_ntlm.h"
#include "net/http/http_auth_ntlm_mechanism.h"
#include "net/http/http_request_info.h"
#include "net/http/mock_allow_http_auth_preferences.h"
#include "net/log/net_log_with_source.h"
#include "net/ntlm/ntlm.h"
#include "net/ntlm/ntlm_buffer_reader.h"
#include "net/ntlm/ntlm_buffer_writer.h"
#include "net/ntlm/ntlm_test_data.h"
#include "net/ssl/ssl_info.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
namespace net {
class HttpAuthHandlerNtlmPortableTest : public PlatformTest {
public:
HttpAuthHandlerNtlmPortableTest() {
http_auth_preferences_ = std::make_unique<MockAllowHttpAuthPreferences>();
http_auth_preferences_->set_ntlm_v2_enabled(false);
factory_ = std::make_unique<HttpAuthHandlerNTLM::Factory>();
factory_->set_http_auth_preferences(http_auth_preferences_.get());
creds_ = AuthCredentials(
base::StrCat({ntlm::test::kNtlmDomain, u"\\", ntlm::test::kUser}),
ntlm::test::kPassword);
}
int CreateHandler() {
url::SchemeHostPort scheme_host_port(GURL("https://foo.com"));
SSLInfo null_ssl_info;
return factory_->CreateAuthHandlerFromString(
"NTLM", HttpAuth::AUTH_SERVER, null_ssl_info, NetworkAnonymizationKey(),
scheme_host_port, NetLogWithSource(), nullptr, &auth_handler_);
}
std::string CreateNtlmAuthHeader(base::span<const uint8_t> buffer) {
std::string output = base::Base64Encode(std::string_view(
reinterpret_cast<const char*>(buffer.data()), buffer.size()));
return "NTLM " + output;
}
HttpAuth::AuthorizationResult HandleAnotherChallenge(
const std::string& challenge) {
HttpAuthChallengeTokenizer tokenizer(challenge);
return GetAuthHandler()->HandleAnotherChallenge(&tokenizer);
}
bool DecodeChallenge(const std::string& challenge, std::string* decoded) {
HttpAuthChallengeTokenizer tokenizer(challenge);
return base::Base64Decode(tokenizer.base64_param(), decoded);
}
int GenerateAuthToken(std::string* token) {
TestCompletionCallback callback;
HttpRequestInfo request_info;
return callback.GetResult(GetAuthHandler()->GenerateAuthToken(
GetCreds(), &request_info, callback.callback(), token));
}
int GetGenerateAuthTokenResult() {
std::string token;
return GenerateAuthToken(&token);
}
AuthCredentials* GetCreds() { return &creds_; }
HttpAuthHandlerNTLM* GetAuthHandler() {
return static_cast<HttpAuthHandlerNTLM*>(auth_handler_.get());
}
static void MockRandom(base::span<uint8_t> output) {
std::ranges::fill(output, 0xaa);
}
static uint64_t MockGetMSTime() {
return 131400439870000000;
}
static std::string MockGetHostName() { return ntlm::test::kHostnameAscii; }
private:
AuthCredentials creds_;
std::unique_ptr<HttpAuthHandler> auth_handler_;
std::unique_ptr<MockAllowHttpAuthPreferences> http_auth_preferences_;
std::unique_ptr<HttpAuthHandlerNTLM::Factory> factory_;
};
TEST_F(HttpAuthHandlerNtlmPortableTest, SimpleConstruction) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_TRUE(GetAuthHandler() != nullptr);
}
TEST_F(HttpAuthHandlerNtlmPortableTest, DoNotAllowDefaultCreds) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_FALSE(GetAuthHandler()->AllowsDefaultCredentials());
}
TEST_F(HttpAuthHandlerNtlmPortableTest, AllowsExplicitCredentials) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_TRUE(GetAuthHandler()->AllowsExplicitCredentials());
}
TEST_F(HttpAuthHandlerNtlmPortableTest, VerifyType1Message) {
ASSERT_EQ(OK, CreateHandler());
std::string token;
ASSERT_EQ(OK, GenerateAuthToken(&token));
ASSERT_EQ("NTLM TlRMTVNTUAABAAAAB4IIAAAAAAAgAAAAAAAAACAAAAA=", token);
}
TEST_F(HttpAuthHandlerNtlmPortableTest, EmptyTokenFails) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_EQ(OK, GetGenerateAuthTokenResult());
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT,
HandleAnotherChallenge("NTLM"));
}
TEST_F(HttpAuthHandlerNtlmPortableTest, InvalidBase64Encoding) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_EQ(OK, GetGenerateAuthTokenResult());
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
HandleAnotherChallenge("NTLM !!!!!!!!!!!!!"));
}
TEST_F(HttpAuthHandlerNtlmPortableTest, CantChangeSchemeMidway) {
ASSERT_EQ(OK, CreateHandler());
ASSERT_EQ(OK, GetGenerateAuthTokenResult());
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
HandleAnotherChallenge("Negotiate SSdtIG5vdCBhIHJlYWwgdG9rZW4h"));
}
TEST_F(HttpAuthHandlerNtlmPortableTest, NtlmV1AuthenticationSuccess) {
HttpAuthNtlmMechanism::ScopedProcSetter proc_setter(MockGetMSTime, MockRandom,
MockGetHostName);
ASSERT_EQ(OK, CreateHandler());
ASSERT_EQ(OK, GetGenerateAuthTokenResult());
std::string token;
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
HandleAnotherChallenge(
CreateNtlmAuthHeader(ntlm::test::kChallengeMsgV1)));
ASSERT_EQ(OK, GenerateAuthToken(&token));
std::string decoded;
ASSERT_TRUE(DecodeChallenge(token, &decoded));
ASSERT_EQ(std::size(ntlm::test::kExpectedAuthenticateMsgSpecResponseV1),
decoded.size());
ASSERT_EQ(
base::as_byte_span(decoded),
base::as_byte_span(ntlm::test::kExpectedAuthenticateMsgSpecResponseV1));
}
}