PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/components/safe_browsing/android/safe_browsing_api_handler_unittest.cc

http://github.com/chromium/chromium
C++ | 238 lines | 195 code | 38 blank | 5 comment | 2 complexity | 2210d8b5919ba6e31153aec679236b31 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  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. #include "components/safe_browsing/android/safe_browsing_api_handler_util.h"
  5. #include <string>
  6. #include "base/strings/stringprintf.h"
  7. #include "components/safe_browsing/core/db/metadata.pb.h"
  8. #include "components/safe_browsing/core/db/util.h"
  9. #include "components/safe_browsing/core/db/v4_test_util.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace safe_browsing {
  12. class SafeBrowsingApiHandlerUtilTest : public ::testing::Test {
  13. protected:
  14. SBThreatType threat_;
  15. ThreatMetadata meta_;
  16. const ThreatMetadata empty_meta_;
  17. UmaRemoteCallResult ResetAndParseJson(const std::string& json) {
  18. threat_ = SB_THREAT_TYPE_EXTENSION; // Should never be seen
  19. meta_ = ThreatMetadata();
  20. return ParseJsonFromGMSCore(json, &threat_, &meta_);
  21. }
  22. };
  23. TEST_F(SafeBrowsingApiHandlerUtilTest, BadJson) {
  24. EXPECT_EQ(UMA_STATUS_JSON_EMPTY, ResetAndParseJson(""));
  25. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  26. EXPECT_EQ(empty_meta_, meta_);
  27. EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("{"));
  28. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  29. EXPECT_EQ(empty_meta_, meta_);
  30. EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE, ResetAndParseJson("[]"));
  31. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  32. EXPECT_EQ(empty_meta_, meta_);
  33. EXPECT_EQ(UMA_STATUS_JSON_FAILED_TO_PARSE,
  34. ResetAndParseJson("{\"matches\":\"foo\"}"));
  35. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  36. EXPECT_EQ(empty_meta_, meta_);
  37. EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
  38. ResetAndParseJson("{\"matches\":[{}]}"));
  39. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  40. EXPECT_EQ(empty_meta_, meta_);
  41. EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
  42. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"junk\"}]}"));
  43. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  44. EXPECT_EQ(empty_meta_, meta_);
  45. EXPECT_EQ(UMA_STATUS_JSON_UNKNOWN_THREAT,
  46. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"999\"}]}"));
  47. EXPECT_EQ(SB_THREAT_TYPE_SAFE, threat_);
  48. EXPECT_EQ(empty_meta_, meta_);
  49. }
  50. TEST_F(SafeBrowsingApiHandlerUtilTest, BasicThreats) {
  51. EXPECT_EQ(UMA_STATUS_MATCH,
  52. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\"}]}"));
  53. EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
  54. EXPECT_EQ(empty_meta_, meta_);
  55. EXPECT_EQ(UMA_STATUS_MATCH,
  56. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\"}]}"));
  57. EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
  58. EXPECT_EQ(empty_meta_, meta_);
  59. }
  60. TEST_F(SafeBrowsingApiHandlerUtilTest, MultipleThreats) {
  61. EXPECT_EQ(
  62. UMA_STATUS_MATCH,
  63. ResetAndParseJson(
  64. "{\"matches\":[{\"threat_type\":\"4\"}, {\"threat_type\":\"5\"}]}"));
  65. EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
  66. EXPECT_EQ(empty_meta_, meta_);
  67. }
  68. TEST_F(SafeBrowsingApiHandlerUtilTest, PhaSubType) {
  69. ThreatMetadata expected;
  70. EXPECT_EQ(UMA_STATUS_MATCH,
  71. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
  72. "\"pha_pattern_type\":\"LANDING\"}]}"));
  73. EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
  74. expected.threat_pattern_type = ThreatPatternType::MALWARE_LANDING;
  75. EXPECT_EQ(expected, meta_);
  76. // Test the ThreatMetadata comparitor for this field.
  77. EXPECT_NE(empty_meta_, meta_);
  78. EXPECT_EQ(UMA_STATUS_MATCH,
  79. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
  80. "\"pha_pattern_type\":\"DISTRIBUTION\"}]}"));
  81. EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
  82. expected.threat_pattern_type = ThreatPatternType::MALWARE_DISTRIBUTION;
  83. EXPECT_EQ(expected, meta_);
  84. EXPECT_EQ(UMA_STATUS_MATCH,
  85. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
  86. "\"pha_pattern_type\":\"junk\"}]}"));
  87. EXPECT_EQ(empty_meta_, meta_);
  88. }
  89. TEST_F(SafeBrowsingApiHandlerUtilTest, SocialEngineeringSubType) {
  90. ThreatMetadata expected;
  91. EXPECT_EQ(
  92. UMA_STATUS_MATCH,
  93. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
  94. "\"se_pattern_type\":\"SOCIAL_ENGINEERING_ADS\"}]}"));
  95. EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
  96. expected.threat_pattern_type = ThreatPatternType::SOCIAL_ENGINEERING_ADS;
  97. EXPECT_EQ(expected, meta_);
  98. EXPECT_EQ(UMA_STATUS_MATCH,
  99. ResetAndParseJson(
  100. "{\"matches\":[{\"threat_type\":\"5\", "
  101. "\"se_pattern_type\":\"SOCIAL_ENGINEERING_LANDING\"}]}"));
  102. EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
  103. expected.threat_pattern_type = ThreatPatternType::SOCIAL_ENGINEERING_LANDING;
  104. EXPECT_EQ(expected, meta_);
  105. EXPECT_EQ(UMA_STATUS_MATCH,
  106. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
  107. "\"se_pattern_type\":\"PHISHING\"}]}"));
  108. EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, threat_);
  109. expected.threat_pattern_type = ThreatPatternType::PHISHING;
  110. EXPECT_EQ(expected, meta_);
  111. EXPECT_EQ(UMA_STATUS_MATCH,
  112. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"5\", "
  113. "\"se_pattern_type\":\"junk\"}]}"));
  114. EXPECT_EQ(empty_meta_, meta_);
  115. }
  116. TEST_F(SafeBrowsingApiHandlerUtilTest, PopulationId) {
  117. ThreatMetadata expected;
  118. EXPECT_EQ(UMA_STATUS_MATCH,
  119. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"4\", "
  120. "\"UserPopulation\":\"foobarbazz\"}]}"));
  121. EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, threat_);
  122. expected.population_id = "foobarbazz";
  123. EXPECT_EQ(expected, meta_);
  124. // Test the ThreatMetadata comparator for this field.
  125. EXPECT_NE(empty_meta_, meta_);
  126. }
  127. TEST_F(SafeBrowsingApiHandlerUtilTest, NoSubresourceFilterSubTypes) {
  128. ThreatMetadata expected;
  129. EXPECT_EQ(UMA_STATUS_MATCH,
  130. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"13\"}]}"));
  131. EXPECT_EQ(SB_THREAT_TYPE_SUBRESOURCE_FILTER, threat_);
  132. expected.threat_pattern_type = ThreatPatternType::NONE;
  133. EXPECT_EQ(expected, meta_);
  134. EXPECT_EQ(UMA_STATUS_MATCH,
  135. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"13\", "
  136. "\"se_pattern_type\":\"junk\"}]}"));
  137. EXPECT_EQ(SB_THREAT_TYPE_SUBRESOURCE_FILTER, threat_);
  138. expected.threat_pattern_type = ThreatPatternType::NONE;
  139. EXPECT_EQ(expected, meta_);
  140. }
  141. TEST_F(SafeBrowsingApiHandlerUtilTest, SubresourceFilterSubTypes) {
  142. typedef SubresourceFilterLevel Level;
  143. typedef SubresourceFilterType Type;
  144. const struct {
  145. const char* abusive_type;
  146. const char* bas_type;
  147. SubresourceFilterMatch expected_match;
  148. } test_cases[] = {
  149. {"warn",
  150. "enforce",
  151. {{Type::ABUSIVE, Level::WARN}, {Type::BETTER_ADS, Level::ENFORCE}}},
  152. {nullptr, "warn", {{Type::BETTER_ADS, Level::WARN}}},
  153. {"asdf",
  154. "",
  155. {{Type::ABUSIVE, Level::ENFORCE}, {Type::BETTER_ADS, Level::ENFORCE}}},
  156. {"warn", nullptr, {{Type::ABUSIVE, Level::WARN}}},
  157. {nullptr, nullptr, {}},
  158. {"",
  159. "",
  160. {{Type::ABUSIVE, Level::ENFORCE}, {Type::BETTER_ADS, Level::ENFORCE}}},
  161. };
  162. for (const auto& test_case : test_cases) {
  163. std::string json = R"({
  164. "matches" : [{
  165. "threat_type":"13"
  166. %s
  167. %s
  168. }]
  169. })";
  170. auto put_kv = [](const char* k, const char* v) {
  171. if (!v)
  172. return std::string();
  173. return base::StringPrintf(",\"%s\":\"%s\"", k, v);
  174. };
  175. json = base::StringPrintf(json.c_str(),
  176. put_kv("sf_absv", test_case.abusive_type).c_str(),
  177. put_kv("sf_bas", test_case.bas_type).c_str());
  178. SCOPED_TRACE(testing::Message() << json);
  179. ASSERT_EQ(UMA_STATUS_MATCH, ResetAndParseJson(json));
  180. EXPECT_EQ(SB_THREAT_TYPE_SUBRESOURCE_FILTER, threat_);
  181. ThreatMetadata expected;
  182. expected.subresource_filter_match = test_case.expected_match;
  183. EXPECT_EQ(expected, meta_);
  184. }
  185. }
  186. TEST_F(SafeBrowsingApiHandlerUtilTest, NoUnwantedSoftwareSubTypes) {
  187. ThreatMetadata expected;
  188. EXPECT_EQ(UMA_STATUS_MATCH,
  189. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"3\"}]}"));
  190. EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED, threat_);
  191. expected.threat_pattern_type = ThreatPatternType::NONE;
  192. EXPECT_EQ(expected, meta_);
  193. EXPECT_EQ(UMA_STATUS_MATCH,
  194. ResetAndParseJson("{\"matches\":[{\"threat_type\":\"3\", "
  195. "\"se_pattern_type\":\"junk\"}]}"));
  196. EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED, threat_);
  197. expected.threat_pattern_type = ThreatPatternType::NONE;
  198. EXPECT_EQ(expected, meta_);
  199. }
  200. } // namespace safe_browsing