blob: 5a272925f00319d6a6d4c64d49e4ca427aecd4c5 [file] [log] [blame]
[email protected]c3b35c22008-09-27 03:19:421// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
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 "testing/gtest/include/gtest/gtest.h"
6
7#include "base/basictypes.h"
8#include "net/http/http_auth_handler_digest.h"
9
10namespace net {
11
12TEST(HttpAuthHandlerDigestTest, ParseChallenge) {
13 static const struct {
14 // The challenge string.
15 const char* challenge;
16 // Expected return value of ParseChallenge.
17 bool parsed_success;
18 // The expected values that were parsed.
19 const char* parsed_realm;
20 const char* parsed_nonce;
21 const char* parsed_domain;
22 const char* parsed_opaque;
23 bool parsed_stale;
24 int parsed_algorithm;
25 int parsed_qop;
26 } tests[] = {
27 {
28 "Digest nonce=\"xyz\", realm=\"Thunder Bluff\"",
29 true,
30 "Thunder Bluff",
31 "xyz",
32 "",
33 "",
34 false,
35 HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
36 HttpAuthHandlerDigest::QOP_UNSPECIFIED
37 },
[email protected]f0a51fb52009-03-05 12:46:3838
[email protected]c3b35c22008-09-27 03:19:4239 {// Check that when algorithm has an unsupported value, parsing fails.
40 "Digest nonce=\"xyz\", algorithm=\"awezum\", realm=\"Thunder\"",
41 false,
42 // The remaining values don't matter (but some have been set already).
43 "",
44 "xyz",
45 "",
46 "",
47 false,
48 HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
49 HttpAuthHandlerDigest::QOP_UNSPECIFIED
50 },
51
52 { // Check that algorithm's value is case insensitive.
53 "Digest nonce=\"xyz\", algorithm=\"mD5\", realm=\"Oblivion\"",
54 true,
55 "Oblivion",
56 "xyz",
57 "",
58 "",
59 false,
60 HttpAuthHandlerDigest::ALGORITHM_MD5,
61 HttpAuthHandlerDigest::QOP_UNSPECIFIED
62 },
63
64 { // Check that md5-sess is recognized, as is single QOP
65 "Digest nonce=\"xyz\", algorithm=\"md5-sess\", "
66 "realm=\"Oblivion\", qop=\"auth\"",
67 true,
68 "Oblivion",
69 "xyz",
70 "",
71 "",
72 false,
73 HttpAuthHandlerDigest::ALGORITHM_MD5_SESS,
74 HttpAuthHandlerDigest::QOP_AUTH
75 },
76
77 { // The realm can't be missing.
78 "Digest nonce=\"xyz\"",
79 false, // FAILED parse.
80 "",
81 "xyz",
82 "",
83 "",
84 false,
85 HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
86 HttpAuthHandlerDigest::QOP_UNSPECIFIED
87 }
88 };
89
[email protected]b85ee492008-09-27 19:27:1490 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
[email protected]c3b35c22008-09-27 03:19:4291 std::string challenge(tests[i].challenge);
92
[email protected]f9ee6b52008-11-08 06:46:2393 scoped_refptr<HttpAuthHandlerDigest> digest = new HttpAuthHandlerDigest;
94 bool ok = digest->ParseChallenge(challenge.begin(), challenge.end());
[email protected]f0a51fb52009-03-05 12:46:3895
[email protected]c3b35c22008-09-27 03:19:4296 EXPECT_EQ(tests[i].parsed_success, ok);
[email protected]f9ee6b52008-11-08 06:46:2397 EXPECT_STREQ(tests[i].parsed_realm, digest->realm_.c_str());
98 EXPECT_STREQ(tests[i].parsed_nonce, digest->nonce_.c_str());
99 EXPECT_STREQ(tests[i].parsed_domain, digest->domain_.c_str());
100 EXPECT_STREQ(tests[i].parsed_opaque, digest->opaque_.c_str());
101 EXPECT_EQ(tests[i].parsed_stale, digest->stale_);
102 EXPECT_EQ(tests[i].parsed_algorithm, digest->algorithm_);
103 EXPECT_EQ(tests[i].parsed_qop, digest->qop_);
[email protected]c3b35c22008-09-27 03:19:42104 }
105}
106
107TEST(HttpAuthHandlerDigestTest, AssembleCredentials) {
108 static const struct {
109 const char* req_method;
110 const char* req_path;
111 const char* challenge;
112 const char* username;
113 const char* password;
114 const char* cnonce;
115 int nonce_count;
116 const char* expected_creds;
117 } tests[] = {
118 { // MD5 with username/password
119 "GET",
120 "/test/drealm1",
121
122 // Challenge
123 "Digest realm=\"DRealm1\", "
124 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\", "
125 "algorithm=MD5, qop=\"auth\"",
126
127 "foo", "bar", // username/password
128 "082c875dcb2ca740", // cnonce
129 1, // nc
130
131 // Authorization
132 "Digest username=\"foo\", realm=\"DRealm1\", "
133 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\", "
134 "uri=\"/test/drealm1\", algorithm=MD5, "
135 "response=\"bcfaa62f1186a31ff1b474a19a17cf57\", "
136 "qop=auth, nc=00000001, cnonce=\"082c875dcb2ca740\""
137 },
138
139 { // MD5 with username but empty password. username has space in it.
140 "GET",
141 "/test/drealm1/",
142
143 // Challenge
144 "Digest realm=\"DRealm1\", "
145 "nonce=\"Ure30oRXBAA=7eca98bbf521ac6642820b11b86bd2d9ed7edc70\", "
146 "algorithm=MD5, qop=\"auth\"",
147
148 "foo bar", "", // Username/password
149 "082c875dcb2ca740", // cnonce
150 1, // nc
151
152 // Authorization
153 "Digest username=\"foo bar\", realm=\"DRealm1\", "
154 "nonce=\"Ure30oRXBAA=7eca98bbf521ac6642820b11b86bd2d9ed7edc70\", "
155 "uri=\"/test/drealm1/\", algorithm=MD5, "
156 "response=\"93c9c6d5930af3b0eb26c745e02b04a0\", "
157 "qop=auth, nc=00000001, cnonce=\"082c875dcb2ca740\""
158 },
159
[email protected]b85a4f12008-10-29 00:11:08160 { // MD5 with no username.
161 "GET",
162 "/test/drealm1/",
163
164 // Challenge
165 "Digest realm=\"DRealm1\", "
166 "nonce=\"7thGplhaBAA=41fb92453c49799cf353c8cd0aabee02d61a98a8\", "
167 "algorithm=MD5, qop=\"auth\"",
168
169 "", "pass", // Username/password
170 "6509bc74daed8263", // cnonce
171 1, // nc
172
173 // Authorization
174 "Digest username=\"\", realm=\"DRealm1\", "
175 "nonce=\"7thGplhaBAA=41fb92453c49799cf353c8cd0aabee02d61a98a8\", "
176 "uri=\"/test/drealm1/\", algorithm=MD5, "
177 "response=\"bc597110f41a62d07f8b70b6977fcb61\", "
178 "qop=auth, nc=00000001, cnonce=\"6509bc74daed8263\""
179 },
180
181 { // MD5 with no username and no password.
182 "GET",
183 "/test/drealm1/",
184
185 // Challenge
186 "Digest realm=\"DRealm1\", "
187 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\", "
188 "algorithm=MD5, qop=\"auth\"",
189
190 "", "", // Username/password
191 "1522e61005789929", // cnonce
192 1, // nc
193
194 // Authorization
195 "Digest username=\"\", realm=\"DRealm1\", "
196 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\", "
197 "uri=\"/test/drealm1/\", algorithm=MD5, "
198 "response=\"22cfa2b30cb500a9591c6d55ec5590a8\", "
199 "qop=auth, nc=00000001, cnonce=\"1522e61005789929\""
200 },
201
[email protected]c3b35c22008-09-27 03:19:42202 { // No algorithm, and no qop.
203 "GET",
204 "/",
205
206 // Challenge
207 "Digest realm=\"Oblivion\", nonce=\"nonce-value\"",
208
209 "FooBar", "pass", // Username/password
210 "", // cnonce
211 1, // nc
212
213 // Authorization
214 "Digest username=\"FooBar\", realm=\"Oblivion\", "
215 "nonce=\"nonce-value\", uri=\"/\", "
216 "response=\"f72ff54ebde2f928860f806ec04acd1b\""
217 },
218
219 { // MD5-sess
220 "GET",
221 "/",
222
223 // Challenge
224 "Digest realm=\"Baztastic\", nonce=\"AAAAAAAA\", "
225 "algorithm=\"md5-sess\", qop=auth",
226
227 "USER", "123", // Username/password
228 "15c07961ed8575c4", // cnonce
229 1, // nc
230
231 // Authorization
232 "Digest username=\"USER\", realm=\"Baztastic\", "
233 "nonce=\"AAAAAAAA\", uri=\"/\", algorithm=MD5-sess, "
234 "response=\"cbc1139821ee7192069580570c541a03\", "
235 "qop=auth, nc=00000001, cnonce=\"15c07961ed8575c4\""
236 }
237 };
[email protected]4de702f42009-09-18 17:46:10238 GURL origin("http://www.example.com");
[email protected]b85ee492008-09-27 19:27:14239 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
[email protected]f9ee6b52008-11-08 06:46:23240 scoped_refptr<HttpAuthHandlerDigest> digest = new HttpAuthHandlerDigest;
[email protected]c3b35c22008-09-27 03:19:42241 std::string challenge = tests[i].challenge;
[email protected]f9ee6b52008-11-08 06:46:23242 EXPECT_TRUE(digest->InitFromChallenge(
[email protected]4de702f42009-09-18 17:46:10243 challenge.begin(), challenge.end(), HttpAuth::AUTH_SERVER, origin));
[email protected]c3b35c22008-09-27 03:19:42244
[email protected]f9ee6b52008-11-08 06:46:23245 std::string creds = digest->AssembleCredentials(tests[i].req_method,
[email protected]c3b35c22008-09-27 03:19:42246 tests[i].req_path, tests[i].username, tests[i].password,
247 tests[i].cnonce, tests[i].nonce_count);
[email protected]f0a51fb52009-03-05 12:46:38248
[email protected]c3b35c22008-09-27 03:19:42249 EXPECT_STREQ(tests[i].expected_creds, creds.c_str());
250 }
251}
252
253} // namespace net