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

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

Issue 1624723002: Move remote_db_manager into the safe_browsing_db component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fetch, rebase, format Created 4 years, 10 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
OLDNEW
(Empty)
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.
4
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"
9
10 namespace safe_browsing {
11
12 class SafeBrowsingApiHandlerUtilTest : public ::testing::Test {
13 protected:
14 SBThreatType threat_;
15 std::string pb_str_;
16
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_);
21 }
22
23 MalwarePatternType::PATTERN_TYPE ParsePatternType() {
24 MalwarePatternType proto;
25 EXPECT_TRUE(proto.ParseFromString(pb_str_));
26 return proto.pattern_type();
27 }
28 };
29
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());
34
35 EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("{"));
36 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
37 EXPECT_TRUE(pb_str_.empty());
38
39 EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("[]"));
40 EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
41 EXPECT_TRUE(pb_str_.empty());
42
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());
47
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());
52
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());
57
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());
62 }
63
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());
69
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());
74 }
75
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());
83 }
84
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());
91
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());
97
98 EXPECT_EQ(UMA_STATUS_UNSAFE,
99 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
100 "\"pha_pattern_type\":\"junk\"}]}"));
101 EXPECT_TRUE(pb_str_.empty());
102 }
103
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());
110
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());
116
117 EXPECT_EQ(UMA_STATUS_UNSAFE,
118 ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
119 "\"se_pattern_type\":\"junk\"}]}"));
120 EXPECT_TRUE(pb_str_.empty());
121 }
122
123 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698