summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/malware_details_unittest.cc
blob: b6136c87d62922820a290248a302c38b4b77a6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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));

  scoped_refptr<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);

  scoped_refptr<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));
  scoped_refptr<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);
}