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

Unified Diff: chrome/browser/safe_browsing/malware_details_unittest.cc

Issue 4822002: Send malware reports when a user opts-in from the safe browsing interstitial ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years 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
« no previous file with comments | « chrome/browser/safe_browsing/malware_details.cc ('k') | chrome/browser/safe_browsing/protocol_manager.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/safe_browsing/malware_details_unittest.cc
===================================================================
--- chrome/browser/safe_browsing/malware_details_unittest.cc (revision 0)
+++ chrome/browser/safe_browsing/malware_details_unittest.cc (revision 0)
@@ -0,0 +1,172 @@
+// Copyright (c) 2010 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 <algorithm>
+#include "chrome/browser/renderer_host/test/test_render_view_host.h"
+
+#include "chrome/browser/browser_thread.h"
+#include "chrome/browser/safe_browsing/malware_details.h"
+#include "chrome/browser/safe_browsing/report.pb.h"
+#include "chrome/browser/tab_contents/navigation_entry.h"
+#include "chrome/browser/tab_contents/test_tab_contents.h"
+#include "chrome/common/render_messages.h"
+#include "chrome/common/render_messages_params.h"
+
+static const char* kOriginalLandingURL = "http://www.originallandingpage.com/";
+static const char* kLandingURL = "http://www.landingpage.com/";
+static const char* kMalwareURL = "http://www.malware.com/";
+static const char* kHttpsURL = "https://www.url.com/";
+
+class MalwareDetailsTest : public RenderViewHostTestHarness {
+ public:
+ MalwareDetailsTest()
+ : ui_thread_(BrowserThread::UI, MessageLoop::current()),
+ io_thread_(BrowserThread::IO, MessageLoop::current()) {
+ }
+
+ virtual void SetUp() {
+ RenderViewHostTestHarness::SetUp();
+ }
+
+ static bool ResourceLessThan(
+ const safe_browsing::ClientMalwareReportRequest::Resource* lhs,
+ const safe_browsing::ClientMalwareReportRequest::Resource* rhs) {
+ return lhs->url() < rhs->url();
+ }
+
+ protected:
+ void InitResource(SafeBrowsingService::UnsafeResource* resource,
+ ResourceType::Type resource_type,
+ const GURL& url) {
+ resource->client = NULL;
+ resource->url = url;
+ resource->resource_type = resource_type;
+ resource->threat_type = SafeBrowsingService::URL_MALWARE;
+ resource->render_process_host_id = contents_->GetRenderProcessHost()->id();
+ resource->render_view_id = contents_->render_view_host()->routing_id();
+ }
+
+ void VerifyResults(
+ const safe_browsing::ClientMalwareReportRequest& report_pb,
+ const safe_browsing::ClientMalwareReportRequest& expected_pb) {
+ EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url());
+ EXPECT_EQ(expected_pb.page_url(), report_pb.page_url());
+ EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url());
+
+ ASSERT_EQ(expected_pb.nodes_size(), report_pb.nodes_size());
+ // Sort the nodes, to make the test deterministic
+ std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*>
+ nodes;
+ for (int i = 0; i < report_pb.nodes_size(); ++i) {
+ const safe_browsing::ClientMalwareReportRequest::Resource& resource =
+ report_pb.nodes(i);
+ nodes.push_back(&resource);
+ }
+ std::sort(nodes.begin(), nodes.end(),
+ &MalwareDetailsTest::ResourceLessThan);
+
+ std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*>
+ expected;
+ for (int i = 0; i < report_pb.nodes_size(); ++i) {
+ const safe_browsing::ClientMalwareReportRequest::Resource& resource =
+ expected_pb.nodes(i);
+ expected.push_back(&resource);
+ }
+ std::sort(expected.begin(), expected.end(),
+ &MalwareDetailsTest::ResourceLessThan);
+
+ for (uint32 i = 0; i < expected.size(); ++i) {
+ EXPECT_EQ(expected[i]->url(), nodes[i]->url());
+ EXPECT_EQ(expected[i]->parent(), nodes[i]->parent());
+ }
+ }
+
+ BrowserThread ui_thread_;
+ BrowserThread io_thread_;
+};
+
+// Tests creating a simple malware report.
+TEST_F(MalwareDetailsTest, MalwareSubResource) {
+ // Start a load.
+ controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
+
+ SafeBrowsingService::UnsafeResource resource;
+ InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
+
+ MalwareDetails* report = new MalwareDetails(contents(), resource);
+
+ scoped_ptr<const std::string> serialized(report->GetSerializedReport());
+ safe_browsing::ClientMalwareReportRequest actual;
+ actual.ParseFromString(*serialized);
+
+ safe_browsing::ClientMalwareReportRequest expected;
+ expected.set_malware_url(kMalwareURL);
+ expected.set_page_url(kLandingURL);
+ expected.set_referrer_url("");
+
+ safe_browsing::ClientMalwareReportRequest::Resource* node =
+ expected.add_nodes();
+ node->set_url(kLandingURL);
+ node = expected.add_nodes();
+ node->set_url(kMalwareURL);
+
+ VerifyResults(actual, expected);
+}
+
+// Tests creating a simple malware report where the subresource has a
+// different original_url.
+TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) {
+ controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
+
+ SafeBrowsingService::UnsafeResource resource;
+ InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
+ resource.original_url = GURL(kOriginalLandingURL);
+
+ MalwareDetails* report = new MalwareDetails(contents(), resource);
+
+ scoped_ptr<const std::string> serialized(report->GetSerializedReport());
+ safe_browsing::ClientMalwareReportRequest actual;
+ actual.ParseFromString(*serialized);
+
+ safe_browsing::ClientMalwareReportRequest expected;
+ expected.set_malware_url(kMalwareURL);
+ expected.set_page_url(kLandingURL);
+ expected.set_referrer_url("");
+
+ safe_browsing::ClientMalwareReportRequest::Resource* node =
+ expected.add_nodes();
+ node->set_url(kLandingURL);
+
+ // Malware url should have originalurl as parent.
+ node = expected.add_nodes();
+ node->set_url(kMalwareURL);
+ node->set_parent(kOriginalLandingURL);
+
+ node = expected.add_nodes();
+ node->set_url(kOriginalLandingURL);
+
+ VerifyResults(actual, expected);
+}
+
+// Verify that https:// urls are dropped.
+TEST_F(MalwareDetailsTest, NotPublicUrl) {
+ controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED);
+ SafeBrowsingService::UnsafeResource resource;
+ InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
+ MalwareDetails* report = new MalwareDetails(contents(), resource);
+
+ scoped_ptr<const std::string> serialized(report->GetSerializedReport());
+ safe_browsing::ClientMalwareReportRequest actual;
+ actual.ParseFromString(*serialized);
+
+ safe_browsing::ClientMalwareReportRequest expected;
+ expected.set_malware_url(kMalwareURL); // No page_url
+ expected.set_referrer_url("");
+
+ safe_browsing::ClientMalwareReportRequest::Resource* node =
+ expected.add_nodes();
+ node->set_url(kMalwareURL); // Only one node
+
+ VerifyResults(actual, expected);
+}
Property changes on: chrome/browser/safe_browsing/malware_details_unittest.cc
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « chrome/browser/safe_browsing/malware_details.cc ('k') | chrome/browser/safe_browsing/protocol_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698