Commit d372b322 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[WebLayer] Add browsertest for SecurityStateClient code.

Add a browsertest to verify expected SecurityStateClient behavior.

Bug: 1052375
Change-Id: I6a063ce298e6576513b019a688452458fd76650e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2144032Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757847}
parent 04650f29
...@@ -1195,6 +1195,8 @@ void BrowserProcessImpl::PreMainMessageLoopRun() { ...@@ -1195,6 +1195,8 @@ void BrowserProcessImpl::PreMainMessageLoopRun() {
CreateNetworkQualityObserver(); CreateNetworkQualityObserver();
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// This needs to be here so that SecurityStateClient is non-null when
// SecurityStateModel code is called.
security_state::SetSecurityStateClient(new ChromeSecurityStateClient()); security_state::SetSecurityStateClient(new ChromeSecurityStateClient());
#endif #endif
} }
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback_forward.h"
#include "base/run_loop.h"
#include "chrome/browser/ssl/chrome_security_state_model_delegate.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
#include "chrome/test/base/android/android_browser_test.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "components/security_state/content/android/security_state_client.h"
#include "content/public/browser/web_contents.h"
class ChromeSecurityStateClientTest : public PlatformBrowserTest {
public:
ChromeSecurityStateClientTest() = default;
~ChromeSecurityStateClientTest() override = default;
protected:
content::WebContents* GetActiveWebContents() {
return chrome_test_utils::GetActiveWebContents(this);
}
};
IN_PROC_BROWSER_TEST_F(ChromeSecurityStateClientTest,
CorrectSecurityStatModelDelegateCreated) {
content::WebContents* contents = GetActiveWebContents();
ASSERT_TRUE(contents);
SecurityStateTabHelper* helper =
SecurityStateTabHelper::FromWebContents(contents);
ASSERT_TRUE(helper);
auto* security_state_client = security_state::GetSecurityStateClient();
ASSERT_TRUE(security_state_client);
{
base::RunLoop run_loop;
helper->set_get_security_level_callback_for_tests_(run_loop.QuitClosure());
EXPECT_EQ(helper->GetSecurityLevel(),
security_state_client->MaybeCreateSecurityStateModelDelegate()
->GetSecurityLevel(contents));
// The test won't finish until SecurityStateTabHelper::GetSecurityLevel()
// is called.
run_loop.Run();
}
}
...@@ -128,6 +128,9 @@ SecurityStateTabHelper::SecurityStateTabHelper( ...@@ -128,6 +128,9 @@ SecurityStateTabHelper::SecurityStateTabHelper(
SecurityStateTabHelper::~SecurityStateTabHelper() {} SecurityStateTabHelper::~SecurityStateTabHelper() {}
security_state::SecurityLevel SecurityStateTabHelper::GetSecurityLevel() { security_state::SecurityLevel SecurityStateTabHelper::GetSecurityLevel() {
if (get_security_level_callback_for_tests_) {
std::move(get_security_level_callback_for_tests_).Run();
}
return security_state::GetSecurityLevel(*GetVisibleSecurityState(), return security_state::GetSecurityLevel(*GetVisibleSecurityState(),
UsedPolicyInstalledCertificate()); UsedPolicyInstalledCertificate());
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "base/callback.h"
#include "base/optional.h" #include "base/optional.h"
#include "components/security_state/core/security_state.h" #include "components/security_state/core/security_state.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
...@@ -38,6 +39,12 @@ class SecurityStateTabHelper ...@@ -38,6 +39,12 @@ class SecurityStateTabHelper
content::NavigationHandle* navigation_handle) override; content::NavigationHandle* navigation_handle) override;
void DidChangeVisibleSecurityState() override; void DidChangeVisibleSecurityState() override;
// Used by tests to specify a callback to be called when
// GetVisibleSecurityState() is called.
void set_get_security_level_callback_for_tests_(base::OnceClosure closure) {
get_security_level_callback_for_tests_ = std::move(closure);
}
private: private:
explicit SecurityStateTabHelper(content::WebContents* web_contents); explicit SecurityStateTabHelper(content::WebContents* web_contents);
friend class content::WebContentsUserData<SecurityStateTabHelper>; friend class content::WebContentsUserData<SecurityStateTabHelper>;
...@@ -54,6 +61,8 @@ class SecurityStateTabHelper ...@@ -54,6 +61,8 @@ class SecurityStateTabHelper
bool /* should_suppress_legacy_tls_warning */>> bool /* should_suppress_legacy_tls_warning */>>
cached_should_suppress_legacy_tls_warning_; cached_should_suppress_legacy_tls_warning_;
base::OnceClosure get_security_level_callback_for_tests_;
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(SecurityStateTabHelper); DISALLOW_COPY_AND_ASSIGN(SecurityStateTabHelper);
......
...@@ -511,6 +511,7 @@ if (is_android) { ...@@ -511,6 +511,7 @@ if (is_android) {
"../browser/metrics/metrics_service_user_demographics_browsertest.cc", "../browser/metrics/metrics_service_user_demographics_browsertest.cc",
"../browser/net/cert_verify_proc_browsertest.cc", "../browser/net/cert_verify_proc_browsertest.cc",
"../browser/profiles/profile_browsertest_android.cc", "../browser/profiles/profile_browsertest_android.cc",
"../browser/ssl/chrome_security_state_client_browsertest.cc",
"../browser/ssl/crlset_browsertest.cc", "../browser/ssl/crlset_browsertest.cc",
"android/browsertests_apk/android_browsertests_jni_onload.cc", "android/browsertests_apk/android_browsertests_jni_onload.cc",
"base/android/android_browser_test_browsertest_android.cc", "base/android/android_browser_test_browsertest_android.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment