QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_api_handler_unittest.cc
blobaf7d97ff41a405840c97f6a547bb50e866e38ba2
1 // Copyright 2015 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.
5 #include "chrome/browser/safe_browsing/metadata.pb.h"
6 #include "chrome/browser/safe_browsing/safe_browsing_api_handler_util.h"
7 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace safe_browsing {
12 class SafeBrowsingApiHandlerUtilTest : public ::testing::Test {
13 protected:
14 SBThreatType threat_;
15 std::string pb_str_;
17 UmaRemoteCallResult ResetAndParseJson(const std::string& json) {
18 threat_ = SB_THREAT_TYPE_EXTENSION; // Should never be seen
19 pb_str_ = "unitialized value";
20 return ParseJsonToThreatAndPB(json, &threat_, &pb_str_);
23 MalwarePatternType::PATTERN_TYPE ParsePatternType() {
24 MalwarePatternType proto;
25 EXPECT_TRUE(proto.ParseFromString(pb_str_));
26 return proto.pattern_type();
30 TEST_F(SafeBrowsingApiHandlerUtilTest, BadJson) {
31 EXPECT_EQ(UMA_STATUS_JSON_EMPTY, ResetAndParseJson(""));
32 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
33 EXPECT_TRUE(pb_str_.empty());
35 EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("{"));
36 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
37 EXPECT_TRUE(pb_str_.empty());
39 EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("[]"));
40 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
41 EXPECT_TRUE(pb_str_.empty());
43 EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE,
44 ResetAndParseJson("{\"matches\":\"foo\"}"));
45 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
46 EXPECT_TRUE(pb_str_.empty());
48 EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
49 ResetAndParseJson("{\"matches\":[{}]}"));
50 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
51 EXPECT_TRUE(pb_str_.empty());
53 EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
54 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"junk\"}]}"));
55 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
56 EXPECT_TRUE(pb_str_.empty());
58 EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
59 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"999\"}]}"));
60 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
61 EXPECT_TRUE(pb_str_.empty());
64 TEST_F(SafeBrowsingApiHandlerUtilTest, BasicThreats) {
65 EXPECT_EQ(UMA_STATUS_UNSAFE,
66 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\"}]}"));
67 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
68 EXPECT_TRUE(pb_str_.empty());
70 EXPECT_EQ(UMA_STATUS_UNSAFE,
71 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\"}]}"));
72 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
73 EXPECT_TRUE(pb_str_.empty());
76 TEST_F(SafeBrowsingApiHandlerUtilTest, MultipleThreats) {
77 EXPECT_EQ(
78 UMA_STATUS_UNSAFE,
79 ResetAndParseJson(
80 "{\"matches\":[{\"threat_type\":\"4\"}, {\"threat_type\":\"5\"}]}"));
81 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
82 EXPECT_TRUE(pb_str_.empty());
85 TEST_F(SafeBrowsingApiHandlerUtilTest, PhaSubType) {
86 EXPECT_EQ(UMA_STATUS_UNSAFE,
87 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
88 "\"pha_pattern_type\":\"LANDING\"}]}"));
89 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
90 EXPECT_EQ(MalwarePatternType::LANDING, ParsePatternType());
92 EXPECT_EQ(UMA_STATUS_UNSAFE,
93 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
94 "\"pha_pattern_type\":\"DISTRIBUTION\"}]}"));
95 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
96 EXPECT_EQ(MalwarePatternType::DISTRIBUTION, ParsePatternType());
98 EXPECT_EQ(UMA_STATUS_UNSAFE,
99 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
100 "\"pha_pattern_type\":\"junk\"}]}"));
101 EXPECT_TRUE(pb_str_.empty());
104 TEST_F(SafeBrowsingApiHandlerUtilTest, SocialEngineeringSubType) {
105 EXPECT_EQ(UMA_STATUS_UNSAFE,
106 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
107 "\"se_pattern_type\":\"LANDING\"}]}"));
108 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
109 EXPECT_EQ(MalwarePatternType::LANDING, ParsePatternType());
111 EXPECT_EQ(UMA_STATUS_UNSAFE,
112 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
113 "\"se_pattern_type\":\"DISTRIBUTION\"}]}"));
114 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
115 EXPECT_EQ(MalwarePatternType::DISTRIBUTION, ParsePatternType());
117 EXPECT_EQ(UMA_STATUS_UNSAFE,
118 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
119 "\"se_pattern_type\":\"junk\"}]}"));
120 EXPECT_TRUE(pb_str_.empty());
123 } // namespace safe_browsing