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/feature_list.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #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" | 11 #include "components/variations/active_field_trials.h" |
12 #include "content/public/browser/permission_type.h" | 12 #include "content/public/browser/permission_type.h" |
13 #include "net/url_request/report_sender.h" | 13 #include "net/url_request/report_sender.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 using content::PermissionType; | 16 using content::PermissionType; |
17 | 17 |
18 namespace safe_browsing { | 18 namespace safe_browsing { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // URL to upload permission action reports. | 22 // URL to upload permission action reports. |
23 const char kPermissionActionReportingUploadUrl[] = | 23 const char kPermissionActionReportingUploadUrl[] = |
24 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" | 24 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" |
25 "permission-action"; | 25 "permission-action"; |
26 | 26 |
27 const char kDummyOrigin[] = "http://example.test/"; | 27 const char kDummyOrigin[] = "http://example.test/"; |
28 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; | 28 const PermissionType kDummyPermission = PermissionType::GEOLOCATION; |
29 const PermissionAction kDummyAction = GRANTED; | 29 const PermissionAction kDummyAction = GRANTED; |
| 30 const SourceUI kDummySourceUI = PROMPT; |
30 const PermissionReport::PermissionType kDummyPermissionReportPermission = | 31 const PermissionReport::PermissionType kDummyPermissionReportPermission = |
31 PermissionReport::GEOLOCATION; | 32 PermissionReport::GEOLOCATION; |
32 const PermissionReport::Action kDummyPermissionReportAction = | 33 const PermissionReport::Action kDummyPermissionReportAction = |
33 PermissionReport::GRANTED; | 34 PermissionReport::GRANTED; |
| 35 const PermissionReport::SourceUI kDummyPermissionReportSourceUI = |
| 36 PermissionReport::PROMPT; |
34 | 37 |
35 const char kDummyTrialOne[] = "trial one"; | 38 const char kDummyTrialOne[] = "trial one"; |
36 const char kDummyGroupOne[] = "group one"; | 39 const char kDummyGroupOne[] = "group one"; |
37 const char kDummyTrialTwo[] = "trial two"; | 40 const char kDummyTrialTwo[] = "trial two"; |
38 const char kDummyGroupTwo[] = "group two"; | 41 const char kDummyGroupTwo[] = "group two"; |
39 | 42 |
40 const char kFeatureOnByDefaultName[] = "OnByDefault"; | 43 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
41 struct base::Feature kFeatureOnByDefault { | 44 struct base::Feature kFeatureOnByDefault { |
42 kFeatureOnByDefaultName, base::FEATURE_ENABLED_BY_DEFAULT | 45 kFeatureOnByDefaultName, base::FEATURE_ENABLED_BY_DEFAULT |
43 }; | 46 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // Owned by |permission_reporter_|. | 84 // Owned by |permission_reporter_|. |
82 MockReportSender* mock_report_sender_; | 85 MockReportSender* mock_report_sender_; |
83 | 86 |
84 std::unique_ptr<PermissionReporter> permission_reporter_; | 87 std::unique_ptr<PermissionReporter> permission_reporter_; |
85 }; | 88 }; |
86 | 89 |
87 // Test that PermissionReporter::SendReport sends a serialized report string to | 90 // Test that PermissionReporter::SendReport sends a serialized report string to |
88 // SafeBrowsing CSD servers. | 91 // SafeBrowsing CSD servers. |
89 TEST_F(PermissionReporterTest, SendReport) { | 92 TEST_F(PermissionReporterTest, SendReport) { |
90 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 93 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
91 kDummyAction); | 94 kDummyAction, kDummySourceUI); |
92 | 95 |
93 PermissionReport permission_report; | 96 PermissionReport permission_report; |
94 ASSERT_TRUE( | 97 ASSERT_TRUE( |
95 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 98 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
96 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); | 99 EXPECT_EQ(kDummyPermissionReportPermission, permission_report.permission()); |
97 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); | 100 EXPECT_EQ(kDummyPermissionReportAction, permission_report.action()); |
| 101 EXPECT_EQ(kDummyPermissionReportSourceUI, permission_report.source_ui()); |
98 EXPECT_EQ(kDummyOrigin, permission_report.origin()); | 102 EXPECT_EQ(kDummyOrigin, permission_report.origin()); |
99 #if defined(OS_ANDROID) | 103 #if defined(OS_ANDROID) |
100 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, | 104 EXPECT_EQ(PermissionReport::ANDROID_PLATFORM, |
101 permission_report.platform_type()); | 105 permission_report.platform_type()); |
102 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ | 106 #elif defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_CHROMEOS) || \ |
103 defined(OS_LINUX) | 107 defined(OS_LINUX) |
104 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, | 108 EXPECT_EQ(PermissionReport::DESKTOP_PLATFORM, |
105 permission_report.platform_type()); | 109 permission_report.platform_type()); |
106 #endif | 110 #endif |
107 | 111 |
(...skipping 26 matching lines...) Expand all Loading... |
134 base::FeatureList::SetInstance(std::move(feature_list)); | 138 base::FeatureList::SetInstance(std::move(feature_list)); |
135 | 139 |
136 // This is necessary to activate both field trials. | 140 // This is necessary to activate both field trials. |
137 base::FeatureList::IsEnabled(kFeatureOnByDefault); | 141 base::FeatureList::IsEnabled(kFeatureOnByDefault); |
138 base::FeatureList::IsEnabled(kFeatureOffByDefault); | 142 base::FeatureList::IsEnabled(kFeatureOffByDefault); |
139 | 143 |
140 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_one->trial_name())); | 144 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_one->trial_name())); |
141 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_two->trial_name())); | 145 EXPECT_TRUE(base::FieldTrialList::IsTrialActive(trial_two->trial_name())); |
142 | 146 |
143 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, | 147 permission_reporter_->SendReport(GURL(kDummyOrigin), kDummyPermission, |
144 kDummyAction); | 148 kDummyAction, kDummySourceUI); |
145 | 149 |
146 PermissionReport permission_report; | 150 PermissionReport permission_report; |
147 ASSERT_TRUE( | 151 ASSERT_TRUE( |
148 permission_report.ParseFromString(mock_report_sender_->latest_report())); | 152 permission_report.ParseFromString(mock_report_sender_->latest_report())); |
149 | 153 |
150 variations::ActiveGroupId field_trial_one = | 154 variations::ActiveGroupId field_trial_one = |
151 variations::MakeActiveGroupId(kDummyTrialOne, kDummyGroupOne); | 155 variations::MakeActiveGroupId(kDummyTrialOne, kDummyGroupOne); |
152 variations::ActiveGroupId field_trial_two = | 156 variations::ActiveGroupId field_trial_two = |
153 variations::MakeActiveGroupId(kDummyTrialTwo, kDummyGroupTwo); | 157 variations::MakeActiveGroupId(kDummyTrialTwo, kDummyGroupTwo); |
154 ActiveGroupIdSet expected_group_ids = {field_trial_one, field_trial_two}; | 158 ActiveGroupIdSet expected_group_ids = {field_trial_one, field_trial_two}; |
155 | 159 |
156 EXPECT_EQ(2, permission_report.field_trials().size()); | 160 EXPECT_EQ(2, permission_report.field_trials().size()); |
157 for (auto field_trial : permission_report.field_trials()) { | 161 for (auto field_trial : permission_report.field_trials()) { |
158 variations::ActiveGroupId group_id = {field_trial.name_id(), | 162 variations::ActiveGroupId group_id = {field_trial.name_id(), |
159 field_trial.group_id()}; | 163 field_trial.group_id()}; |
160 EXPECT_EQ(1U, expected_group_ids.erase(group_id)); | 164 EXPECT_EQ(1U, expected_group_ids.erase(group_id)); |
161 } | 165 } |
162 EXPECT_EQ(0U, expected_group_ids.size()); | 166 EXPECT_EQ(0U, expected_group_ids.size()); |
163 } | 167 } |
164 | 168 |
165 } // namespace safe_browsing | 169 } // namespace safe_browsing |
OLD | NEW |