#include "net/android/http_auth_negotiate_android.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "net/android/dummy_spnego_authenticator.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_auth_challenge_tokenizer.h"
#include "net/http/mock_allow_http_auth_preferences.h"
#include "net/log/net_log_with_source.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net::android {
TEST(HttpAuthNegotiateAndroidTest, GenerateAuthToken) {
base::test::TaskEnvironment task_environment;
DummySpnegoAuthenticator::EnsureTestAccountExists();
std::string auth_token;
DummySpnegoAuthenticator authenticator;
net::test::GssContextMockImpl mockContext;
authenticator.ExpectSecurityContext("Negotiate", GSS_S_COMPLETE, 0,
mockContext, "", "DummyToken");
MockAllowHttpAuthPreferences prefs;
prefs.set_auth_android_negotiate_account_type(
"org.chromium.test.DummySpnegoAuthenticator");
HttpAuthNegotiateAndroid auth(&prefs);
EXPECT_TRUE(auth.Init(NetLogWithSource()));
TestCompletionCallback callback;
EXPECT_EQ(OK, callback.GetResult(auth.GenerateAuthToken(
nullptr, "Dummy", std::string(), &auth_token,
NetLogWithSource(), callback.callback())));
EXPECT_EQ("Negotiate DummyToken", auth_token);
DummySpnegoAuthenticator::RemoveTestAccounts();
}
TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_FirstRound) {
MockAllowHttpAuthPreferences prefs;
prefs.set_auth_android_negotiate_account_type(
"org.chromium.test.DummySpnegoAuthenticator");
HttpAuthNegotiateAndroid auth(&prefs);
HttpAuthChallengeTokenizer challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth.ParseChallenge(&challenge));
}
TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_UnexpectedTokenFirstRound) {
MockAllowHttpAuthPreferences prefs;
prefs.set_auth_android_negotiate_account_type(
"org.chromium.test.DummySpnegoAuthenticator");
HttpAuthNegotiateAndroid auth(&prefs);
HttpAuthChallengeTokenizer challenge("Negotiate Zm9vYmFy");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
auth.ParseChallenge(&challenge));
}
TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_TwoRounds) {
MockAllowHttpAuthPreferences prefs;
prefs.set_auth_android_negotiate_account_type(
"org.chromium.test.DummySpnegoAuthenticator");
HttpAuthNegotiateAndroid auth(&prefs);
HttpAuthChallengeTokenizer first_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth.ParseChallenge(&first_challenge));
HttpAuthChallengeTokenizer second_challenge("Negotiate Zm9vYmFy");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth.ParseChallenge(&second_challenge));
}
TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_MissingTokenSecondRound) {
MockAllowHttpAuthPreferences prefs;
prefs.set_auth_android_negotiate_account_type(
"org.chromium.test.DummySpnegoAuthenticator");
HttpAuthNegotiateAndroid auth(&prefs);
HttpAuthChallengeTokenizer first_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth.ParseChallenge(&first_challenge));
HttpAuthChallengeTokenizer second_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT,
auth.ParseChallenge(&second_challenge));
}
}