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

Side by Side Diff: components/ssl_errors/error_classification_unittest.cc

Issue 1355413003: Move error classification into the ssl_errors component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
estark 2015/09/23 22:42:19 nit: update copyright year
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/ssl_errors/error_classification.h"
6
7 #include "base/files/file_path.h"
8 #include "base/strings/string_split.h"
9 #include "base/time/time.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/test_data_directory.h"
12 #include "net/cert/x509_cert_types.h"
13 #include "net/cert/x509_certificate.h"
14 #include "net/test/cert_test_util.h"
15 #include "net/test/test_certificate_data.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
18
19 using base::Time;
20
21 namespace ssl_errors {
22
23 // Placeholder class to allow friending.
24 class ErrorClassificationTest : public testing::Test {};
25
26 TEST_F(ErrorClassificationTest, TestNameMismatch) {
27 scoped_refptr<net::X509Certificate> google_cert(
28 net::X509Certificate::CreateFromBytes(
29 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
30 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert.get());
31 base::Time time = base::Time::NowFromSystemTime();
32 std::vector<std::string> dns_names_google;
33 dns_names_google.push_back("www");
34 dns_names_google.push_back("google");
35 dns_names_google.push_back("com");
36 std::vector<std::vector<std::string>> dns_name_tokens_google;
37 dns_name_tokens_google.push_back(dns_names_google);
38 int cert_error = net::ERR_CERT_COMMON_NAME_INVALID;
39 {
40 GURL origin("https://google.com");
41 std::vector<std::string> host_name_tokens = base::SplitString(
42 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
43 ErrorClassification ssl_error(time, origin, cert_error, *google_cert);
44 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch());
45 EXPECT_FALSE(
46 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_google));
47 EXPECT_FALSE(
48 ssl_error.AnyNamesUnderName(dns_name_tokens_google, host_name_tokens));
49 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
50 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
51 EXPECT_TRUE(ssl_error.IsCertLikelyFromSameDomain());
52 }
53
54 {
55 GURL origin("https://foo.blah.google.com");
56 std::vector<std::string> host_name_tokens = base::SplitString(
57 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
58 ErrorClassification ssl_error(time, origin, cert_error, *google_cert);
59 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
60 EXPECT_FALSE(
61 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_google));
62 EXPECT_FALSE(
63 ssl_error.AnyNamesUnderName(dns_name_tokens_google, host_name_tokens));
64 EXPECT_TRUE(ssl_error.IsCertLikelyFromSameDomain());
65 }
66
67 {
68 GURL origin("https://foo.www.google.com");
69 std::vector<std::string> host_name_tokens = base::SplitString(
70 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
71 ErrorClassification ssl_error(time, origin, cert_error, *google_cert);
72 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
73 EXPECT_TRUE(
74 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_google));
75 EXPECT_FALSE(
76 ssl_error.AnyNamesUnderName(dns_name_tokens_google, host_name_tokens));
77 EXPECT_TRUE(ssl_error.IsCertLikelyFromSameDomain());
78 }
79
80 {
81 GURL origin("https://www.google.com.foo");
82 std::vector<std::string> host_name_tokens = base::SplitString(
83 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
84 ErrorClassification ssl_error(time, origin, cert_error, *google_cert);
85 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
86 EXPECT_FALSE(
87 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_google));
88 EXPECT_FALSE(
89 ssl_error.AnyNamesUnderName(dns_name_tokens_google, host_name_tokens));
90 EXPECT_FALSE(ssl_error.IsCertLikelyFromSameDomain());
91 }
92
93 {
94 GURL origin("https://www.foogoogle.com.");
95 std::vector<std::string> host_name_tokens = base::SplitString(
96 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
97 ErrorClassification ssl_error(time, origin, cert_error, *google_cert);
98 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
99 EXPECT_FALSE(
100 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_google));
101 EXPECT_FALSE(
102 ssl_error.AnyNamesUnderName(dns_name_tokens_google, host_name_tokens));
103 EXPECT_FALSE(ssl_error.IsCertLikelyFromSameDomain());
104 }
105
106 scoped_refptr<net::X509Certificate> webkit_cert(
107 net::X509Certificate::CreateFromBytes(
108 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
109 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert.get());
110 std::vector<std::string> dns_names_webkit;
111 dns_names_webkit.push_back("webkit");
112 dns_names_webkit.push_back("org");
113 std::vector<std::vector<std::string>> dns_name_tokens_webkit;
114 dns_name_tokens_webkit.push_back(dns_names_webkit);
115 {
116 GURL origin("https://a.b.webkit.org");
117 std::vector<std::string> host_name_tokens = base::SplitString(
118 origin.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
119 ErrorClassification ssl_error(time, origin, cert_error, *webkit_cert);
120 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch());
121 EXPECT_FALSE(
122 ssl_error.NameUnderAnyNames(host_name_tokens, dns_name_tokens_webkit));
123 EXPECT_FALSE(
124 ssl_error.AnyNamesUnderName(dns_name_tokens_webkit, host_name_tokens));
125 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens));
126 EXPECT_FALSE(ssl_error.IsCertLikelyFromMultiTenantHosting());
127 EXPECT_TRUE(ssl_error.IsCertLikelyFromSameDomain());
128 }
129 }
130
131 TEST_F(ErrorClassificationTest, TestHostNameHasKnownTLD) {
132 EXPECT_TRUE(ErrorClassification::IsHostNameKnownTLD("www.google.com"));
133 EXPECT_TRUE(ErrorClassification::IsHostNameKnownTLD("b.appspot.com"));
134 EXPECT_FALSE(ErrorClassification::IsHostNameKnownTLD("a.private"));
135 }
136
137 TEST_F(ErrorClassificationTest, TestPrivateURL) {
138 EXPECT_FALSE(
139 ErrorClassification::IsHostnameNonUniqueOrDotless("www.foogoogle.com."));
140 EXPECT_TRUE(ErrorClassification::IsHostnameNonUniqueOrDotless("go"));
141 EXPECT_TRUE(
142 ErrorClassification::IsHostnameNonUniqueOrDotless("172.17.108.108"));
143 EXPECT_TRUE(ErrorClassification::IsHostnameNonUniqueOrDotless("foo.blah"));
144 }
145
146 } // namespace ssl_errors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698