| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/net/spdyproxy/http_auth_handler_spdyproxy.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_request_info.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kValidOrigin[] = "https://www.proxy.com/"; |
| 19 const char kValidChallenge[] = "SpdyProxy realm=\"SpdyProxy\", " |
| 20 "ps=\"1-2-3-4\""; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 namespace spdyproxy { |
| 25 |
| 26 using net::ERR_INVALID_RESPONSE; |
| 27 using net::ERR_UNSUPPORTED_AUTH_SCHEME; |
| 28 using net::OK; |
| 29 using net::AuthCredentials; |
| 30 using net::BoundNetLog; |
| 31 using net::CompletionCallback; |
| 32 using net::Error; |
| 33 using net::HttpAuth; |
| 34 using net::HttpAuthHandler; |
| 35 using net::HttpRequestInfo; |
| 36 |
| 37 TEST(HttpAuthHandlerSpdyProxyTest, GenerateAuthToken) { |
| 38 // Verifies that challenge parsing is expected as described in individual |
| 39 // cases below. |
| 40 static const struct { |
| 41 Error err1, err2; |
| 42 const char* origin; |
| 43 const char* challenge; |
| 44 const char* username; |
| 45 const char* sid; |
| 46 const char* expected_credentials; |
| 47 } tests[] = { |
| 48 // A well-formed challenge where a sid is provided produces a valid |
| 49 // response header echoing the sid and ps token. |
| 50 { OK, OK, |
| 51 kValidOrigin, |
| 52 kValidChallenge, |
| 53 "", |
| 54 "sid-string", |
| 55 "SpdyProxy ps=\"1-2-3-4\", sid=\"sid-string\"",}, |
| 56 |
| 57 // A non-SSL origin returns ERR_UNSUPPORTED_AUTH_SCHEME. |
| 58 { ERR_UNSUPPORTED_AUTH_SCHEME, OK, |
| 59 "http://www.proxy.com/", "", "", "", "",}, |
| 60 |
| 61 // An SSL origin not matching the authorized origin returns |
| 62 // ERR_UNSUPPORTED_AUTH_SCHEME. |
| 63 { ERR_UNSUPPORTED_AUTH_SCHEME, OK, |
| 64 "https://www.unconfigured.com/", "", "", "", "",}, |
| 65 |
| 66 // Absent ps token yields ERR_INVALID_RESPONSE. |
| 67 { ERR_INVALID_RESPONSE, OK, |
| 68 kValidOrigin, "SpdyProxy realm=\"SpdyProxy\"", "", "", "",}, |
| 69 }; |
| 70 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 71 GURL origin(tests[i].origin); |
| 72 GURL authorized_origin(kValidOrigin); |
| 73 HttpAuthHandlerSpdyProxy::Factory factory(authorized_origin); |
| 74 scoped_ptr<HttpAuthHandler> spdyproxy; |
| 75 EXPECT_EQ(tests[i].err1, factory.CreateAuthHandlerFromString( |
| 76 tests[i].challenge, HttpAuth::AUTH_PROXY, origin, BoundNetLog(), |
| 77 &spdyproxy)); |
| 78 if (tests[i].err1 != OK) { |
| 79 continue; |
| 80 } |
| 81 AuthCredentials credentials(ASCIIToUTF16(tests[i].username), |
| 82 ASCIIToUTF16(tests[i].sid)); |
| 83 HttpRequestInfo request_info; |
| 84 std::string auth_token; |
| 85 int rv = spdyproxy->GenerateAuthToken(&credentials, &request_info, |
| 86 CompletionCallback(), &auth_token); |
| 87 EXPECT_EQ(tests[i].err2, rv); |
| 88 if (tests[i].err2 != OK) { |
| 89 continue; |
| 90 } |
| 91 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); |
| 92 } |
| 93 } |
| 94 |
| 95 TEST(HttpAuthHandlerSpdyProxyTest, NonProxyAuthTypeFails) { |
| 96 // Verifies that an authorization request fails if requested by an ordinary |
| 97 // site. (i.e., HttpAuth::AUTH_SERVER) |
| 98 GURL origin(kValidOrigin); |
| 99 GURL accepted_origin(kValidOrigin); |
| 100 HttpAuthHandlerSpdyProxy::Factory factory(accepted_origin); |
| 101 scoped_ptr<HttpAuthHandler> spdyproxy; |
| 102 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, factory.CreateAuthHandlerFromString( |
| 103 kValidChallenge, HttpAuth::AUTH_SERVER, origin, |
| 104 BoundNetLog(), &spdyproxy)); |
| 105 } |
| 106 |
| 107 TEST(HttpAuthHandlerSpdyProxyTest, HandleAnotherChallenge) { |
| 108 // Verifies that any repeat challenge is treated as a failure. |
| 109 GURL origin(kValidOrigin); |
| 110 GURL accepted_origin(kValidOrigin); |
| 111 HttpAuthHandlerSpdyProxy::Factory factory(accepted_origin); |
| 112 scoped_ptr<HttpAuthHandler> spdyproxy; |
| 113 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( |
| 114 kValidChallenge, HttpAuth::AUTH_PROXY, origin, |
| 115 BoundNetLog(), &spdyproxy)); |
| 116 std::string challenge(kValidChallenge); |
| 117 HttpAuth::ChallengeTokenizer tok(challenge.begin(), |
| 118 challenge.end()); |
| 119 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, |
| 120 spdyproxy->HandleAnotherChallenge(&tok)); |
| 121 } |
| 122 |
| 123 TEST(HttpAuthHandlerSpdyProxyTest, ParseChallenge) { |
| 124 // Verifies that various challenge strings are parsed appropriately as |
| 125 // described below. |
| 126 static const struct { |
| 127 const char* challenge; |
| 128 int expected_rv; |
| 129 const char* expected_ps; |
| 130 const char* expected_realm; |
| 131 } tests[] = { |
| 132 // Absent parameters fails. |
| 133 { "SpdyProxy", ERR_INVALID_RESPONSE, "", "", }, |
| 134 |
| 135 // Empty parameters fails. |
| 136 { "SpdyProxy ps=\"\"", ERR_INVALID_RESPONSE, "", "", }, |
| 137 |
| 138 // Valid challenge parses successfully. |
| 139 { kValidChallenge, OK, "1-2-3-4", "SpdyProxy", }, |
| 140 }; |
| 141 GURL origin(kValidOrigin); |
| 142 GURL accepted_origin(kValidOrigin); |
| 143 HttpAuthHandlerSpdyProxy::Factory factory(accepted_origin); |
| 144 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 145 std::string challenge = tests[i].challenge; |
| 146 scoped_ptr<HttpAuthHandler> spdyproxy; |
| 147 int rv = factory.CreateAuthHandlerFromString( |
| 148 challenge, HttpAuth::AUTH_PROXY, origin, BoundNetLog(), &spdyproxy); |
| 149 EXPECT_EQ(tests[i].expected_rv, rv); |
| 150 if (rv == OK) { |
| 151 EXPECT_EQ(tests[i].expected_realm, spdyproxy->realm()); |
| 152 HttpAuthHandlerSpdyProxy* as_spdyproxy = |
| 153 static_cast<HttpAuthHandlerSpdyProxy*>(spdyproxy.get()); |
| 154 EXPECT_EQ(tests[i].expected_ps, |
| 155 as_spdyproxy->ps_token_); |
| 156 } |
| 157 } |
| 158 } |
| 159 |
| 160 } // namespace spdyproxy |
| OLD | NEW |