1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/android/http_auth_negotiate_android.h"
6
7 #include "base/run_loop.h"
8 #include "base/test/task_environment.h"
9 #include "net/android/dummy_spnego_authenticator.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/http/http_auth_challenge_tokenizer.h"
13 #include "net/http/mock_allow_http_auth_preferences.h"
14 #include "net/log/net_log_with_source.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace net::android {
18
TEST(HttpAuthNegotiateAndroidTest,GenerateAuthToken)19 TEST(HttpAuthNegotiateAndroidTest, GenerateAuthToken) {
20 base::test::TaskEnvironment task_environment;
21
22 DummySpnegoAuthenticator::EnsureTestAccountExists();
23
24 std::string auth_token;
25
26 DummySpnegoAuthenticator authenticator;
27 net::test::GssContextMockImpl mockContext;
28 authenticator.ExpectSecurityContext("Negotiate", GSS_S_COMPLETE, 0,
29 mockContext, "", "DummyToken");
30
31 MockAllowHttpAuthPreferences prefs;
32 prefs.set_auth_android_negotiate_account_type(
33 "org.chromium.test.DummySpnegoAuthenticator");
34 HttpAuthNegotiateAndroid auth(&prefs);
35 EXPECT_TRUE(auth.Init(NetLogWithSource()));
36
37 TestCompletionCallback callback;
38 EXPECT_EQ(OK, callback.GetResult(auth.GenerateAuthToken(
39 nullptr, "Dummy", std::string(), &auth_token,
40 NetLogWithSource(), callback.callback())));
41
42 EXPECT_EQ("Negotiate DummyToken", auth_token);
43
44 DummySpnegoAuthenticator::RemoveTestAccounts();
45 }
46
TEST(HttpAuthNegotiateAndroidTest,ParseChallenge_FirstRound)47 TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_FirstRound) {
48 // The first round should just consist of an unadorned "Negotiate" header.
49 MockAllowHttpAuthPreferences prefs;
50 prefs.set_auth_android_negotiate_account_type(
51 "org.chromium.test.DummySpnegoAuthenticator");
52 HttpAuthNegotiateAndroid auth(&prefs);
53 HttpAuthChallengeTokenizer challenge("Negotiate");
54 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
55 auth.ParseChallenge(&challenge));
56 }
57
TEST(HttpAuthNegotiateAndroidTest,ParseChallenge_UnexpectedTokenFirstRound)58 TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_UnexpectedTokenFirstRound) {
59 // If the first round challenge has an additional authentication token, it
60 // should be treated as an invalid challenge from the server.
61 MockAllowHttpAuthPreferences prefs;
62 prefs.set_auth_android_negotiate_account_type(
63 "org.chromium.test.DummySpnegoAuthenticator");
64 HttpAuthNegotiateAndroid auth(&prefs);
65 HttpAuthChallengeTokenizer challenge("Negotiate Zm9vYmFy");
66 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
67 auth.ParseChallenge(&challenge));
68 }
69
TEST(HttpAuthNegotiateAndroidTest,ParseChallenge_TwoRounds)70 TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_TwoRounds) {
71 // The first round should just have "Negotiate", and the second round should
72 // have a valid base64 token associated with it.
73 MockAllowHttpAuthPreferences prefs;
74 prefs.set_auth_android_negotiate_account_type(
75 "org.chromium.test.DummySpnegoAuthenticator");
76 HttpAuthNegotiateAndroid auth(&prefs);
77 HttpAuthChallengeTokenizer first_challenge("Negotiate");
78 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
79 auth.ParseChallenge(&first_challenge));
80
81 HttpAuthChallengeTokenizer second_challenge("Negotiate Zm9vYmFy");
82 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
83 auth.ParseChallenge(&second_challenge));
84 }
85
TEST(HttpAuthNegotiateAndroidTest,ParseChallenge_MissingTokenSecondRound)86 TEST(HttpAuthNegotiateAndroidTest, ParseChallenge_MissingTokenSecondRound) {
87 // If a later-round challenge is simply "Negotiate", it should be treated as
88 // an authentication challenge rejection from the server or proxy.
89 MockAllowHttpAuthPreferences prefs;
90 prefs.set_auth_android_negotiate_account_type(
91 "org.chromium.test.DummySpnegoAuthenticator");
92 HttpAuthNegotiateAndroid auth(&prefs);
93 HttpAuthChallengeTokenizer first_challenge("Negotiate");
94 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
95 auth.ParseChallenge(&first_challenge));
96
97 HttpAuthChallengeTokenizer second_challenge("Negotiate");
98 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT,
99 auth.ParseChallenge(&second_challenge));
100 }
101
102 } // namespace net::android
103