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