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
3 // found in the LICENSE file.
5 #include "chrome/browser/ssl/ssl_error_classification.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_split.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"
13 #include "net/base/test_data_directory.h"
14 #include "net/cert/x509_cert_types.h"
15 #include "net/cert/x509_certificate.h"
16 #include "net/test/cert_test_util.h"
17 #include "net/test/test_certificate_data.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 using content::WebContents
;
24 class SSLErrorClassificationTest
: public ChromeRenderViewHostTestHarness
{
26 SSLErrorClassificationTest() {
27 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD
);
31 TEST_F(SSLErrorClassificationTest
, TestNameMismatch
) {
32 scoped_refptr
<net::X509Certificate
> google_cert(
33 net::X509Certificate::CreateFromBytes(
34 reinterpret_cast<const char*>(google_der
), sizeof(google_der
)));
35 ASSERT_NE(static_cast<net::X509Certificate
*>(NULL
), google_cert
.get());
36 base::Time time
= base::Time::NowFromSystemTime();
37 std::vector
<std::string
> dns_names_google
;
38 dns_names_google
.push_back("www");
39 dns_names_google
.push_back("google");
40 dns_names_google
.push_back("com");
41 std::vector
<std::vector
<std::string
>> dns_name_tokens_google
;
42 dns_name_tokens_google
.push_back(dns_names_google
);
43 int cert_error
= net::ERR_CERT_COMMON_NAME_INVALID
;
44 WebContents
* contents
= web_contents();
46 GURL
origin("https://google.com");
47 std::string host_name
= origin
.host();
48 std::vector
<std::string
> host_name_tokens
;
49 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
50 SSLErrorClassification
ssl_error(contents
,
55 EXPECT_TRUE(ssl_error
.IsWWWSubDomainMatch());
56 EXPECT_FALSE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
57 dns_name_tokens_google
));
58 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_google
,
60 EXPECT_FALSE(ssl_error
.IsSubDomainOutsideWildcard(host_name_tokens
));
61 EXPECT_FALSE(ssl_error
.IsCertLikelyFromMultiTenantHosting());
65 GURL
origin("https://foo.blah.google.com");
66 std::string host_name
= origin
.host();
67 std::vector
<std::string
> host_name_tokens
;
68 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
69 SSLErrorClassification
ssl_error(contents
,
74 EXPECT_FALSE(ssl_error
.IsWWWSubDomainMatch());
75 EXPECT_FALSE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
76 dns_name_tokens_google
));
77 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_google
,
82 GURL
origin("https://foo.www.google.com");
83 std::string host_name
= origin
.host();
84 std::vector
<std::string
> host_name_tokens
;
85 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
86 SSLErrorClassification
ssl_error(contents
,
91 EXPECT_FALSE(ssl_error
.IsWWWSubDomainMatch());
92 EXPECT_TRUE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
93 dns_name_tokens_google
));
94 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_google
,
99 GURL
origin("https://www.google.com.foo");
100 std::string host_name
= origin
.host();
101 std::vector
<std::string
> host_name_tokens
;
102 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
103 SSLErrorClassification
ssl_error(contents
,
108 EXPECT_FALSE(ssl_error
.IsWWWSubDomainMatch());
109 EXPECT_FALSE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
110 dns_name_tokens_google
));
111 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_google
,
116 GURL
origin("https://www.foogoogle.com.");
117 std::string host_name
= origin
.host();
118 std::vector
<std::string
> host_name_tokens
;
119 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
120 SSLErrorClassification
ssl_error(contents
,
125 EXPECT_FALSE(ssl_error
.IsWWWSubDomainMatch());
126 EXPECT_FALSE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
127 dns_name_tokens_google
));
128 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_google
,
132 scoped_refptr
<net::X509Certificate
> webkit_cert(
133 net::X509Certificate::CreateFromBytes(
134 reinterpret_cast<const char*>(webkit_der
), sizeof(webkit_der
)));
135 ASSERT_NE(static_cast<net::X509Certificate
*>(NULL
), webkit_cert
.get());
136 std::vector
<std::string
> dns_names_webkit
;
137 dns_names_webkit
.push_back("webkit");
138 dns_names_webkit
.push_back("org");
139 std::vector
<std::vector
<std::string
>> dns_name_tokens_webkit
;
140 dns_name_tokens_webkit
.push_back(dns_names_webkit
);
142 GURL
origin("https://a.b.webkit.org");
143 std::string host_name
= origin
.host();
144 std::vector
<std::string
> host_name_tokens
;
145 base::SplitStringDontTrim(host_name
, '.', &host_name_tokens
);
146 SSLErrorClassification
ssl_error(contents
,
151 EXPECT_FALSE(ssl_error
.IsWWWSubDomainMatch());
152 EXPECT_FALSE(ssl_error
.NameUnderAnyNames(host_name_tokens
,
153 dns_name_tokens_webkit
));
154 EXPECT_FALSE(ssl_error
.AnyNamesUnderName(dns_name_tokens_webkit
,
156 EXPECT_TRUE(ssl_error
.IsSubDomainOutsideWildcard(host_name_tokens
));
157 EXPECT_FALSE(ssl_error
.IsCertLikelyFromMultiTenantHosting());
161 TEST_F(SSLErrorClassificationTest
, TestHostNameHasKnownTLD
) {
162 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD("www.google.com"));
163 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD("b.appspot.com"));
164 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD("a.private"));
167 TEST_F(SSLErrorClassificationTest
, TestPrivateURL
) {
168 EXPECT_FALSE(SSLErrorClassification::IsHostnameNonUniqueOrDotless(
169 "www.foogoogle.com."));
170 EXPECT_TRUE(SSLErrorClassification::IsHostnameNonUniqueOrDotless("go"));
172 SSLErrorClassification::IsHostnameNonUniqueOrDotless("172.17.108.108"));
173 EXPECT_TRUE(SSLErrorClassification::IsHostnameNonUniqueOrDotless("foo.blah"));