Commit 896333d6 authored by Jialiu Lin's avatar Jialiu Lin Committed by Commit Bot

Add browser test to verify IP collection in client-side detection

Bug: 783977
Change-Id: Ib9977a73f9f8021bf9204cd594a9e40fd8d03341
Reviewed-on: https://chromium-review.googlesource.com/777553
Commit-Queue: Jialiu Lin <jialiul@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517884}
parent 3224cfec
...@@ -56,6 +56,8 @@ class ClientSideDetectionHost : public content::WebContentsObserver, ...@@ -56,6 +56,8 @@ class ClientSideDetectionHost : public content::WebContentsObserver,
virtual scoped_refptr<SafeBrowsingDatabaseManager> database_manager(); virtual scoped_refptr<SafeBrowsingDatabaseManager> database_manager();
BrowseInfo* GetBrowseInfo() const { return browse_info_.get(); }
protected: protected:
explicit ClientSideDetectionHost(content::WebContents* tab); explicit ClientSideDetectionHost(content::WebContents* tab);
...@@ -67,15 +69,18 @@ class ClientSideDetectionHost : public content::WebContentsObserver, ...@@ -67,15 +69,18 @@ class ClientSideDetectionHost : public content::WebContentsObserver,
SafeBrowsingUIManager* ui_manager, SafeBrowsingUIManager* ui_manager,
SafeBrowsingDatabaseManager* database_manager); SafeBrowsingDatabaseManager* database_manager);
// Called when pre-classification checks are done for the malware classifiers.
// Overridden in test.
virtual void OnMalwarePreClassificationDone(bool should_classify);
private: private:
friend class ClientSideDetectionHostTest; friend class ClientSideDetectionHostTest;
class ShouldClassifyUrlRequest; class ShouldClassifyUrlRequest;
friend class ShouldClassifyUrlRequest; friend class ShouldClassifyUrlRequest;
// These methods are called when pre-classification checks are done for // Called when pre-classification checks are done for the phishing
// the phishing and malware clasifiers. // classifiers.
void OnPhishingPreClassificationDone(bool should_classify); void OnPhishingPreClassificationDone(bool should_classify);
void OnMalwarePreClassificationDone(bool should_classify);
// Verdict is an encoded ClientPhishingRequest protocol message. // Verdict is an encoded ClientPhishingRequest protocol message.
void OnPhishingDetectionDone(const std::string& verdict); void OnPhishingDetectionDone(const std::string& verdict);
......
// Copyright 2017 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 "chrome/browser/safe_browsing/client_side_detection_host.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using content::WebContents;
namespace safe_browsing {
namespace {
class TestClientSideDetectionHost : public ClientSideDetectionHost {
public:
explicit TestClientSideDetectionHost(WebContents* tab)
: ClientSideDetectionHost(tab) {}
private:
void OnMalwarePreClassificationDone(bool should_classify_not_used) override {
ClientSideDetectionHost::OnMalwarePreClassificationDone(true);
}
void DidStopLoading() override {}
};
bool FindExpectedIPUrlInfo(const IPUrlInfo& expected_info,
const std::vector<IPUrlInfo>& ip_url_vector) {
auto result = std::find_if(
ip_url_vector.begin(), ip_url_vector.end(),
[expected_info](const IPUrlInfo& ip_url_info) {
return expected_info.url == ip_url_info.url &&
expected_info.method == ip_url_info.method &&
expected_info.referrer == ip_url_info.referrer &&
expected_info.resource_type == ip_url_info.resource_type;
});
return result != ip_url_vector.end();
}
} // namespace
using ClientSideDetectionHostBrowserTest = InProcessBrowserTest;
IN_PROC_BROWSER_TEST_F(ClientSideDetectionHostBrowserTest,
VerifyIPAddressCollection) {
browser()->profile()->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled,
false);
ASSERT_TRUE(embedded_test_server()->Start());
std::unique_ptr<TestClientSideDetectionHost> csd_host =
base::MakeUnique<TestClientSideDetectionHost>(
browser()->tab_strip_model()->GetActiveWebContents());
GURL page_url(embedded_test_server()->GetURL("/safe_browsing/malware.html"));
ui_test_utils::NavigateToURL(browser(), page_url);
BrowseInfo* browse_info = csd_host->GetBrowseInfo();
ASSERT_EQ(1u, browse_info->ips.size());
const std::vector<IPUrlInfo>& ip_urls =
browse_info->ips[embedded_test_server()->base_url().host()];
IPUrlInfo expected_result_1(
embedded_test_server()->GetURL("/safe_browsing/malware_image.png").spec(),
"GET", page_url.spec(), content::RESOURCE_TYPE_IMAGE);
IPUrlInfo expected_result_2(embedded_test_server()
->GetURL("/safe_browsing/malware_iframe.html")
.spec(),
"GET", page_url.spec(),
content::RESOURCE_TYPE_SUB_FRAME);
ASSERT_TRUE(FindExpectedIPUrlInfo(expected_result_1, ip_urls));
ASSERT_TRUE(FindExpectedIPUrlInfo(expected_result_2, ip_urls));
}
} // namespace safe_browsing
...@@ -1654,6 +1654,7 @@ test("browser_tests") { ...@@ -1654,6 +1654,7 @@ test("browser_tests") {
sources += [ sources += [
"../browser/safe_browsing/certificate_reporting_service_browsertest.cc", "../browser/safe_browsing/certificate_reporting_service_browsertest.cc",
"../browser/safe_browsing/chrome_password_protection_service_browsertest.cc", "../browser/safe_browsing/chrome_password_protection_service_browsertest.cc",
"../browser/safe_browsing/client_side_detection_host_browsertest.cc",
"../browser/safe_browsing/permission_reporter_browsertest.cc", "../browser/safe_browsing/permission_reporter_browsertest.cc",
"../browser/safe_browsing/safe_browsing_blocking_page_test.cc", "../browser/safe_browsing/safe_browsing_blocking_page_test.cc",
"../browser/safe_browsing/safe_browsing_navigation_observer_browsertest.cc", "../browser/safe_browsing/safe_browsing_navigation_observer_browsertest.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