Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/time/time.h" | 9 #include "base/time/time.h" |
| 9 #include "net/base/test_data_directory.h" | 10 #include "net/base/test_data_directory.h" |
| 11 #include "net/cert/x509_cert_types.h" | |
| 10 #include "net/cert/x509_certificate.h" | 12 #include "net/cert/x509_certificate.h" |
| 11 #include "net/test/cert_test_util.h" | 13 #include "net/test/cert_test_util.h" |
| 12 #include "net/test/test_certificate_data.h" | 14 #include "net/test/test_certificate_data.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | |
| 14 | 17 |
| 15 using base::Time; | 18 using base::Time; |
| 16 | 19 |
| 17 TEST(SSLErrorClassification, TestDateInvalidScore) { | 20 class SSLErrorClassificationTest : public testing::Test { |
| 21 public: | |
| 22 virtual void SetUp() OVERRIDE { } | |
| 23 | |
| 24 std::vector<std::vector<std::string>> GetTokenizedDNSNames( | |
|
felt
2014/07/21 21:34:13
I'm worried about having logic like this in a test
radhikabhar
2014/07/22 16:03:38
Done.
| |
| 25 std::vector<std::string>& dns_names) const{ | |
| 26 std::vector<std::vector<std::string>> dns_name_tokens; | |
| 27 for (size_t i = 0; i < dns_names.size(); ++i) { | |
| 28 std::vector<std::string> dns_name_token_single; | |
| 29 if (dns_names[i].empty() || | |
| 30 dns_names[i].find('\0') != std::string::npos) { | |
| 31 dns_name_token_single.push_back(std::string()); | |
| 32 } else { | |
| 33 base::SplitStringDontTrim(dns_names[i], | |
| 34 '.', | |
| 35 &dns_name_token_single); | |
| 36 } | |
| 37 dns_name_tokens.push_back(dns_name_token_single); | |
| 38 } | |
| 39 return dns_name_tokens; | |
| 40 } | |
| 41 | |
| 42 }; | |
| 43 | |
| 44 TEST_F(SSLErrorClassificationTest, TestDateInvalidScore) { | |
| 18 base::FilePath certs_dir = net::GetTestCertsDirectory(); | 45 base::FilePath certs_dir = net::GetTestCertsDirectory(); |
| 19 scoped_refptr<net::X509Certificate> expired_cert = | 46 scoped_refptr<net::X509Certificate> expired_cert = |
| 20 net::ImportCertFromFile(certs_dir, "expired_cert.pem"); | 47 net::ImportCertFromFile(certs_dir, "expired_cert.pem"); |
| 21 base::Time time; | 48 base::Time time; |
| 49 GURL origin("https://example.com"); | |
| 22 | 50 |
| 23 { | 51 { |
| 24 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time)); | 52 EXPECT_TRUE(base::Time::FromString("Wed, 03 Jan 2007 12:00:00 GMT", &time)); |
| 25 SSLErrorClassification ssl_error(time, *expired_cert); | 53 SSLErrorClassification ssl_error(time, origin, *expired_cert); |
| 26 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry()); | 54 EXPECT_FLOAT_EQ(0.2f, ssl_error.CalculateScoreTimePassedSinceExpiry()); |
| 27 } | 55 } |
| 28 | 56 |
| 29 { | 57 { |
| 30 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time)); | 58 EXPECT_TRUE(base::Time::FromString("Sat, 06 Jan 2007 12:00:00 GMT", &time)); |
| 31 SSLErrorClassification ssl_error(time, *expired_cert); | 59 SSLErrorClassification ssl_error(time, origin, *expired_cert); |
| 32 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry()); | 60 EXPECT_FLOAT_EQ(0.3f, ssl_error.CalculateScoreTimePassedSinceExpiry()); |
| 33 } | 61 } |
| 34 | 62 |
| 35 { | 63 { |
| 36 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time)); | 64 EXPECT_TRUE(base::Time::FromString("Mon, 08 Jan 2007 12:00:00 GMT", &time)); |
| 37 SSLErrorClassification ssl_error(time, *expired_cert); | 65 SSLErrorClassification ssl_error(time, origin, *expired_cert); |
| 38 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry()); | 66 EXPECT_FLOAT_EQ(0.4f, ssl_error.CalculateScoreTimePassedSinceExpiry()); |
| 39 } | 67 } |
| 68 } | |
| 40 | 69 |
| 70 TEST_F(SSLErrorClassificationTest, TestNameMismatch) { | |
| 71 scoped_refptr<net::X509Certificate> google_cert( | |
| 72 net::X509Certificate::CreateFromBytes( | |
| 73 reinterpret_cast<const char*>(google_der), sizeof(google_der))); | |
| 74 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), google_cert); | |
| 75 base::Time time = base::Time::NowFromSystemTime(); | |
| 76 std::vector<std::string> dns_names_google; | |
| 77 google_cert->GetDNSNames(&dns_names_google); | |
| 78 std::vector<std::vector<std::string>> dns_name_tokens_google = | |
| 79 GetTokenizedDNSNames(dns_names_google); | |
| 80 | |
| 81 { | |
| 82 GURL origin("https://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(time, origin, *google_cert); | |
| 87 EXPECT_TRUE(ssl_error.IsWWWSubDomainMatch()); | |
| 88 EXPECT_FALSE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 89 dns_name_tokens_google)); | |
| 90 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 91 dns_name_tokens_google)); | |
| 92 EXPECT_FALSE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); | |
| 93 EXPECT_FALSE(ssl_error.IsSelfSigned()); | |
| 94 } | |
| 95 | |
| 96 { | |
| 97 GURL origin("https://foo.blah.google.com"); | |
| 98 std::string host_name = origin.host(); | |
| 99 std::vector<std::string> host_name_tokens; | |
| 100 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); | |
| 101 SSLErrorClassification ssl_error(time, origin, *google_cert); | |
| 102 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); | |
| 103 EXPECT_FALSE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 104 dns_name_tokens_google)); | |
| 105 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 106 dns_name_tokens_google)); | |
| 107 } | |
| 108 | |
| 109 { | |
| 110 GURL origin("https://foo.www.google.com"); | |
| 111 std::string host_name = origin.host(); | |
| 112 std::vector<std::string> host_name_tokens; | |
| 113 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); | |
| 114 SSLErrorClassification ssl_error(time, origin, *google_cert); | |
| 115 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); | |
| 116 EXPECT_TRUE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 117 dns_name_tokens_google)); | |
| 118 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 119 dns_name_tokens_google)); | |
| 120 } | |
| 121 | |
| 122 { | |
| 123 GURL origin("https://www.google.com.foo"); | |
| 124 std::string host_name = origin.host(); | |
| 125 std::vector<std::string> host_name_tokens; | |
| 126 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); | |
| 127 SSLErrorClassification ssl_error(time, origin, *google_cert); | |
| 128 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); | |
| 129 EXPECT_FALSE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 130 dns_name_tokens_google)); | |
| 131 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 132 dns_name_tokens_google)); | |
| 133 } | |
| 134 | |
| 135 { | |
| 136 GURL origin("https://www.foogoogle.com."); | |
| 137 std::string host_name = origin.host(); | |
| 138 std::vector<std::string> host_name_tokens; | |
| 139 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); | |
| 140 SSLErrorClassification ssl_error(time, origin, *google_cert); | |
| 141 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); | |
| 142 EXPECT_FALSE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 143 dns_name_tokens_google)); | |
| 144 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 145 dns_name_tokens_google)); | |
| 146 } | |
| 147 | |
| 148 scoped_refptr<net::X509Certificate> webkit_cert( | |
| 149 net::X509Certificate::CreateFromBytes( | |
| 150 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); | |
| 151 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), webkit_cert); | |
| 152 std::vector<std::string> dns_names_webkit; | |
| 153 webkit_cert->GetDNSNames(&dns_names_webkit); | |
| 154 std::vector<std::vector<std::string>> dns_name_tokens_webkit = | |
| 155 GetTokenizedDNSNames(dns_names_webkit); | |
| 156 { | |
| 157 GURL origin("https://a.b.webkit.org"); | |
| 158 std::string host_name = origin.host(); | |
| 159 std::vector<std::string> host_name_tokens; | |
| 160 base::SplitStringDontTrim(host_name, '.', &host_name_tokens); | |
| 161 SSLErrorClassification ssl_error(time, origin, *webkit_cert); | |
| 162 EXPECT_FALSE(ssl_error.IsWWWSubDomainMatch()); | |
| 163 EXPECT_FALSE(ssl_error.IsSubDomainMatch(host_name_tokens, | |
| 164 dns_name_tokens_webkit)); | |
| 165 EXPECT_FALSE(ssl_error.IsSubDomainInverseMatch(host_name_tokens, | |
| 166 dns_name_tokens_webkit)); | |
| 167 EXPECT_TRUE(ssl_error.IsSubDomainOutsideWildcard(host_name_tokens)); | |
| 168 } | |
| 169 | |
| 170 scoped_refptr<net::X509Certificate> self_signed_cert = | |
| 171 net::ImportCertFromFile(net::GetTestCertsDirectory(), | |
| 172 "unittest.selfsigned.der"); | |
| 173 ASSERT_NE(static_cast<net::X509Certificate*>(NULL), self_signed_cert); | |
| 174 { | |
| 175 GURL origin("https://example.com"); | |
| 176 SSLErrorClassification ssl_error(time, origin, *self_signed_cert); | |
| 177 EXPECT_TRUE(ssl_error.IsSelfSigned()); | |
| 178 } | |
| 41 } | 179 } |
| 180 | |
| 181 TEST_F(SSLErrorClassificationTest, TestHostNameHasKnownTLD) { | |
| 182 std::string url1 = "www.google.com"; | |
| 183 std::string url2 = "b.appspot.com"; | |
| 184 std::string url3 = "a.private"; | |
| 185 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url1)); | |
| 186 EXPECT_TRUE(SSLErrorClassification::IsHostNameKnownTLD(url2)); | |
| 187 EXPECT_FALSE(SSLErrorClassification::IsHostNameKnownTLD(url3)); | |
| 188 } | |
| 189 | |
| OLD | NEW |