Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: chrome/browser/ssl/ssl_error_classification_unittest.cc

Issue 400323002: Refactor the captive portal code to move from the ssl_blocking_page class to the ssl_error_classific (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase-Update Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/ssl/ssl_error_classification.h" 5 #include "chrome/browser/ssl/ssl_error_classification.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/strings/string_split.h" 8 #include "base/strings/string_split.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "content/public/browser/web_contents.h"
12 #include "net/base/net_errors.h"
10 #include "net/base/test_data_directory.h" 13 #include "net/base/test_data_directory.h"
11 #include "net/cert/x509_cert_types.h" 14 #include "net/cert/x509_cert_types.h"
12 #include "net/cert/x509_certificate.h" 15 #include "net/cert/x509_certificate.h"
13 #include "net/test/cert_test_util.h" 16 #include "net/test/cert_test_util.h"
14 #include "net/test/test_certificate_data.h" 17 #include "net/test/test_certificate_data.h"
15 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 19 #include "url/gurl.h"
17 20
18 using base::Time; 21 using base::Time;
22 using content::WebContents;
19 23
20 TEST(SSLErrorClassificationTest, TestDateInvalidScore) { 24 class SSLErrorClassificationTest : public ChromeRenderViewHostTestHarness {
25 public:
26 SSLErrorClassificationTest() {
27 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
28 }
29 };
30
31 TEST_F(SSLErrorClassificationTest, TestDateInvalidScore) {
21 base::FilePath certs_dir = net::GetTestCertsDirectory(); 32 base::FilePath certs_dir = net::GetTestCertsDirectory();
22 scoped_refptr<net::X509Certificate> expired_cert = 33 scoped_refptr<net::X509Certificate> expired_cert =
23 net::ImportCertFromFile(certs_dir, "expired_cert.pem"); 34 net::ImportCertFromFile(certs_dir, "expired_cert.pem");
24 base::Time time; 35 base::Time time;
25 GURL origin("https://example.com"); 36 GURL origin("https://example.com");
37 int cert_error = net::ERR_CERT_DATE_INVALID;
38 WebContents* contents = web_contents();
26 39
27 { 40 {
28 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time)); 41 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time));
29 SSLErrorClassification ssl_error(time, origin, *expired_cert); 42 SSLErrorClassification ssl_error(contents,
43 time,
44 origin,
45 cert_error,
46 *expired_cert);
30 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 47 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry());
31 } 48 }
32 49
33 { 50 {
34 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time)); 51 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time));
35 SSLErrorClassification ssl_error(time, origin, *expired_cert); 52 SSLErrorClassification ssl_error(contents,
53 time,
54 origin,
55 cert_error,
56 *expired_cert);
36 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 57 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry());
37 } 58 }
38 59
39 { 60 {
40 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time)); 61 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time));
41 SSLErrorClassification ssl_error(time, origin, *expired_cert); 62 SSLErrorClassification ssl_error(contents,
63 time,
64 origin,
65 cert_error,
66 *expired_cert);
42 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 67 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry());
43 } 68 }
44 } 69 }
45 70
46 TEST(SSLErrorClassificationTest, TestNameMismatch) { 71 TEST_F(SSLErrorClassificationTest, TestNameMismatch) {
47 scoped_refptr<net::X509Certificate> google_cert( 72 scoped_refptr<net::X509Certificate> google_cert(
48 net::X509Certificate::CreateFromBytes( 73 net::X509Certificate::CreateFromBytes(
49 reinterpret_cast<const char*>(google_der), sizeof(google_der))); 74 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
50 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert); 75 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert);
51 base::Time time = base::Time::NowFromSystemTime(); 76 base::Time time = base::Time::NowFromSystemTime();
52 std::vector<std::string> dns_names_google; 77 std::vector<std::string> dns_names_google;
53 dns_names_google.push_back("www"); 78 dns_names_google.push_back("www");
54 dns_names_google.push_back("google"); 79 dns_names_google.push_back("google");
55 dns_names_google.push_back("com"); 80 dns_names_google.push_back("com");
56 std::vector<std::vector<std::string>> dns_name_tokens_google; 81 std::vector<std::vector<std::string>> dns_name_tokens_google;
57 dns_name_tokens_google.push_back(dns_names_google); 82 dns_name_tokens_google.push_back(dns_names_google);
83 int cert_error = net::ERR_CERT_COMMON_NAME_INVALID;
84 WebContents* contents = web_contents();
58 { 85 {
59 GURL origin("https://google.com"); 86 GURL origin("https://google.com");
60 std::string host_name = origin.host(); 87 std::string host_name = origin.host();
61 std::vector<std::string> host_name_tokens; 88 std::vector<std::string> host_name_tokens;
62 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 89 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
63 SSLErrorClassification ssl_error(time, origin, *google_cert); 90 SSLErrorClassification ssl_error(contents,
91 time,
92 origin,
93 cert_error,
94 *google_cert);
64 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch()); 95 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch());
65 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 96 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
66 dns_name_tokens_google)); 97 dns_name_tokens_google));
67 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 98 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
68 host_name_tokens)); 99 host_name_tokens));
69 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); 100 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
101 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
70 } 102 }
71 103
72 { 104 {
73 GURL origin("https://foo.blah.google.com"); 105 GURL origin("https://foo.blah.google.com");
74 std::string host_name = origin.host(); 106 std::string host_name = origin.host();
75 std::vector<std::string> host_name_tokens; 107 std::vector<std::string> host_name_tokens;
76 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 108 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
77 SSLErrorClassification ssl_error(time, origin, *google_cert); 109 SSLErrorClassification ssl_error(contents,
110 time,
111 origin,
112 cert_error,
113 *google_cert);
78 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 114 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
79 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 115 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
80 dns_name_tokens_google)); 116 dns_name_tokens_google));
81 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 117 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
82 host_name_tokens)); 118 host_name_tokens));
83 } 119 }
84 120
85 { 121 {
86 GURL origin("https://foo.www.google.com"); 122 GURL origin("https://foo.www.google.com");
87 std::string host_name = origin.host(); 123 std::string host_name = origin.host();
88 std::vector<std::string> host_name_tokens; 124 std::vector<std::string> host_name_tokens;
89 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 125 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
90 SSLErrorClassification ssl_error(time, origin, *google_cert); 126 SSLErrorClassification ssl_error(contents,
127 time,
128 origin,
129 cert_error,
130 *google_cert);
91 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 131 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
92 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens, 132 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens,
93 dns_name_tokens_google)); 133 dns_name_tokens_google));
94 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 134 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
95 host_name_tokens)); 135 host_name_tokens));
96 } 136 }
97 137
98 { 138 {
99 GURL origin("https://www.google.com.foo"); 139 GURL origin("https://www.google.com.foo");
100 std::string host_name = origin.host(); 140 std::string host_name = origin.host();
101 std::vector<std::string> host_name_tokens; 141 std::vector<std::string> host_name_tokens;
102 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 142 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
103 SSLErrorClassification ssl_error(time, origin, *google_cert); 143 SSLErrorClassification ssl_error(contents,
144 time,
145 origin,
146 cert_error,
147 *google_cert);
104 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 148 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
105 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 149 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
106 dns_name_tokens_google)); 150 dns_name_tokens_google));
107 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 151 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
108 host_name_tokens)); 152 host_name_tokens));
109 } 153 }
110 154
111 { 155 {
112 GURL origin("https://www.foogoogle.com."); 156 GURL origin("https://www.foogoogle.com.");
113 std::string host_name = origin.host(); 157 std::string host_name = origin.host();
114 std::vector<std::string> host_name_tokens; 158 std::vector<std::string> host_name_tokens;
115 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 159 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
116 SSLErrorClassification ssl_error(time, origin, *google_cert); 160 SSLErrorClassification ssl_error(contents,
161 time,
162 origin,
163 cert_error,
164 *google_cert);
117 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 165 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
118 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 166 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
119 dns_name_tokens_google)); 167 dns_name_tokens_google));
120 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 168 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
121 host_name_tokens)); 169 host_name_tokens));
122 } 170 }
123 171
124 scoped_refptr<net::X509Certificate> webkit_cert( 172 scoped_refptr<net::X509Certificate> webkit_cert(
125 net::X509Certificate::CreateFromBytes( 173 net::X509Certificate::CreateFromBytes(
126 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); 174 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
127 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert); 175 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert);
128 std::vector<std::string> dns_names_webkit; 176 std::vector<std::string> dns_names_webkit;
129 dns_names_webkit.push_back("webkit"); 177 dns_names_webkit.push_back("webkit");
130 dns_names_webkit.push_back("org"); 178 dns_names_webkit.push_back("org");
131 std::vector<std::vector<std::string>> dns_name_tokens_webkit; 179 std::vector<std::vector<std::string>> dns_name_tokens_webkit;
132 dns_name_tokens_webkit.push_back(dns_names_webkit); 180 dns_name_tokens_webkit.push_back(dns_names_webkit);
133 { 181 {
134 GURL origin("https://a.b.webkit.org"); 182 GURL origin("https://a.b.webkit.org");
135 std::string host_name = origin.host(); 183 std::string host_name = origin.host();
136 std::vector<std::string> host_name_tokens; 184 std::vector<std::string> host_name_tokens;
137 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 185 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
138 SSLErrorClassification ssl_error(time, origin, *webkit_cert); 186 SSLErrorClassification ssl_error(contents,
187 time,
188 origin,
189 cert_error,
190 *webkit_cert);
139 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 191 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
140 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 192 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
141 dns_name_tokens_webkit)); 193 dns_name_tokens_webkit));
142 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit, 194 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit,
143 host_name_tokens)); 195 host_name_tokens));
144 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); 196 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
197 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
145 } 198 }
146 } 199 }
147 200
148 TEST(SSLErrorClassificationTest, TestHostNameHasKnownTLD) { 201 TEST_F(SSLErrorClassificationTest, TestHostNameHasKnownTLD) {
149 std::string url1 = "www.google.com"; 202 std::string url1 = "www.google.com";
150 std::string url2 = "b.appspot.com"; 203 std::string url2 = "b.appspot.com";
151 std::string url3 = "a.private"; 204 std::string url3 = "a.private";
152 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1)); 205 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1));
153 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2)); 206 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2));
154 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3)); 207 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3));
155 } 208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698