juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/reporting/reporting_service.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 10 | #include "base/bind.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 12 | #include "base/test/scoped_feature_list.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 13 | #include "base/time/tick_clock.h" |
| 14 | #include "base/values.h" |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 15 | #include "net/base/features.h" |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 16 | #include "net/base/network_isolation_key.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 17 | #include "net/base/schemeful_site.h" |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 18 | #include "net/reporting/mock_persistent_reporting_store.h" |
| 19 | #include "net/reporting/reporting_browsing_data_remover.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 20 | #include "net/reporting/reporting_cache.h" |
| 21 | #include "net/reporting/reporting_context.h" |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 22 | #include "net/reporting/reporting_endpoint.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 23 | #include "net/reporting/reporting_policy.h" |
| 24 | #include "net/reporting/reporting_report.h" |
| 25 | #include "net/reporting/reporting_service.h" |
| 26 | #include "net/reporting/reporting_test_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 27 | #include "net/test/test_with_task_environment.h" |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 28 | #include "testing/gmock/include/gmock/gmock.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 30 | #include "url/gurl.h" |
| 31 | #include "url/origin.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 32 | |
| 33 | namespace net { |
| 34 | namespace { |
| 35 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 36 | using CommandType = MockPersistentReportingStore::Command::Type; |
| 37 | |
| 38 | // The tests are parametrized on a boolean value which represents whether to use |
| 39 | // a MockPersistentReportingStore (if false, no store is used). |
| 40 | class ReportingServiceTest : public ::testing::TestWithParam<bool>, |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 41 | public WithTaskEnvironment { |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 42 | protected: |
| 43 | const GURL kUrl_ = GURL("https://origin/path"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 44 | const GURL kUrl2_ = GURL("https://origin2/path"); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 45 | const url::Origin kOrigin_ = url::Origin::Create(kUrl_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 46 | const url::Origin kOrigin2_ = url::Origin::Create(kUrl2_); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 47 | const GURL kEndpoint_ = GURL("https://endpoint/"); |
Douglas Creager | f6cb49f7 | 2018-07-19 20:14:53 | [diff] [blame] | 48 | const std::string kUserAgent_ = "Mozilla/1.0"; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 49 | const std::string kGroup_ = "group"; |
| 50 | const std::string kType_ = "type"; |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 51 | const NetworkIsolationKey kNik_ = |
| 52 | NetworkIsolationKey(SchemefulSite(kOrigin_), SchemefulSite(kOrigin_)); |
| 53 | const NetworkIsolationKey kNik2_ = |
| 54 | NetworkIsolationKey(SchemefulSite(kOrigin2_), SchemefulSite(kOrigin2_)); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 55 | const ReportingEndpointGroupKey kGroupKey_ = |
Matt Menke | 17b23c4 | 2020-10-22 05:14:57 | [diff] [blame] | 56 | ReportingEndpointGroupKey(kNik_, kOrigin_, kGroup_); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 57 | const ReportingEndpointGroupKey kGroupKey2_ = |
Matt Menke | 17b23c4 | 2020-10-22 05:14:57 | [diff] [blame] | 58 | ReportingEndpointGroupKey(kNik2_, kOrigin2_, kGroup_); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 59 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 60 | ReportingServiceTest() { |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 61 | feature_list_.InitAndEnableFeature( |
| 62 | features::kPartitionNelAndReportingByNetworkIsolationKey); |
| 63 | Init(); |
| 64 | } |
| 65 | |
| 66 | // Initializes, or re-initializes, |service_| and its dependencies. |
| 67 | void Init() { |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 68 | if (GetParam()) |
| 69 | store_ = std::make_unique<MockPersistentReportingStore>(); |
| 70 | else |
| 71 | store_ = nullptr; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 72 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 73 | auto test_context = std::make_unique<TestReportingContext>( |
| 74 | &clock_, &tick_clock_, ReportingPolicy(), store_.get()); |
| 75 | context_ = test_context.get(); |
| 76 | |
| 77 | service_ = ReportingService::CreateForTesting(std::move(test_context)); |
| 78 | } |
| 79 | |
| 80 | // If the store exists, simulate finishing loading the store, which should |
| 81 | // make the rest of the test run synchronously. |
| 82 | void FinishLoading(bool load_success) { |
| 83 | if (store_) |
| 84 | store_->FinishLoading(load_success); |
| 85 | } |
| 86 | |
| 87 | MockPersistentReportingStore* store() { return store_.get(); } |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 88 | TestReportingContext* context() { return context_; } |
| 89 | ReportingService* service() { return service_.get(); } |
| 90 | |
| 91 | private: |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 92 | base::test::ScopedFeatureList feature_list_; |
| 93 | |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 94 | base::SimpleTestClock clock_; |
| 95 | base::SimpleTestTickClock tick_clock_; |
| 96 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 97 | std::unique_ptr<MockPersistentReportingStore> store_; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 98 | TestReportingContext* context_; |
| 99 | std::unique_ptr<ReportingService> service_; |
| 100 | }; |
| 101 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 102 | TEST_P(ReportingServiceTest, QueueReport) { |
Matt Menke | dd2ed7a | 2020-10-22 04:09:20 | [diff] [blame] | 103 | service()->QueueReport(kUrl_, kNik_, kUserAgent_, kGroup_, kType_, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 104 | std::make_unique<base::DictionaryValue>(), 0); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 105 | FinishLoading(true /* load_success */); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 106 | |
| 107 | std::vector<const ReportingReport*> reports; |
| 108 | context()->cache()->GetReports(&reports); |
| 109 | ASSERT_EQ(1u, reports.size()); |
| 110 | EXPECT_EQ(kUrl_, reports[0]->url); |
Matt Menke | dd2ed7a | 2020-10-22 04:09:20 | [diff] [blame] | 111 | EXPECT_EQ(kNik_, reports[0]->network_isolation_key); |
Douglas Creager | f6cb49f7 | 2018-07-19 20:14:53 | [diff] [blame] | 112 | EXPECT_EQ(kUserAgent_, reports[0]->user_agent); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 113 | EXPECT_EQ(kGroup_, reports[0]->group); |
| 114 | EXPECT_EQ(kType_, reports[0]->type); |
| 115 | } |
| 116 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 117 | TEST_P(ReportingServiceTest, QueueReportSanitizeUrl) { |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 118 | // Same as kUrl_ but with username, password, and fragment. |
| 119 | GURL url = GURL("https://username:password@origin/path#fragment"); |
Matt Menke | dd2ed7a | 2020-10-22 04:09:20 | [diff] [blame] | 120 | service()->QueueReport(url, kNik_, kUserAgent_, kGroup_, kType_, |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 121 | std::make_unique<base::DictionaryValue>(), 0); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 122 | FinishLoading(true /* load_success */); |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 123 | |
| 124 | std::vector<const ReportingReport*> reports; |
| 125 | context()->cache()->GetReports(&reports); |
| 126 | ASSERT_EQ(1u, reports.size()); |
| 127 | EXPECT_EQ(kUrl_, reports[0]->url); |
Matt Menke | dd2ed7a | 2020-10-22 04:09:20 | [diff] [blame] | 128 | EXPECT_EQ(kNik_, reports[0]->network_isolation_key); |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 129 | EXPECT_EQ(kUserAgent_, reports[0]->user_agent); |
| 130 | EXPECT_EQ(kGroup_, reports[0]->group); |
| 131 | EXPECT_EQ(kType_, reports[0]->type); |
| 132 | } |
| 133 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 134 | TEST_P(ReportingServiceTest, DontQueueReportInvalidUrl) { |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 135 | GURL url = GURL("https://"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 136 | // This does not trigger an attempt to load from the store because the url |
| 137 | // is immediately rejected as invalid. |
Matt Menke | dd2ed7a | 2020-10-22 04:09:20 | [diff] [blame] | 138 | service()->QueueReport(url, kNik_, kUserAgent_, kGroup_, kType_, |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 139 | std::make_unique<base::DictionaryValue>(), 0); |
| 140 | |
| 141 | std::vector<const ReportingReport*> reports; |
| 142 | context()->cache()->GetReports(&reports); |
| 143 | ASSERT_EQ(0u, reports.size()); |
| 144 | } |
| 145 | |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 146 | TEST_P(ReportingServiceTest, QueueReportNetworkIsolationKeyDisabled) { |
| 147 | base::test::ScopedFeatureList feature_list; |
| 148 | feature_list.InitAndDisableFeature( |
| 149 | features::kPartitionNelAndReportingByNetworkIsolationKey); |
| 150 | |
| 151 | // Re-create the store, so it reads the new feature value. |
| 152 | Init(); |
| 153 | |
| 154 | service()->QueueReport(kUrl_, kNik_, kUserAgent_, kGroup_, kType_, |
| 155 | std::make_unique<base::DictionaryValue>(), 0); |
| 156 | FinishLoading(true /* load_success */); |
| 157 | |
| 158 | std::vector<const ReportingReport*> reports; |
| 159 | context()->cache()->GetReports(&reports); |
| 160 | ASSERT_EQ(1u, reports.size()); |
| 161 | |
| 162 | // NetworkIsolationKey should be empty, instead of kNik_; |
| 163 | EXPECT_EQ(NetworkIsolationKey(), reports[0]->network_isolation_key); |
| 164 | EXPECT_NE(kNik_, reports[0]->network_isolation_key); |
| 165 | |
| 166 | EXPECT_EQ(kUrl_, reports[0]->url); |
| 167 | EXPECT_EQ(kUserAgent_, reports[0]->user_agent); |
| 168 | EXPECT_EQ(kGroup_, reports[0]->group); |
| 169 | EXPECT_EQ(kType_, reports[0]->type); |
| 170 | } |
| 171 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 172 | TEST_P(ReportingServiceTest, ProcessReportToHeader) { |
| 173 | service()->ProcessReportToHeader(kUrl_, kNik_, |
| 174 | "{\"endpoints\":[{\"url\":\"" + |
| 175 | kEndpoint_.spec() + |
| 176 | "\"}]," |
| 177 | "\"group\":\"" + |
| 178 | kGroup_ + |
| 179 | "\"," |
| 180 | "\"max_age\":86400}"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 181 | FinishLoading(true /* load_success */); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 182 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 183 | EXPECT_EQ(1u, context()->cache()->GetEndpointCount()); |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 184 | EXPECT_TRUE(context()->cache()->GetEndpointForTesting( |
| 185 | ReportingEndpointGroupKey(kNik_, kOrigin_, kGroup_), kEndpoint_)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 186 | } |
| 187 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 188 | TEST_P(ReportingServiceTest, ProcessReportingEndpointsHeader) { |
| 189 | base::test::ScopedFeatureList feature_list; |
| 190 | feature_list.InitAndEnableFeature(net::features::kDocumentReporting); |
| 191 | service()->ProcessReportingEndpointsHeader( |
| 192 | kOrigin_, kNik_, kGroup_ + "=\"" + kEndpoint_.spec() + "\""); |
| 193 | FinishLoading(true /* load_success */); |
| 194 | |
| 195 | EXPECT_EQ(1u, context()->cache()->GetEndpointCount()); |
| 196 | EXPECT_TRUE(context()->cache()->GetEndpointForTesting( |
| 197 | ReportingEndpointGroupKey(kNik_, kOrigin_, kGroup_), kEndpoint_)); |
| 198 | } |
| 199 | |
| 200 | TEST_P(ReportingServiceTest, ProcessReportingEndpointsHeaderPathAbsolute) { |
| 201 | base::test::ScopedFeatureList feature_list; |
| 202 | feature_list.InitAndEnableFeature(net::features::kDocumentReporting); |
| 203 | service()->ProcessReportingEndpointsHeader(kOrigin_, kNik_, |
| 204 | kGroup_ + "=\"/path-absolute\""); |
Rodney Ding | b880943 | 2020-03-12 22:19:59 | [diff] [blame] | 205 | FinishLoading(true /* load_success */); |
| 206 | |
| 207 | EXPECT_EQ(1u, context()->cache()->GetEndpointCount()); |
| 208 | } |
| 209 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 210 | TEST_P(ReportingServiceTest, ProcessReportToHeaderPathAbsolute) { |
| 211 | service()->ProcessReportToHeader( |
| 212 | kUrl_, kNik_, |
| 213 | "{\"endpoints\":[{\"url\":\"/path-absolute\"}]," |
| 214 | "\"group\":\"" + |
| 215 | kGroup_ + |
| 216 | "\"," |
| 217 | "\"max_age\":86400}"); |
| 218 | FinishLoading(true /* load_success */); |
| 219 | |
| 220 | EXPECT_EQ(1u, context()->cache()->GetEndpointCount()); |
| 221 | } |
| 222 | |
| 223 | TEST_P(ReportingServiceTest, ProcessReportToHeader_TooLong) { |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 224 | const std::string header_too_long = |
| 225 | "{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() + |
| 226 | "\"}]," |
| 227 | "\"group\":\"" + |
| 228 | kGroup_ + |
| 229 | "\"," |
Douglas Creager | bca6442 | 2018-06-18 13:54:42 | [diff] [blame] | 230 | "\"max_age\":86400," + |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 231 | "\"junk\":\"" + std::string(32 * 1024, 'a') + "\"}"; |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 232 | // This does not trigger an attempt to load from the store because the header |
| 233 | // is immediately rejected as invalid. |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 234 | service()->ProcessReportToHeader(kUrl_, kNik_, header_too_long); |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 235 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 236 | EXPECT_EQ(0u, context()->cache()->GetEndpointCount()); |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 237 | } |
| 238 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 239 | TEST_P(ReportingServiceTest, ProcessReportToHeader_TooDeep) { |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 240 | const std::string header_too_deep = "{\"endpoints\":[{\"url\":\"" + |
| 241 | kEndpoint_.spec() + |
| 242 | "\"}]," |
| 243 | "\"group\":\"" + |
| 244 | kGroup_ + |
| 245 | "\"," |
Douglas Creager | bca6442 | 2018-06-18 13:54:42 | [diff] [blame] | 246 | "\"max_age\":86400," + |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 247 | "\"junk\":[[[[[[[[[[]]]]]]]]]]}"; |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 248 | // This does not trigger an attempt to load from the store because the header |
| 249 | // is immediately rejected as invalid. |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 250 | service()->ProcessReportToHeader(kUrl_, kNik_, header_too_deep); |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 251 | |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 252 | EXPECT_EQ(0u, context()->cache()->GetEndpointCount()); |
Julia Tuttle | 7d87494 | 2018-03-02 01:19:13 | [diff] [blame] | 253 | } |
| 254 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 255 | TEST_P(ReportingServiceTest, ProcessReportToHeaderNetworkIsolationKeyDisabled) { |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 256 | base::test::ScopedFeatureList feature_list; |
| 257 | feature_list.InitAndDisableFeature( |
| 258 | features::kPartitionNelAndReportingByNetworkIsolationKey); |
| 259 | |
| 260 | // Re-create the store, so it reads the new feature value. |
| 261 | Init(); |
| 262 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 263 | service()->ProcessReportToHeader(kUrl_, kNik_, |
| 264 | "{\"endpoints\":[{\"url\":\"" + |
| 265 | kEndpoint_.spec() + |
| 266 | "\"}]," |
| 267 | "\"group\":\"" + |
| 268 | kGroup_ + |
| 269 | "\"," |
| 270 | "\"max_age\":86400}"); |
Matt Menke | 055f5c9 | 2020-10-22 19:33:23 | [diff] [blame] | 271 | FinishLoading(true /* load_success */); |
| 272 | |
| 273 | EXPECT_EQ(1u, context()->cache()->GetEndpointCount()); |
| 274 | EXPECT_FALSE(context()->cache()->GetEndpointForTesting( |
| 275 | ReportingEndpointGroupKey(kNik_, kOrigin_, kGroup_), kEndpoint_)); |
| 276 | EXPECT_TRUE(context()->cache()->GetEndpointForTesting( |
| 277 | ReportingEndpointGroupKey(NetworkIsolationKey(), kOrigin_, kGroup_), |
| 278 | kEndpoint_)); |
| 279 | } |
| 280 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 281 | TEST_P(ReportingServiceTest, WriteToStore) { |
| 282 | if (!store()) |
| 283 | return; |
| 284 | |
| 285 | MockPersistentReportingStore::CommandList expected_commands; |
| 286 | |
| 287 | // This first call to any public method triggers a load. The load will block |
| 288 | // until we call FinishLoading. |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 289 | service()->ProcessReportToHeader(kUrl_, kNik_, |
| 290 | "{\"endpoints\":[{\"url\":\"" + |
| 291 | kEndpoint_.spec() + |
| 292 | "\"}]," |
| 293 | "\"group\":\"" + |
| 294 | kGroup_ + |
| 295 | "\"," |
| 296 | "\"max_age\":86400}"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 297 | expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS); |
| 298 | EXPECT_THAT(store()->GetAllCommands(), |
| 299 | testing::UnorderedElementsAreArray(expected_commands)); |
| 300 | |
| 301 | // Unblock the load. The will let the remaining calls to the service complete |
| 302 | // without blocking. |
| 303 | FinishLoading(true /* load_success */); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 304 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT, |
| 305 | kGroupKey_, kEndpoint_); |
| 306 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP, |
| 307 | kGroupKey_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 308 | EXPECT_THAT(store()->GetAllCommands(), |
| 309 | testing::UnorderedElementsAreArray(expected_commands)); |
| 310 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 311 | service()->ProcessReportToHeader(kUrl2_, kNik2_, |
| 312 | "{\"endpoints\":[{\"url\":\"" + |
| 313 | kEndpoint_.spec() + |
| 314 | "\"}]," |
| 315 | "\"group\":\"" + |
| 316 | kGroup_ + |
| 317 | "\"," |
| 318 | "\"max_age\":86400}"); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 319 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT, |
| 320 | kGroupKey2_, kEndpoint_); |
| 321 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP, |
| 322 | kGroupKey2_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 323 | EXPECT_THAT(store()->GetAllCommands(), |
| 324 | testing::UnorderedElementsAreArray(expected_commands)); |
| 325 | |
Matt Menke | 17b23c4 | 2020-10-22 05:14:57 | [diff] [blame] | 326 | service()->QueueReport(kUrl_, kNik_, kUserAgent_, kGroup_, kType_, |
| 327 | std::make_unique<base::DictionaryValue>(), 0); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 328 | expected_commands.emplace_back( |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 329 | CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, kGroupKey_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 330 | EXPECT_THAT(store()->GetAllCommands(), |
| 331 | testing::UnorderedElementsAreArray(expected_commands)); |
| 332 | |
| 333 | service()->RemoveBrowsingData(ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS, |
| 334 | base::BindRepeating([](const GURL& url) { |
| 335 | return url.host() == "origin"; |
| 336 | })); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 337 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT, |
| 338 | kGroupKey_, kEndpoint_); |
| 339 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP, |
| 340 | kGroupKey_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 341 | expected_commands.emplace_back(CommandType::FLUSH); |
| 342 | EXPECT_THAT(store()->GetAllCommands(), |
| 343 | testing::UnorderedElementsAreArray(expected_commands)); |
| 344 | |
| 345 | service()->RemoveAllBrowsingData( |
| 346 | ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 347 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT, |
| 348 | kGroupKey2_, kEndpoint_); |
| 349 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP, |
| 350 | kGroupKey2_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 351 | expected_commands.emplace_back(CommandType::FLUSH); |
| 352 | EXPECT_THAT(store()->GetAllCommands(), |
| 353 | testing::UnorderedElementsAreArray(expected_commands)); |
| 354 | } |
| 355 | |
| 356 | TEST_P(ReportingServiceTest, WaitUntilLoadFinishesBeforeWritingToStore) { |
| 357 | if (!store()) |
| 358 | return; |
| 359 | |
| 360 | MockPersistentReportingStore::CommandList expected_commands; |
| 361 | |
| 362 | // This first call to any public method triggers a load. The load will block |
| 363 | // until we call FinishLoading. |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 364 | service()->ProcessReportToHeader(kUrl_, kNik_, |
| 365 | "{\"endpoints\":[{\"url\":\"" + |
| 366 | kEndpoint_.spec() + |
| 367 | "\"}]," |
| 368 | "\"group\":\"" + |
| 369 | kGroup_ + |
| 370 | "\"," |
| 371 | "\"max_age\":86400}"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 372 | expected_commands.emplace_back(CommandType::LOAD_REPORTING_CLIENTS); |
| 373 | EXPECT_THAT(store()->GetAllCommands(), |
| 374 | testing::UnorderedElementsAreArray(expected_commands)); |
| 375 | |
Rodney Ding | 329e4bb | 2021-03-19 22:21:53 | [diff] [blame^] | 376 | service()->ProcessReportToHeader(kUrl2_, kNik2_, |
| 377 | "{\"endpoints\":[{\"url\":\"" + |
| 378 | kEndpoint_.spec() + |
| 379 | "\"}]," |
| 380 | "\"group\":\"" + |
| 381 | kGroup_ + |
| 382 | "\"," |
| 383 | "\"max_age\":86400}"); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 384 | EXPECT_THAT(store()->GetAllCommands(), |
| 385 | testing::UnorderedElementsAreArray(expected_commands)); |
| 386 | |
Matt Menke | 17b23c4 | 2020-10-22 05:14:57 | [diff] [blame] | 387 | service()->QueueReport(kUrl_, kNik_, kUserAgent_, kGroup_, kType_, |
| 388 | std::make_unique<base::DictionaryValue>(), 0); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 389 | EXPECT_THAT(store()->GetAllCommands(), |
| 390 | testing::UnorderedElementsAreArray(expected_commands)); |
| 391 | |
| 392 | service()->RemoveBrowsingData(ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS, |
| 393 | base::BindRepeating([](const GURL& url) { |
| 394 | return url.host() == "origin"; |
| 395 | })); |
| 396 | EXPECT_THAT(store()->GetAllCommands(), |
| 397 | testing::UnorderedElementsAreArray(expected_commands)); |
| 398 | |
| 399 | service()->RemoveAllBrowsingData( |
| 400 | ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS); |
| 401 | EXPECT_THAT(store()->GetAllCommands(), |
| 402 | testing::UnorderedElementsAreArray(expected_commands)); |
| 403 | |
| 404 | // Unblock the load. The will let the remaining calls to the service complete |
| 405 | // without blocking. |
| 406 | FinishLoading(true /* load_success */); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 407 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT, |
| 408 | kGroupKey_, kEndpoint_); |
| 409 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT, |
| 410 | kGroupKey2_, kEndpoint_); |
| 411 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP, |
| 412 | kGroupKey_); |
| 413 | expected_commands.emplace_back(CommandType::ADD_REPORTING_ENDPOINT_GROUP, |
| 414 | kGroupKey2_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 415 | expected_commands.emplace_back( |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 416 | CommandType::UPDATE_REPORTING_ENDPOINT_GROUP_ACCESS_TIME, kGroupKey_); |
| 417 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT, |
| 418 | kGroupKey_, kEndpoint_); |
| 419 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP, |
| 420 | kGroupKey_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 421 | expected_commands.emplace_back(CommandType::FLUSH); |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 422 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT, |
| 423 | kGroupKey2_, kEndpoint_); |
| 424 | expected_commands.emplace_back(CommandType::DELETE_REPORTING_ENDPOINT_GROUP, |
| 425 | kGroupKey2_); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 426 | expected_commands.emplace_back(CommandType::FLUSH); |
| 427 | EXPECT_THAT(store()->GetAllCommands(), |
| 428 | testing::UnorderedElementsAreArray(expected_commands)); |
| 429 | } |
| 430 | |
| 431 | INSTANTIATE_TEST_SUITE_P(ReportingServiceStoreTest, |
| 432 | ReportingServiceTest, |
| 433 | ::testing::Bool()); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 434 | } // namespace |
| 435 | } // namespace net |