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

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

Issue 2448943002: Refactor SecurityStateModel/Clients for simplicity and reusability. (Closed)
Patch Set: Remove *SecurityModelClient. Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: components/security_state/core/security_state_model_unittest.cc
diff --git a/components/security_state/security_state_model_unittest.cc b/components/security_state/core/security_state_model_unittest.cc
similarity index 77%
rename from components/security_state/security_state_model_unittest.cc
rename to components/security_state/core/security_state_model_unittest.cc
index 59d668b78374a541c1c39f7627663395b7ce975f..6ea02e2a40fd83bf36e8da8aff8b9a1bb8630ada 100644
--- a/components/security_state/security_state_model_unittest.cc
+++ b/components/security_state/core/security_state_model_unittest.cc
@@ -2,13 +2,13 @@
// 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_model.h"
#include <stdint.h>
#include "base/command_line.h"
-#include "components/security_state/security_state_model_client.h"
-#include "components/security_state/switches.h"
+#include "base/memory/ptr_util.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"
@@ -24,9 +24,9 @@ namespace {
const char kHttpsUrl[] = "https://foo.test";
const char kHttpUrl[] = "http://foo.test";
-class TestSecurityStateModelClient : public SecurityStateModelClient {
+class TestSecurityStateModel {
public:
- TestSecurityStateModelClient()
+ TestSecurityStateModel()
: url_(kHttpsUrl),
connection_status_(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT),
@@ -39,7 +39,7 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
cert_ =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_2016.pem");
}
- ~TestSecurityStateModelClient() override {}
+ virtual ~TestSecurityStateModel() {}
void set_connection_status(int connection_status) {
connection_status_ = connection_status;
@@ -70,9 +70,9 @@ class TestSecurityStateModelClient : public SecurityStateModelClient {
void UseHttpUrl() { url_ = GURL(kHttpUrl); }
- // SecurityStateModelClient:
- void GetVisibleSecurityState(
- SecurityStateModel::VisibleSecurityState* state) override {
+ std::unique_ptr<SecurityStateModel::VisibleSecurityState>
+ GetVisibleSecurityState() {
+ auto state = base::MakeUnique<SecurityStateModel::VisibleSecurityState>();
state->connection_info_initialized = true;
state->url = url_;
state->certificate = cert_;
@@ -85,15 +85,18 @@ 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 UsedPolicyInstalledCertificate() { return false; }
- bool IsOriginSecure(const GURL& url) override {
- return url_ == GURL(kHttpsUrl);
+ void GetSecurityInfo(SecurityStateModel::SecurityInfo* security_info) {
+ model_.GetSecurityInfo(security_info, GetVisibleSecurityState(),
+ UsedPolicyInstalledCertificate());
}
private:
+ SecurityStateModel model_;
GURL url_;
scoped_refptr<net::X509Certificate> cert_;
int connection_status_;
@@ -105,12 +108,12 @@ 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);
+ TestSecurityStateModel model;
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
@@ -121,10 +124,8 @@ TEST(SecurityStateModelTest, SHA1Warning) {
// 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);
+ TestSecurityStateModel model;
+ model.SetDisplayedMixedContent(true);
SecurityStateModel::SecurityInfo security_info1;
model.GetSecurityInfo(&security_info1);
EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
@@ -133,8 +134,8 @@ TEST(SecurityStateModelTest, SHA1WarningMixedContent) {
security_info1.mixed_content_status);
EXPECT_EQ(SecurityStateModel::NONE, security_info1.security_level);
- client.SetDisplayedMixedContent(false);
- client.SetRanMixedContent(true);
+ model.SetDisplayedMixedContent(false);
+ model.SetRanMixedContent(true);
SecurityStateModel::SecurityInfo security_info2;
model.GetSecurityInfo(&security_info2);
EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
@@ -147,10 +148,8 @@ TEST(SecurityStateModelTest, SHA1WarningMixedContent) {
// 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);
+ TestSecurityStateModel model;
+ model.AddCertStatus(net::CERT_STATUS_DATE_INVALID);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_EQ(SecurityStateModel::DEPRECATED_SHA1_MINOR,
@@ -161,45 +160,39 @@ TEST(SecurityStateModelTest, SHA1WarningBrokenHTTPS) {
// Tests that |security_info.is_secure_protocol_and_ciphersuite| is
// computed correctly.
TEST(SecurityStateModelTest, SecureProtocolAndCiphersuite) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+ TestSecurityStateModel model;
// 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
+ model.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
+ model.SetCipherSuite(ciphersuite);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_EQ(net::OBSOLETE_SSL_NONE, security_info.obsolete_ssl_status);
}
TEST(SecurityStateModelTest, NonsecureProtocol) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+ TestSecurityStateModel model;
// 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
+ model.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_1
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
+ model.SetCipherSuite(ciphersuite);
SecurityStateModel::SecurityInfo security_info;
model.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);
+ TestSecurityStateModel model;
// 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
+ model.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
+ model.SetCipherSuite(ciphersuite);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_EQ(net::OBSOLETE_SSL_MASK_KEY_EXCHANGE | net::OBSOLETE_SSL_MASK_CIPHER,
@@ -208,16 +201,14 @@ TEST(SecurityStateModelTest, NonsecureCiphersuite) {
// Tests that the malware/phishing status is set, and it overrides valid HTTPS.
TEST(SecurityStateModelTest, MalwareOverride) {
- TestSecurityStateModelClient client;
- SecurityStateModel model;
- model.SetClient(&client);
+ TestSecurityStateModel model;
// 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
+ model.set_connection_status(net::SSL_CONNECTION_VERSION_TLS1_2
<< net::SSL_CONNECTION_VERSION_SHIFT);
- client.SetCipherSuite(ciphersuite);
- client.set_fails_malware_check(true);
+ model.SetCipherSuite(ciphersuite);
+ model.set_fails_malware_check(true);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.fails_malware_check);
@@ -227,10 +218,8 @@ TEST(SecurityStateModelTest, MalwareOverride) {
// 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);
+ TestSecurityStateModel model;
+ model.set_fails_malware_check(true);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.fails_malware_check);
@@ -243,11 +232,9 @@ TEST(SecurityStateModelTest, 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);
+ TestSecurityStateModel model;
+ model.UseHttpUrl();
+ model.set_displayed_password_field_on_http(true);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
@@ -261,11 +248,9 @@ TEST(SecurityStateModelTest, 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);
+ TestSecurityStateModel model;
+ model.UseHttpUrl();
+ model.set_displayed_credit_card_field_on_http(true);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
@@ -277,12 +262,10 @@ TEST(SecurityStateModelTest, CreditCardFieldWarning) {
// 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);
+ TestSecurityStateModel model;
+ model.UseHttpUrl();
+ model.set_displayed_password_field_on_http(true);
+ model.set_displayed_credit_card_field_on_http(true);
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_TRUE(security_info.displayed_private_user_data_input_on_http);
@@ -292,16 +275,12 @@ TEST(SecurityStateModelTest, HttpWarningNotSetWithoutSwitch) {
// 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);
+ TestSecurityStateModel model;
+ model.UseHttpUrl();
SecurityStateModel::SecurityInfo security_info;
model.GetSecurityInfo(&security_info);
EXPECT_FALSE(security_info.displayed_private_user_data_input_on_http);
EXPECT_EQ(SecurityStateModel::NONE, security_info.security_level);
}
-} // namespace
-
} // namespace security_state

Powered by Google App Engine
This is Rietveld 408576698