xref: /aosp_15_r20/external/cronet/net/log/net_log_util_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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/log/net_log_util.h"
6 
7 #include <set>
8 #include <string_view>
9 #include <vector>
10 
11 #include "base/containers/contains.h"
12 #include "base/feature_list.h"
13 #include "base/files/file_path.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/ranges/algorithm.h"
16 #include "base/test/scoped_feature_list.h"
17 #include "base/test/task_environment.h"
18 #include "base/values.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/net_info_source_list.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/dns/public/doh_provider_entry.h"
23 #include "net/http/http_cache.h"
24 #include "net/http/http_transaction.h"
25 #include "net/log/net_log_source.h"
26 #include "net/log/net_log_with_source.h"
27 #include "net/log/test_net_log.h"
28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
29 #include "net/url_request/url_request_context.h"
30 #include "net/url_request/url_request_context_builder.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 
34 namespace net {
35 
36 namespace {
37 
38 // Make sure GetNetConstants doesn't crash.
TEST(NetLogUtil,GetNetConstants)39 TEST(NetLogUtil, GetNetConstants) {
40   base::Value constants(GetNetConstants());
41 }
42 
43 // Make sure GetNetInfo doesn't crash when called on contexts with and without
44 // caches, and they have the same number of elements.
TEST(NetLogUtil,GetNetInfo)45 TEST(NetLogUtil, GetNetInfo) {
46   base::test::TaskEnvironment task_environment;
47 
48   auto context = CreateTestURLRequestContextBuilder()->Build();
49   HttpCache* http_cache = context->http_transaction_factory()->GetCache();
50 
51   // Get NetInfo when there's no cache backend (It's only created on first use).
52   EXPECT_FALSE(http_cache->GetCurrentBackend());
53   base::Value::Dict net_info_without_cache(GetNetInfo(context.get()));
54   EXPECT_FALSE(http_cache->GetCurrentBackend());
55   EXPECT_GT(net_info_without_cache.size(), 0u);
56 
57   // Force creation of a cache backend, and get NetInfo again.
58   disk_cache::Backend* backend = nullptr;
59   EXPECT_EQ(OK, context->http_transaction_factory()->GetCache()->GetBackend(
60                     &backend, TestCompletionCallback().callback()));
61   EXPECT_TRUE(http_cache->GetCurrentBackend());
62   base::Value::Dict net_info_with_cache = GetNetInfo(context.get());
63   EXPECT_GT(net_info_with_cache.size(), 0u);
64 
65   EXPECT_EQ(net_info_without_cache.size(), net_info_with_cache.size());
66 }
67 
68 // Verify that active Field Trials are reflected.
TEST(NetLogUtil,GetNetInfoIncludesFieldTrials)69 TEST(NetLogUtil, GetNetInfoIncludesFieldTrials) {
70   base::test::TaskEnvironment task_environment;
71 
72   // Clear all Field Trials.
73   base::test::ScopedFeatureList scoped_feature_list;
74   scoped_feature_list.InitWithFeatureList(
75       std::make_unique<base::FeatureList>());
76 
77   // Add and activate a new Field Trial.
78   base::FieldTrialList::CreateFieldTrial("NewFieldTrial", "Active");
79   EXPECT_EQ(base::FieldTrialList::FindFullName("NewFieldTrial"), "Active");
80 
81   auto context = CreateTestURLRequestContextBuilder()->Build();
82   base::Value net_info(GetNetInfo(context.get()));
83 
84   // Verify that the returned information reflects the new trial.
85   ASSERT_TRUE(net_info.is_dict());
86   base::Value::List* trials =
87       net_info.GetDict().FindList("activeFieldTrialGroups");
88   ASSERT_NE(nullptr, trials);
89   EXPECT_EQ(1u, trials->size());
90   EXPECT_TRUE((*trials)[0].is_string());
91   EXPECT_EQ("NewFieldTrial:Active", (*trials)[0].GetString());
92 }
93 
94 // Demonstrate that disabling a provider causes it to be added to the list of
95 // disabled DoH providers.
96 //
97 // TODO(https://crbug.com/1306495) Stop using the real DoH provider list.
TEST(NetLogUtil,GetNetInfoIncludesDisabledDohProviders)98 TEST(NetLogUtil, GetNetInfoIncludesDisabledDohProviders) {
99   constexpr std::string_view kArbitraryProvider = "Google";
100   base::test::TaskEnvironment task_environment;
101 
102   for (bool provider_enabled : {false, true}) {
103     // Get the DoH provider entry.
104     auto provider_list = net::DohProviderEntry::GetList();
105     auto provider_it = base::ranges::find(provider_list, kArbitraryProvider,
106                                           &net::DohProviderEntry::provider);
107     CHECK(provider_it != provider_list.end());
108     const DohProviderEntry& provider_entry = **provider_it;
109 
110     // Enable or disable the provider's feature according to `provider_enabled`.
111     base::test::ScopedFeatureList scoped_feature_list;
112     scoped_feature_list.InitWithFeatureState(provider_entry.feature,
113                                              provider_enabled);
114     EXPECT_EQ(provider_enabled,
115               base::FeatureList::IsEnabled(provider_entry.feature));
116 
117     // Verify that the provider is present in the list of disabled providers iff
118     // we disabled it.
119     auto context = CreateTestURLRequestContextBuilder()->Build();
120     base::Value net_info(GetNetInfo(context.get()));
121     ASSERT_TRUE(net_info.is_dict());
122     const base::Value::List* disabled_doh_providers_list =
123         net_info.GetDict().FindList(kNetInfoDohProvidersDisabledDueToFeature);
124     CHECK(disabled_doh_providers_list);
125     EXPECT_EQ(!provider_enabled,
126               base::Contains(*disabled_doh_providers_list,
127                              base::Value(kArbitraryProvider)));
128   }
129 }
130 
131 // Make sure CreateNetLogEntriesForActiveObjects works for requests from a
132 // single URLRequestContext.
TEST(NetLogUtil,CreateNetLogEntriesForActiveObjectsOneContext)133 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsOneContext) {
134   base::test::TaskEnvironment task_environment;
135 
136   // Using same context for each iteration makes sure deleted requests don't
137   // appear in the list, or result in crashes.
138   auto context = CreateTestURLRequestContextBuilder()->Build();
139   TestDelegate delegate;
140   for (size_t num_requests = 0; num_requests < 5; ++num_requests) {
141     std::vector<std::unique_ptr<URLRequest>> requests;
142     for (size_t i = 0; i < num_requests; ++i) {
143       requests.push_back(context->CreateRequest(GURL("about:life"),
144                                                 DEFAULT_PRIORITY, &delegate,
145                                                 TRAFFIC_ANNOTATION_FOR_TESTS));
146     }
147     std::set<URLRequestContext*> contexts;
148     contexts.insert(context.get());
149     RecordingNetLogObserver net_log_observer;
150     CreateNetLogEntriesForActiveObjects(contexts, &net_log_observer);
151     auto entry_list = net_log_observer.GetEntries();
152     ASSERT_EQ(num_requests, entry_list.size());
153 
154     for (size_t i = 0; i < num_requests; ++i) {
155       EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id);
156     }
157   }
158 }
159 
160 // Make sure CreateNetLogEntriesForActiveObjects works with multiple
161 // URLRequestContexts.
TEST(NetLogUtil,CreateNetLogEntriesForActiveObjectsMultipleContexts)162 TEST(NetLogUtil, CreateNetLogEntriesForActiveObjectsMultipleContexts) {
163   base::test::TaskEnvironment task_environment;
164 
165   TestDelegate delegate;
166   for (size_t num_requests = 0; num_requests < 5; ++num_requests) {
167     std::vector<std::unique_ptr<URLRequestContext>> contexts;
168     std::vector<std::unique_ptr<URLRequest>> requests;
169     std::set<URLRequestContext*> context_set;
170     for (size_t i = 0; i < num_requests; ++i) {
171       contexts.push_back(CreateTestURLRequestContextBuilder()->Build());
172       context_set.insert(contexts[i].get());
173       requests.push_back(
174           contexts[i]->CreateRequest(GURL("about:hats"), DEFAULT_PRIORITY,
175                                      &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
176     }
177     RecordingNetLogObserver net_log_observer;
178     CreateNetLogEntriesForActiveObjects(context_set, &net_log_observer);
179     auto entry_list = net_log_observer.GetEntries();
180     ASSERT_EQ(num_requests, entry_list.size());
181 
182     for (size_t i = 0; i < num_requests; ++i) {
183       EXPECT_EQ(entry_list[i].source.id, requests[i]->net_log().source().id);
184     }
185   }
186 }
187 
188 }  // namespace
189 
190 }  // namespace net
191