• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
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_context.h"
6 
7 #include <optional>
8 #include <string>
9 
10 #include "base/test/scoped_feature_list.h"
11 #include "net/base/features.h"
12 #include "net/reporting/mock_persistent_reporting_store.h"
13 #include "net/reporting/reporting_test_util.h"
14 #include "net/url_request/url_request_context_builder.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
18 
19 namespace net {
20 namespace {
21 
22 // The tests are parametrized on a boolean value which represents whether to use
23 // a MockPersistentReportingStore or not.
24 class ReportingContextTest : public ReportingTestBase,
25                              public ::testing::WithParamInterface<bool> {
26  protected:
ReportingContextTest()27   ReportingContextTest() {
28     feature_list_.InitAndEnableFeature(
29         features::kPartitionConnectionsByNetworkIsolationKey);
30     std::unique_ptr<MockPersistentReportingStore> store;
31     if (GetParam()) {
32       store = std::make_unique<MockPersistentReportingStore>();
33     }
34     store_ = store.get();
35     UseStore(std::move(store));
36   }
37 
store()38   MockPersistentReportingStore* store() { return store_.get(); }
39 
40  private:
41   base::test::ScopedFeatureList feature_list_;
42   raw_ptr<MockPersistentReportingStore> store_;
43 };
44 
TEST_P(ReportingContextTest,ReportingContextConstructionWithFeatureEnabled)45 TEST_P(ReportingContextTest, ReportingContextConstructionWithFeatureEnabled) {
46   base::test::ScopedFeatureList feature_list;
47   feature_list.InitAndEnableFeature(
48       net::features::kReportingApiEnableEnterpriseCookieIssues);
49   base::flat_map<std::string, GURL> test_enterprise_endpoints{
50       {"endpoint-1", GURL("https://example.com/reports")},
51       {"endpoint-2", GURL("https://reporting.example/cookie-issues")},
52       {"endpoint-3", GURL("https://report-collector.example")},
53   };
54   EXPECT_EQ(0u, cache()->GetEnterpriseEndpointsForTesting().size());
55   std::unique_ptr<URLRequestContext> url_request_context =
56       CreateTestURLRequestContextBuilder()->Build();
57   std::unique_ptr<ReportingContext> reporting_context_ptr =
58       ReportingContext::Create(ReportingPolicy(), url_request_context.get(),
59                                store(), test_enterprise_endpoints);
60 
61   std::vector<ReportingEndpoint> expected_enterprise_endpoints = {
62       {ReportingEndpointGroupKey(NetworkAnonymizationKey(),
63                                  /*reporting_source=*/std::nullopt,
64                                  /*origin=*/std::nullopt, "endpoint-1",
65                                  ReportingTargetType::kEnterprise),
66        {.url = GURL("https://example.com/reports")}},
67       {ReportingEndpointGroupKey(NetworkAnonymizationKey(),
68                                  /*reporting_source=*/std::nullopt,
69                                  /*origin=*/std::nullopt, "endpoint-2",
70                                  ReportingTargetType::kEnterprise),
71        {.url = GURL("https://reporting.example/cookie-issues")}},
72       {ReportingEndpointGroupKey(NetworkAnonymizationKey(),
73                                  /*reporting_source=*/std::nullopt,
74                                  /*origin=*/std::nullopt, "endpoint-3",
75                                  ReportingTargetType::kEnterprise),
76        {.url = GURL("https://report-collector.example")}}};
77 
78   EXPECT_EQ(expected_enterprise_endpoints,
79             reporting_context_ptr->cache()->GetEnterpriseEndpointsForTesting());
80 }
81 
TEST_P(ReportingContextTest,ReportingContextConstructionWithFeatureDisabled)82 TEST_P(ReportingContextTest, ReportingContextConstructionWithFeatureDisabled) {
83   base::test::ScopedFeatureList feature_list;
84   feature_list.InitAndDisableFeature(
85       net::features::kReportingApiEnableEnterpriseCookieIssues);
86   base::flat_map<std::string, GURL> test_enterprise_endpoints{
87       {"endpoint-1", GURL("https://example.com/reports")},
88       {"endpoint-2", GURL("https://reporting.example/cookie-issues")},
89       {"endpoint-3", GURL("https://report-collector.example")},
90   };
91   EXPECT_EQ(0u, cache()->GetEnterpriseEndpointsForTesting().size());
92   std::unique_ptr<URLRequestContext> url_request_context =
93       CreateTestURLRequestContextBuilder()->Build();
94   std::unique_ptr<ReportingContext> reporting_context_ptr =
95       ReportingContext::Create(ReportingPolicy(), url_request_context.get(),
96                                store(), test_enterprise_endpoints);
97 
98   EXPECT_EQ(0u, reporting_context_ptr->cache()
99                     ->GetEnterpriseEndpointsForTesting()
100                     .size());
101 }
102 
103 INSTANTIATE_TEST_SUITE_P(ReportingContextStoreTest,
104                          ReportingContextTest,
105                          ::testing::Bool());
106 }  // namespace
107 }  // namespace net
108