File: engine_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (227 lines) | stat: -rw-r--r-- 10,148 bytes parent folder | download | duplicates (7)
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
// 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 "cronet_c.h"

#include "base/check.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "components/cronet/native/test/test_util.h"
#include "net/cert/mock_cert_verifier.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const char* kUserAgent = "EngineTest/1";

class EngineTest : public ::testing::Test {
 public:
  EngineTest(const EngineTest&) = delete;
  EngineTest& operator=(const EngineTest&) = delete;

 protected:
  EngineTest() = default;
  ~EngineTest() override {}
};

TEST_F(EngineTest, StartCronetEngine) {
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EngineParams_user_agent_set(engine_params, kUserAgent);
  EXPECT_EQ(Cronet_RESULT_SUCCESS,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_Engine_Destroy(engine);
  Cronet_EngineParams_Destroy(engine_params);
}

TEST_F(EngineTest, CronetEngineDefaultUserAgent) {
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  // Version and DefaultUserAgent don't require engine start.
  std::string version = Cronet_Engine_GetVersionString(engine);
  std::string default_agent = Cronet_Engine_GetDefaultUserAgent(engine);
  EXPECT_NE(default_agent.find(version), std::string::npos);
  Cronet_Engine_Destroy(engine);
}

TEST_F(EngineTest, InitDifferentEngines) {
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EnginePtr first_engine = Cronet_Engine_Create();
  Cronet_Engine_StartWithParams(first_engine, engine_params);
  Cronet_EnginePtr second_engine = Cronet_Engine_Create();
  Cronet_Engine_StartWithParams(second_engine, engine_params);
  Cronet_EnginePtr third_engine = Cronet_Engine_Create();
  Cronet_Engine_StartWithParams(third_engine, engine_params);
  Cronet_EngineParams_Destroy(engine_params);
  Cronet_Engine_Destroy(first_engine);
  Cronet_Engine_Destroy(second_engine);
  Cronet_Engine_Destroy(third_engine);
}

TEST_F(EngineTest, StartResults) {
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  // Disable runtime CHECK of the result, so it could be verified.
  Cronet_EngineParams_enable_check_result_set(engine_params, false);
  Cronet_EngineParams_http_cache_mode_set(
      engine_params, Cronet_EngineParams_HTTP_CACHE_MODE_DISK);
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_ARGUMENT_STORAGE_PATH_MUST_EXIST,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_EngineParams_storage_path_set(engine_params, "InvalidPath");
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_ARGUMENT_STORAGE_PATH_MUST_EXIST,
            Cronet_Engine_StartWithParams(engine, engine_params));
  base::ScopedTempDir temp_dir;
  EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
  base::FilePath temp_path = base::MakeAbsoluteFilePath(temp_dir.GetPath());
  Cronet_EngineParams_storage_path_set(engine_params,
                                       temp_path.AsUTF8Unsafe().c_str());
  // Now the engine should start successfully.
  EXPECT_EQ(Cronet_RESULT_SUCCESS,
            Cronet_Engine_StartWithParams(engine, engine_params));
  // The second start should fail.
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_STATE_ENGINE_ALREADY_STARTED,
            Cronet_Engine_StartWithParams(engine, engine_params));
  // The second engine should fail because storage path is already used.
  Cronet_EnginePtr second_engine = Cronet_Engine_Create();
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_STATE_STORAGE_PATH_IN_USE,
            Cronet_Engine_StartWithParams(second_engine, engine_params));
  // Shutdown first engine to free storage path.
  EXPECT_EQ(Cronet_RESULT_SUCCESS, Cronet_Engine_Shutdown(engine));
  // Now the second engine should start.
  EXPECT_EQ(Cronet_RESULT_SUCCESS,
            Cronet_Engine_StartWithParams(second_engine, engine_params));
  Cronet_Engine_Destroy(second_engine);
  Cronet_Engine_Destroy(engine);
  Cronet_EngineParams_Destroy(engine_params);
}

TEST_F(EngineTest, InvalidPkpParams) {
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  // Disable runtime CHECK of the result, so it could be verified.
  Cronet_EngineParams_enable_check_result_set(engine_params, false);
  // Try adding invalid public key pins.
  Cronet_PublicKeyPinsPtr public_key_pins = Cronet_PublicKeyPins_Create();
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  EXPECT_EQ(Cronet_RESULT_NULL_POINTER_HOSTNAME,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_EngineParams_public_key_pins_clear(engine_params);
  // Detect long host name.
  Cronet_PublicKeyPins_host_set(public_key_pins, std::string(256, 'a').c_str());
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_ARGUMENT_INVALID_HOSTNAME,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_EngineParams_public_key_pins_clear(engine_params);
  // Detect invalid host name.
  Cronet_PublicKeyPins_host_set(public_key_pins, "invalid:host/name");
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_ARGUMENT_INVALID_HOSTNAME,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_EngineParams_public_key_pins_clear(engine_params);
  // Set valid host name.
  Cronet_PublicKeyPins_host_set(public_key_pins, "valid.host.name");
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  // Detect missing pins.
  EXPECT_EQ(Cronet_RESULT_NULL_POINTER_SHA256_PINS,
            Cronet_Engine_StartWithParams(engine, engine_params));
  // Detect invalid pin.
  Cronet_EngineParams_public_key_pins_clear(engine_params);
  Cronet_PublicKeyPins_pins_sha256_add(public_key_pins, "invalid_sha256");
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  EXPECT_EQ(Cronet_RESULT_ILLEGAL_ARGUMENT_INVALID_PIN,
            Cronet_Engine_StartWithParams(engine, engine_params));
  // THe engine cannot start with these params, and have to be destroyed.
  Cronet_Engine_Destroy(engine);
  Cronet_EngineParams_Destroy(engine_params);
  Cronet_PublicKeyPins_Destroy(public_key_pins);
}

TEST_F(EngineTest, ValidPkpParams) {
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  // Disable runtime CHECK of the result, so it could be verified.
  Cronet_EngineParams_enable_check_result_set(engine_params, false);
  // Add valid public key pins.
  Cronet_PublicKeyPinsPtr public_key_pins = Cronet_PublicKeyPins_Create();
  Cronet_PublicKeyPins_host_set(public_key_pins, "valid.host.name");
  Cronet_PublicKeyPins_pins_sha256_add(
      public_key_pins, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
  Cronet_EngineParams_public_key_pins_add(engine_params, public_key_pins);
  // The engine should start successfully.
  EXPECT_EQ(Cronet_RESULT_SUCCESS,
            Cronet_Engine_StartWithParams(engine, engine_params));
  Cronet_Engine_Destroy(engine);
  Cronet_EngineParams_Destroy(engine_params);
  Cronet_PublicKeyPins_Destroy(public_key_pins);
}

// Verify that Cronet_Engine_SetMockCertVerifierForTesting() doesn't crash or
// leak anything.
TEST_F(EngineTest, SetMockCertVerifierForTesting) {
  auto cert_verifier(std::make_unique<net::MockCertVerifier>());
  Cronet_EnginePtr engine = Cronet_Engine_Create();
  Cronet_Engine_SetMockCertVerifierForTesting(engine, cert_verifier.release());
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_Engine_StartWithParams(engine, engine_params);
  Cronet_Engine_Destroy(engine);
  Cronet_EngineParams_Destroy(engine_params);
}

TEST_F(EngineTest, StartNetLogToFile) {
  base::ScopedTempDir temp_dir;
  EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
  base::FilePath temp_path = base::MakeAbsoluteFilePath(temp_dir.GetPath());
  base::FilePath net_log_file =
      temp_path.Append(FILE_PATH_LITERAL("netlog.json"));

  Cronet_EnginePtr engine = Cronet_Engine_Create();
  Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
  Cronet_EngineParams_experimental_options_set(
      engine_params,
      "{ \"QUIC\" : {\"max_server_configs_stored_in_properties\" : 8} }");
  // Test that net log cannot start/stop before engine start.
  EXPECT_FALSE(Cronet_Engine_StartNetLogToFile(
      engine, net_log_file.AsUTF8Unsafe().c_str(), true));
  Cronet_Engine_StopNetLog(engine);

  // Start the engine.
  Cronet_Engine_StartWithParams(engine, engine_params);
  Cronet_EngineParams_Destroy(engine_params);

  // Test that normal start/stop net log works.
  EXPECT_TRUE(Cronet_Engine_StartNetLogToFile(
      engine, net_log_file.AsUTF8Unsafe().c_str(), true));
  Cronet_Engine_StopNetLog(engine);

  // Test that double start/stop net log works.
  EXPECT_TRUE(Cronet_Engine_StartNetLogToFile(
      engine, net_log_file.AsUTF8Unsafe().c_str(), true));
  // Test that second start fails.
  EXPECT_FALSE(Cronet_Engine_StartNetLogToFile(
      engine, net_log_file.AsUTF8Unsafe().c_str(), true));
  // Test that multiple stops work.
  Cronet_Engine_StopNetLog(engine);
  Cronet_Engine_StopNetLog(engine);
  Cronet_Engine_StopNetLog(engine);

  // Test that net log contains effective experimental options.
  std::string net_log;
  EXPECT_TRUE(base::ReadFileToString(net_log_file, &net_log));
  EXPECT_TRUE(
      net_log.find(
          "{\"QUIC\":{\"max_server_configs_stored_in_properties\":8}") !=
      std::string::npos);

  // Test that bad file name fails.
  EXPECT_FALSE(Cronet_Engine_StartNetLogToFile(engine, "bad/file/name", true));

  Cronet_Engine_Shutdown(engine);
  // Test that net log cannot start/stop after engine shutdown.
  EXPECT_FALSE(Cronet_Engine_StartNetLogToFile(
      engine, net_log_file.AsUTF8Unsafe().c_str(), true));
  Cronet_Engine_StopNetLog(engine);
  Cronet_Engine_Destroy(engine);
}

}  // namespace