File: log_net_log_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (305 lines) | stat: -rw-r--r-- 10,921 bytes parent folder | download | duplicates (3)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>
#include <string_view>

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/path_service.h"
#include "base/threading/scoped_blocking_call.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "services/network/public/cpp/network_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chrome_browser_net {
namespace {

// Test fixture for running tests with --log-net-log with no explicit file
// specified.
class LogNetLogTest : public InProcessBrowserTest {
 public:
  LogNetLogTest() = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(network::switches::kLogNetLog);
  }

  void TearDownInProcessBrowserTestFixture() override { VerifyNetLog(); }

 private:
  // Verify that the netlog file was written to the user data dir.
  void VerifyNetLog() {
    base::FilePath user_data_dir;
    ASSERT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
    auto net_log_path = user_data_dir.AppendASCII("netlog.json");

    // Read the netlog from disk.
    std::string file_contents;
    ASSERT_TRUE(base::ReadFileToString(net_log_path, &file_contents))
        << "Could not read: " << net_log_path;

    // Parse it as JSON.
    auto parsed = base::JSONReader::Read(file_contents);
    EXPECT_TRUE(parsed);

    // Detailed checking is done by LogNetLogExplicitFileTest, so this test just
    // accepts any valid JSON.
  }
};

IN_PROC_BROWSER_TEST_F(LogNetLogTest, Exists) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/simple.html"));
  EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
}

// Test fixture for running tests with --log-net-log, and a parameterized value
// for --net-log-capture-mode.
//
// Asserts that a netlog file was created, appears valid, and stripped cookies
// in accordance to the --net-log-capture-mode flag.
class LogNetLogExplicitFileTest
    : public InProcessBrowserTest,
      public testing::WithParamInterface<const char*> {
 public:
  LogNetLogExplicitFileTest() = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
    net_log_path_ = tmp_dir_.GetPath().AppendASCII("netlog.json");

    command_line->AppendSwitchPath(network::switches::kLogNetLog,
                                   net_log_path_);

    if (GetParam()) {
      command_line->AppendSwitchASCII(network::switches::kNetLogCaptureMode,
                                      GetParam());
    }
  }

  void TearDownInProcessBrowserTestFixture() override { VerifyNetLog(); }

 private:
  // Verify that the netlog file was written, appears to be well formed, and
  // includes the requested level of data.
  void VerifyNetLog() {
    // Read the netlog from disk.
    std::string file_contents;
    ASSERT_TRUE(base::ReadFileToString(net_log_path_, &file_contents))
        << "Could not read: " << net_log_path_;

    // Parse it as JSON.
    auto parsed = base::JSONReader::Read(file_contents);
    ASSERT_TRUE(parsed);

    // Ensure the root value is a dictionary.
    ASSERT_TRUE(parsed->is_dict());
    const base::Value::Dict& main = parsed->GetDict();

    // Ensure it has a "constants" property.
    const base::Value::Dict* constants = main.FindDict("constants");
    ASSERT_TRUE(constants);
    ASSERT_FALSE(constants->empty());

    // Ensure it has an "events" property.
    const base::Value::List* events = main.FindList("events");
    ASSERT_TRUE(events);
    ASSERT_FALSE(events->empty());

    // Verify that cookies were stripped when the --net-log-capture-mode flag
    // was omitted, and not stripped when it was given a value of
    // IncludeSensitive
    bool include_cookies =
        GetParam() && std::string_view(GetParam()) == "IncludeSensitive";

    if (include_cookies) {
      EXPECT_TRUE(file_contents.find("Set-Cookie: name=Good;Max-Age=3600") !=
                  std::string::npos);
    } else {
      EXPECT_TRUE(file_contents.find("Set-Cookie: [22 bytes were stripped]") !=
                  std::string::npos);
    }
  }

  base::FilePath net_log_path_;
  base::ScopedTempDir tmp_dir_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         LogNetLogExplicitFileTest,
                         ::testing::Values(nullptr, "IncludeSensitive"));

IN_PROC_BROWSER_TEST_P(LogNetLogExplicitFileTest, Basic) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/set_cookie_header.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
}

// Test fixture for NetLog with invalid duration values.
//
// Tests handling of invalid values for the --net-log-duration flag.
// This ensures that when invalid duration values are provided,browser continues
// to function properly by:
// 1. Successfully creating a NetLog file
// 2. Continuing to log network activity throughout the browser session
// 3. Generating a properly formatted JSON log file
//
// The test operates by setting various invalid duration values, performing
// network operations, and verifying the NetLog continues to function rather
// than failing or stopping prematurely.
class LogNetLogInvalidDurationTest
    : public InProcessBrowserTest,
      public testing::WithParamInterface<const char*> {
 public:
  LogNetLogInvalidDurationTest() = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    net_log_path_ =
        temp_dir_.GetPath().Append(FILE_PATH_LITERAL("netlog.json"));

    // Add the NetLog path
    command_line->AppendSwitchPath(network::switches::kLogNetLog,
                                   net_log_path_);
    command_line->AppendSwitchASCII(network::switches::kLogNetLogDuration,
                                    GetParam());
    command_line->AppendSwitchASCII(network::switches::kNetLogCaptureMode,
                                    "Default");
  }

  void TearDownInProcessBrowserTestFixture() override {
    // Verify the log file exists and is valid
    std::string file_contents;
    ASSERT_TRUE(base::ReadFileToString(net_log_path_, &file_contents))
        << "Could not read: " << net_log_path_;

    // Parse it as JSON
    std::optional<base::Value> log_value =
        base::JSONReader::Read(file_contents);
    ASSERT_TRUE(log_value.has_value());
    EXPECT_TRUE(log_value->is_dict());
  }

 private:
  base::ScopedTempDir temp_dir_;
  base::FilePath net_log_path_;
};

// Test cases: empty string, non-integer value, zero value, negative value
INSTANTIATE_TEST_SUITE_P(InvalidDurations,
                         LogNetLogInvalidDurationTest,
                         ::testing::Values("", "abc", "0", "-5"));

IN_PROC_BROWSER_TEST_P(LogNetLogInvalidDurationTest, InvalidDurationHandling) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // Generate some network activity
  GURL url(embedded_test_server()->GetURL("/simple.html"));
  EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  // Generate more traffic to verify NetLog is still active
  url = embedded_test_server()->GetURL("/title1.html");
  EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  // NetLog should continue until browser shutdown
  // Verification of log file happens in TearDownInProcessBrowserTestFixture
}

// Test fixture for NetLog with a valid duration value.
//
// Tests the --net-log-duration flag with a valid positive integer value.
// This ensures that when a valid duration is provided, the NetLog system:
// 1. Properly starts capturing network events
// 2. Automatically stops capturing after the specified duration (1 second)
// 3. Generates a valid JSON file containing the captured events
//
// The test operates by:
// - Setting up a 1-second NetLog duration
// - Performing network operations to generate capturable events
// - Waiting for the duration to complete
// - Polling to verify a valid JSON file appears
class LogNetLogValidDurationTest : public InProcessBrowserTest {
 public:
  LogNetLogValidDurationTest() = default;
  // Polling interval when waiting for the NetLog file to be written
  static constexpr base::TimeDelta kPollInterval = base::Milliseconds(10);
  // Maximum number of polling attempts (total wait time = kPollInterval *
  // kMaxPollAttempts)
  static constexpr int kMaxPollAttempts = 200;  // 2 seconds total

  void SetUpCommandLine(base::CommandLine* command_line) override {
    // Create a temp directory for storing our netlog file.
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    net_log_path_ =
        temp_dir_.GetPath().Append(FILE_PATH_LITERAL("netlog_valid.json"));

    // Specify a 1-second NetLog duration.
    command_line->AppendSwitchPath(network::switches::kLogNetLog,
                                   net_log_path_);
    command_line->AppendSwitchASCII(network::switches::kLogNetLogDuration, "1");
    command_line->AppendSwitchASCII(network::switches::kNetLogCaptureMode,
                                    "Default");
  }

  bool LogFileExistsAndIsValidJson() {
    // Allow blocking file I/O in this test method:
    base::ScopedAllowBlockingForTesting allow_blocking;

    if (!base::PathExists(net_log_path_)) {
      return false;
    }

    std::string file_contents;
    if (!base::ReadFileToString(net_log_path_, &file_contents)) {
      return false;
    }

    std::optional<base::Value> parsed_json =
        base::JSONReader::Read(file_contents);
    if (!parsed_json.has_value() || !parsed_json->is_dict()) {
      return false;
    }
    return true;
  }
  void RunLoopFor(base::TimeDelta duration) {
    base::RunLoop run_loop;
    base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(), duration);
    run_loop.Run();
  }

 private:
  base::ScopedTempDir temp_dir_;
  base::FilePath net_log_path_;
};

// This test confirms that the NetLog stops after 1 second and a valid JSON file
// eventually appears on disk.
IN_PROC_BROWSER_TEST_F(LogNetLogValidDurationTest, SucceedsWithOneSecond) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // Wait ~1 second (the NetLog's duration).
  RunLoopFor(base::Seconds(1));

  // Now poll until the file is written and valid JSON, or we time out.
  bool success = false;
  for (int i = 0; i < kMaxPollAttempts; ++i) {
    if (LogFileExistsAndIsValidJson()) {
      success = true;
      break;
    }
    RunLoopFor(kPollInterval);
  }

  EXPECT_TRUE(success);
}
}  // namespace

}  // namespace chrome_browser_net