1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "testing/gtest/include/gtest/gtest.h"
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h"
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/http/http_auth_handler_digest.h"
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace net {
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(HttpAuthHandlerDigestTest, ParseChallenge) {
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static const struct {
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // The challenge string.
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* challenge;
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Expected return value of ParseChallenge.
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool parsed_success;
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // The expected values that were parsed.
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* parsed_realm;
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* parsed_nonce;
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* parsed_domain;
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* parsed_opaque;
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool parsed_stale;
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    int parsed_algorithm;
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    int parsed_qop;
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  } tests[] = {
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    {
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest nonce=\"xyz\", realm=\"Thunder Bluff\"",
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      true,
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Thunder Bluff",
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_UNSPECIFIED
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // Check that when algorithm has an unsupported value, parsing fails.
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest nonce=\"xyz\", algorithm=\"awezum\", realm=\"Thunder\"",
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // The remaining values don't matter (but some have been set already).
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_UNSPECIFIED
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // Check that algorithm's value is case insensitive.
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest nonce=\"xyz\", algorithm=\"mD5\", realm=\"Oblivion\"",
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      true,
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Oblivion",
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_MD5,
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_UNSPECIFIED
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // Check that md5-sess is recognized, as is single QOP
65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest nonce=\"xyz\", algorithm=\"md5-sess\", "
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott          "realm=\"Oblivion\", qop=\"auth\"",
67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      true,
68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Oblivion",
69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_MD5_SESS,
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_AUTH
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // We allow the realm to be omitted, and will default it to empty string.
78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // See http://crbug.com/20984.
79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest nonce=\"xyz\"",
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      true,
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_UNSPECIFIED
88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // Try with realm set to empty string.
91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"\", nonce=\"xyz\"",
92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      true,
93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "xyz",
95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "",
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      false,
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED,
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      HttpAuthHandlerDigest::QOP_UNSPECIFIED
100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    }
101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    std::string challenge(tests[i].challenge);
105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    scoped_refptr<HttpAuthHandlerDigest> digest = new HttpAuthHandlerDigest;
107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    bool ok = digest->ParseChallenge(challenge.begin(), challenge.end());
108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_EQ(tests[i].parsed_success, ok);
110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_STREQ(tests[i].parsed_realm, digest->realm_.c_str());
111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_STREQ(tests[i].parsed_nonce, digest->nonce_.c_str());
112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_STREQ(tests[i].parsed_domain, digest->domain_.c_str());
113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_STREQ(tests[i].parsed_opaque, digest->opaque_.c_str());
114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_EQ(tests[i].parsed_stale, digest->stale_);
115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_EQ(tests[i].parsed_algorithm, digest->algorithm_);
116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_EQ(tests[i].parsed_qop, digest->qop_);
117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(HttpAuthHandlerDigestTest, AssembleCredentials) {
121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static const struct {
122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* req_method;
123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* req_path;
124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* challenge;
125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* username;
126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* password;
127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* cnonce;
128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    int nonce_count;
129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const char* expected_creds;
130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  } tests[] = {
131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // MD5 with username/password
132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/test/drealm1",
134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"DRealm1\", "
137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\", "
138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "algorithm=MD5, qop=\"auth\"",
139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "foo", "bar", // username/password
141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "082c875dcb2ca740", // cnonce
142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"foo\", realm=\"DRealm1\", "
146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\", "
147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "uri=\"/test/drealm1\", algorithm=MD5, "
148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"bcfaa62f1186a31ff1b474a19a17cf57\", "
149c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "qop=auth, nc=00000001, cnonce=\"082c875dcb2ca740\""
150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // MD5 with username but empty password. username has space in it.
153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/test/drealm1/",
155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"DRealm1\", "
158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"Ure30oRXBAA=7eca98bbf521ac6642820b11b86bd2d9ed7edc70\", "
159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "algorithm=MD5, qop=\"auth\"",
160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "foo bar", "", // Username/password
162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "082c875dcb2ca740", // cnonce
163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"foo bar\", realm=\"DRealm1\", "
167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"Ure30oRXBAA=7eca98bbf521ac6642820b11b86bd2d9ed7edc70\", "
168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "uri=\"/test/drealm1/\", algorithm=MD5, "
169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"93c9c6d5930af3b0eb26c745e02b04a0\", "
170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "qop=auth, nc=00000001, cnonce=\"082c875dcb2ca740\""
171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // MD5 with no username.
174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/test/drealm1/",
176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"DRealm1\", "
179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"7thGplhaBAA=41fb92453c49799cf353c8cd0aabee02d61a98a8\", "
180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "algorithm=MD5, qop=\"auth\"",
181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "", "pass", // Username/password
183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "6509bc74daed8263", // cnonce
184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"\", realm=\"DRealm1\", "
188c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"7thGplhaBAA=41fb92453c49799cf353c8cd0aabee02d61a98a8\", "
189c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "uri=\"/test/drealm1/\", algorithm=MD5, "
190c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"bc597110f41a62d07f8b70b6977fcb61\", "
191c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "qop=auth, nc=00000001, cnonce=\"6509bc74daed8263\""
192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // MD5 with no username and no password.
195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/test/drealm1/",
197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
199c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"DRealm1\", "
200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\", "
201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "algorithm=MD5, qop=\"auth\"",
202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "", "", // Username/password
204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "1522e61005789929", // cnonce
205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"\", realm=\"DRealm1\", "
209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\", "
210c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "uri=\"/test/drealm1/\", algorithm=MD5, "
211c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"22cfa2b30cb500a9591c6d55ec5590a8\", "
212c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "qop=auth, nc=00000001, cnonce=\"1522e61005789929\""
213c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
214c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
215c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // No algorithm, and no qop.
216c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
217c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/",
218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"Oblivion\", nonce=\"nonce-value\"",
221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "FooBar", "pass", // Username/password
223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "", // cnonce
224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
226c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
227c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"FooBar\", realm=\"Oblivion\", "
228c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"nonce-value\", uri=\"/\", "
229c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"f72ff54ebde2f928860f806ec04acd1b\""
230c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    },
231c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
232c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    { // MD5-sess
233c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "GET",
234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "/",
235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Challenge
237c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest realm=\"Baztastic\", nonce=\"AAAAAAAA\", "
238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "algorithm=\"md5-sess\", qop=auth",
239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "USER", "123", // Username/password
241c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "15c07961ed8575c4", // cnonce
242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      1, // nc
243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      // Authorization
245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "Digest username=\"USER\", realm=\"Baztastic\", "
246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "nonce=\"AAAAAAAA\", uri=\"/\", algorithm=MD5-sess, "
247c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "response=\"cbc1139821ee7192069580570c541a03\", "
248c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      "qop=auth, nc=00000001, cnonce=\"15c07961ed8575c4\""
249c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    }
250c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
251c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  GURL origin("http://www.example.com");
252c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    scoped_refptr<HttpAuthHandlerDigest> digest = new HttpAuthHandlerDigest;
254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    std::string challenge = tests[i].challenge;
255c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_TRUE(digest->InitFromChallenge(
256c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott        challenge.begin(), challenge.end(), HttpAuth::AUTH_SERVER, origin));
257c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    std::string creds = digest->AssembleCredentials(tests[i].req_method,
259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott        tests[i].req_path, tests[i].username, tests[i].password,
260c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott        tests[i].cnonce, tests[i].nonce_count);
261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
262c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    EXPECT_STREQ(tests[i].expected_creds, creds.c_str());
263c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
264c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
265c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} // namespace net
267