Home | History | Annotate | Download | only in safe_browsing
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 
      6 #include "base/logging.h"
      7 #include "base/strings/stringprintf.h"
      8 #include "base/time/time.h"
      9 #include "chrome/browser/safe_browsing/ping_manager.h"
     10 #include "google_apis/google_api_keys.h"
     11 #include "net/base/escape.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 using base::Time;
     15 using base::TimeDelta;
     16 
     17 static const char kUrlPrefix[] = "https://prefix.com/foo";
     18 static const char kClient[] = "unittest";
     19 static const char kAppVer[] = "1.0";
     20 
     21 class SafeBrowsingPingManagerTest : public testing::Test {
     22  protected:
     23   std::string key_param_;
     24 
     25   virtual void SetUp() {
     26     std::string key = google_apis::GetAPIKey();
     27     if (!key.empty()) {
     28       key_param_ = base::StringPrintf(
     29           "&key=%s",
     30           net::EscapeQueryParamValue(key, true).c_str());
     31     }
     32   }
     33 };
     34 
     35 TEST_F(SafeBrowsingPingManagerTest, TestSafeBrowsingHitUrl) {
     36   SafeBrowsingProtocolConfig config;
     37   config.client_name = kClient;
     38   config.url_prefix = kUrlPrefix;
     39   SafeBrowsingPingManager pm(NULL, config);
     40 
     41   pm.version_ = kAppVer;
     42 
     43   GURL malicious_url("http://malicious.url.com");
     44   GURL page_url("http://page.url.com");
     45   GURL referrer_url("http://referrer.url.com");
     46   EXPECT_EQ("https://prefix.com/foo/report?client=unittest&appver=1.0&"
     47             "pver=2.2" + key_param_ +
     48             "&evts=malblhit&evtd=http%3A%2F%2Fmalicious.url.com%2F&"
     49             "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer."
     50             "url.com%2F&evtb=1",
     51             pm.SafeBrowsingHitUrl(
     52                 malicious_url, page_url, referrer_url,
     53                 true, SB_THREAT_TYPE_URL_MALWARE).spec());
     54 
     55   EXPECT_EQ("https://prefix.com/foo/report?client=unittest&appver=1.0&"
     56             "pver=2.2" + key_param_ + "&evts=phishblhit&"
     57             "evtd=http%3A%2F%2Fmalicious.url.com%2F&"
     58             "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer."
     59             "url.com%2F&evtb=0",
     60             pm.SafeBrowsingHitUrl(
     61                 malicious_url, page_url, referrer_url,
     62                 false, SB_THREAT_TYPE_URL_PHISHING).spec());
     63 
     64   EXPECT_EQ("https://prefix.com/foo/report?client=unittest&appver=1.0&"
     65             "pver=2.2" + key_param_ + "&evts=binurlhit&"
     66             "evtd=http%3A%2F%2Fmalicious.url.com%2F&"
     67             "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer."
     68             "url.com%2F&evtb=0",
     69             pm.SafeBrowsingHitUrl(
     70                 malicious_url, page_url, referrer_url,
     71                 false, SB_THREAT_TYPE_BINARY_MALWARE_URL).spec());
     72 
     73   EXPECT_EQ("https://prefix.com/foo/report?client=unittest&appver=1.0&"
     74             "pver=2.2" + key_param_ + "&evts=binhashhit&"
     75             "evtd=http%3A%2F%2Fmalicious.url.com%2F&"
     76             "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer."
     77             "url.com%2F&evtb=0",
     78             pm.SafeBrowsingHitUrl(
     79                 malicious_url, page_url, referrer_url,
     80                 false, SB_THREAT_TYPE_BINARY_MALWARE_HASH).spec());
     81 
     82   EXPECT_EQ("https://prefix.com/foo/report?client=unittest&appver=1.0&"
     83             "pver=2.2" + key_param_ + "&evts=phishcsdhit&"
     84             "evtd=http%3A%2F%2Fmalicious.url.com%2F&"
     85             "evtr=http%3A%2F%2Fpage.url.com%2F&evhr=http%3A%2F%2Freferrer."
     86             "url.com%2F&evtb=0",
     87             pm.SafeBrowsingHitUrl(
     88                 malicious_url, page_url, referrer_url,
     89                 false, SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL).spec());
     90 }
     91 
     92 TEST_F(SafeBrowsingPingManagerTest, TestMalwareDetailsUrl) {
     93   SafeBrowsingProtocolConfig config;
     94   config.client_name = kClient;
     95   config.url_prefix = kUrlPrefix;
     96   SafeBrowsingPingManager pm(NULL, config);
     97 
     98   pm.version_ = kAppVer;
     99   EXPECT_EQ("https://prefix.com/foo/clientreport/malware?"
    100             "client=unittest&appver=1.0&pver=1.0" + key_param_,
    101             pm.MalwareDetailsUrl().spec());
    102 }
    103