1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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) {
// The first round should just consist of an unadorned "Negotiate" header.
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) {
// If the first round challenge has an additional authentication token, it
// should be treated as an invalid challenge from the server.
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) {
// The first round should just have "Negotiate", and the second round should
// have a valid base64 token associated with it.
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) {
// If a later-round challenge is simply "Negotiate", it should be treated as
// an authentication challenge rejection from the server or proxy.
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));
}
} // namespace net::android
|