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

Unified Diff: components/safe_browsing_db/safe_browsing_api_handler_unittest.cc

Issue 1726403006: Switch Safe Browsing's metadata from string to struct. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ParseJson in test 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 side-by-side diff with in-line comments
Download patch
Index: components/safe_browsing_db/safe_browsing_api_handler_unittest.cc
diff --git a/components/safe_browsing_db/safe_browsing_api_handler_unittest.cc b/components/safe_browsing_db/safe_browsing_api_handler_unittest.cc
index 20cbe053cf09e78860689f148786ae0304cb98ec..15b50634c57ea980851004985b8011246ee4acfd 100644
--- a/components/safe_browsing_db/safe_browsing_api_handler_unittest.cc
+++ b/components/safe_browsing_db/safe_browsing_api_handler_unittest.cc
@@ -4,6 +4,7 @@
#include "components/safe_browsing_db/metadata.pb.h"
#include "components/safe_browsing_db/safe_browsing_api_handler_util.h"
+#include "components/safe_browsing_db/testing_util.h"
#include "components/safe_browsing_db/util.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -12,65 +13,61 @@ namespace safe_browsing {
class SafeBrowsingApiHandlerUtilTest : public ::testing::Test {
protected:
SBThreatType threat_;
- std::string pb_str_;
+ ThreatMetadata meta_;
+ const ThreatMetadata empty_meta_;
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_);
+ meta_ = ThreatMetadata();
+ return ParseJsonFromGMSCore(json, &threat_, &meta_);
}
- 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(empty_meta_, meta_);
EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("{"));
EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("[]"));
EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
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(empty_meta_, meta_);
EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
ResetAndParseJson("{\"matches\":[{}]}"));
EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
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(empty_meta_, meta_);
EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
ResetAndParseJson("{\"matches\":[{\"threat_type\":\"999\"}]}"));
EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
}
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(empty_meta_, meta_);
EXPECT_EQ(UMA_STATUS_UNSAFE,
ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\"}]}"));
EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
}
TEST_F(SafeBrowsingApiHandlerUtilTest, MultipleThreats) {
@@ -79,45 +76,68 @@ TEST_F(SafeBrowsingApiHandlerUtilTest, MultipleThreats) {
ResetAndParseJson(
"{\"matches\":[{\"threat_type\":\"4\"}, {\"threat_type\":\"5\"}]}"));
EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
}
TEST_F(SafeBrowsingApiHandlerUtilTest, PhaSubType) {
+ ThreatMetadata expected;
+
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());
+ expected.threat_pattern_type = ThreatPatternType::LANDING;
+ EXPECT_EQ(expected, meta_);
+ // Test the ThreatMetadata comparitor for this field.
+ EXPECT_NE(empty_meta_, meta_);
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());
+ expected.threat_pattern_type = ThreatPatternType::DISTRIBUTION;
+ EXPECT_EQ(expected, meta_);
EXPECT_EQ(UMA_STATUS_UNSAFE,
ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
"\"pha_pattern_type\":\"junk\"}]}"));
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
}
TEST_F(SafeBrowsingApiHandlerUtilTest, SocialEngineeringSubType) {
+ ThreatMetadata expected;
+
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());
+ expected.threat_pattern_type = ThreatPatternType::LANDING;
+ EXPECT_EQ(expected, meta_);
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());
+ expected.threat_pattern_type = ThreatPatternType::DISTRIBUTION;
+ EXPECT_EQ(expected, meta_);
EXPECT_EQ(UMA_STATUS_UNSAFE,
ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
"\"se_pattern_type\":\"junk\"}]}"));
- EXPECT_TRUE(pb_str_.empty());
+ EXPECT_EQ(empty_meta_, meta_);
+}
+
+TEST_F(SafeBrowsingApiHandlerUtilTest, PopulationId) {
+ ThreatMetadata expected;
+
+ EXPECT_EQ(UMA_STATUS_UNSAFE,
+ ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
+ "\"UserPopulation\":\"foobarbazz\"}]}"));
+ EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
+ expected.population_id = "foobarbazz";
+ EXPECT_EQ(expected, meta_);
+ // Test the ThreatMetadata comparator for this field.
+ EXPECT_NE(empty_meta_, meta_);
}
} // namespace safe_browsing
« no previous file with comments | « components/safe_browsing_db/safe_browsing_api_handler.cc ('k') | components/safe_browsing_db/safe_browsing_api_handler_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698