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

Unified Diff: components/security_state/core/security_state_unittest.cc

Issue 2448943002: Refactor SecurityStateModel/Clients for simplicity and reusability. (Closed)
Patch Set: fix DEPS. Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: components/security_state/core/security_state_unittest.cc
diff --git a/components/security_state/security_state_model_unittest.cc b/components/security_state/core/security_state_unittest.cc
similarity index 51%
rename from components/security_state/security_state_model_unittest.cc
rename to components/security_state/core/security_state_unittest.cc
index 7776a0c5ba5e492df8a36efef7d189ad3a2db7c9..2aa42e058fa978c2fb23cc7e3af6d726297d5caf 100644
--- a/components/security_state/security_state_model_unittest.cc
+++ b/components/security_state/core/security_state_unittest.cc
@@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/security_state/security_state_model.h"
+#include "components/security_state/core/security_state.h"
#include <stdint.h>
+#include "base/bind.h"
#include "base/command_line.h"
+#include "base/memory/ptr_util.h"
#include "base/test/histogram_tester.h"
-#include "components/security_state/security_state_model_client.h"
-#include "components/security_state/switches.h"
+#include "components/security_state/core/switches.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/ssl_cipher_suite_names.h"
#include "net/ssl/ssl_connection_status_flags.h"
@@ -25,9 +26,13 @@ namespace {
const char kHttpsUrl[] = "https://foo.test";
const char kHttpUrl[] = "http://foo.test";
-class TestSecurityStateModelClient : public SecurityStateModelClient {
+bool IsOriginSecure(const GURL& url) {
+ return url == GURL(kHttpsUrl);
+}
+
+class TestSecurityStateHelper {
public:
- TestSecurityStateModelClient()
+ TestSecurityStateHelper()
: url_(kHttpsUrl),
connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT),
@@ -40,7 +45,7 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
cert_ =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem");
}
- ~TestSecurityStateModelClient() override {}
+ virtual ~TestSecurityStateHelper() {}
void set_connection_status(int connection_status) {
connection_status_ = connection_status;
@@ -71,9 +76,8 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
void UseHttpUrl() { url_ = GURL(kHttpUrl); }
- // SecurityStateModelClient:
- void GetVisibleSecurityState(
- SecurityStateModel::VisibleSecurityState* state) override {
+ std::unique_ptr<VisibleSecurityState> GetVisibleSecurityState() {
+ auto state = base::MakeUnique<VisibleSecurityState>();
state->connection_info_initialized = true;
state->url = url_;
state->certificate = cert_;
@@ -86,12 +90,14 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
state->displayed_password_field_on_http = displayed_password_field_on_http_;
state->displayed_credit_card_field_on_http =
displayed_credit_card_field_on_http_;
+ return state;
}
- bool UsedPolicyInstalledCertificate() override { return false; }
-
- bool IsOriginSecure(const GURL& url) override {
- return url_ == GURL(kHttpsUrl);
+ void GetSecurityInfo(SecurityInfo* security_info) {
+ security_state::GetSecurityInfo(
+ GetVisibleSecurityState(),
+ false /* used policy installed certificate */,
+ base::Bind(&IsOriginSecure), security_info);
}
private:
@@ -106,229 +112,193 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
bool displayed_credit_card_field_on_http_;
};
+} // namespace
+
// Tests that SHA1-signed certificates expiring in 2016 downgrade the
// security state of the page.
-TEST(SecurityStateModelTest, SHA1Warning) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
- EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
- security_info.sha1_deprecation_status);
- EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
+TEST(SecurityStateTest, SHA1Warning) {
+ TestSecurityStateHelper helper;
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
+ EXPECT_EQ(DEPRECATED_SHA1_MINOR, security_info.sha1_deprecation_status);
+ EXPECT_EQ(NONE, security_info.security_level);
}
// Tests that SHA1 warnings don't interfere with the handling of mixed
// content.
-TEST(SecurityStateModelTest, SHA1WarningMixedContent) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
- client.SetDisplayedMixedContent(true);
- SecurityStateModel::SecurityInfo security_info1;
- model.GetSecurityInfo(&security_info1);
- EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
- security_info1.sha1_deprecation_status);
- EXPECT_EQ(SecurityStateModel::CONTENT_STATUS_DISPLAYED,
- security_info1.mixed_content_status);
- EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level);
+TEST(SecurityStateTest, SHA1WarningMixedContent) {
+ TestSecurityStateHelper helper;
+ helper.SetDisplayedMixedContent(true);
+ SecurityInfo security_info1;
+ helper.GetSecurityInfo(&security_info1);
+ EXPECT_EQ(DEPRECATED_SHA1_MINOR, security_info1.sha1_deprecation_status);
+ EXPECT_EQ(CONTENT_STATUS_DISPLAYED, security_info1.mixed_content_status);
+ EXPECT_EQ(NONE, security_info1.security_level);
- client.SetDisplayedMixedContent(false);
- client.SetRanMixedContent(true);
- SecurityStateModel::SecurityInfo security_info2;
- model.GetSecurityInfo(&security_info2);
- EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
- security_info2.sha1_deprecation_status);
- EXPECT_EQ(SecurityStateModel::CONTENT_STATUS_RAN,
- security_info2.mixed_content_status);
- EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info2.security_level);
+ helper.SetDisplayedMixedContent(false);
+ helper.SetRanMixedContent(true);
+ SecurityInfo security_info2;
+ helper.GetSecurityInfo(&security_info2);
+ EXPECT_EQ(DEPRECATED_SHA1_MINOR, security_info2.sha1_deprecation_status);
+ EXPECT_EQ(CONTENT_STATUS_RAN, security_info2.mixed_content_status);
+ EXPECT_EQ(DANGEROUS, security_info2.security_level);
}
// Tests that SHA1 warnings don't interfere with the handling of major
// cert errors.
-TEST(SecurityStateModelTest, SHA1WarningBrokenHTTPS) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
- client.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
- EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
- security_info.sha1_deprecation_status);
- EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
+TEST(SecurityStateTest, SHA1WarningBrokenHTTPS) {
+ TestSecurityStateHelper helper;
+ helper.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
+ EXPECT_EQ(DEPRECATED_SHA1_MINOR, security_info.sha1_deprecation_status);
+ EXPECT_EQ(DANGEROUS, security_info.security_level);
}
// Tests that |security_info.is_secure_protocol_and_ciphersuite| is
// computed correctly.
-TEST(SecurityStateModelTest, SecureProtocolAndCiphersuite) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+TEST(SecurityStateTest, SecureProtocolAndCiphersuite) {
+ TestSecurityStateHelper helper;
// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-4
const uint16_t ciphersuite = 0xc02f;
- client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
+ helper.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ helper.SetCipherSuite(ciphersuite);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_EQ(net::OBSOLETE_SSL_NONE, security_info.obsolete_ssl_status);
}
-TEST(SecurityStateModelTest, NonsecureProtocol) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+TEST(SecurityStateTest, NonsecureProtocol) {
+ TestSecurityStateHelper helper;
// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-4
const uint16_t ciphersuite = 0xc02f;
- client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
+ helper.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ helper.SetCipherSuite(ciphersuite);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_EQ(net::OBSOLETE_SSL_MASK_PROTOCOL, security_info.obsolete_ssl_status);
}
-TEST(SecurityStateModelTest, NonsecureCiphersuite) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+TEST(SecurityStateTest, NonsecureCiphersuite) {
+ TestSecurityStateHelper helper;
// TLS_RSA_WITH_AES_128_CCM_8 from
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-4
const uint16_t ciphersuite = 0xc0a0;
- client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
+ helper.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ helper.SetCipherSuite(ciphersuite);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_EQ(net::OBSOLETE_SSL_MASK_KEY_EXCHANGE | net::OBSOLETE_SSL_MASK_CIPHER,
security_info.obsolete_ssl_status);
}
// Tests that the malware/phishing status is set, and it overrides valid HTTPS.
-TEST(SecurityStateModelTest, MalwareOverride) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+TEST(SecurityStateTest, MalwareOverride) {
+ TestSecurityStateHelper helper;
// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from
// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-4
const uint16_t ciphersuite = 0xc02f;
- client.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
+ helper.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
- client.set_fails_malware_check(true);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ helper.SetCipherSuite(ciphersuite);
+ helper.set_fails_malware_check(true);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.fails_malware_check);
- EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
+ EXPECT_EQ(DANGEROUS, security_info.security_level);
}
// Tests that the malware/phishing status is set, even if other connection info
// is not available.
-TEST(SecurityStateModelTest, MalwareWithoutCOnnectionState) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
- client.set_fails_malware_check(true);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+TEST(SecurityStateTest, MalwareWithoutCOnnectionState) {
+ TestSecurityStateHelper helper;
+ helper.set_fails_malware_check(true);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.fails_malware_check);
- EXPECT_EQ(SecurityStateModel::DANGEROUS, security_info.security_level);
+ EXPECT_EQ(DANGEROUS, security_info.security_level);
}
// Tests that password fields cause the security level to be downgraded
// to HTTP_SHOW_WARNING when the command-line switch is set.
-TEST(SecurityStateModelTest, PasswordFieldWarning) {
+TEST(SecurityStateTest, PasswordFieldWarning) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
- switches::kMarkHttpAs,
- switches::kMarkHttpWithPasswordsOrCcWithChip);
- TestSecurityStateModelClient client;
- client.UseHttpUrl();
- SecurityStateModel model;
- model.SetClient(&client);
- client.set_displayed_password_field_on_http(true);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ switches::kMarkHttpAs, switches::kMarkHttpWithPasswordsOrCcWithChip);
+ TestSecurityStateHelper helper;
+ helper.UseHttpUrl();
+ helper.set_displayed_password_field_on_http(true);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
- EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING,
- security_info.security_level);
+ EXPECT_EQ(HTTP_SHOW_WARNING, security_info.security_level);
}
// Tests that credit card fields cause the security level to be downgraded
// to HTTP_SHOW_WARNING when the command-line switch is set.
-TEST(SecurityStateModelTest, CreditCardFieldWarning) {
+TEST(SecurityStateTest, CreditCardFieldWarning) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
- switches::kMarkHttpAs,
- switches::kMarkHttpWithPasswordsOrCcWithChip);
- TestSecurityStateModelClient client;
- client.UseHttpUrl();
- SecurityStateModel model;
- model.SetClient(&client);
- client.set_displayed_credit_card_field_on_http(true);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+ switches::kMarkHttpAs, switches::kMarkHttpWithPasswordsOrCcWithChip);
+ TestSecurityStateHelper helper;
+ helper.UseHttpUrl();
+ helper.set_displayed_credit_card_field_on_http(true);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
- EXPECT_EQ(SecurityStateModel::HTTP_SHOW_WARNING,
- security_info.security_level);
+ EXPECT_EQ(HTTP_SHOW_WARNING, security_info.security_level);
}
// Tests that neither password nor credit fields cause the security
// level to be downgraded to HTTP_SHOW_WARNING when the command-line switch
// is NOT set.
-TEST(SecurityStateModelTest, HttpWarningNotSetWithoutSwitch) {
- TestSecurityStateModelClient client;
- client.UseHttpUrl();
- SecurityStateModel model;
- model.SetClient(&client);
- client.set_displayed_password_field_on_http(true);
- client.set_displayed_credit_card_field_on_http(true);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+TEST(SecurityStateTest, HttpWarningNotSetWithoutSwitch) {
+ TestSecurityStateHelper helper;
+ helper.UseHttpUrl();
+ helper.set_displayed_password_field_on_http(true);
+ helper.set_displayed_credit_card_field_on_http(true);
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
- EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
+ EXPECT_EQ(NONE, security_info.security_level);
}
// Tests that |displayed_private_user_data_input_on_http| is not set
// when the corresponding VisibleSecurityState flags are not set.
-TEST(SecurityStateModelTest, PrivateUserDataNotSet) {
- TestSecurityStateModelClient client;
- client.UseHttpUrl();
- SecurityStateModel model;
- model.SetClient(&client);
- SecurityStateModel::SecurityInfo security_info;
- model.GetSecurityInfo(&security_info);
+TEST(SecurityStateTest, PrivateUserDataNotSet) {
+ TestSecurityStateHelper helper;
+ helper.UseHttpUrl();
+ SecurityInfo security_info;
+ helper.GetSecurityInfo(&security_info);
EXPECT_FALSE(security_info.displayed_private_user_data_input_on_http);
- EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
+ EXPECT_EQ(NONE, security_info.security_level);
}
// Tests that SSL.MarkHttpAsStatus histogram is updated when security state is
// computed for a page.
-TEST(SecurityStateModelTest, MarkHttpAsStatusHistogram) {
+TEST(SecurityStateTest, MarkHttpAsStatusHistogram) {
const char* kHistogramName = "SSL.MarkHttpAsStatus";
base::HistogramTester histograms;
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kMarkHttpAs, switches::kMarkHttpWithPasswordsOrCcWithChip);
- TestSecurityStateModelClient client;
- client.UseHttpUrl();
- SecurityStateModel model;
- model.SetClient(&client);
+ TestSecurityStateHelper helper;
+ helper.UseHttpUrl();
// Ensure histogram recorded correctly when a non-secure password input is
// found on the page.
- client.set_displayed_password_field_on_http(true);
- SecurityStateModel::SecurityInfo security_info;
+ helper.set_displayed_password_field_on_http(true);
+ SecurityInfo security_info;
histograms.ExpectTotalCount(kHistogramName, 0);
- model.GetSecurityInfo(&security_info);
+ helper.GetSecurityInfo(&security_info);
histograms.ExpectUniqueSample(kHistogramName, 2 /* HTTP_SHOW_WARNING */, 1);
// Ensure histogram recorded correctly even without a password input.
- client.set_displayed_password_field_on_http(false);
- model.GetSecurityInfo(&security_info);
+ helper.set_displayed_password_field_on_http(false);
+ helper.GetSecurityInfo(&security_info);
histograms.ExpectUniqueSample(kHistogramName, 2 /* HTTP_SHOW_WARNING */, 2);
}
-} // namespace
-
} // namespace security_state

Powered by Google App Engine
This is Rietveld 408576698