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

Side by Side Diff: chrome/browser/safe_browsing/malware_details_unittest.cc

Issue 6208003: Add a Node message in the malware details protocol buffer. This allows us to ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include "chrome/browser/renderer_host/test/test_render_view_host.h" 6 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
7 7
8 #include "chrome/browser/browser_thread.h" 8 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/safe_browsing/malware_details.h" 9 #include "chrome/browser/safe_browsing/malware_details.h"
10 #include "chrome/browser/safe_browsing/report.pb.h" 10 #include "chrome/browser/safe_browsing/report.pb.h"
11 #include "chrome/browser/tab_contents/navigation_entry.h" 11 #include "chrome/browser/tab_contents/navigation_entry.h"
12 #include "chrome/browser/tab_contents/test_tab_contents.h" 12 #include "chrome/browser/tab_contents/test_tab_contents.h"
13 #include "chrome/common/render_messages.h" 13 #include "chrome/common/render_messages.h"
14 #include "chrome/common/render_messages_params.h" 14 #include "chrome/common/render_messages_params.h"
15 15
16 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/"; 16 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/";
17 static const char* kLandingURL = "http://www.landingpage.com/"; 17 static const char* kLandingURL = "http://www.landingpage.com/";
18 static const char* kMalwareURL = "http://www.malware.com/"; 18 static const char* kMalwareURL = "http://www.malware.com/";
19 static const char* kHttpsURL = "https://www.url.com/"; 19 static const char* kHttpsURL = "https://www.url.com/";
20 20
21 using safe_browsing::ClientMalwareReportRequest;
22
21 class MalwareDetailsTest : public RenderViewHostTestHarness { 23 class MalwareDetailsTest : public RenderViewHostTestHarness {
22 public: 24 public:
23 MalwareDetailsTest() 25 MalwareDetailsTest()
24 : ui_thread_(BrowserThread::UI, MessageLoop::current()), 26 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
25 io_thread_(BrowserThread::IO, MessageLoop::current()) { 27 io_thread_(BrowserThread::IO, MessageLoop::current()) {
26 } 28 }
27 29
28 virtual void SetUp() { 30 virtual void SetUp() {
29 RenderViewHostTestHarness::SetUp(); 31 RenderViewHostTestHarness::SetUp();
30 } 32 }
31 33
32 static bool ResourceLessThan( 34 static bool ResourceLessThan(
33 const safe_browsing::ClientMalwareReportRequest::Resource* lhs, 35 const ClientMalwareReportRequest::Resource* lhs,
34 const safe_browsing::ClientMalwareReportRequest::Resource* rhs) { 36 const ClientMalwareReportRequest::Resource* rhs) {
35 return lhs->url() < rhs->url(); 37 return lhs->id() < rhs->id();
36 } 38 }
37 39
38 protected: 40 protected:
39 void InitResource(SafeBrowsingService::UnsafeResource* resource, 41 void InitResource(SafeBrowsingService::UnsafeResource* resource,
40 ResourceType::Type resource_type, 42 ResourceType::Type resource_type,
41 const GURL& url) { 43 const GURL& url) {
42 resource->client = NULL; 44 resource->client = NULL;
43 resource->url = url; 45 resource->url = url;
44 resource->resource_type = resource_type; 46 resource->resource_type = resource_type;
45 resource->threat_type = SafeBrowsingService::URL_MALWARE; 47 resource->threat_type = SafeBrowsingService::URL_MALWARE;
46 resource->render_process_host_id = contents_->GetRenderProcessHost()->id(); 48 resource->render_process_host_id = contents_->GetRenderProcessHost()->id();
47 resource->render_view_id = contents_->render_view_host()->routing_id(); 49 resource->render_view_id = contents_->render_view_host()->routing_id();
48 } 50 }
49 51
50 void VerifyResults( 52 void VerifyResults(const ClientMalwareReportRequest& report_pb,
51 const safe_browsing::ClientMalwareReportRequest& report_pb, 53 const ClientMalwareReportRequest& expected_pb) {
52 const safe_browsing::ClientMalwareReportRequest& expected_pb) {
53 EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url()); 54 EXPECT_EQ(expected_pb.malware_url(), report_pb.malware_url());
54 EXPECT_EQ(expected_pb.page_url(), report_pb.page_url()); 55 EXPECT_EQ(expected_pb.page_url(), report_pb.page_url());
55 EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url()); 56 EXPECT_EQ(expected_pb.referrer_url(), report_pb.referrer_url());
56 57
57 ASSERT_EQ(expected_pb.nodes_size(), report_pb.nodes_size()); 58 ASSERT_EQ(expected_pb.resources_size(), report_pb.resources_size());
58 // Sort the nodes, to make the test deterministic 59 // Sort the resources, to make the test deterministic
59 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> 60 std::vector<const ClientMalwareReportRequest::Resource*> resources;
60 nodes; 61 for (int i = 0; i < report_pb.resources_size(); ++i) {
61 for (int i = 0; i < report_pb.nodes_size(); ++i) { 62 const ClientMalwareReportRequest::Resource& resource =
62 const safe_browsing::ClientMalwareReportRequest::Resource& resource = 63 report_pb.resources(i);
63 report_pb.nodes(i); 64 resources.push_back(&resource);
64 nodes.push_back(&resource);
65 } 65 }
66 std::sort(nodes.begin(), nodes.end(), 66 std::sort(resources.begin(), resources.end(),
67 &MalwareDetailsTest::ResourceLessThan); 67 &MalwareDetailsTest::ResourceLessThan);
68 68
69 std::vector<const safe_browsing::ClientMalwareReportRequest::Resource*> 69 std::vector<const ClientMalwareReportRequest::Resource*> expected;
70 expected; 70 for (int i = 0; i < report_pb.resources_size(); ++i) {
71 for (int i = 0; i < report_pb.nodes_size(); ++i) { 71 const ClientMalwareReportRequest::Resource& resource =
72 const safe_browsing::ClientMalwareReportRequest::Resource& resource = 72 expected_pb.resources(i);
73 expected_pb.nodes(i);
74 expected.push_back(&resource); 73 expected.push_back(&resource);
75 } 74 }
76 std::sort(expected.begin(), expected.end(), 75 std::sort(expected.begin(), expected.end(),
77 &MalwareDetailsTest::ResourceLessThan); 76 &MalwareDetailsTest::ResourceLessThan);
78 77
79 for (uint32 i = 0; i < expected.size(); ++i) { 78 for (uint32 i = 0; i < expected.size(); ++i) {
80 EXPECT_EQ(expected[i]->url(), nodes[i]->url()); 79 EXPECT_EQ(expected[i]->id(), resources[i]->id());
81 EXPECT_EQ(expected[i]->parent(), nodes[i]->parent()); 80 EXPECT_EQ(expected[i]->url(), resources[i]->url());
81 EXPECT_EQ(expected[i]->parent_id(), resources[i]->parent_id());
82 } 82 }
83 } 83 }
84 84
85 BrowserThread ui_thread_; 85 BrowserThread ui_thread_;
86 BrowserThread io_thread_; 86 BrowserThread io_thread_;
87 }; 87 };
88 88
89 // Tests creating a simple malware report. 89 // Tests creating a simple malware report.
90 TEST_F(MalwareDetailsTest, MalwareSubResource) { 90 TEST_F(MalwareDetailsTest, MalwareSubResource) {
91 // Start a load. 91 // Start a load.
92 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 92 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
93 93
94 SafeBrowsingService::UnsafeResource resource; 94 SafeBrowsingService::UnsafeResource resource;
95 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 95 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
96 96
97 scoped_refptr<MalwareDetails> report = new MalwareDetails( 97 scoped_refptr<MalwareDetails> report = new MalwareDetails(
98 contents(), resource); 98 contents(), resource);
99 99
100 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 100 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
101 safe_browsing::ClientMalwareReportRequest actual; 101 ClientMalwareReportRequest actual;
102 actual.ParseFromString(*serialized); 102 actual.ParseFromString(*serialized);
103 103
104 safe_browsing::ClientMalwareReportRequest expected; 104 ClientMalwareReportRequest expected;
105 expected.set_malware_url(kMalwareURL); 105 expected.set_malware_url(kMalwareURL);
106 expected.set_page_url(kLandingURL); 106 expected.set_page_url(kLandingURL);
107 expected.set_referrer_url(""); 107 expected.set_referrer_url("");
108 108
109 safe_browsing::ClientMalwareReportRequest::Resource* node = 109 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
110 expected.add_nodes(); 110 pb_resource->set_id(0);
111 node->set_url(kLandingURL); 111 pb_resource->set_url(kLandingURL);
112 node = expected.add_nodes(); 112 pb_resource = expected.add_resources();
113 node->set_url(kMalwareURL); 113 pb_resource->set_id(1);
114 pb_resource->set_url(kMalwareURL);
114 115
115 VerifyResults(actual, expected); 116 VerifyResults(actual, expected);
116 } 117 }
117 118
118 // Tests creating a simple malware report where the subresource has a 119 // Tests creating a simple malware report where the subresource has a
119 // different original_url. 120 // different original_url.
120 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { 121 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) {
121 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED); 122 controller().LoadURL(GURL(kLandingURL), GURL(), PageTransition::TYPED);
122 123
123 SafeBrowsingService::UnsafeResource resource; 124 SafeBrowsingService::UnsafeResource resource;
124 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 125 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
125 resource.original_url = GURL(kOriginalLandingURL); 126 resource.original_url = GURL(kOriginalLandingURL);
126 127
127 scoped_refptr<MalwareDetails> report = new MalwareDetails( 128 scoped_refptr<MalwareDetails> report = new MalwareDetails(
128 contents(), resource); 129 contents(), resource);
129 130
130 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 131 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
131 safe_browsing::ClientMalwareReportRequest actual; 132 ClientMalwareReportRequest actual;
132 actual.ParseFromString(*serialized); 133 actual.ParseFromString(*serialized);
133 134
134 safe_browsing::ClientMalwareReportRequest expected; 135 ClientMalwareReportRequest expected;
135 expected.set_malware_url(kMalwareURL); 136 expected.set_malware_url(kMalwareURL);
136 expected.set_page_url(kLandingURL); 137 expected.set_page_url(kLandingURL);
137 expected.set_referrer_url(""); 138 expected.set_referrer_url("");
138 139
139 safe_browsing::ClientMalwareReportRequest::Resource* node = 140 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
140 expected.add_nodes(); 141 pb_resource->set_id(0);
141 node->set_url(kLandingURL); 142 pb_resource->set_url(kLandingURL);
142 143
143 // Malware url should have originalurl as parent. 144 pb_resource = expected.add_resources();
144 node = expected.add_nodes(); 145 pb_resource->set_id(1);
145 node->set_url(kMalwareURL); 146 pb_resource->set_url(kOriginalLandingURL);
146 node->set_parent(kOriginalLandingURL);
147 147
148 node = expected.add_nodes(); 148 pb_resource = expected.add_resources();
149 node->set_url(kOriginalLandingURL); 149 pb_resource->set_id(2);
150 pb_resource->set_url(kMalwareURL);
151 // The Resource for kMmalwareUrl should have the Resource for
152 // kOriginalLandingURL (with id 1) as parent.
153 pb_resource->set_parent_id(1);
150 154
151 VerifyResults(actual, expected); 155 VerifyResults(actual, expected);
152 } 156 }
153 157
154 // Verify that https:// urls are dropped. 158 // Verify that https:// urls are dropped.
155 TEST_F(MalwareDetailsTest, NotPublicUrl) { 159 TEST_F(MalwareDetailsTest, NotPublicUrl) {
156 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED); 160 controller().LoadURL(GURL(kHttpsURL), GURL(), PageTransition::TYPED);
157 SafeBrowsingService::UnsafeResource resource; 161 SafeBrowsingService::UnsafeResource resource;
158 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL)); 162 InitResource(&resource, ResourceType::SUB_RESOURCE, GURL(kMalwareURL));
159 scoped_refptr<MalwareDetails> report = new MalwareDetails( 163 scoped_refptr<MalwareDetails> report = new MalwareDetails(
160 contents(), resource); 164 contents(), resource);
161 165
162 scoped_ptr<const std::string> serialized(report->GetSerializedReport()); 166 scoped_ptr<const std::string> serialized(report->GetSerializedReport());
163 safe_browsing::ClientMalwareReportRequest actual; 167 ClientMalwareReportRequest actual;
164 actual.ParseFromString(*serialized); 168 actual.ParseFromString(*serialized);
165 169
166 safe_browsing::ClientMalwareReportRequest expected; 170 ClientMalwareReportRequest expected;
167 expected.set_malware_url(kMalwareURL); // No page_url 171 expected.set_malware_url(kMalwareURL); // No page_url
168 expected.set_referrer_url(""); 172 expected.set_referrer_url("");
169 173
170 safe_browsing::ClientMalwareReportRequest::Resource* node = 174 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources();
171 expected.add_nodes(); 175 pb_resource->set_url(kMalwareURL); // Only one resource
172 node->set_url(kMalwareURL); // Only one node
173 176
174 VerifyResults(actual, expected); 177 VerifyResults(actual, expected);
175 } 178 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/malware_details.cc ('k') | chrome/browser/safe_browsing/report.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698