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

Unified Diff: chrome/browser/safe_browsing/safe_browsing_api_handler_unittest.cc

Issue 1620813005: Revert of Move remote_db_manager into the safe_browsing_db component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/safe_browsing/safe_browsing_api_handler_unittest.cc
diff --git a/chrome/browser/safe_browsing/safe_browsing_api_handler_unittest.cc b/chrome/browser/safe_browsing/safe_browsing_api_handler_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..af7d97ff41a405840c97f6a547bb50e866e38ba2
--- /dev/null
+++ b/chrome/browser/safe_browsing/safe_browsing_api_handler_unittest.cc
@@ -0,0 +1,123 @@
+// Copyright 2015 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 "chrome/browser/safe_browsing/metadata.pb.h"
+#include "chrome/browser/safe_browsing/safe_browsing_api_handler_util.h"
+#include "chrome/browser/safe_browsing/safe_browsing_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace safe_browsing {
+
+class SafeBrowsingApiHandlerUtilTest : public ::testing::Test {
+ protected:
+ SBThreatType threat_;
+ std::string pb_str_;
+
+ UmaRemoteCallResult ResetAndParseJson(const std::string& json) {
+ threat_ = SB_THREAT_TYPE_EXTENSION; // Should never be seen
+ pb_str_ = "unitialized value";
+ return ParseJsonToThreatAndPB(json, &threat_, &pb_str_);
+ }
+
+ MalwarePatternType::PATTERN_TYPE ParsePatternType() {
+ MalwarePatternType proto;
+ EXPECT_TRUE(proto.ParseFromString(pb_str_));
+ return proto.pattern_type();
+ }
+};
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, BadJson) {
+ EXPECT_EQ(UMA_STATUS_JSON_EMPTY, ResetAndParseJson(""));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("{"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("[]"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE,
+ ResetAndParseJson("{\"matches\":\"foo\"}"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
+ ResetAndParseJson("{\"matches\":[{}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"junk\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"999\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+}
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, BasicThreats) {
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+}
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, MultipleThreats) {
+ EXPECT_EQ(
+ UMA_STATUS_UNSAFE,
+ ResetAndParseJson(
+ "{\"matches\":[{\"threat_type\":\"4\"}, {\"threat_type\":\"5\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
+ EXPECT_TRUE(pb_str_.empty());
+}
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, PhaSubType) {
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
+ "\"pha_pattern_type\":\"LANDING\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
+ EXPECT_EQ(MalwarePatternType::LANDING, ParsePatternType());
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
+ "\"pha_pattern_type\":\"DISTRIBUTION\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
+ EXPECT_EQ(MalwarePatternType::DISTRIBUTION, ParsePatternType());
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
+ "\"pha_pattern_type\":\"junk\"}]}"));
+ EXPECT_TRUE(pb_str_.empty());
+}
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, SocialEngineeringSubType) {
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
+ "\"se_pattern_type\":\"LANDING\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
+ EXPECT_EQ(MalwarePatternType::LANDING, ParsePatternType());
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
+ "\"se_pattern_type\":\"DISTRIBUTION\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
+ EXPECT_EQ(MalwarePatternType::DISTRIBUTION, ParsePatternType());
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
+ "\"se_pattern_type\":\"junk\"}]}"));
+ EXPECT_TRUE(pb_str_.empty());
+}
+
+} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698