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

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: Removed arguments from functions 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 virtual void SetUp() OVERRIDE {
31 ChromeRenderViewHostTestHarness::SetUp();
32 }
33
34 virtual void TearDown() OVERRIDE {
35 ChromeRenderViewHostTestHarness::TearDown();
36 }
37 };
38
39 TEST_F(SSLErrorClassificationTest, TestDateInvalidScore) {
21 base::FilePath certs_dir = net::GetTestCertsDirectory(); 40 base::FilePath certs_dir = net::GetTestCertsDirectory();
22 scoped_refptr<net::X509Certificate> expired_cert = 41 scoped_refptr<net::X509Certificate> expired_cert =
23 net::ImportCertFromFile(certs_dir, "expired_cert.pem"); 42 net::ImportCertFromFile(certs_dir, "expired_cert.pem");
24 base::Time time; 43 base::Time time;
25 GURL origin("https://example.com"); 44 GURL origin("https://example.com");
45 int cert_error = net::ERR_CERT_DATE_INVALID;
46 WebContents* contents = web_contents();
26 47
27 { 48 {
28 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time)); 49 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time));
29 SSLErrorClassification ssl_error(time, origin, *expired_cert); 50 SSLErrorClassification ssl_error(contents,
51 time,
52 origin,
53 cert_error,
54 *expired_cert);
30 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 55 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry());
31 } 56 }
32 57
33 { 58 {
34 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time)); 59 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time));
35 SSLErrorClassification ssl_error(time, origin, *expired_cert); 60 SSLErrorClassification ssl_error(contents,
61 time,
62 origin,
63 cert_error,
64 *expired_cert);
36 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 65 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry());
37 } 66 }
38 67
39 { 68 {
40 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time)); 69 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time));
41 SSLErrorClassification ssl_error(time, origin, *expired_cert); 70 SSLErrorClassification ssl_error(contents,
71 time,
72 origin,
73 cert_error,
74 *expired_cert);
42 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry()); 75 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry());
43 } 76 }
44 } 77 }
45 78
46 TEST(SSLErrorClassificationTest, TestNameMismatch) { 79 TEST_F(SSLErrorClassificationTest, TestNameMismatch) {
47 scoped_refptr<net::X509Certificate> google_cert( 80 scoped_refptr<net::X509Certificate> google_cert(
48 net::X509Certificate::CreateFromBytes( 81 net::X509Certificate::CreateFromBytes(
49 reinterpret_cast<const char*>(google_der), sizeof(google_der))); 82 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
50 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert); 83 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert);
51 base::Time time = base::Time::NowFromSystemTime(); 84 base::Time time = base::Time::NowFromSystemTime();
52 std::vector<std::string> dns_names_google; 85 std::vector<std::string> dns_names_google;
53 dns_names_google.push_back("www"); 86 dns_names_google.push_back("www");
54 dns_names_google.push_back("google"); 87 dns_names_google.push_back("google");
55 dns_names_google.push_back("com"); 88 dns_names_google.push_back("com");
56 std::vector<std::vector<std::string>> dns_name_tokens_google; 89 std::vector<std::vector<std::string>> dns_name_tokens_google;
57 dns_name_tokens_google.push_back(dns_names_google); 90 dns_name_tokens_google.push_back(dns_names_google);
91 int cert_error = net::ERR_CERT_COMMON_NAME_INVALID;
92 WebContents* contents = web_contents();
58 { 93 {
59 GURL origin("https://google.com"); 94 GURL origin("https://google.com");
60 std::string host_name = origin.host(); 95 std::string host_name = origin.host();
61 std::vector<std::string> host_name_tokens; 96 std::vector<std::string> host_name_tokens;
62 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 97 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
63 SSLErrorClassification ssl_error(time, origin, *google_cert); 98 SSLErrorClassification ssl_error(contents,
99 time,
100 origin,
101 cert_error,
102 *google_cert);
64 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch()); 103 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch());
65 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 104 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
66 dns_name_tokens_google)); 105 dns_name_tokens_google));
67 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 106 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
68 host_name_tokens)); 107 host_name_tokens));
69 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); 108 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
70 } 109 }
71 110
72 { 111 {
73 GURL origin("https://foo.blah.google.com"); 112 GURL origin("https://foo.blah.google.com");
74 std::string host_name = origin.host(); 113 std::string host_name = origin.host();
75 std::vector<std::string> host_name_tokens; 114 std::vector<std::string> host_name_tokens;
76 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 115 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
77 SSLErrorClassification ssl_error(time, origin, *google_cert); 116 SSLErrorClassification ssl_error(contents,
117 time,
118 origin,
119 cert_error,
120 *google_cert);
78 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 121 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
79 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 122 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
80 dns_name_tokens_google)); 123 dns_name_tokens_google));
81 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 124 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
82 host_name_tokens)); 125 host_name_tokens));
83 } 126 }
84 127
85 { 128 {
86 GURL origin("https://foo.www.google.com"); 129 GURL origin("https://foo.www.google.com");
87 std::string host_name = origin.host(); 130 std::string host_name = origin.host();
88 std::vector<std::string> host_name_tokens; 131 std::vector<std::string> host_name_tokens;
89 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 132 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
90 SSLErrorClassification ssl_error(time, origin, *google_cert); 133 SSLErrorClassification ssl_error(contents,
134 time,
135 origin,
136 cert_error,
137 *google_cert);
91 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 138 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
92 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens, 139 EXPECT_TRUE(ssl_error.NameUnderAnyNames(host_name_tokens,
93 dns_name_tokens_google)); 140 dns_name_tokens_google));
94 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 141 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
95 host_name_tokens)); 142 host_name_tokens));
96 } 143 }
97 144
98 { 145 {
99 GURL origin("https://www.google.com.foo"); 146 GURL origin("https://www.google.com.foo");
100 std::string host_name = origin.host(); 147 std::string host_name = origin.host();
101 std::vector<std::string> host_name_tokens; 148 std::vector<std::string> host_name_tokens;
102 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 149 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
103 SSLErrorClassification ssl_error(time, origin, *google_cert); 150 SSLErrorClassification ssl_error(contents,
151 time,
152 origin,
153 cert_error,
154 *google_cert);
104 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 155 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
105 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 156 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
106 dns_name_tokens_google)); 157 dns_name_tokens_google));
107 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 158 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
108 host_name_tokens)); 159 host_name_tokens));
109 } 160 }
110 161
111 { 162 {
112 GURL origin("https://www.foogoogle.com."); 163 GURL origin("https://www.foogoogle.com.");
113 std::string host_name = origin.host(); 164 std::string host_name = origin.host();
114 std::vector<std::string> host_name_tokens; 165 std::vector<std::string> host_name_tokens;
115 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 166 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
116 SSLErrorClassification ssl_error(time, origin, *google_cert); 167 SSLErrorClassification ssl_error(contents,
168 time,
169 origin,
170 cert_error,
171 *google_cert);
117 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 172 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
118 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 173 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
119 dns_name_tokens_google)); 174 dns_name_tokens_google));
120 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google, 175 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_google,
121 host_name_tokens)); 176 host_name_tokens));
122 } 177 }
123 178
124 scoped_refptr<net::X509Certificate> webkit_cert( 179 scoped_refptr<net::X509Certificate> webkit_cert(
125 net::X509Certificate::CreateFromBytes( 180 net::X509Certificate::CreateFromBytes(
126 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); 181 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
127 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert); 182 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert);
128 std::vector<std::string> dns_names_webkit; 183 std::vector<std::string> dns_names_webkit;
129 dns_names_webkit.push_back("webkit"); 184 dns_names_webkit.push_back("webkit");
130 dns_names_webkit.push_back("org"); 185 dns_names_webkit.push_back("org");
131 std::vector<std::vector<std::string>> dns_name_tokens_webkit; 186 std::vector<std::vector<std::string>> dns_name_tokens_webkit;
132 dns_name_tokens_webkit.push_back(dns_names_webkit); 187 dns_name_tokens_webkit.push_back(dns_names_webkit);
133 { 188 {
134 GURL origin("https://a.b.webkit.org"); 189 GURL origin("https://a.b.webkit.org");
135 std::string host_name = origin.host(); 190 std::string host_name = origin.host();
136 std::vector<std::string> host_name_tokens; 191 std::vector<std::string> host_name_tokens;
137 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); 192 base::SplitStringDontTrim(host_name, '.', &host_name_tokens);
138 SSLErrorClassification ssl_error(time, origin, *webkit_cert); 193 SSLErrorClassification ssl_error(contents,
194 time,
195 origin,
196 cert_error,
197 *webkit_cert);
139 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); 198 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
140 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens, 199 EXPECT_FALSE(ssl_error.NameUnderAnyNames(host_name_tokens,
141 dns_name_tokens_webkit)); 200 dns_name_tokens_webkit));
142 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit, 201 EXPECT_FALSE(ssl_error.AnyNamesUnderName(dns_name_tokens_webkit,
143 host_name_tokens)); 202 host_name_tokens));
144 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); 203 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
145 } 204 }
146 } 205 }
147 206
148 TEST(SSLErrorClassificationTest, TestHostNameHasKnownTLD) { 207 TEST_F(SSLErrorClassificationTest, TestHostNameHasKnownTLD) {
149 std::string url1 = "www.google.com"; 208 std::string url1 = "www.google.com";
150 std::string url2 = "b.appspot.com"; 209 std::string url2 = "b.appspot.com";
151 std::string url3 = "a.private"; 210 std::string url3 = "a.private";
152 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1)); 211 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1));
153 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2)); 212 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2));
154 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3)); 213 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3));
155 } 214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698