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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
// Copyright 2013 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.h"
#include <array>
#include "base/memory/raw_ptr.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/task_environment.h"
#include "base/threading/simple_thread.h"
#include "base/values.h"
#include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::Pair;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::SizeIs;
using ::testing::StrEq;
using ::testing::UnorderedElementsAre;
const int kThreads = 10;
const int kEvents = 100;
int CaptureModeToInt(NetLogCaptureMode capture_mode) {
return static_cast<int>(capture_mode);
}
base::Value CaptureModeToValue(NetLogCaptureMode capture_mode) {
return base::Value(CaptureModeToInt(capture_mode));
}
base::Value::Dict NetCaptureModeParams(NetLogCaptureMode capture_mode) {
base::Value::Dict dict;
dict.Set("capture_mode", CaptureModeToValue(capture_mode));
return dict;
}
TEST(NetLogTest, BasicGlobalEvents) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
RecordingNetLogObserver net_log_observer;
auto entries = net_log_observer.GetEntries();
EXPECT_EQ(0u, entries.size());
task_environment.FastForwardBy(base::Seconds(1234));
base::TimeTicks ticks0 = base::TimeTicks::Now();
NetLog::Get()->AddGlobalEntry(NetLogEventType::CANCELLED);
task_environment.FastForwardBy(base::Seconds(5678));
base::TimeTicks ticks1 = base::TimeTicks::Now();
EXPECT_LE(ticks0, ticks1);
NetLog::Get()->AddGlobalEntry(NetLogEventType::FAILED);
task_environment.FastForwardBy(base::Seconds(91011));
EXPECT_LE(ticks1, base::TimeTicks::Now());
entries = net_log_observer.GetEntries();
ASSERT_EQ(2u, entries.size());
EXPECT_EQ(NetLogEventType::CANCELLED, entries[0].type);
EXPECT_EQ(NetLogSourceType::NONE, entries[0].source.type);
EXPECT_NE(NetLogSource::kInvalidId, entries[0].source.id);
EXPECT_EQ(ticks0, entries[0].source.start_time);
EXPECT_EQ(NetLogEventPhase::NONE, entries[0].phase);
EXPECT_EQ(ticks0, entries[0].time);
EXPECT_FALSE(entries[0].HasParams());
EXPECT_EQ(NetLogEventType::FAILED, entries[1].type);
EXPECT_EQ(NetLogSourceType::NONE, entries[1].source.type);
EXPECT_NE(NetLogSource::kInvalidId, entries[1].source.id);
EXPECT_LT(entries[0].source.id, entries[1].source.id);
EXPECT_EQ(ticks1, entries[1].source.start_time);
EXPECT_EQ(NetLogEventPhase::NONE, entries[1].phase);
EXPECT_EQ(ticks1, entries[1].time);
EXPECT_FALSE(entries[1].HasParams());
}
enum class AddEntryType {
kAddGlobalEntry,
kAddEntry,
};
class NetLogAddEntryTest : public testing::TestWithParam<AddEntryType> {};
TEST_P(NetLogAddEntryTest, StripsNonAllowlistedParamsInHeavilyRedactedMode) {
RecordingNetLogObserver default_net_log_observer(NetLogCaptureMode::kDefault);
RecordingNetLogObserver heavily_redacted_net_log_observer(
NetLogCaptureMode::kHeavilyRedacted);
auto params = base::Value::Dict()
.Set("should_be_stripped", "should_be_stripped_value")
.Set("method", "method_value");
auto& net_log = *NetLog::Get();
// Run the test against both AddGlobalEntry() and AddEntry(), since they
// exercise different code paths internally.
switch (GetParam()) {
case AddEntryType::kAddGlobalEntry:
net_log.AddGlobalEntry(
NetLogEventType::CANCELLED,
[&](NetLogCaptureMode capture_mode) { return params.Clone(); });
break;
case AddEntryType::kAddEntry:
net_log.AddEntry(NetLogEventType::CANCELLED,
NetLogSource(NetLogSourceType::NONE, net_log.NextID()),
NetLogEventPhase::NONE, [&] { return params.Clone(); });
break;
}
EXPECT_THAT(
default_net_log_observer.GetEntries(),
ElementsAre(Field(
&NetLogEntry::params,
UnorderedElementsAre(
Pair("should_be_stripped",
Property(&base::Value::GetIfString,
Pointee(StrEq("should_be_stripped_value")))),
Pair("method", Property(&base::Value::GetIfString,
Pointee(StrEq("method_value"))))))));
EXPECT_THAT(heavily_redacted_net_log_observer.GetEntries(),
ElementsAre(Field(
&NetLogEntry::params,
ElementsAre(Pair(
"method", Property(&base::Value::GetIfString,
Pointee(StrEq("method_value"))))))));
}
INSTANTIATE_TEST_SUITE_P(NetLogAddEntryTest,
NetLogAddEntryTest,
testing::Values(AddEntryType::kAddGlobalEntry,
AddEntryType::kAddEntry));
TEST(NetLogTest, BasicEventsWithSource) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
RecordingNetLogObserver net_log_observer;
auto entries = net_log_observer.GetEntries();
EXPECT_EQ(0u, entries.size());
task_environment.FastForwardBy(base::Seconds(9876));
base::TimeTicks source0_start_ticks = base::TimeTicks::Now();
NetLogWithSource source0 =
NetLogWithSource::Make(NetLogSourceType::URL_REQUEST);
task_environment.FastForwardBy(base::Seconds(1));
base::TimeTicks source0_event0_ticks = base::TimeTicks::Now();
source0.BeginEvent(NetLogEventType::REQUEST_ALIVE);
task_environment.FastForwardBy(base::Seconds(5432));
base::TimeTicks source1_start_ticks = base::TimeTicks::Now();
NetLogWithSource source1 = NetLogWithSource::Make(NetLogSourceType::SOCKET);
task_environment.FastForwardBy(base::Seconds(1));
base::TimeTicks source1_event0_ticks = base::TimeTicks::Now();
source1.BeginEvent(NetLogEventType::SOCKET_ALIVE);
task_environment.FastForwardBy(base::Seconds(10));
base::TimeTicks source1_event1_ticks = base::TimeTicks::Now();
source1.EndEvent(NetLogEventType::SOCKET_ALIVE);
task_environment.FastForwardBy(base::Seconds(1));
base::TimeTicks source0_event1_ticks = base::TimeTicks::Now();
source0.EndEvent(NetLogEventType::REQUEST_ALIVE);
task_environment.FastForwardBy(base::Seconds(123));
entries = net_log_observer.GetEntries();
ASSERT_EQ(4u, entries.size());
EXPECT_EQ(NetLogEventType::REQUEST_ALIVE, entries[0].type);
EXPECT_EQ(NetLogSourceType::URL_REQUEST, entries[0].source.type);
EXPECT_NE(NetLogSource::kInvalidId, entries[0].source.id);
EXPECT_EQ(source0_start_ticks, entries[0].source.start_time);
EXPECT_EQ(NetLogEventPhase::BEGIN, entries[0].phase);
EXPECT_EQ(source0_event0_ticks, entries[0].time);
EXPECT_FALSE(entries[0].HasParams());
EXPECT_EQ(NetLogEventType::SOCKET_ALIVE, entries[1].type);
EXPECT_EQ(NetLogSourceType::SOCKET, entries[1].source.type);
EXPECT_NE(NetLogSource::kInvalidId, entries[1].source.id);
EXPECT_LT(entries[0].source.id, entries[1].source.id);
EXPECT_EQ(source1_start_ticks, entries[1].source.start_time);
EXPECT_EQ(NetLogEventPhase::BEGIN, entries[1].phase);
EXPECT_EQ(source1_event0_ticks, entries[1].time);
EXPECT_FALSE(entries[1].HasParams());
EXPECT_EQ(NetLogEventType::SOCKET_ALIVE, entries[2].type);
EXPECT_EQ(NetLogSourceType::SOCKET, entries[2].source.type);
EXPECT_EQ(entries[1].source.id, entries[2].source.id);
EXPECT_EQ(source1_start_ticks, entries[2].source.start_time);
EXPECT_EQ(NetLogEventPhase::END, entries[2].phase);
EXPECT_EQ(source1_event1_ticks, entries[2].time);
EXPECT_FALSE(entries[2].HasParams());
EXPECT_EQ(NetLogEventType::REQUEST_ALIVE, entries[3].type);
EXPECT_EQ(NetLogSourceType::URL_REQUEST, entries[3].source.type);
EXPECT_EQ(entries[0].source.id, entries[3].source.id);
EXPECT_EQ(source0_start_ticks, entries[3].source.start_time);
EXPECT_EQ(NetLogEventPhase::END, entries[3].phase);
EXPECT_EQ(source0_event1_ticks, entries[3].time);
EXPECT_FALSE(entries[3].HasParams());
}
// Check that the correct CaptureMode is sent to NetLog Value callbacks.
TEST(NetLogTest, CaptureModes) {
NetLogCaptureMode kModes[] = {
NetLogCaptureMode::kDefault,
NetLogCaptureMode::kIncludeSensitive,
NetLogCaptureMode::kEverything,
};
RecordingNetLogObserver net_log_observer;
for (NetLogCaptureMode mode : kModes) {
net_log_observer.SetObserverCaptureMode(mode);
NetLog::Get()->AddGlobalEntry(NetLogEventType::SOCKET_ALIVE,
[&](NetLogCaptureMode capture_mode) {
return NetCaptureModeParams(capture_mode);
});
auto entries = net_log_observer.GetEntries();
ASSERT_EQ(1u, entries.size());
EXPECT_EQ(NetLogEventType::SOCKET_ALIVE, entries[0].type);
EXPECT_EQ(NetLogSourceType::NONE, entries[0].source.type);
EXPECT_NE(NetLogSource::kInvalidId, entries[0].source.id);
EXPECT_GE(base::TimeTicks::Now(), entries[0].source.start_time);
EXPECT_EQ(NetLogEventPhase::NONE, entries[0].phase);
EXPECT_GE(base::TimeTicks::Now(), entries[0].time);
ASSERT_EQ(CaptureModeToInt(mode),
GetIntegerValueFromParams(entries[0], "capture_mode"));
net_log_observer.Clear();
}
}
class CountingObserver : public NetLog::ThreadSafeObserver {
public:
CountingObserver() = default;
~CountingObserver() override {
if (net_log())
net_log()->RemoveObserver(this);
}
void OnAddEntry(const NetLogEntry& entry) override { ++count_; }
int count() const { return count_; }
private:
int count_ = 0;
};
class LoggingObserver : public NetLog::ThreadSafeObserver {
public:
LoggingObserver() = default;
~LoggingObserver() override {
if (net_log())
net_log()->RemoveObserver(this);
}
void OnAddEntry(const NetLogEntry& entry) override {
// TODO(crbug.com/40257546): This should be updated to be a
// base::Value::Dict instead of a std::unique_ptr.
std::unique_ptr<base::Value::Dict> dict =
std::make_unique<base::Value::Dict>(entry.ToDict());
ASSERT_TRUE(dict);
values_.push_back(std::move(dict));
}
size_t GetNumValues() const { return values_.size(); }
base::Value::Dict* GetDict(size_t index) const {
return values_[index].get();
}
private:
std::vector<std::unique_ptr<base::Value::Dict>> values_;
};
void AddEvent(NetLog* net_log) {
net_log->AddGlobalEntry(NetLogEventType::CANCELLED,
[&](NetLogCaptureMode capture_mode) {
return NetCaptureModeParams(capture_mode);
});
}
// A thread that waits until an event has been signalled before calling
// RunTestThread.
class NetLogTestThread : public base::SimpleThread {
public:
NetLogTestThread() : base::SimpleThread("NetLogTest") {}
NetLogTestThread(const NetLogTestThread&) = delete;
NetLogTestThread& operator=(const NetLogTestThread&) = delete;
// We'll wait for |start_event| to be triggered before calling a subclass's
// subclass's RunTestThread() function.
void Init(NetLog* net_log, base::WaitableEvent* start_event) {
start_event_ = start_event;
net_log_ = net_log;
}
void Run() override {
start_event_->Wait();
RunTestThread();
}
// Subclasses must override this with the code they want to run on their
// thread.
virtual void RunTestThread() = 0;
protected:
raw_ptr<NetLog> net_log_ = nullptr;
private:
// Only triggered once all threads have been created, to make it less likely
// each thread completes before the next one starts.
raw_ptr<base::WaitableEvent> start_event_ = nullptr;
};
// A thread that adds a bunch of events to the NetLog.
class AddEventsTestThread : public NetLogTestThread {
public:
AddEventsTestThread() = default;
AddEventsTestThread(const AddEventsTestThread&) = delete;
AddEventsTestThread& operator=(const AddEventsTestThread&) = delete;
~AddEventsTestThread() override = default;
private:
void RunTestThread() override {
for (int i = 0; i < kEvents; ++i)
AddEvent(net_log_);
}
};
// A thread that adds and removes an observer from the NetLog repeatedly.
class AddRemoveObserverTestThread : public NetLogTestThread {
public:
AddRemoveObserverTestThread() = default;
AddRemoveObserverTestThread(const AddRemoveObserverTestThread&) = delete;
AddRemoveObserverTestThread& operator=(const AddRemoveObserverTestThread&) =
delete;
~AddRemoveObserverTestThread() override { EXPECT_TRUE(!observer_.net_log()); }
private:
void RunTestThread() override {
for (int i = 0; i < kEvents; ++i) {
ASSERT_FALSE(observer_.net_log());
net_log_->AddObserver(&observer_, NetLogCaptureMode::kIncludeSensitive);
ASSERT_EQ(net_log_, observer_.net_log());
ASSERT_EQ(NetLogCaptureMode::kIncludeSensitive, observer_.capture_mode());
net_log_->RemoveObserver(&observer_);
ASSERT_TRUE(!observer_.net_log());
}
}
CountingObserver observer_;
};
// Creates |kThreads| threads of type |ThreadType| and then runs them all
// to completion.
template <class ThreadType>
void RunTestThreads(NetLog* net_log) {
// Must outlive `threads`.
base::WaitableEvent start_event(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
std::array<ThreadType, kThreads> threads;
for (size_t i = 0; i < std::size(threads); ++i) {
threads[i].Init(net_log, &start_event);
threads[i].Start();
}
start_event.Signal();
for (size_t i = 0; i < std::size(threads); ++i)
threads[i].Join();
}
// Makes sure that events on multiple threads are dispatched to all observers.
TEST(NetLogTest, NetLogEventThreads) {
// Attach some observers. They'll safely detach themselves on destruction.
CountingObserver observers[3];
for (auto& observer : observers) {
NetLog::Get()->AddObserver(&observer, NetLogCaptureMode::kEverything);
}
// Run a bunch of threads to completion, each of which will emit events to
// |net_log|.
RunTestThreads<AddEventsTestThread>(NetLog::Get());
// Check that each observer saw the emitted events.
const int kTotalEvents = kThreads * kEvents;
for (const auto& observer : observers)
EXPECT_EQ(kTotalEvents, observer.count());
}
// Test adding and removing a single observer.
TEST(NetLogTest, NetLogAddRemoveObserver) {
CountingObserver observer;
AddEvent(NetLog::Get());
EXPECT_EQ(0, observer.count());
EXPECT_EQ(nullptr, observer.net_log());
EXPECT_FALSE(NetLog::Get()->IsCapturing());
// Add the observer and add an event.
NetLog::Get()->AddObserver(&observer, NetLogCaptureMode::kIncludeSensitive);
EXPECT_TRUE(NetLog::Get()->IsCapturing());
EXPECT_EQ(NetLog::Get(), observer.net_log());
EXPECT_EQ(NetLogCaptureMode::kIncludeSensitive, observer.capture_mode());
EXPECT_TRUE(NetLog::Get()->IsCapturing());
AddEvent(NetLog::Get());
EXPECT_EQ(1, observer.count());
AddEvent(NetLog::Get());
EXPECT_EQ(2, observer.count());
// Remove observer and add an event.
NetLog::Get()->RemoveObserver(&observer);
EXPECT_EQ(nullptr, observer.net_log());
EXPECT_FALSE(NetLog::Get()->IsCapturing());
AddEvent(NetLog::Get());
EXPECT_EQ(2, observer.count());
// Add the observer a final time, this time with a different capture mdoe, and
// add an event.
NetLog::Get()->AddObserver(&observer, NetLogCaptureMode::kEverything);
EXPECT_EQ(NetLog::Get(), observer.net_log());
EXPECT_EQ(NetLogCaptureMode::kEverything, observer.capture_mode());
EXPECT_TRUE(NetLog::Get()->IsCapturing());
AddEvent(NetLog::Get());
EXPECT_EQ(3, observer.count());
}
// Test adding and removing two observers at different log levels.
TEST(NetLogTest, NetLogTwoObservers) {
std::array<LoggingObserver, 2> observer;
// Add first observer.
NetLog::Get()->AddObserver(&observer[0],
NetLogCaptureMode::kIncludeSensitive);
EXPECT_EQ(NetLog::Get(), observer[0].net_log());
EXPECT_EQ(nullptr, observer[1].net_log());
EXPECT_EQ(NetLogCaptureMode::kIncludeSensitive, observer[0].capture_mode());
EXPECT_TRUE(NetLog::Get()->IsCapturing());
// Add second observer observer.
NetLog::Get()->AddObserver(&observer[1], NetLogCaptureMode::kEverything);
EXPECT_EQ(NetLog::Get(), observer[0].net_log());
EXPECT_EQ(NetLog::Get(), observer[1].net_log());
EXPECT_EQ(NetLogCaptureMode::kIncludeSensitive, observer[0].capture_mode());
EXPECT_EQ(NetLogCaptureMode::kEverything, observer[1].capture_mode());
EXPECT_TRUE(NetLog::Get()->IsCapturing());
// Add event and make sure both observers receive it at their respective log
// levels.
std::optional<int> param;
AddEvent(NetLog::Get());
ASSERT_EQ(1U, observer[0].GetNumValues());
param = observer[0].GetDict(0)->FindDict("params")->FindInt("capture_mode");
ASSERT_TRUE(param);
EXPECT_EQ(CaptureModeToInt(observer[0].capture_mode()), param.value());
ASSERT_EQ(1U, observer[1].GetNumValues());
param = observer[1].GetDict(0)->FindDict("params")->FindInt("capture_mode");
ASSERT_TRUE(param);
EXPECT_EQ(CaptureModeToInt(observer[1].capture_mode()), param.value());
// Remove second observer.
NetLog::Get()->RemoveObserver(&observer[1]);
EXPECT_EQ(NetLog::Get(), observer[0].net_log());
EXPECT_EQ(nullptr, observer[1].net_log());
EXPECT_EQ(NetLogCaptureMode::kIncludeSensitive, observer[0].capture_mode());
EXPECT_TRUE(NetLog::Get()->IsCapturing());
// Add event and make sure only second observer gets it.
AddEvent(NetLog::Get());
EXPECT_EQ(2U, observer[0].GetNumValues());
EXPECT_EQ(1U, observer[1].GetNumValues());
// Remove first observer.
NetLog::Get()->RemoveObserver(&observer[0]);
EXPECT_EQ(nullptr, observer[0].net_log());
EXPECT_EQ(nullptr, observer[1].net_log());
EXPECT_FALSE(NetLog::Get()->IsCapturing());
// Add event and make sure neither observer gets it.
AddEvent(NetLog::Get());
EXPECT_EQ(2U, observer[0].GetNumValues());
EXPECT_EQ(1U, observer[1].GetNumValues());
}
// Makes sure that adding and removing observers simultaneously on different
// threads works.
TEST(NetLogTest, NetLogAddRemoveObserverThreads) {
// Run a bunch of threads to completion, each of which will repeatedly add
// and remove an observer, and set its logging level.
RunTestThreads<AddRemoveObserverTestThread>(NetLog::Get());
}
// Tests that serializing a NetLogEntry with empty parameters omits a value for
// "params".
TEST(NetLogTest, NetLogEntryToValueEmptyParams) {
// NetLogEntry with no params.
NetLogEntry entry1(NetLogEventType::REQUEST_ALIVE, NetLogSource(),
NetLogEventPhase::BEGIN, base::TimeTicks(),
base::Value::Dict());
ASSERT_TRUE(entry1.params.empty());
ASSERT_FALSE(entry1.ToDict().Find("params"));
}
} // namespace
} // namespace net
|