| 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 |
| 18 // URL to upload permission action reports. | 22 // URL to upload permission action reports. |
| 19 const char kPermissionActionReportingUploadUrl[] = | 23 const char kPermissionActionReportingUploadUrl[] = |
| 20 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 24 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
| 21 "permission-action"; | 25 "permission-action"; |
| 22 | 26 |
| 23 const char kDummyOrigin[] = "http://example.test/"; | 27 const char kDummyOrigin[] = "http://example.test/"; |
| 24 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; | 28 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; |
| 25 const PermissionAction kDummyAction = GRANTED; | 29 const PermissionAction kDummyAction = GRANTED; |
| 26 const PermissionReport::PermissionType kDummyPermissionReportPermission = | 30 const PermissionReport::PermissionType kDummyPermissionReportPermission = |
| 27 PermissionReport::GEOLOCATION; | 31 PermissionReport::GEOLOCATION; |
| 28 const PermissionReport::Action kDummyPermissionReportAction = | 32 const PermissionReport::Action kDummyPermissionReportAction = |
| 29 PermissionReport::GRANTED; | 33 PermissionReport::GRANTED; |
| 30 | 34 |
| 35 const char kDummyTrialOne[] = "trial one"; |
| 36 const char kDummyGroupOne[] = "group one"; |
| 37 const char kDummyTrialTwo[] = "trial two"; |
| 38 const char kDummyGroupTwo[] = "group two"; |
| 39 |
| 40 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
| 41 struct base::Feature kFeatureOnByDefault { |
| 42 kFeatureOnByDefaultName, base::FEATURE_ENABLED_BY_DEFAULT |
| 43 }; |
| 44 |
| 45 const char kFeatureOffByDefaultName[] = "OffByDefault"; |
| 46 struct base::Feature kFeatureOffByDefault { |
| 47 kFeatureOffByDefaultName, base::FEATURE_DISABLED_BY_DEFAULT |
| 48 }; |
| 49 |
| 31 // A mock ReportSender that keeps track of the last report sent. | 50 // A mock ReportSender that keeps track of the last report sent. |
| 32 class MockReportSender : public net::ReportSender { | 51 class MockReportSender : public net::ReportSender { |
| 33 public: | 52 public: |
| 34 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} | 53 MockReportSender() : net::ReportSender(nullptr, DO_NOT_SEND_COOKIES) {} |
| 35 ~MockReportSender() override {} | 54 ~MockReportSender() override {} |
| 36 | 55 |
| 37 void Send(const GURL& report_uri, const std::string& report) override { | 56 void Send(const GURL& report_uri, const std::string& report) override { |
| 38 latest_report_uri_ = report_uri; | 57 latest_report_uri_ = report_uri; |
| 39 latest_report_ = report; | 58 latest_report_ = report; |
| 40 } | 59 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 70 TEST_F(PermissionReporterTest, SendReport) { | 89 TEST_F(PermissionReporterTest, SendReport) { |
| 71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 90 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
| 72 kDummyAction); | 91 kDummyAction); |
| 73 | 92 |
| 74 PermissionReport permission_report; | 93 PermissionReport permission_report; |
| 75 ASSERT_TRUE( | 94 ASSERT_TRUE( |
| 76 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 95 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
| 77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); | 96 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
| 78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); | 97 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | 98 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| 99 #if defined(OS_ANDROID) |
| 100 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, |
| 101 permission_report.platform_type()); |
| 102 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ |
| 103 defined(OS_LINUX) |
| 104 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, |
| 105 permission_report.platform_type()); |
| 106 #endif |
| 80 | 107 |
| 81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), | 108 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| 82 mock_report_sender_->latest_report_uri()); | 109 mock_report_sender_->latest_report_uri()); |
| 83 } | 110 } |
| 84 | 111 |
| 112 // Test that PermissionReporter::SendReport sends a serialized report string |
| 113 // with field trials to SafeBrowsing CSD servers. |
| 114 TEST_F(PermissionReporterTest, SendReportWithFieldTrials) { |
| 115 typedef std::set<variations::ActiveGroupId, variations::ActiveGroupIdCompare> |
| 116 ActiveGroupIdSet; |
| 117 |
| 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); |
| 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 |