| OLD | NEW |
| (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 "base/logging.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/safe_browsing/remote_database_manager.h" |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_api_handler.h" |
| 12 #include "components/variations/variations_associated_data.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 class TestSafeBrowsingApiHandler : public SafeBrowsingApiHandler { |
| 18 public: |
| 19 void StartURLCheck(const URLCheckCallback& callback, |
| 20 const GURL& url, |
| 21 const std::vector<SBThreatType>& threat_types) override {} |
| 22 }; |
| 23 |
| 24 class RemoteDatabaseManagerTest : public testing::Test { |
| 25 protected: |
| 26 RemoteDatabaseManagerTest() : field_trials_(new base::FieldTrialList(NULL)) {} |
| 27 |
| 28 void SetUp() override { |
| 29 SafeBrowsingApiHandler::SetInstance(&api_handler_); |
| 30 db_ = new RemoteSafeBrowsingDatabaseManager(); |
| 31 } |
| 32 |
| 33 // Setup the two field trial params. These are read in db_'s ctor. |
| 34 void SetFieldTrialParams(const std::string enabled_val, |
| 35 const std::string types_to_check_val) { |
| 36 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 37 // a DCHECK. |
| 38 field_trials_.reset(); |
| 39 field_trials_.reset(new base::FieldTrialList(NULL)); |
| 40 variations::testing::ClearAllVariationIDs(); |
| 41 variations::testing::ClearAllVariationParams(); |
| 42 |
| 43 const std::string group_name = "GroupFoo"; // Value not used |
| 44 const std::string experiment_name = "SafeBrowsingAndroid"; |
| 45 ASSERT_TRUE( |
| 46 base::FieldTrialList::CreateFieldTrial(experiment_name, group_name)); |
| 47 |
| 48 std::map<std::string, std::string> params; |
| 49 if (!enabled_val.empty()) |
| 50 params["enabled"] = enabled_val; |
| 51 if (!types_to_check_val.empty()) |
| 52 params["types_to_check"] = types_to_check_val; |
| 53 |
| 54 ASSERT_TRUE(variations::AssociateVariationParams(experiment_name, |
| 55 group_name, params)); |
| 56 } |
| 57 |
| 58 scoped_ptr<base::FieldTrialList> field_trials_; |
| 59 TestSafeBrowsingApiHandler api_handler_; |
| 60 scoped_refptr<RemoteSafeBrowsingDatabaseManager> db_; |
| 61 }; |
| 62 |
| 63 TEST_F(RemoteDatabaseManagerTest, DisabledViaNullOrTrial) { |
| 64 EXPECT_FALSE(db_->IsSupported()); |
| 65 |
| 66 SetFieldTrialParams("true", ""); |
| 67 db_ = new RemoteSafeBrowsingDatabaseManager(); |
| 68 EXPECT_TRUE(db_->IsSupported()); |
| 69 |
| 70 SafeBrowsingApiHandler::SetInstance(nullptr); |
| 71 EXPECT_FALSE(db_->IsSupported()); |
| 72 } |
| 73 |
| 74 TEST_F(RemoteDatabaseManagerTest, TypesToCheckDefault) { |
| 75 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_MAIN_FRAME)); |
| 76 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_SUB_FRAME)); |
| 77 // The rest are false. |
| 78 for (int t = content::RESOURCE_TYPE_SUB_FRAME + 1; |
| 79 t < content::RESOURCE_TYPE_LAST_TYPE; t++) { |
| 80 EXPECT_FALSE( |
| 81 db_->CanCheckResourceType(static_cast<content::ResourceType>(t))); |
| 82 } |
| 83 } |
| 84 TEST_F(RemoteDatabaseManagerTest, TypesToCheckFromTrial) { |
| 85 SetFieldTrialParams("true", "1,2,blah, 9"); |
| 86 db_ = new RemoteSafeBrowsingDatabaseManager(); |
| 87 EXPECT_TRUE(db_->CanCheckResourceType( |
| 88 content::RESOURCE_TYPE_MAIN_FRAME)); // defaulted |
| 89 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_SUB_FRAME)); |
| 90 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_STYLESHEET)); |
| 91 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_SCRIPT)); |
| 92 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_IMAGE)); |
| 93 // ... |
| 94 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_MEDIA)); |
| 95 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_WORKER)); |
| 96 } |
| 97 |
| 98 } // namespace |
| OLD | NEW |