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

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

Issue 516373003: Refactor captive portal code from SSLBlockingPage to SSLErrorClassification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed misplaced brace Created 6 years, 3 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
« no previous file with comments | « chrome/browser/ssl/ssl_error_classification.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.get()); 75 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert.get());
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));
70 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting()); 101 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
71 } 102 }
72 103
73 { 104 {
74 GURL origin("https://foo.blah.google.com"); 105 GURL origin("https://foo.blah.google.com");
75 std::string host_name = origin.host(); 106 std::string host_name = origin.host();
76 std::vector<std::string> host_name_tokens; 107 std::vector<std::string> host_name_tokens;
77 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 108 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
78 SSLErrorClassification ssl_error(time, origin, *google_cert); 109 SSLErrorClassification ssl_error(contents,
110 time,
111 origin,
112 cert_error,
113 *google_cert);
79 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 114 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
80 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 115 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
81 dns_name_tokens_google)); 116 dns_name_tokens_google));
82 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 117 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
83 host_name_tokens)); 118 host_name_tokens));
84 } 119 }
85 120
86 { 121 {
87 GURL origin("https://foo.www.google.com"); 122 GURL origin("https://foo.www.google.com");
88 std::string host_name = origin.host(); 123 std::string host_name = origin.host();
89 std::vector<std::string> host_name_tokens; 124 std::vector<std::string> host_name_tokens;
90 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 125 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
91 SSLErrorClassification ssl_error(time, origin, *google_cert); 126 SSLErrorClassification ssl_error(contents,
127 time,
128 origin,
129 cert_error,
130 *google_cert);
92 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 131 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
93 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens, 132 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens,
94 dns_name_tokens_google)); 133 dns_name_tokens_google));
95 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 134 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
96 host_name_tokens)); 135 host_name_tokens));
97 } 136 }
98 137
99 { 138 {
100 GURL origin("https://www.google.com.foo"); 139 GURL origin("https://www.google.com.foo");
101 std::string host_name = origin.host(); 140 std::string host_name = origin.host();
102 std::vector<std::string> host_name_tokens; 141 std::vector<std::string> host_name_tokens;
103 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 142 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
104 SSLErrorClassification ssl_error(time, origin, *google_cert); 143 SSLErrorClassification ssl_error(contents,
144 time,
145 origin,
146 cert_error,
147 *google_cert);
105 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 148 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
106 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 149 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
107 dns_name_tokens_google)); 150 dns_name_tokens_google));
108 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 151 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
109 host_name_tokens)); 152 host_name_tokens));
110 } 153 }
111 154
112 { 155 {
113 GURL origin("https://www.foogoogle.com."); 156 GURL origin("https://www.foogoogle.com.");
114 std::string host_name = origin.host(); 157 std::string host_name = origin.host();
115 std::vector<std::string> host_name_tokens; 158 std::vector<std::string> host_name_tokens;
116 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 159 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
117 SSLErrorClassification ssl_error(time, origin, *google_cert); 160 SSLErrorClassification ssl_error(contents,
161 time,
162 origin,
163 cert_error,
164 *google_cert);
118 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 165 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
119 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 166 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
120 dns_name_tokens_google)); 167 dns_name_tokens_google));
121 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 168 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
122 host_name_tokens)); 169 host_name_tokens));
123 } 170 }
124 171
125 scoped_refptr<net::X509Certificate> webkit_cert( 172 scoped_refptr<net::X509Certificate> webkit_cert(
126 net::X509Certificate::CreateFromBytes( 173 net::X509Certificate::CreateFromBytes(
127 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); 174 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
128 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert.get()); 175 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert.get());
129 std::vector<std::string> dns_names_webkit; 176 std::vector<std::string> dns_names_webkit;
130 dns_names_webkit.push_back("webkit"); 177 dns_names_webkit.push_back("webkit");
131 dns_names_webkit.push_back("org"); 178 dns_names_webkit.push_back("org");
132 std::vector<std::vector<std::string>> dns_name_tokens_webkit; 179 std::vector<std::vector<std::string>> dns_name_tokens_webkit;
133 dns_name_tokens_webkit.push_back(dns_names_webkit); 180 dns_name_tokens_webkit.push_back(dns_names_webkit);
134 { 181 {
135 GURL origin("https://a.b.webkit.org"); 182 GURL origin("https://a.b.webkit.org");
136 std::string host_name = origin.host(); 183 std::string host_name = origin.host();
137 std::vector<std::string> host_name_tokens; 184 std::vector<std::string> host_name_tokens;
138 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 185 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
139 SSLErrorClassification ssl_error(time, origin, *webkit_cert); 186 SSLErrorClassification ssl_error(contents,
187 time,
188 origin,
189 cert_error,
190 *webkit_cert);
140 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 191 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
141 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 192 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
142 dns_name_tokens_webkit)); 193 dns_name_tokens_webkit));
143 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit, 194 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit,
144 host_name_tokens)); 195 host_name_tokens));
145 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); 196 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
146 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting()); 197 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
147 } 198 }
148 } 199 }
149 200
150 TEST(SSLErrorClassificationTest, TestHostNameHasKnownTLD) { 201 TEST_F(SSLErrorClassificationTest, TestHostNameHasKnownTLD) {
151 std::string url1 = "www.google.com"; 202 std::string url1 = "www.google.com";
152 std::string url2 = "b.appspot.com"; 203 std::string url2 = "b.appspot.com";
153 std::string url3 = "a.private"; 204 std::string url3 = "a.private";
154 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1)); 205 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1));
155 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2)); 206 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2));
156 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3)); 207 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3));
157 } 208 }
OLDNEW
« no previous file with comments | « chrome/browser/ssl/ssl_error_classification.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698