File: http_response_info_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (321 lines) | stat: -rw-r--r-- 13,424 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 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/http/http_response_info.h"

#include "base/pickle.h"
#include "net/base/proxy_chain.h"
#include "net/cert/signed_certificate_timestamp.h"
#include "net/cert/signed_certificate_timestamp_and_status.h"
#include "net/http/http_response_headers.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/test/cert_test_util.h"
#include "net/test/ct_test_util.h"
#include "net/test/test_data_directory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

class HttpResponseInfoTest : public testing::Test {
 protected:
  void SetUp() override {
    response_info_.headers = base::MakeRefCounted<HttpResponseHeaders>("");
  }

  void PickleAndRestore(const HttpResponseInfo& response_info,
                        HttpResponseInfo* restored_response_info) const {
    std::unique_ptr<base::Pickle> pickle = response_info.MakePickle(
        /*skip_transient_headers=*/false, /*response_truncated=*/false);
    bool truncated = false;
    EXPECT_TRUE(restored_response_info->InitFromPickle(*pickle, &truncated));
  }

  HttpResponseInfo response_info_;
};

TEST_F(HttpResponseInfoTest, UnusedSincePrefetchDefault) {
  EXPECT_FALSE(response_info_.unused_since_prefetch);
}

TEST_F(HttpResponseInfoTest, UnusedSincePrefetchCopy) {
  response_info_.unused_since_prefetch = true;
  HttpResponseInfo response_info_clone(response_info_);
  EXPECT_TRUE(response_info_clone.unused_since_prefetch);
}

TEST_F(HttpResponseInfoTest, UnusedSincePrefetchPersistFalse) {
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_FALSE(restored_response_info.unused_since_prefetch);
}

TEST_F(HttpResponseInfoTest, UnusedSincePrefetchPersistTrue) {
  response_info_.unused_since_prefetch = true;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.unused_since_prefetch);
}

TEST_F(HttpResponseInfoTest, ProxyChainDefault) {
  EXPECT_FALSE(response_info_.proxy_chain.IsValid());
  EXPECT_FALSE(response_info_.WasFetchedViaProxy());
}

TEST_F(HttpResponseInfoTest, ProxyChainCopy) {
  response_info_.proxy_chain =
      ProxyChain::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTP, "foo", 80);
  HttpResponseInfo response_info_clone(response_info_);
  EXPECT_TRUE(response_info_clone.proxy_chain.IsValid());
  EXPECT_TRUE(response_info_clone.WasFetchedViaProxy());
}

TEST_F(HttpResponseInfoTest, ProxyChainPersistDirect) {
  response_info_.proxy_chain = ProxyChain::Direct();
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.proxy_chain.IsValid());
  EXPECT_FALSE(restored_response_info.WasFetchedViaProxy());
}

TEST_F(HttpResponseInfoTest, ProxyChainPersistProxy) {
  response_info_.proxy_chain =
      ProxyChain::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTP, "foo", 80);
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.proxy_chain.IsValid());
  EXPECT_TRUE(restored_response_info.WasFetchedViaProxy());
}

TEST_F(HttpResponseInfoTest, PKPBypassPersistTrue) {
  response_info_.ssl_info.pkp_bypassed = true;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.ssl_info.pkp_bypassed);
}

TEST_F(HttpResponseInfoTest, PKPBypassPersistFalse) {
  response_info_.ssl_info.pkp_bypassed = false;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_FALSE(restored_response_info.ssl_info.pkp_bypassed);
}

TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedDefault) {
  EXPECT_FALSE(response_info_.async_revalidation_requested);
}

TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedCopy) {
  response_info_.async_revalidation_requested = true;
  HttpResponseInfo response_info_clone(response_info_);
  EXPECT_TRUE(response_info_clone.async_revalidation_requested);
}

TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedAssign) {
  response_info_.async_revalidation_requested = true;
  HttpResponseInfo response_info_clone;
  response_info_clone = response_info_;
  EXPECT_TRUE(response_info_clone.async_revalidation_requested);
}

TEST_F(HttpResponseInfoTest, AsyncRevalidationRequestedNotPersisted) {
  response_info_.async_revalidation_requested = true;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_FALSE(restored_response_info.async_revalidation_requested);
}

TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutDefault) {
  EXPECT_TRUE(response_info_.stale_revalidate_timeout.is_null());
}

TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutCopy) {
  base::Time test_time = base::Time::FromSecondsSinceUnixEpoch(1000);
  response_info_.stale_revalidate_timeout = test_time;
  HttpResponseInfo response_info_clone(response_info_);
  EXPECT_EQ(test_time, response_info_clone.stale_revalidate_timeout);
}

TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutRestoreValue) {
  base::Time test_time = base::Time::FromSecondsSinceUnixEpoch(1000);
  response_info_.stale_revalidate_timeout = test_time;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(test_time, restored_response_info.stale_revalidate_timeout);
}

TEST_F(HttpResponseInfoTest, StaleRevalidationTimeoutRestoreNoValue) {
  EXPECT_TRUE(response_info_.stale_revalidate_timeout.is_null());
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.stale_revalidate_timeout.is_null());
}

// Test that key_exchange_group is preserved for ECDHE ciphers.
TEST_F(HttpResponseInfoTest, KeyExchangeGroupECDHE) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
                                &response_info_.ssl_info.connection_status);
  SSLConnectionStatusSetCipherSuite(
      0xcca8 /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */,
      &response_info_.ssl_info.connection_status);
  response_info_.ssl_info.key_exchange_group = 23;  // secp256r1
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(23, restored_response_info.ssl_info.key_exchange_group);
}

// Test that key_exchange_group is preserved for TLS 1.3.
TEST_F(HttpResponseInfoTest, KeyExchangeGroupTLS13) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_3,
                                &response_info_.ssl_info.connection_status);
  SSLConnectionStatusSetCipherSuite(0x1303 /* TLS_CHACHA20_POLY1305_SHA256 */,
                                    &response_info_.ssl_info.connection_status);
  response_info_.ssl_info.key_exchange_group = 23;  // secp256r1
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(23, restored_response_info.ssl_info.key_exchange_group);
}

// Test that key_exchange_group is discarded for non-ECDHE ciphers prior to TLS
// 1.3, to account for the historical key_exchange_info field. See
// https://crbug.com/639421.
TEST_F(HttpResponseInfoTest, LegacyKeyExchangeInfoDHE) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
                                &response_info_.ssl_info.connection_status);
  SSLConnectionStatusSetCipherSuite(
      0x0093 /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */,
      &response_info_.ssl_info.connection_status);
  response_info_.ssl_info.key_exchange_group = 1024;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(0, restored_response_info.ssl_info.key_exchange_group);
}

// Test that key_exchange_group is discarded for unknown ciphers prior to TLS
// 1.3, to account for the historical key_exchange_info field. See
// https://crbug.com/639421.
TEST_F(HttpResponseInfoTest, LegacyKeyExchangeInfoUnknown) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
                                &response_info_.ssl_info.connection_status);
  SSLConnectionStatusSetCipherSuite(0xffff,
                                    &response_info_.ssl_info.connection_status);
  response_info_.ssl_info.key_exchange_group = 1024;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(0, restored_response_info.ssl_info.key_exchange_group);
}

// Test that peer_signature_algorithm is preserved.
TEST_F(HttpResponseInfoTest, PeerSignatureAlgorithm) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  response_info_.ssl_info.peer_signature_algorithm =
      0x0804;  // rsa_pss_rsae_sha256
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(0x0804, restored_response_info.ssl_info.peer_signature_algorithm);
}

// Test that encrypted_client_hello is preserved.
TEST_F(HttpResponseInfoTest, EncryptedClientHello) {
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
  {
    HttpResponseInfo restored_response_info;
    PickleAndRestore(response_info_, &restored_response_info);
    EXPECT_FALSE(restored_response_info.ssl_info.encrypted_client_hello);
  }

  response_info_.ssl_info.encrypted_client_hello = true;
  {
    HttpResponseInfo restored_response_info;
    PickleAndRestore(response_info_, &restored_response_info);
    EXPECT_TRUE(restored_response_info.ssl_info.encrypted_client_hello);
  }
}

// Tests that cache entries loaded over SSLv3 (no longer supported) are dropped.
TEST_F(HttpResponseInfoTest, FailsInitFromPickleWithSSLV3) {
  // A valid certificate is needed for ssl_info.is_valid() to be true.
  response_info_.ssl_info.cert =
      ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");

  // Non-SSLv3 versions should succeed.
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
                                &response_info_.ssl_info.connection_status);
  std::unique_ptr<base::Pickle> tls12_pickle = response_info_.MakePickle(
      /*skip_transient_headers=*/false, /*response_truncated=*/false);
  bool truncated = false;
  HttpResponseInfo restored_tls12_response_info;
  EXPECT_TRUE(
      restored_tls12_response_info.InitFromPickle(*tls12_pickle, &truncated));
  EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2,
            SSLConnectionStatusToVersion(
                restored_tls12_response_info.ssl_info.connection_status));
  EXPECT_FALSE(truncated);

  // SSLv3 should fail.
  SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_SSL3,
                                &response_info_.ssl_info.connection_status);
  std::unique_ptr<base::Pickle> ssl3_pickle = response_info_.MakePickle(
      /*skip_transient_headers=*/false, /*response_truncated=*/false);
  HttpResponseInfo restored_ssl3_response_info;
  EXPECT_FALSE(
      restored_ssl3_response_info.InitFromPickle(*ssl3_pickle, &truncated));
}

// Test that `dns_aliases` is preserved.
TEST_F(HttpResponseInfoTest, DnsAliases) {
  response_info_.dns_aliases = {"alias1", "alias2", "alias3"};
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_THAT(restored_response_info.dns_aliases,
              testing::ElementsAre("alias1", "alias2", "alias3"));
}

// Test that an empty `dns_aliases` is preserved and doesn't throw an error.
TEST_F(HttpResponseInfoTest, EmptyDnsAliases) {
  response_info_.dns_aliases = {};
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.dns_aliases.empty());
}

// Test that `browser_run_id` is preserved.
TEST_F(HttpResponseInfoTest, BrowserRunId) {
  response_info_.browser_run_id = 1;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_EQ(1, restored_response_info.browser_run_id);
}

// Test that an empty `browser_run_id` is preserved and doesn't throw an error.
TEST_F(HttpResponseInfoTest, EmptyBrowserRunId) {
  response_info_.browser_run_id = std::nullopt;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_FALSE(restored_response_info.browser_run_id.has_value());
}

// Test that did_use_shared_dictionary is preserved .
TEST_F(HttpResponseInfoTest, DidUseSharedDictionary) {
  response_info_.did_use_shared_dictionary = true;
  HttpResponseInfo restored_response_info;
  PickleAndRestore(response_info_, &restored_response_info);
  EXPECT_TRUE(restored_response_info.did_use_shared_dictionary);
}

}  // namespace

}  // namespace net