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