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; | |
| 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 20 matching lines...) Expand all Loading... | |
| 61 | 83 |
| 62 // Owned by |permission_reporter_|. | 84 // Owned by |permission_reporter_|. |
| 63 MockReportSender* mock_report_sender_; | 85 MockReportSender* mock_report_sender_; |
| 64 | 86 |
| 65 std::unique_ptr<PermissionReporter> permission_reporter_; | 87 std::unique_ptr<PermissionReporter> permission_reporter_; |
| 66 }; | 88 }; |
| 67 | 89 |
| 68 // Test that PermissionReporter::SendReport sends a serialized report string to | 90 // Test that PermissionReporter::SendReport sends a serialized report string to |
| 69 // SafeBrowsing CSD servers. | 91 // SafeBrowsing CSD servers. |
| 70 TEST_F(PermissionReporterTest, SendReport) { | 92 TEST_F(PermissionReporterTest, SendReport) { |
| 93 // Send permission report. | |
|
raymes
2016/06/22 02:16:27
nit: remove
stefanocs
2016/06/22 03:40:20
Done.
| |
| 71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 94 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
| 72 kDummyAction); | 95 kDummyAction); |
| 73 | 96 |
| 74 PermissionReport permission_report; | 97 PermissionReport permission_report; |
| 75 ASSERT_TRUE( | 98 ASSERT_TRUE( |
| 76 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 99 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
| 77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); | 100 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
| 78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); | 101 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | 102 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| 103 #if defined(OS_ANDROID) | |
| 104 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, | |
| 105 permission_report.platform_type()); | |
| 106 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ | |
| 107 defined(OS_LINUX) | |
| 108 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, | |
| 109 permission_report.platform_type()); | |
| 110 #endif | |
| 80 | 111 |
| 81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), | 112 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| 82 mock_report_sender_->latest_report_uri()); | 113 mock_report_sender_->latest_report_uri()); |
| 83 } | 114 } |
| 84 | 115 |
| 116 // Test that PermissionReporter::SendReport sends a serialized report string | |
| 117 // with field trials to SafeBrowsing CSD servers. | |
| 118 TEST_F(PermissionReporterTest, SendReportWithFieldTrials) { | |
| 119 // Add and activate dummy field trials. | |
| 120 base::FieldTrialList field_trial_list(nullptr); | |
| 121 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 122 base::FieldTrial* trial_one = | |
| 123 base::FieldTrialList::CreateFieldTrial(kDummyTrialOne, kDummyGroupOne); | |
| 124 base::FieldTrial* trial_two = | |
| 125 base::FieldTrialList::CreateFieldTrial(kDummyTrialTwo, kDummyGroupTwo); | |
| 126 | |
| 127 feature_list->RegisterFieldTrialOverride( | |
| 128 kFeatureOnByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 129 trial_one); | |
| 130 feature_list->RegisterFieldTrialOverride( | |
| 131 kFeatureOffByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 132 trial_two); | |
| 133 | |
| 134 base::FeatureList::ClearInstanceForTesting(); | |
| 135 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 136 | |
| 137 // Enable features, this will also activate both field trials. | |
| 138 base::FeatureList::IsEnabled(kFeatureOnByDefault); | |
| 139 base::FeatureList::IsEnabled(kFeatureOffByDefault); | |
| 140 | |
| 141 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_one->trial_name())); | |
| 142 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_two->trial_name())); | |
| 143 | |
| 144 // Send permission report. | |
|
raymes
2016/06/22 02:16:27
nit: remove
stefanocs
2016/06/22 03:40:20
Done.
| |
| 145 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | |
| 146 kDummyAction); | |
| 147 | |
| 148 PermissionReport permission_report; | |
| 149 ASSERT_TRUE( | |
| 150 permission_report.ParseFromString(mock_report_sender_->latest_report())); | |
| 151 | |
| 152 variations::ActiveGroupId field_trial_one = | |
| 153 variations::MakeActiveGroupId(kDummyTrialOne, kDummyGroupOne); | |
| 154 variations::ActiveGroupId field_trial_two = | |
| 155 variations::MakeActiveGroupId(kDummyTrialTwo, kDummyGroupTwo); | |
| 156 ActiveGroupIdSet expected_group_ids = {field_trial_one, field_trial_two}; | |
| 157 | |
| 158 EXPECT_EQ(2, permission_report.field_trials().size()); | |
| 159 for (auto field_trial : permission_report.field_trials()) { | |
| 160 variations::ActiveGroupId group_id = {field_trial.name_id(), | |
| 161 field_trial.group_id()}; | |
| 162 EXPECT_FALSE(expected_group_ids.find(group_id) == expected_group_ids.end()); | |
|
raymes
2016/06/22 02:16:27
I don't think this is needed - you can check the r
stefanocs
2016/06/22 03:40:20
Done.
| |
| 163 expected_group_ids.erase(group_id); | |
| 164 } | |
| 165 EXPECT_EQ(0U, expected_group_ids.size()); | |
| 166 } | |
| 167 | |
| 85 } // namespace safe_browsing | 168 } // namespace safe_browsing |
| OLD | NEW |