| 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 safe_browsing { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class TestSafeBrowsingApiHandler : public SafeBrowsingApiHandler { | |
| 20 public: | |
| 21 void StartURLCheck(const URLCheckCallback& callback, | |
| 22 const GURL& url, | |
| 23 const std::vector<SBThreatType>& threat_types) override {} | |
| 24 }; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 class RemoteDatabaseManagerTest : public testing::Test { | |
| 29 protected: | |
| 30 RemoteDatabaseManagerTest() : field_trials_(new base::FieldTrialList(NULL)) {} | |
| 31 | |
| 32 void SetUp() override { | |
| 33 SafeBrowsingApiHandler::SetInstance(&api_handler_); | |
| 34 db_ = new RemoteSafeBrowsingDatabaseManager(); | |
| 35 } | |
| 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 // Destroy the existing FieldTrialList before creating a new one to avoid | |
| 40 // a DCHECK. | |
| 41 field_trials_.reset(); | |
| 42 field_trials_.reset(new base::FieldTrialList(NULL)); | |
| 43 variations::testing::ClearAllVariationIDs(); | |
| 44 variations::testing::ClearAllVariationParams(); | |
| 45 | |
| 46 const std::string group_name = "GroupFoo"; // Value not used | |
| 47 const std::string experiment_name = "SafeBrowsingAndroid"; | |
| 48 ASSERT_TRUE( | |
| 49 base::FieldTrialList::CreateFieldTrial(experiment_name, group_name)); | |
| 50 | |
| 51 std::map<std::string, std::string> params; | |
| 52 if (!types_to_check_val.empty()) | |
| 53 params["types_to_check"] = types_to_check_val; | |
| 54 | |
| 55 ASSERT_TRUE(variations::AssociateVariationParams(experiment_name, | |
| 56 group_name, params)); | |
| 57 } | |
| 58 | |
| 59 scoped_ptr<base::FieldTrialList> field_trials_; | |
| 60 TestSafeBrowsingApiHandler api_handler_; | |
| 61 scoped_refptr<RemoteSafeBrowsingDatabaseManager> db_; | |
| 62 }; | |
| 63 | |
| 64 TEST_F(RemoteDatabaseManagerTest, DisabledViaNull) { | |
| 65 EXPECT_TRUE(db_->IsSupported()); | |
| 66 | |
| 67 SafeBrowsingApiHandler::SetInstance(nullptr); | |
| 68 EXPECT_FALSE(db_->IsSupported()); | |
| 69 } | |
| 70 | |
| 71 TEST_F(RemoteDatabaseManagerTest, TypesToCheckDefault) { | |
| 72 // Most are true, a few are false. | |
| 73 for (int t_int = 0; t_int < content::RESOURCE_TYPE_LAST_TYPE; t_int++) { | |
| 74 content::ResourceType t = static_cast<content::ResourceType>(t_int); | |
| 75 switch (t) { | |
| 76 case content::RESOURCE_TYPE_STYLESHEET: | |
| 77 case content::RESOURCE_TYPE_IMAGE: | |
| 78 case content::RESOURCE_TYPE_FONT_RESOURCE: | |
| 79 case content::RESOURCE_TYPE_FAVICON: | |
| 80 EXPECT_FALSE(db_->CanCheckResourceType(t)); | |
| 81 break; | |
| 82 default: | |
| 83 EXPECT_TRUE(db_->CanCheckResourceType(t)); | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 TEST_F(RemoteDatabaseManagerTest, TypesToCheckFromTrial) { | |
| 90 SetFieldTrialParams("1,2,blah, 9"); | |
| 91 db_ = new RemoteSafeBrowsingDatabaseManager(); | |
| 92 EXPECT_TRUE(db_->CanCheckResourceType( | |
| 93 content::RESOURCE_TYPE_MAIN_FRAME)); // defaulted | |
| 94 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_SUB_FRAME)); | |
| 95 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_STYLESHEET)); | |
| 96 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_SCRIPT)); | |
| 97 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_IMAGE)); | |
| 98 // ... | |
| 99 EXPECT_FALSE(db_->CanCheckResourceType(content::RESOURCE_TYPE_MEDIA)); | |
| 100 EXPECT_TRUE(db_->CanCheckResourceType(content::RESOURCE_TYPE_WORKER)); | |
| 101 } | |
| 102 | |
| 103 } // namespace safe_browsing | |
| OLD | NEW |