Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / net / spdyproxy / http_auth_handler_spdyproxy_unittest.cc
blobf777e2b1c486b743769d4e1b90b63a75fa46e1b5
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/net/spdyproxy/http_auth_handler_spdyproxy.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_request_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace {
20 const char kValidOrigin[] = "https://www.proxy.com/";
21 const char kValidOrigin2[] = "http://www.proxy2.com/";
22 const char kValidChallenge[] = "SpdyProxy realm=\"SpdyProxy\", "
23 "ps=\"1-2-3-4\"";
25 } // namespace
27 namespace spdyproxy {
29 using net::ERR_INVALID_RESPONSE;
30 using net::ERR_UNSUPPORTED_AUTH_SCHEME;
31 using net::OK;
32 using net::AuthCredentials;
33 using net::BoundNetLog;
34 using net::CompletionCallback;
35 using net::Error;
36 using net::HttpAuth;
37 using net::HttpAuthHandler;
38 using net::HttpRequestInfo;
40 TEST(HttpAuthHandlerSpdyProxyTest, GenerateAuthToken) {
41 // Verifies that challenge parsing is expected as described in individual
42 // cases below.
43 static const struct {
44 Error err1, // Expected response from hander creation
45 err2; // Expected response from GenerateAuthToken
46 const char* origin; // Origin for challenge
47 const char* challenge; // Challenge string
48 const char* expected_credentials;
49 } tests[] = {
50 // A well-formed challenge where a sid is provided produces a valid
51 // response header echoing the sid and ps token, for either origin.
52 { OK, OK,
53 kValidOrigin,
54 kValidChallenge,
55 "SpdyProxy ps=\"1-2-3-4\", sid=\"sid-string\"",},
57 { OK, OK,
58 kValidOrigin2,
59 kValidChallenge,
60 "SpdyProxy ps=\"1-2-3-4\", sid=\"sid-string\"",},
62 // An origin matching host but not scheme returns
63 // ERR_UNSUPPORTED_AUTH_SCHEME
64 { ERR_UNSUPPORTED_AUTH_SCHEME, OK,
65 "http://www.proxy.com/", "", "",},
67 // An SSL origin not matching the authorized origin returns
68 // ERR_UNSUPPORTED_AUTH_SCHEME.
69 { ERR_UNSUPPORTED_AUTH_SCHEME, OK,
70 "https://www.unconfigured.com/", "", "",},
72 // Absent ps token yields ERR_INVALID_RESPONSE.
73 { ERR_INVALID_RESPONSE, OK,
74 kValidOrigin, "SpdyProxy realm=\"SpdyProxy\"", "",},
77 // Run each test case for both proxy and server auth.
78 HttpAuth::Target targets[] = { HttpAuth::AUTH_SERVER, HttpAuth::AUTH_PROXY };
81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(targets); ++i) {
82 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(tests); ++j) {
83 GURL origin(tests[j].origin);
84 GURL authorized_origin(kValidOrigin);
85 GURL authorized_origin2(kValidOrigin2);
86 std::vector<GURL> authorized_origins;
87 authorized_origins.push_back(authorized_origin);
88 authorized_origins.push_back(authorized_origin2);
89 HttpAuthHandlerSpdyProxy::Factory factory(authorized_origins);
90 scoped_ptr<HttpAuthHandler> spdyproxy;
91 EXPECT_EQ(tests[j].err1, factory.CreateAuthHandlerFromString(
92 tests[j].challenge, targets[i], origin, BoundNetLog(),
93 &spdyproxy));
94 if (tests[j].err1 != OK)
95 continue;
96 AuthCredentials credentials(base::ASCIIToUTF16(""),
97 base::ASCIIToUTF16("sid-string"));
98 HttpRequestInfo request_info;
99 std::string auth_token;
100 int rv = spdyproxy->GenerateAuthToken(&credentials, &request_info,
101 CompletionCallback(), &auth_token);
102 EXPECT_EQ(tests[j].err2, rv);
103 if (tests[i].err2 != OK)
104 continue;
105 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str());
110 TEST(HttpAuthHandlerSpdyProxyTest, HandleAnotherChallenge) {
111 // Verifies that any repeat challenge is treated as a failure.
112 GURL origin(kValidOrigin);
113 GURL accepted_origin(kValidOrigin);
114 std::vector<GURL> accepted_origins;
115 accepted_origins.push_back(accepted_origin);
116 HttpAuthHandlerSpdyProxy::Factory factory(accepted_origins);
117 scoped_ptr<HttpAuthHandler> spdyproxy;
118 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString(
119 kValidChallenge, HttpAuth::AUTH_PROXY, origin,
120 BoundNetLog(), &spdyproxy));
121 std::string challenge(kValidChallenge);
122 HttpAuth::ChallengeTokenizer tok(challenge.begin(),
123 challenge.end());
124 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT,
125 spdyproxy->HandleAnotherChallenge(&tok));
128 TEST(HttpAuthHandlerSpdyProxyTest, ParseChallenge) {
129 // Verifies that various challenge strings are parsed appropriately as
130 // described below.
131 static const struct {
132 const char* challenge;
133 int expected_rv;
134 const char* expected_ps;
135 const char* expected_realm;
136 } tests[] = {
137 // Absent parameters fails.
138 { "SpdyProxy", ERR_INVALID_RESPONSE, "", "", },
140 // Empty parameters fails.
141 { "SpdyProxy ps=\"\"", ERR_INVALID_RESPONSE, "", "", },
143 // Valid challenge parses successfully.
144 { kValidChallenge, OK, "1-2-3-4", "SpdyProxy", },
146 GURL origin(kValidOrigin);
147 GURL accepted_origin(kValidOrigin);
148 GURL accepted_origin2(kValidOrigin2);
149 std::vector<GURL> accepted_origins;
150 accepted_origins.push_back(accepted_origin2);
151 accepted_origins.push_back(accepted_origin);
152 HttpAuthHandlerSpdyProxy::Factory factory(accepted_origins);
153 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
154 std::string challenge = tests[i].challenge;
155 scoped_ptr<HttpAuthHandler> spdyproxy;
156 int rv = factory.CreateAuthHandlerFromString(
157 challenge, HttpAuth::AUTH_PROXY, origin, BoundNetLog(), &spdyproxy);
158 EXPECT_EQ(tests[i].expected_rv, rv);
159 if (rv == OK) {
160 EXPECT_EQ(tests[i].expected_realm, spdyproxy->realm());
161 HttpAuthHandlerSpdyProxy* as_spdyproxy =
162 static_cast<HttpAuthHandlerSpdyProxy*>(spdyproxy.get());
163 EXPECT_EQ(tests[i].expected_ps,
164 as_spdyproxy->ps_token_);
169 } // namespace spdyproxy