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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/login/login_handler.h" | 9 #include "chrome/browser/ui/login/login_handler.h" |
9 #include "net/base/auth.h" | 10 #include "net/base/auth.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 12 #include "url/gurl.h" |
12 | 13 |
13 TEST(LoginPromptTest, GetSignonRealm) { | 14 namespace { |
| 15 |
| 16 const char kHttpUrl[] = "http://example.com/foo/bar"; |
| 17 const char kBasicAuthScheme[] = "Basic"; |
| 18 const char kFooRealm[] = "Foo"; |
| 19 const char kInsecureProxy[] = "Your connection to this site is not private."; |
| 20 |
| 21 enum TargetType { PROXY, SERVER }; |
| 22 |
| 23 const struct TestCase { |
| 24 const char* const request_url; |
| 25 struct { |
| 26 TargetType target_type; |
| 27 const char* const scheme; |
| 28 const char* const realm; |
| 29 const char* const challenger; |
| 30 } auth_info; |
| 31 struct { |
| 32 const char* const authority; |
| 33 const char* const explanation; |
| 34 const char* const signon_realm; |
| 35 } expected; |
| 36 } kTestCases[]{ |
| 37 // Insecure proxy |
| 38 {kHttpUrl, |
| 39 {PROXY, kBasicAuthScheme, kFooRealm, "http://example.com"}, |
| 40 {"The proxy http://example.com requires a username and password.", |
| 41 kInsecureProxy, "example.com:80/Foo"}}, |
| 42 |
| 43 // Insecure proxy on non-standard port |
| 44 {kHttpUrl, |
| 45 {PROXY, kBasicAuthScheme, kFooRealm, "http://example.com:8009"}, |
| 46 {"The proxy http://example.com:8009 requires a username and password.", |
| 47 kInsecureProxy, "example.com:8009/Foo"}}, |
| 48 |
| 49 // Secure proxy |
| 50 {kHttpUrl, |
| 51 {PROXY, kBasicAuthScheme, kFooRealm, "https://example.com"}, |
| 52 {"The proxy https://example.com requires a username and password.", "", |
| 53 "example.com:443/Foo"}}, |
| 54 |
| 55 // Secure proxy on non-standard port |
| 56 {kHttpUrl, |
| 57 {PROXY, kBasicAuthScheme, kFooRealm, "https://example.com:446"}, |
| 58 {"The proxy https://example.com:446 requires a username and password.", "", |
| 59 "example.com:446/Foo"}}, |
| 60 |
| 61 // localhost |
| 62 {kHttpUrl, |
| 63 {PROXY, kBasicAuthScheme, kFooRealm, "http://localhost:7323"}, |
| 64 {"The proxy http://localhost:7323 requires a username and password.", "", |
| 65 "localhost:7323/Foo"}}, |
| 66 |
| 67 // Secure server |
| 68 {"https://www.nowhere.org/dir/index.html", |
| 69 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 70 {"https://www.nowhere.org requires a username and password.", "", |
| 71 "https://www.nowhere.org/Foo"}}, |
| 72 |
| 73 // URL uses default port. |
| 74 {"https://www.nowhere.org:443/dir/index.html", |
| 75 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 76 {"https://www.nowhere.org requires a username and password.", "", |
| 77 "https://www.nowhere.org/Foo"}}, |
| 78 |
| 79 // URL uses non-default port. |
| 80 {"https://www.nowhere.org:8443/dir/index.html", |
| 81 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 82 {"https://www.nowhere.org:8443 requires a username and password.", "", |
| 83 "https://www.nowhere.org:8443/Foo"}}, |
| 84 |
| 85 // URL has no trailing slash. |
| 86 {"https://www.nowhere.org", |
| 87 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 88 {"https://www.nowhere.org requires a username and password.", "", |
| 89 "https://www.nowhere.org/Foo"}}, |
| 90 |
| 91 // username:password |
| 92 {"https://foo:bar@www.nowhere.org/dir/index.html", |
| 93 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 94 {"https://www.nowhere.org requires a username and password.", "", |
| 95 "https://www.nowhere.org/Foo"}}, |
| 96 |
| 97 // query |
| 98 {"https://www.nowhere.org/dir/index.html?id=965362", |
| 99 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 100 {"https://www.nowhere.org requires a username and password.", "", |
| 101 "https://www.nowhere.org/Foo"}}, |
| 102 |
| 103 // reference |
| 104 {"https://www.nowhere.org/dir/index.html#toc", |
| 105 {SERVER, kBasicAuthScheme, kFooRealm, nullptr}, |
| 106 {"https://www.nowhere.org requires a username and password.", "", |
| 107 "https://www.nowhere.org/Foo"}}, |
| 108 }; |
| 109 |
| 110 } // namespace |
| 111 |
| 112 TEST(LoginHandlerTest, DialogStringsAndRealm) { |
14 scoped_refptr<net::AuthChallengeInfo> auth_info = new net::AuthChallengeInfo; | 113 scoped_refptr<net::AuthChallengeInfo> auth_info = new net::AuthChallengeInfo; |
15 auth_info->is_proxy = false; // server auth | 114 for (const auto& test_case : kTestCases) { |
16 // auth_info->host is intentionally left empty. | 115 GURL request_url(test_case.request_url); |
17 auth_info->scheme = "Basic"; | 116 auth_info->is_proxy = test_case.auth_info.target_type == PROXY; |
18 auth_info->realm = "WallyWorld"; | 117 auth_info->scheme = test_case.auth_info.scheme; |
| 118 auth_info->realm = test_case.auth_info.realm; |
| 119 auth_info->challenger = url::Origin( |
| 120 test_case.auth_info.challenger ? GURL(test_case.auth_info.challenger) |
| 121 : request_url); |
19 | 122 |
20 std::string url[] = { | 123 SCOPED_TRACE(::testing::Message() |
21 "https://www.nowhere.org/dir/index.html", | 124 << "request_url:" << test_case.request_url |
22 "https://www.nowhere.org:443/dir/index.html", // default port | 125 << " auth_info: { is_proxy:" << auth_info->is_proxy |
23 "https://www.nowhere.org:8443/dir/index.html", // non-default port | 126 << " scheme:'" << auth_info->scheme << "' realm:'" |
24 "https://www.nowhere.org", // no trailing slash | 127 << auth_info->realm << "' challenger:'" |
25 "https://foo:bar@www.nowhere.org/dir/index.html", // username:password | 128 << auth_info->challenger.Serialize() << "' }"); |
26 "https://www.nowhere.org/dir/index.html?id=965362", // query | 129 base::string16 authority; |
27 "https://www.nowhere.org/dir/index.html#toc", // reference | 130 base::string16 explanation; |
28 }; | |
29 | 131 |
30 std::string expected[] = { | 132 LoginHandler::GetDialogStrings(request_url, *auth_info, &authority, |
31 "https://www.nowhere.org/WallyWorld", | 133 &explanation); |
32 "https://www.nowhere.org/WallyWorld", | 134 EXPECT_STREQ(test_case.expected.authority, |
33 "https://www.nowhere.org:8443/WallyWorld", | 135 base::UTF16ToASCII(authority).c_str()); |
34 "https://www.nowhere.org/WallyWorld", | 136 EXPECT_STREQ(test_case.expected.explanation, |
35 "https://www.nowhere.org/WallyWorld", | 137 base::UTF16ToASCII(explanation).c_str()); |
36 "https://www.nowhere.org/WallyWorld", | |
37 "https://www.nowhere.org/WallyWorld" | |
38 }; | |
39 | 138 |
40 for (size_t i = 0; i < arraysize(url); i++) { | 139 EXPECT_STREQ(test_case.expected.signon_realm, |
41 std::string key = GetSignonRealm(GURL(url[i]), *auth_info.get()); | 140 LoginHandler::GetSignonRealm(request_url, *auth_info).c_str()); |
42 EXPECT_EQ(expected[i], key); | |
43 } | 141 } |
44 } | 142 } |
OLD | NEW |