Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/permission_reporter.h" | 5 #include "chrome/browser/safe_browsing/permission_reporter.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | |
| 7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/field_trial.h" | |
| 8 #include "chrome/common/safe_browsing/permission_report.pb.h" | 10 #include "chrome/common/safe_browsing/permission_report.pb.h" |
| 11 #include "components/variations/active_field_trials.h" | |
| 9 #include "content/public/browser/permission_type.h" | 12 #include "content/public/browser/permission_type.h" |
| 10 #include "net/url_request/report_sender.h" | 13 #include "net/url_request/report_sender.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 15 |
| 13 using content::PermissionType; | 16 using content::PermissionType; |
| 14 | 17 |
| 15 namespace safe_browsing { | 18 namespace safe_browsing { |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 21 | |
| 22 typedef std::set<variations::ActiveGroupId, variations::ActiveGroupIdCompare> | |
| 23 ActiveGroupIdSet; | |
|
Alexei Svitkine (slow)
2016/06/28 15:15:27
Nit: Seems strange to typedef this if you only use
stefanocs
2016/06/28 23:56:15
Done.
| |
| 24 | |
| 18 // URL to upload permission action reports. | 25 // URL to upload permission action reports. |
| 19 const char kPermissionActionReportingUploadUrl[] = | 26 const char kPermissionActionReportingUploadUrl[] = |
| 20 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 27 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| 21 "permission-action"; | 28 "permission-action"; |
| 22 | 29 |
| 23 const char kDummyOrigin[] = "http://example.test/"; | 30 const char kDummyOrigin[] = "http://example.test/"; |
| 24 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; | 31 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; |
| 25 const PermissionAction kDummyAction = GRANTED; | 32 const PermissionAction kDummyAction = GRANTED; |
| 26 const PermissionReport::PermissionType kDummyPermissionReportPermission = | 33 const PermissionReport::PermissionType kDummyPermissionReportPermission = |
| 27 PermissionReport::GEOLOCATION; | 34 PermissionReport::GEOLOCATION; |
| 28 const PermissionReport::Action kDummyPermissionReportAction = | 35 const PermissionReport::Action kDummyPermissionReportAction = |
| 29 PermissionReport::GRANTED; | 36 PermissionReport::GRANTED; |
| 30 | 37 |
| 38 const char kDummyTrialOne[] = "trial one"; | |
| 39 const char kDummyGroupOne[] = "group one"; | |
| 40 const char kDummyTrialTwo[] = "trial two"; | |
| 41 const char kDummyGroupTwo[] = "group two"; | |
| 42 | |
| 43 const char kFeatureOnByDefaultName[] = "OnByDefault"; | |
| 44 struct base::Feature kFeatureOnByDefault { | |
| 45 kFeatureOnByDefaultName, base::FEATURE_ENABLED_BY_DEFAULT | |
| 46 }; | |
| 47 | |
| 48 const char kFeatureOffByDefaultName[] = "OffByDefault"; | |
| 49 struct base::Feature kFeatureOffByDefault { | |
| 50 kFeatureOffByDefaultName, base::FEATURE_DISABLED_BY_DEFAULT | |
| 51 }; | |
| 52 | |
| 31 // A mock ReportSender that keeps track of the last report sent. | 53 // A mock ReportSender that keeps track of the last report sent. |
| 32 class MockReportSender : public net::ReportSender { | 54 class MockReportSender : public net::ReportSender { |
| 33 public: | 55 public: |
| 34 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | 56 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
| 35 ~MockReportSender() override {} | 57 ~MockReportSender() override {} |
| 36 | 58 |
| 37 void Send(const GURL& report_uri, const std::string& report) override { | 59 void Send(const GURL& report_uri, const std::string& report) override { |
| 38 latest_report_uri_ = report_uri; | 60 latest_report_uri_ = report_uri; |
| 39 latest_report_ = report; | 61 latest_report_ = report; |
| 40 } | 62 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 70 TEST_F(PermissionReporterTest, SendReport) { | 92 TEST_F(PermissionReporterTest, SendReport) { |
| 71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 93 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
| 72 kDummyAction); | 94 kDummyAction); |
| 73 | 95 |
| 74 PermissionReport permission_report; | 96 PermissionReport permission_report; |
| 75 ASSERT_TRUE( | 97 ASSERT_TRUE( |
| 76 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 98 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
| 77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); | 99 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
| 78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); | 100 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | 101 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| 102 #if defined(OS_ANDROID) | |
| 103 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, | |
| 104 permission_report.platform_type()); | |
| 105 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ | |
| 106 defined(OS_LINUX) | |
| 107 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, | |
| 108 permission_report.platform_type()); | |
| 109 #endif | |
| 80 | 110 |
| 81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), | 111 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| 82 mock_report_sender_->latest_report_uri()); | 112 mock_report_sender_->latest_report_uri()); |
| 83 } | 113 } |
| 84 | 114 |
| 115 // Test that PermissionReporter::SendReport sends a serialized report string | |
| 116 // with field trials to SafeBrowsing CSD servers. | |
| 117 TEST_F(PermissionReporterTest, SendReportWithFieldTrials) { | |
| 118 // Add and activate dummy field trials. | |
| 119 base::FieldTrialList field_trial_list(nullptr); | |
| 120 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 121 base::FieldTrial* trial_one = | |
| 122 base::FieldTrialList::CreateFieldTrial(kDummyTrialOne, kDummyGroupOne); | |
| 123 base::FieldTrial* trial_two = | |
| 124 base::FieldTrialList::CreateFieldTrial(kDummyTrialTwo, kDummyGroupTwo); | |
| 125 | |
| 126 feature_list->RegisterFieldTrialOverride( | |
| 127 kFeatureOnByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 128 trial_one); | |
| 129 feature_list->RegisterFieldTrialOverride( | |
| 130 kFeatureOffByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 131 trial_two); | |
| 132 | |
| 133 base::FeatureList::ClearInstanceForTesting(); | |
| 134 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 135 | |
| 136 // This is necessary to activate both field trials. | |
| 137 base::FeatureList::IsEnabled(kFeatureOnByDefault); | |
|
Nathan Parker
2016/06/27 18:17:20
If a field trial doesn't get activated until some
stefanocs
2016/06/28 04:51:45
Acknowledged.
| |
| 138 base::FeatureList::IsEnabled(kFeatureOffByDefault); | |
| 139 | |
| 140 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_one->trial_name())); | |
| 141 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_two->trial_name())); | |
| 142 | |
| 143 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | |
| 144 kDummyAction); | |
| 145 | |
| 146 PermissionReport permission_report; | |
| 147 ASSERT_TRUE( | |
| 148 permission_report.ParseFromString(mock_report_sender_->latest_report())); | |
| 149 | |
| 150 variations::ActiveGroupId field_trial_one = | |
| 151 variations::MakeActiveGroupId(kDummyTrialOne, kDummyGroupOne); | |
| 152 variations::ActiveGroupId field_trial_two = | |
| 153 variations::MakeActiveGroupId(kDummyTrialTwo, kDummyGroupTwo); | |
| 154 ActiveGroupIdSet expected_group_ids = {field_trial_one, field_trial_two}; | |
| 155 | |
| 156 EXPECT_EQ(2, permission_report.field_trials().size()); | |
| 157 for (auto field_trial : permission_report.field_trials()) { | |
| 158 variations::ActiveGroupId group_id = {field_trial.name_id(), | |
| 159 field_trial.group_id()}; | |
| 160 EXPECT_EQ(1U, expected_group_ids.erase(group_id)); | |
| 161 } | |
| 162 EXPECT_EQ(0U, expected_group_ids.size()); | |
| 163 } | |
| 164 | |
| 85 } // namespace safe_browsing | 165 } // namespace safe_browsing |
| OLD | NEW |