blob: 3627616fdb35d3d520aa4b70b550084a2fc96525 [file] [log] [blame]
xunjielif24ee5f2015-11-23 18:05:261// Copyright 2015 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
5#include "components/cronet/url_request_context_config.h"
6
mmenke51629db12017-06-28 13:34:127#include "base/test/scoped_task_environment.h"
tfhef3618f2016-01-11 23:07:088#include "base/values.h"
pauljensen9041eb3c2015-12-09 12:29:019#include "net/cert/cert_verifier.h"
xunjielif24ee5f2015-11-23 18:05:2610#include "net/http/http_network_session.h"
mikecironef22f9812016-10-04 03:40:1911#include "net/log/net_log.h"
12#include "net/log/net_log_with_source.h"
xunjielif24ee5f2015-11-23 18:05:2613#include "net/proxy/proxy_config.h"
14#include "net/proxy/proxy_config_service_fixed.h"
15#include "net/url_request/url_request_context.h"
16#include "net/url_request/url_request_context_builder.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace cronet {
20
mmenke51629db12017-06-28 13:34:1221TEST(URLRequestContextConfigTest, TestExperimentalOptionParsing) {
22 base::test::ScopedTaskEnvironment scoped_task_environment_(
23 base::test::ScopedTaskEnvironment::MainThreadType::IO);
24
pauljensen9041eb3c2015-12-09 12:29:0125 URLRequestContextConfig config(
26 // Enable QUIC.
27 true,
mefc5da5712016-02-09 20:14:2328 // QUIC User Agent ID.
29 "Default QUIC User Agent ID",
pauljensen9041eb3c2015-12-09 12:29:0130 // Enable SPDY.
31 true,
xunjieli186d2bf2017-04-18 13:45:4732 // Enable Brotli.
33 false,
pauljensen9041eb3c2015-12-09 12:29:0134 // Type of http cache.
35 URLRequestContextConfig::HttpCacheType::DISK,
36 // Max size of http cache in bytes.
37 1024000,
38 // Disable caching for HTTP responses. Other information may be stored in
39 // the cache.
40 false,
41 // Storage path for http cache and cookie storage.
42 "/data/data/org.chromium.net/app_cronet_test/test_storage",
43 // User-Agent request header field.
44 "fake agent",
45 // JSON encoded experimental options.
rtenneti6971c172016-01-15 20:12:1046 "{\"QUIC\":{\"max_server_configs_stored_in_properties\":2,"
mefc5da5712016-02-09 20:14:2347 "\"user_agent_id\":\"Custom QUIC UAID\","
rtenneti64e809d02015-12-11 00:26:2048 "\"idle_connection_timeout_seconds\":300,"
Jana Iyengar903dec22017-11-28 00:44:2349 "\"close_sessions_on_ip_change\":true,"
rtennetid073dd22016-08-04 01:58:3350 "\"race_cert_verification\":true,"
pauljensene92c4092015-12-09 19:13:4851 "\"connection_options\":\"TIME,TBBR,REJ\"},"
mgershaf2c12c2016-08-22 16:33:5452 "\"AsyncDNS\":{\"enable\":true},"
xunjielid67295e2017-03-16 21:05:4153 "\"UnknownOption\":{\"foo\":true},"
mgershaf2c12c2016-08-22 16:33:5454 "\"HostResolverRules\":{\"host_resolver_rules\":"
mgershb3fe8082017-02-28 20:09:2055 "\"MAP * 127.0.0.1\"},"
56 // See http://crbug.com/696569.
mgershaf9a9232017-04-13 20:19:0357 "\"disable_ipv6_on_wifi\":true}",
pauljensen9041eb3c2015-12-09 12:29:0158 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:3959 std::unique_ptr<net::CertVerifier>(),
60 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:0261 false,
62 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:2863 true,
64 // Certificate verifier cache data.
65 "");
xunjielif24ee5f2015-11-23 18:05:2666
xunjielif24ee5f2015-11-23 18:05:2667 net::URLRequestContextBuilder builder;
pauljensene92c4092015-12-09 19:13:4868 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:0269 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
xunjielid67295e2017-03-16 21:05:4170 EXPECT_FALSE(config.effective_experimental_options->HasKey("UnknownOption"));
xunjielif24ee5f2015-11-23 18:05:2671 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:1072 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:3073 std::make_unique<net::ProxyConfigServiceFixed>(
ricea85ec57952016-08-31 09:34:1074 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:5875 std::unique_ptr<net::URLRequestContext> context(builder.Build());
xunjielif24ee5f2015-11-23 18:05:2676 const net::HttpNetworkSession::Params* params =
77 context->GetNetworkSessionParams();
78 // Check Quic Connection options.
79 net::QuicTagVector quic_connection_options;
80 quic_connection_options.push_back(net::kTIME);
81 quic_connection_options.push_back(net::kTBBR);
82 quic_connection_options.push_back(net::kREJ);
83 EXPECT_EQ(quic_connection_options, params->quic_connection_options);
84
mefc5da5712016-02-09 20:14:2385 // Check Custom QUIC User Agent Id.
86 EXPECT_EQ("Custom QUIC UAID", params->quic_user_agent_id);
87
rtenneti6971c172016-01-15 20:12:1088 // Check max_server_configs_stored_in_properties.
89 EXPECT_EQ(2u, params->quic_max_server_configs_stored_in_properties);
xunjielif24ee5f2015-11-23 18:05:2690
rtenneti64e809d02015-12-11 00:26:2091 // Check idle_connection_timeout_seconds.
92 EXPECT_EQ(300, params->quic_idle_connection_timeout_seconds);
93
Jana Iyengar903dec22017-11-28 00:44:2394 EXPECT_TRUE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:3695 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change);
Zhongyi Shi64795622017-11-20 02:21:4996 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:2897 EXPECT_FALSE(params->quic_migrate_sessions_early_v2);
jrid26566952016-02-04 21:06:4298
rtennetid073dd22016-08-04 01:58:3399 // Check race_cert_verification.
100 EXPECT_TRUE(params->quic_race_cert_verification);
101
Misha Efimov63957912017-12-06 07:13:26102#if defined(ENABLE_BUILT_IN_DNS)
103 // Check AsyncDNS resolver is enabled (not supported on iOS).
tfhef3618f2016-01-11 23:07:08104 EXPECT_TRUE(context->host_resolver()->GetDnsConfigAsValue());
Misha Efimov63957912017-12-06 07:13:26105#endif // defined(ENABLE_BUILT_IN_DNS)
mgershaf2c12c2016-08-22 16:33:54106
mgershaf9a9232017-04-13 20:19:03107 // Check IPv6 is disabled when on wifi.
108 EXPECT_TRUE(context->host_resolver()->GetNoIPv6OnWifi());
mgershb3fe8082017-02-28 20:09:20109
mgershaf2c12c2016-08-22 16:33:54110 net::HostResolver::RequestInfo info(net::HostPortPair("abcde", 80));
111 net::AddressList addresses;
112 EXPECT_EQ(net::OK, context->host_resolver()->ResolveFromCache(
tfarina428341112016-09-22 13:38:20113 info, &addresses, net::NetLogWithSource()));
xunjielif24ee5f2015-11-23 18:05:26114}
115
mmenke51629db12017-06-28 13:34:12116TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationOptions) {
117 base::test::ScopedTaskEnvironment scoped_task_environment_(
118 base::test::ScopedTaskEnvironment::MainThreadType::IO);
119
jric69a88422016-02-12 19:48:36120 URLRequestContextConfig config(
121 // Enable QUIC.
122 true,
123 // QUIC User Agent ID.
124 "Default QUIC User Agent ID",
125 // Enable SPDY.
126 true,
xunjieli186d2bf2017-04-18 13:45:47127 // Enable Brotli.
128 false,
jric69a88422016-02-12 19:48:36129 // Type of http cache.
130 URLRequestContextConfig::HttpCacheType::DISK,
131 // Max size of http cache in bytes.
132 1024000,
133 // Disable caching for HTTP responses. Other information may be stored in
134 // the cache.
135 false,
136 // Storage path for http cache and cookie storage.
137 "/data/data/org.chromium.net/app_cronet_test/test_storage",
138 // User-Agent request header field.
139 "fake agent",
140 // JSON encoded experimental options.
jrie68df0e2016-02-12 22:01:42141 "{\"QUIC\":{\"migrate_sessions_on_network_change\":true,"
142 "\"migrate_sessions_early\":true}}",
jric69a88422016-02-12 19:48:36143 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:39144 std::unique_ptr<net::CertVerifier>(),
145 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:02146 false,
147 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:28148 true,
149 // Certificate verifier cache data.
150 "");
jric69a88422016-02-12 19:48:36151
152 net::URLRequestContextBuilder builder;
153 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:02154 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
jric69a88422016-02-12 19:48:36155 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:10156 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30157 std::make_unique<net::ProxyConfigServiceFixed>(
ricea85ec57952016-08-31 09:34:10158 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:58159 std::unique_ptr<net::URLRequestContext> context(builder.Build());
jric69a88422016-02-12 19:48:36160 const net::HttpNetworkSession::Params* params =
161 context->GetNetworkSessionParams();
162
Jana Iyengar903dec22017-11-28 00:44:23163 EXPECT_FALSE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:36164 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change);
jrie68df0e2016-02-12 22:01:42165 EXPECT_TRUE(params->quic_migrate_sessions_early);
jric69a88422016-02-12 19:48:36166}
167
Yixin Wang10f477ed2017-11-21 04:20:20168TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationV2Options) {
Zhongyi Shi64795622017-11-20 02:21:49169 base::test::ScopedTaskEnvironment scoped_task_environment_(
170 base::test::ScopedTaskEnvironment::MainThreadType::IO);
171
172 URLRequestContextConfig config(
173 // Enable QUIC.
174 true,
175 // QUIC User Agent ID.
176 "Default QUIC User Agent ID",
177 // Enable SPDY.
178 true,
179 // Enable Brotli.
180 false,
181 // Type of http cache.
182 URLRequestContextConfig::HttpCacheType::DISK,
183 // Max size of http cache in bytes.
184 1024000,
185 // Disable caching for HTTP responses. Other information may be stored in
186 // the cache.
187 false,
188 // Storage path for http cache and cookie storage.
189 "/data/data/org.chromium.net/app_cronet_test/test_storage",
190 // User-Agent request header field.
191 "fake agent",
192 // JSON encoded experimental options.
Zhongyi Shif4683a32017-12-01 00:03:28193 "{\"QUIC\":{\"migrate_sessions_on_network_change_v2\":true,"
194 "\"migrate_sessions_early_v2\":true}}",
Zhongyi Shi64795622017-11-20 02:21:49195 // MockCertVerifier to use for testing purposes.
196 std::unique_ptr<net::CertVerifier>(),
197 // Enable network quality estimator.
198 false,
199 // Enable Public Key Pinning bypass for local trust anchors.
200 true,
201 // Certificate verifier cache data.
202 "");
203
204 net::URLRequestContextBuilder builder;
205 net::NetLog net_log;
206 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
207 // Set a ProxyConfigService to avoid DCHECK failure when building.
208 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30209 std::make_unique<net::ProxyConfigServiceFixed>(
Zhongyi Shi64795622017-11-20 02:21:49210 net::ProxyConfig::CreateDirect()));
211 std::unique_ptr<net::URLRequestContext> context(builder.Build());
212 const net::HttpNetworkSession::Params* params =
213 context->GetNetworkSessionParams();
214
215 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:28216 EXPECT_TRUE(params->quic_migrate_sessions_early_v2);
Zhongyi Shi64795622017-11-20 02:21:49217}
218
Yixin Wang10f477ed2017-11-21 04:20:20219TEST(URLRequestContextConfigTest, SetQuicHostWhitelist) {
220 base::test::ScopedTaskEnvironment scoped_task_environment_(
221 base::test::ScopedTaskEnvironment::MainThreadType::IO);
222
223 URLRequestContextConfig config(
224 // Enable QUIC.
225 true,
226 // QUIC User Agent ID.
227 "Default QUIC User Agent ID",
228 // Enable SPDY.
229 true,
230 // Enable Brotli.
231 false,
232 // Type of http cache.
233 URLRequestContextConfig::HttpCacheType::DISK,
234 // Max size of http cache in bytes.
235 1024000,
236 // Disable caching for HTTP responses. Other information may be stored in
237 // the cache.
238 false,
239 // Storage path for http cache and cookie storage.
240 "/data/data/org.chromium.net/app_cronet_test/test_storage",
241 // User-Agent request header field.
242 "fake agent",
243 // JSON encoded experimental options.
244 "{\"QUIC\":{\"host_whitelist\":\"www.example.com,www.example.org\"}}",
245 // MockCertVerifier to use for testing purposes.
246 std::unique_ptr<net::CertVerifier>(),
247 // Enable network quality estimator.
248 false,
249 // Enable Public Key Pinning bypass for local trust anchors.
250 true,
251 // Certificate verifier cache data.
252 "");
253
254 net::URLRequestContextBuilder builder;
255 net::NetLog net_log;
256 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
257 // Set a ProxyConfigService to avoid DCHECK failure when building.
258 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30259 std::make_unique<net::ProxyConfigServiceFixed>(
Yixin Wang10f477ed2017-11-21 04:20:20260 net::ProxyConfig::CreateDirect()));
261 std::unique_ptr<net::URLRequestContext> context(builder.Build());
262 const net::HttpNetworkSession::Params* params =
263 context->GetNetworkSessionParams();
264
265 EXPECT_TRUE(
266 base::ContainsKey(params->quic_host_whitelist, "www.example.com"));
267 EXPECT_TRUE(
268 base::ContainsKey(params->quic_host_whitelist, "www.example.org"));
269}
270
Yixin Wang983875152017-11-21 20:30:13271TEST(URLRequestContextConfigTest, SetQuicMaxTimeBeforeCryptoHandshake) {
272 base::test::ScopedTaskEnvironment scoped_task_environment_(
273 base::test::ScopedTaskEnvironment::MainThreadType::IO);
274
275 URLRequestContextConfig config(
276 // Enable QUIC.
277 true,
278 // QUIC User Agent ID.
279 "Default QUIC User Agent ID",
280 // Enable SPDY.
281 true,
282 // Enable Brotli.
283 false,
284 // Type of http cache.
285 URLRequestContextConfig::HttpCacheType::DISK,
286 // Max size of http cache in bytes.
287 1024000,
288 // Disable caching for HTTP responses. Other information may be stored in
289 // the cache.
290 false,
291 // Storage path for http cache and cookie storage.
292 "/data/data/org.chromium.net/app_cronet_test/test_storage",
293 // User-Agent request header field.
294 "fake agent",
295 // JSON encoded experimental options.
296 "{\"QUIC\":{\"max_time_before_crypto_handshake_seconds\":7,"
297 "\"max_idle_time_before_crypto_handshake_seconds\":11}}",
298 // MockCertVerifier to use for testing purposes.
299 std::unique_ptr<net::CertVerifier>(),
300 // Enable network quality estimator.
301 false,
302 // Enable Public Key Pinning bypass for local trust anchors.
303 true,
304 // Certificate verifier cache data.
305 "");
306
307 net::URLRequestContextBuilder builder;
308 net::NetLog net_log;
309 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
310 // Set a ProxyConfigService to avoid DCHECK failure when building.
311 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30312 std::make_unique<net::ProxyConfigServiceFixed>(
Yixin Wang983875152017-11-21 20:30:13313 net::ProxyConfig::CreateDirect()));
314 std::unique_ptr<net::URLRequestContext> context(builder.Build());
315 const net::HttpNetworkSession::Params* params =
316 context->GetNetworkSessionParams();
317
318 EXPECT_EQ(7, params->quic_max_time_before_crypto_handshake_seconds);
319 EXPECT_EQ(11, params->quic_max_idle_time_before_crypto_handshake_seconds);
320}
321
Yixin Wang1875fdb2017-12-06 02:26:49322TEST(URLURLRequestContextConfigTest, SetQuicConnectionOptions) {
323 base::test::ScopedTaskEnvironment scoped_task_environment_(
324 base::test::ScopedTaskEnvironment::MainThreadType::IO);
325
326 URLRequestContextConfig config(
327 // Enable QUIC.
328 true,
329 // QUIC User Agent ID.
330 "Default QUIC User Agent ID",
331 // Enable SPDY.
332 true,
333 // Enable Brotli.
334 false,
335 // Type of http cache.
336 URLRequestContextConfig::HttpCacheType::DISK,
337 // Max size of http cache in bytes.
338 1024000,
339 // Disable caching for HTTP responses. Other information may be stored in
340 // the cache.
341 false,
342 // Storage path for http cache and cookie storage.
343 "/data/data/org.chromium.net/app_cronet_test/test_storage",
344 // User-Agent request header field.
345 "fake agent",
346 // JSON encoded experimental options.
347 "{\"QUIC\":{\"connection_options\":\"TIME,TBBR,REJ\","
348 "\"client_connection_options\":\"TBBR,1RTT\"}}",
349 // MockCertVerifier to use for testing purposes.
350 std::unique_ptr<net::CertVerifier>(),
351 // Enable network quality estimator.
352 false,
353 // Enable Public Key Pinning bypass for local trust anchors.
354 true,
355 // Certificate verifier cache data.
356 "");
357
358 net::URLRequestContextBuilder builder;
359 net::NetLog net_log;
360 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
361 // Set a ProxyConfigService to avoid DCHECK failure when building.
362 builder.set_proxy_config_service(
363 base::MakeUnique<net::ProxyConfigServiceFixed>(
364 net::ProxyConfig::CreateDirect()));
365 std::unique_ptr<net::URLRequestContext> context(builder.Build());
366 const net::HttpNetworkSession::Params* params =
367 context->GetNetworkSessionParams();
368
369 net::QuicTagVector connection_options;
370 connection_options.push_back(net::kTIME);
371 connection_options.push_back(net::kTBBR);
372 connection_options.push_back(net::kREJ);
373 EXPECT_EQ(connection_options, params->quic_connection_options);
374
375 net::QuicTagVector client_connection_options;
376 client_connection_options.push_back(net::kTBBR);
377 client_connection_options.push_back(net::k1RTT);
378 EXPECT_EQ(client_connection_options, params->quic_client_connection_options);
379}
380
juliatuttle50d9c4b2016-08-23 22:49:19381// See stale_host_resolver_unittest.cc for test of StaleDNS options.
382
xunjielif24ee5f2015-11-23 18:05:26383} // namespace cronet