OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "net/base/net_errors.h" | 6 #include "net/base/net_errors.h" |
7 #include "net/http/http_auth_challenge_tokenizer.h" | 7 #include "net/http/http_auth_challenge_tokenizer.h" |
8 #include "net/http/http_auth_sspi_win.h" | 8 #include "net/http/http_auth_sspi_win.h" |
9 #include "net/http/mock_sspi_library_win.h" | 9 #include "net/http/mock_sspi_library_win.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 MockSSPILibrary mock_library; | 40 MockSSPILibrary mock_library; |
41 mock_library.ExpectQuerySecurityPackageInfo(L"NTLM", SEC_E_OK, &package_info); | 41 mock_library.ExpectQuerySecurityPackageInfo(L"NTLM", SEC_E_OK, &package_info); |
42 ULONG max_token_length = kMaxTokenLength; | 42 ULONG max_token_length = kMaxTokenLength; |
43 int rv = DetermineMaxTokenLength(&mock_library, L"NTLM", &max_token_length); | 43 int rv = DetermineMaxTokenLength(&mock_library, L"NTLM", &max_token_length); |
44 EXPECT_EQ(OK, rv); | 44 EXPECT_EQ(OK, rv); |
45 EXPECT_EQ(1337, max_token_length); | 45 EXPECT_EQ(1337, max_token_length); |
46 } | 46 } |
47 | 47 |
48 TEST(HttpAuthSSPITest, DetermineMaxTokenLength_InvalidPackage) { | 48 TEST(HttpAuthSSPITest, DetermineMaxTokenLength_InvalidPackage) { |
49 MockSSPILibrary mock_library; | 49 MockSSPILibrary mock_library; |
50 mock_library.ExpectQuerySecurityPackageInfo(L"Foo", SEC_E_SECPKG_NOT_FOUND, | 50 mock_library.ExpectQuerySecurityPackageInfo( |
51 NULL); | 51 L"Foo", SEC_E_SECPKG_NOT_FOUND, NULL); |
52 ULONG max_token_length = kMaxTokenLength; | 52 ULONG max_token_length = kMaxTokenLength; |
53 int rv = DetermineMaxTokenLength(&mock_library, L"Foo", &max_token_length); | 53 int rv = DetermineMaxTokenLength(&mock_library, L"Foo", &max_token_length); |
54 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); | 54 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); |
55 // |DetermineMaxTokenLength()| interface states that |max_token_length| should | 55 // |DetermineMaxTokenLength()| interface states that |max_token_length| should |
56 // not change on failure. | 56 // not change on failure. |
57 EXPECT_EQ(100, max_token_length); | 57 EXPECT_EQ(100, max_token_length); |
58 } | 58 } |
59 | 59 |
60 TEST(HttpAuthSSPITest, ParseChallenge_FirstRound) { | 60 TEST(HttpAuthSSPITest, ParseChallenge_FirstRound) { |
61 // The first round should just consist of an unadorned "Negotiate" header. | 61 // The first round should just consist of an unadorned "Negotiate" header. |
62 MockSSPILibrary mock_library; | 62 MockSSPILibrary mock_library; |
63 HttpAuthSSPI auth_sspi(&mock_library, "Negotiate", | 63 HttpAuthSSPI auth_sspi( |
64 NEGOSSP_NAME, kMaxTokenLength); | 64 &mock_library, "Negotiate", NEGOSSP_NAME, kMaxTokenLength); |
65 std::string challenge_text = "Negotiate"; | 65 std::string challenge_text = "Negotiate"; |
66 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), | 66 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), |
67 challenge_text.end()); | 67 challenge_text.end()); |
68 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, | 68 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, |
69 auth_sspi.ParseChallenge(&challenge)); | 69 auth_sspi.ParseChallenge(&challenge)); |
70 } | 70 } |
71 | 71 |
72 TEST(HttpAuthSSPITest, ParseChallenge_TwoRounds) { | 72 TEST(HttpAuthSSPITest, ParseChallenge_TwoRounds) { |
73 // The first round should just have "Negotiate", and the second round should | 73 // The first round should just have "Negotiate", and the second round should |
74 // have a valid base64 token associated with it. | 74 // have a valid base64 token associated with it. |
75 MockSSPILibrary mock_library; | 75 MockSSPILibrary mock_library; |
76 HttpAuthSSPI auth_sspi(&mock_library, "Negotiate", | 76 HttpAuthSSPI auth_sspi( |
77 NEGOSSP_NAME, kMaxTokenLength); | 77 &mock_library, "Negotiate", NEGOSSP_NAME, kMaxTokenLength); |
78 std::string first_challenge_text = "Negotiate"; | 78 std::string first_challenge_text = "Negotiate"; |
79 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), | 79 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), |
80 first_challenge_text.end()); | 80 first_challenge_text.end()); |
81 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, | 81 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, |
82 auth_sspi.ParseChallenge(&first_challenge)); | 82 auth_sspi.ParseChallenge(&first_challenge)); |
83 | 83 |
84 // Generate an auth token and create another thing. | 84 // Generate an auth token and create another thing. |
85 std::string auth_token; | 85 std::string auth_token; |
86 EXPECT_EQ(OK, auth_sspi.GenerateAuthToken(NULL, "HTTP/intranet.google.com", | 86 EXPECT_EQ(OK, |
87 &auth_token)); | 87 auth_sspi.GenerateAuthToken( |
| 88 NULL, "HTTP/intranet.google.com", &auth_token)); |
88 | 89 |
89 std::string second_challenge_text = "Negotiate Zm9vYmFy"; | 90 std::string second_challenge_text = "Negotiate Zm9vYmFy"; |
90 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), | 91 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), |
91 second_challenge_text.end()); | 92 second_challenge_text.end()); |
92 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, | 93 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, |
93 auth_sspi.ParseChallenge(&second_challenge)); | 94 auth_sspi.ParseChallenge(&second_challenge)); |
94 } | 95 } |
95 | 96 |
96 TEST(HttpAuthSSPITest, ParseChallenge_UnexpectedTokenFirstRound) { | 97 TEST(HttpAuthSSPITest, ParseChallenge_UnexpectedTokenFirstRound) { |
97 // If the first round challenge has an additional authentication token, it | 98 // If the first round challenge has an additional authentication token, it |
98 // should be treated as an invalid challenge from the server. | 99 // should be treated as an invalid challenge from the server. |
99 MockSSPILibrary mock_library; | 100 MockSSPILibrary mock_library; |
100 HttpAuthSSPI auth_sspi(&mock_library, "Negotiate", | 101 HttpAuthSSPI auth_sspi( |
101 NEGOSSP_NAME, kMaxTokenLength); | 102 &mock_library, "Negotiate", NEGOSSP_NAME, kMaxTokenLength); |
102 std::string challenge_text = "Negotiate Zm9vYmFy"; | 103 std::string challenge_text = "Negotiate Zm9vYmFy"; |
103 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), | 104 HttpAuthChallengeTokenizer challenge(challenge_text.begin(), |
104 challenge_text.end()); | 105 challenge_text.end()); |
105 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID, | 106 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID, |
106 auth_sspi.ParseChallenge(&challenge)); | 107 auth_sspi.ParseChallenge(&challenge)); |
107 } | 108 } |
108 | 109 |
109 TEST(HttpAuthSSPITest, ParseChallenge_MissingTokenSecondRound) { | 110 TEST(HttpAuthSSPITest, ParseChallenge_MissingTokenSecondRound) { |
110 // If a later-round challenge is simply "Negotiate", it should be treated as | 111 // If a later-round challenge is simply "Negotiate", it should be treated as |
111 // an authentication challenge rejection from the server or proxy. | 112 // an authentication challenge rejection from the server or proxy. |
112 MockSSPILibrary mock_library; | 113 MockSSPILibrary mock_library; |
113 HttpAuthSSPI auth_sspi(&mock_library, "Negotiate", | 114 HttpAuthSSPI auth_sspi( |
114 NEGOSSP_NAME, kMaxTokenLength); | 115 &mock_library, "Negotiate", NEGOSSP_NAME, kMaxTokenLength); |
115 std::string first_challenge_text = "Negotiate"; | 116 std::string first_challenge_text = "Negotiate"; |
116 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), | 117 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), |
117 first_challenge_text.end()); | 118 first_challenge_text.end()); |
118 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, | 119 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, |
119 auth_sspi.ParseChallenge(&first_challenge)); | 120 auth_sspi.ParseChallenge(&first_challenge)); |
120 | 121 |
121 std::string auth_token; | 122 std::string auth_token; |
122 EXPECT_EQ(OK, auth_sspi.GenerateAuthToken(NULL, "HTTP/intranet.google.com", | 123 EXPECT_EQ(OK, |
123 &auth_token)); | 124 auth_sspi.GenerateAuthToken( |
| 125 NULL, "HTTP/intranet.google.com", &auth_token)); |
124 std::string second_challenge_text = "Negotiate"; | 126 std::string second_challenge_text = "Negotiate"; |
125 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), | 127 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), |
126 second_challenge_text.end()); | 128 second_challenge_text.end()); |
127 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, | 129 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, |
128 auth_sspi.ParseChallenge(&second_challenge)); | 130 auth_sspi.ParseChallenge(&second_challenge)); |
129 } | 131 } |
130 | 132 |
131 TEST(HttpAuthSSPITest, ParseChallenge_NonBase64EncodedToken) { | 133 TEST(HttpAuthSSPITest, ParseChallenge_NonBase64EncodedToken) { |
132 // If a later-round challenge has an invalid base64 encoded token, it should | 134 // If a later-round challenge has an invalid base64 encoded token, it should |
133 // be treated as an invalid challenge. | 135 // be treated as an invalid challenge. |
134 MockSSPILibrary mock_library; | 136 MockSSPILibrary mock_library; |
135 HttpAuthSSPI auth_sspi(&mock_library, "Negotiate", | 137 HttpAuthSSPI auth_sspi( |
136 NEGOSSP_NAME, kMaxTokenLength); | 138 &mock_library, "Negotiate", NEGOSSP_NAME, kMaxTokenLength); |
137 std::string first_challenge_text = "Negotiate"; | 139 std::string first_challenge_text = "Negotiate"; |
138 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), | 140 HttpAuthChallengeTokenizer first_challenge(first_challenge_text.begin(), |
139 first_challenge_text.end()); | 141 first_challenge_text.end()); |
140 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, | 142 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT, |
141 auth_sspi.ParseChallenge(&first_challenge)); | 143 auth_sspi.ParseChallenge(&first_challenge)); |
142 | 144 |
143 std::string auth_token; | 145 std::string auth_token; |
144 EXPECT_EQ(OK, auth_sspi.GenerateAuthToken(NULL, "HTTP/intranet.google.com", | 146 EXPECT_EQ(OK, |
145 &auth_token)); | 147 auth_sspi.GenerateAuthToken( |
| 148 NULL, "HTTP/intranet.google.com", &auth_token)); |
146 std::string second_challenge_text = "Negotiate =happyjoy="; | 149 std::string second_challenge_text = "Negotiate =happyjoy="; |
147 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), | 150 HttpAuthChallengeTokenizer second_challenge(second_challenge_text.begin(), |
148 second_challenge_text.end()); | 151 second_challenge_text.end()); |
149 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID, | 152 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID, |
150 auth_sspi.ParseChallenge(&second_challenge)); | 153 auth_sspi.ParseChallenge(&second_challenge)); |
151 } | 154 } |
152 | 155 |
153 } // namespace net | 156 } // namespace net |
OLD | NEW |