net_log_util_unittest.cc 7.6 KB

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