remote_database_manager_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/remote_database_manager.h"
  5. #include <map>
  6. #include <memory>
  7. #include "base/metrics/field_trial.h"
  8. #include "base/run_loop.h"
  9. #include "base/time/time.h"
  10. #include "components/safe_browsing/android/safe_browsing_api_handler_bridge.h"
  11. #include "components/variations/variations_associated_data.h"
  12. #include "content/public/test/browser_task_environment.h"
  13. #include "services/network/public/mojom/fetch_api.mojom.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. namespace safe_browsing {
  16. namespace {
  17. class BlackHoleInterceptor : public safe_browsing::UrlCheckInterceptor {
  18. public:
  19. void Check(
  20. std::unique_ptr<SafeBrowsingApiHandlerBridge::ResponseCallback> callback,
  21. const GURL& url) const override{};
  22. ~BlackHoleInterceptor() override{};
  23. };
  24. } // namespace
  25. class RemoteDatabaseManagerTest : public testing::Test {
  26. protected:
  27. RemoteDatabaseManagerTest() {}
  28. void SetUp() override {
  29. SafeBrowsingApiHandlerBridge::GetInstance().SetInterceptorForTesting(
  30. &url_interceptor_);
  31. db_ = new RemoteSafeBrowsingDatabaseManager();
  32. }
  33. void TearDown() override {
  34. db_ = nullptr;
  35. base::RunLoop().RunUntilIdle();
  36. }
  37. // Setup the two field trial params. These are read in db_'s ctor.
  38. void SetFieldTrialParams(const std::string types_to_check_val) {
  39. variations::testing::ClearAllVariationIDs();
  40. variations::testing::ClearAllVariationParams();
  41. const std::string group_name = "GroupFoo"; // Value not used
  42. const std::string experiment_name = "SafeBrowsingAndroid";
  43. ASSERT_TRUE(
  44. base::FieldTrialList::CreateFieldTrial(experiment_name, group_name));
  45. std::map<std::string, std::string> params;
  46. if (!types_to_check_val.empty())
  47. params["types_to_check"] = types_to_check_val;
  48. ASSERT_TRUE(variations::AssociateVariationParams(experiment_name,
  49. group_name, params));
  50. }
  51. content::BrowserTaskEnvironment task_environment_;
  52. BlackHoleInterceptor url_interceptor_;
  53. scoped_refptr<RemoteSafeBrowsingDatabaseManager> db_;
  54. };
  55. TEST_F(RemoteDatabaseManagerTest, DestinationsToCheckDefault) {
  56. // Most are true, a few are false.
  57. for (int t_int = 0;
  58. t_int <= static_cast<int>(network::mojom::RequestDestination::kMaxValue);
  59. t_int++) {
  60. network::mojom::RequestDestination t =
  61. static_cast<network::mojom::RequestDestination>(t_int);
  62. switch (t) {
  63. case network::mojom::RequestDestination::kStyle:
  64. case network::mojom::RequestDestination::kImage:
  65. case network::mojom::RequestDestination::kFont:
  66. EXPECT_FALSE(db_->CanCheckRequestDestination(t));
  67. break;
  68. default:
  69. EXPECT_TRUE(db_->CanCheckRequestDestination(t));
  70. break;
  71. }
  72. }
  73. }
  74. TEST_F(RemoteDatabaseManagerTest, DestinationsToCheckFromTrial) {
  75. SetFieldTrialParams("7,16,blah, 20");
  76. db_ = new RemoteSafeBrowsingDatabaseManager();
  77. EXPECT_TRUE(db_->CanCheckRequestDestination(
  78. network::mojom::RequestDestination::kDocument)); // defaulted
  79. EXPECT_TRUE(db_->CanCheckRequestDestination(
  80. network::mojom::RequestDestination::kIframe));
  81. EXPECT_TRUE(db_->CanCheckRequestDestination(
  82. network::mojom::RequestDestination::kFrame));
  83. EXPECT_TRUE(db_->CanCheckRequestDestination(
  84. network::mojom::RequestDestination::kFencedframe));
  85. EXPECT_TRUE(db_->CanCheckRequestDestination(
  86. network::mojom::RequestDestination::kStyle));
  87. EXPECT_FALSE(db_->CanCheckRequestDestination(
  88. network::mojom::RequestDestination::kScript));
  89. EXPECT_FALSE(db_->CanCheckRequestDestination(
  90. network::mojom::RequestDestination::kImage));
  91. // ...
  92. EXPECT_FALSE(db_->CanCheckRequestDestination(
  93. network::mojom::RequestDestination::kVideo));
  94. EXPECT_TRUE(db_->CanCheckRequestDestination(
  95. network::mojom::RequestDestination::kWorker));
  96. }
  97. } // namespace safe_browsing