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 // Add and activate dummy field trials. | |
| 94 base::FieldTrialList field_trial_list(nullptr); | |
|
raymes
2016/06/22 01:23:44
nit: is this needed?
stefanocs
2016/06/22 02:07:06
Yes, the constructor will initialize a singleton i
| |
| 95 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 96 base::FieldTrial* trial_one = | |
| 97 base::FieldTrialList::CreateFieldTrial(kDummyTrialOne, kDummyGroupOne); | |
| 98 base::FieldTrial* trial_two = | |
| 99 base::FieldTrialList::CreateFieldTrial(kDummyTrialTwo, kDummyGroupTwo); | |
| 100 | |
| 101 feature_list->RegisterFieldTrialOverride( | |
| 102 kFeatureOnByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 103 trial_one); | |
|
raymes
2016/06/22 01:23:44
I guess this isn't needed for the on-by-default fe
stefanocs
2016/06/22 02:07:06
The test doesn't seem to work with this removed. W
raymes
2016/06/22 02:16:27
Oh I assumed the default would apply in that case.
| |
| 104 feature_list->RegisterFieldTrialOverride( | |
| 105 kFeatureOffByDefaultName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 106 trial_two); | |
| 107 | |
| 108 base::FeatureList::ClearInstanceForTesting(); | |
| 109 base::FeatureList::SetInstance(std::move(feature_list)); | |
| 110 | |
| 111 // Enable features, this will also activate both field trials. | |
| 112 base::FeatureList::IsEnabled(kFeatureOnByDefault); | |
| 113 base::FeatureList::IsEnabled(kFeatureOffByDefault); | |
|
raymes
2016/06/22 01:23:44
These just check whether the feature is enabled, r
stefanocs
2016/06/22 02:07:06
While this seems to be a non-modifying function, c
raymes
2016/06/22 02:16:27
I see. Perhaps make a note in the comment
| |
| 114 | |
| 115 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_one->trial_name())); | |
| 116 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_two->trial_name())); | |
|
raymes
2016/06/22 01:23:44
Since there is a lot of setup/checking to test the
stefanocs
2016/06/22 02:07:05
Done.
| |
| 117 | |
| 71 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 118 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
| 72 kDummyAction); | 119 kDummyAction); |
| 73 | 120 |
| 74 PermissionReport permission_report; | 121 PermissionReport permission_report; |
| 75 ASSERT_TRUE( | 122 ASSERT_TRUE( |
| 76 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 123 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
| 77 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); | 124 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
| 78 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); | 125 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 79 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | 126 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
| 127 #if defined(OS_ANDROID) | |
| 128 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, | |
| 129 permission_report.platform_type()); | |
| 130 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ | |
| 131 defined(OS_LINUX) | |
| 132 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, | |
| 133 permission_report.platform_type()); | |
| 134 #endif | |
| 135 | |
| 136 variations::ActiveGroupId field_trial_one = | |
| 137 variations::MakeActiveGroupId(kDummyTrialOne, kDummyGroupOne); | |
| 138 variations::ActiveGroupId field_trial_two = | |
| 139 variations::MakeActiveGroupId(kDummyTrialTwo, kDummyGroupTwo); | |
| 140 ActiveGroupIdSet expected_group_ids = {field_trial_one, field_trial_two}; | |
| 141 | |
| 142 EXPECT_EQ(2, permission_report.field_trials().size()); | |
| 143 for (auto field_trial : permission_report.field_trials()) { | |
| 144 variations::ActiveGroupId group_id = {field_trial.name_id(), | |
| 145 field_trial.group_id()}; | |
| 146 ActiveGroupIdSet::iterator expected_group = | |
| 147 expected_group_ids.find(group_id); | |
| 148 EXPECT_FALSE(expected_group == expected_group_ids.end()); | |
| 149 expected_group_ids.erase(expected_group); | |
|
raymes
2016/06/22 01:23:44
Could you just call erase on group_id?
stefanocs
2016/06/22 02:07:06
Done.
| |
| 150 } | |
| 151 EXPECT_EQ(0U, expected_group_ids.size()); | |
| 80 | 152 |
| 81 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), | 153 EXPECT_EQ(GURL(kPermissionActionReportingUploadUrl), |
| 82 mock_report_sender_->latest_report_uri()); | 154 mock_report_sender_->latest_report_uri()); |
| 83 } | 155 } |
| 84 | 156 |
| 85 } // namespace safe_browsing | 157 } // namespace safe_browsing |
| OLD | NEW |