blob: 20f938c7d6a4099feabb934c979f2331bfb8c737 [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
Gyuyoung Kim6afb5082018-01-19 13:35:577#include <memory>
8
mmenke51629db12017-06-28 13:34:129#include "base/test/scoped_task_environment.h"
tfhef3618f2016-01-11 23:07:0810#include "base/values.h"
pauljensen9041eb3c2015-12-09 12:29:0111#include "net/cert/cert_verifier.h"
xunjielif24ee5f2015-11-23 18:05:2612#include "net/http/http_network_session.h"
mikecironef22f9812016-10-04 03:40:1913#include "net/log/net_log.h"
14#include "net/log/net_log_with_source.h"
xunjielif24ee5f2015-11-23 18:05:2615#include "net/proxy/proxy_config.h"
16#include "net/proxy/proxy_config_service_fixed.h"
17#include "net/url_request/url_request_context.h"
18#include "net/url_request/url_request_context_builder.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace cronet {
22
mmenke51629db12017-06-28 13:34:1223TEST(URLRequestContextConfigTest, TestExperimentalOptionParsing) {
24 base::test::ScopedTaskEnvironment scoped_task_environment_(
25 base::test::ScopedTaskEnvironment::MainThreadType::IO);
26
pauljensen9041eb3c2015-12-09 12:29:0127 URLRequestContextConfig config(
28 // Enable QUIC.
29 true,
mefc5da5712016-02-09 20:14:2330 // QUIC User Agent ID.
31 "Default QUIC User Agent ID",
pauljensen9041eb3c2015-12-09 12:29:0132 // Enable SPDY.
33 true,
xunjieli186d2bf2017-04-18 13:45:4734 // Enable Brotli.
35 false,
pauljensen9041eb3c2015-12-09 12:29:0136 // Type of http cache.
37 URLRequestContextConfig::HttpCacheType::DISK,
38 // Max size of http cache in bytes.
39 1024000,
40 // Disable caching for HTTP responses. Other information may be stored in
41 // the cache.
42 false,
43 // Storage path for http cache and cookie storage.
44 "/data/data/org.chromium.net/app_cronet_test/test_storage",
45 // User-Agent request header field.
46 "fake agent",
47 // JSON encoded experimental options.
rtenneti6971c172016-01-15 20:12:1048 "{\"QUIC\":{\"max_server_configs_stored_in_properties\":2,"
mefc5da5712016-02-09 20:14:2349 "\"user_agent_id\":\"Custom QUIC UAID\","
rtenneti64e809d02015-12-11 00:26:2050 "\"idle_connection_timeout_seconds\":300,"
Jana Iyengar903dec22017-11-28 00:44:2351 "\"close_sessions_on_ip_change\":true,"
rtennetid073dd22016-08-04 01:58:3352 "\"race_cert_verification\":true,"
pauljensene92c4092015-12-09 19:13:4853 "\"connection_options\":\"TIME,TBBR,REJ\"},"
mgershaf2c12c2016-08-22 16:33:5454 "\"AsyncDNS\":{\"enable\":true},"
xunjielid67295e2017-03-16 21:05:4155 "\"UnknownOption\":{\"foo\":true},"
mgershaf2c12c2016-08-22 16:33:5456 "\"HostResolverRules\":{\"host_resolver_rules\":"
mgershb3fe8082017-02-28 20:09:2057 "\"MAP * 127.0.0.1\"},"
58 // See http://crbug.com/696569.
mgershaf9a9232017-04-13 20:19:0359 "\"disable_ipv6_on_wifi\":true}",
pauljensen9041eb3c2015-12-09 12:29:0160 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:3961 std::unique_ptr<net::CertVerifier>(),
62 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:0263 false,
64 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:2865 true,
66 // Certificate verifier cache data.
67 "");
xunjielif24ee5f2015-11-23 18:05:2668
xunjielif24ee5f2015-11-23 18:05:2669 net::URLRequestContextBuilder builder;
pauljensene92c4092015-12-09 19:13:4870 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:0271 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
xunjielid67295e2017-03-16 21:05:4172 EXPECT_FALSE(config.effective_experimental_options->HasKey("UnknownOption"));
xunjielif24ee5f2015-11-23 18:05:2673 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:1074 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:3075 std::make_unique<net::ProxyConfigServiceFixed>(
ricea85ec57952016-08-31 09:34:1076 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:5877 std::unique_ptr<net::URLRequestContext> context(builder.Build());
xunjielif24ee5f2015-11-23 18:05:2678 const net::HttpNetworkSession::Params* params =
79 context->GetNetworkSessionParams();
80 // Check Quic Connection options.
81 net::QuicTagVector quic_connection_options;
82 quic_connection_options.push_back(net::kTIME);
83 quic_connection_options.push_back(net::kTBBR);
84 quic_connection_options.push_back(net::kREJ);
85 EXPECT_EQ(quic_connection_options, params->quic_connection_options);
86
mefc5da5712016-02-09 20:14:2387 // Check Custom QUIC User Agent Id.
88 EXPECT_EQ("Custom QUIC UAID", params->quic_user_agent_id);
89
rtenneti6971c172016-01-15 20:12:1090 // Check max_server_configs_stored_in_properties.
91 EXPECT_EQ(2u, params->quic_max_server_configs_stored_in_properties);
xunjielif24ee5f2015-11-23 18:05:2692
rtenneti64e809d02015-12-11 00:26:2093 // Check idle_connection_timeout_seconds.
94 EXPECT_EQ(300, params->quic_idle_connection_timeout_seconds);
95
Jana Iyengar903dec22017-11-28 00:44:2396 EXPECT_TRUE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:3697 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change);
Zhongyi Shi64795622017-11-20 02:21:4998 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:2899 EXPECT_FALSE(params->quic_migrate_sessions_early_v2);
jrid26566952016-02-04 21:06:42100
rtennetid073dd22016-08-04 01:58:33101 // Check race_cert_verification.
102 EXPECT_TRUE(params->quic_race_cert_verification);
103
Misha Efimov63957912017-12-06 07:13:26104#if defined(ENABLE_BUILT_IN_DNS)
105 // Check AsyncDNS resolver is enabled (not supported on iOS).
tfhef3618f2016-01-11 23:07:08106 EXPECT_TRUE(context->host_resolver()->GetDnsConfigAsValue());
Misha Efimov63957912017-12-06 07:13:26107#endif // defined(ENABLE_BUILT_IN_DNS)
mgershaf2c12c2016-08-22 16:33:54108
mgershaf9a9232017-04-13 20:19:03109 // Check IPv6 is disabled when on wifi.
110 EXPECT_TRUE(context->host_resolver()->GetNoIPv6OnWifi());
mgershb3fe8082017-02-28 20:09:20111
mgershaf2c12c2016-08-22 16:33:54112 net::HostResolver::RequestInfo info(net::HostPortPair("abcde", 80));
113 net::AddressList addresses;
114 EXPECT_EQ(net::OK, context->host_resolver()->ResolveFromCache(
tfarina428341112016-09-22 13:38:20115 info, &addresses, net::NetLogWithSource()));
xunjielif24ee5f2015-11-23 18:05:26116}
117
mmenke51629db12017-06-28 13:34:12118TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationOptions) {
119 base::test::ScopedTaskEnvironment scoped_task_environment_(
120 base::test::ScopedTaskEnvironment::MainThreadType::IO);
121
jric69a88422016-02-12 19:48:36122 URLRequestContextConfig config(
123 // Enable QUIC.
124 true,
125 // QUIC User Agent ID.
126 "Default QUIC User Agent ID",
127 // Enable SPDY.
128 true,
xunjieli186d2bf2017-04-18 13:45:47129 // Enable Brotli.
130 false,
jric69a88422016-02-12 19:48:36131 // Type of http cache.
132 URLRequestContextConfig::HttpCacheType::DISK,
133 // Max size of http cache in bytes.
134 1024000,
135 // Disable caching for HTTP responses. Other information may be stored in
136 // the cache.
137 false,
138 // Storage path for http cache and cookie storage.
139 "/data/data/org.chromium.net/app_cronet_test/test_storage",
140 // User-Agent request header field.
141 "fake agent",
142 // JSON encoded experimental options.
jrie68df0e2016-02-12 22:01:42143 "{\"QUIC\":{\"migrate_sessions_on_network_change\":true,"
144 "\"migrate_sessions_early\":true}}",
jric69a88422016-02-12 19:48:36145 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:39146 std::unique_ptr<net::CertVerifier>(),
147 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:02148 false,
149 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:28150 true,
151 // Certificate verifier cache data.
152 "");
jric69a88422016-02-12 19:48:36153
154 net::URLRequestContextBuilder builder;
155 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:02156 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
jric69a88422016-02-12 19:48:36157 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:10158 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30159 std::make_unique<net::ProxyConfigServiceFixed>(
ricea85ec57952016-08-31 09:34:10160 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:58161 std::unique_ptr<net::URLRequestContext> context(builder.Build());
jric69a88422016-02-12 19:48:36162 const net::HttpNetworkSession::Params* params =
163 context->GetNetworkSessionParams();
164
Jana Iyengar903dec22017-11-28 00:44:23165 EXPECT_FALSE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:36166 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change);
jrie68df0e2016-02-12 22:01:42167 EXPECT_TRUE(params->quic_migrate_sessions_early);
jric69a88422016-02-12 19:48:36168}
169
Yixin Wang10f477ed2017-11-21 04:20:20170TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationV2Options) {
Zhongyi Shi64795622017-11-20 02:21:49171 base::test::ScopedTaskEnvironment scoped_task_environment_(
172 base::test::ScopedTaskEnvironment::MainThreadType::IO);
173
174 URLRequestContextConfig config(
175 // Enable QUIC.
176 true,
177 // QUIC User Agent ID.
178 "Default QUIC User Agent ID",
179 // Enable SPDY.
180 true,
181 // Enable Brotli.
182 false,
183 // Type of http cache.
184 URLRequestContextConfig::HttpCacheType::DISK,
185 // Max size of http cache in bytes.
186 1024000,
187 // Disable caching for HTTP responses. Other information may be stored in
188 // the cache.
189 false,
190 // Storage path for http cache and cookie storage.
191 "/data/data/org.chromium.net/app_cronet_test/test_storage",
192 // User-Agent request header field.
193 "fake agent",
194 // JSON encoded experimental options.
Zhongyi Shif4683a32017-12-01 00:03:28195 "{\"QUIC\":{\"migrate_sessions_on_network_change_v2\":true,"
Zhongyi Shi73f23ca872017-12-13 18:37:13196 "\"migrate_sessions_early_v2\":true,"
Zhongyi Shi8b1e43f2017-12-13 20:46:30197 "\"max_time_on_non_default_network_seconds\":10,"
198 "\"max_migrations_to_non_default_network_on_path_degrading\":4}}",
Zhongyi Shi64795622017-11-20 02:21:49199 // MockCertVerifier to use for testing purposes.
200 std::unique_ptr<net::CertVerifier>(),
201 // Enable network quality estimator.
202 false,
203 // Enable Public Key Pinning bypass for local trust anchors.
204 true,
205 // Certificate verifier cache data.
206 "");
207
208 net::URLRequestContextBuilder builder;
209 net::NetLog net_log;
210 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
211 // Set a ProxyConfigService to avoid DCHECK failure when building.
212 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30213 std::make_unique<net::ProxyConfigServiceFixed>(
Zhongyi Shi64795622017-11-20 02:21:49214 net::ProxyConfig::CreateDirect()));
215 std::unique_ptr<net::URLRequestContext> context(builder.Build());
216 const net::HttpNetworkSession::Params* params =
217 context->GetNetworkSessionParams();
218
219 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:28220 EXPECT_TRUE(params->quic_migrate_sessions_early_v2);
Zhongyi Shi73f23ca872017-12-13 18:37:13221 EXPECT_EQ(base::TimeDelta::FromSeconds(10),
222 params->quic_max_time_on_non_default_network);
Zhongyi Shi8b1e43f2017-12-13 20:46:30223 EXPECT_EQ(
224 4, params->quic_max_migrations_to_non_default_network_on_path_degrading);
Zhongyi Shi64795622017-11-20 02:21:49225}
226
Yixin Wang10f477ed2017-11-21 04:20:20227TEST(URLRequestContextConfigTest, SetQuicHostWhitelist) {
228 base::test::ScopedTaskEnvironment scoped_task_environment_(
229 base::test::ScopedTaskEnvironment::MainThreadType::IO);
230
231 URLRequestContextConfig config(
232 // Enable QUIC.
233 true,
234 // QUIC User Agent ID.
235 "Default QUIC User Agent ID",
236 // Enable SPDY.
237 true,
238 // Enable Brotli.
239 false,
240 // Type of http cache.
241 URLRequestContextConfig::HttpCacheType::DISK,
242 // Max size of http cache in bytes.
243 1024000,
244 // Disable caching for HTTP responses. Other information may be stored in
245 // the cache.
246 false,
247 // Storage path for http cache and cookie storage.
248 "/data/data/org.chromium.net/app_cronet_test/test_storage",
249 // User-Agent request header field.
250 "fake agent",
251 // JSON encoded experimental options.
252 "{\"QUIC\":{\"host_whitelist\":\"www.example.com,www.example.org\"}}",
253 // MockCertVerifier to use for testing purposes.
254 std::unique_ptr<net::CertVerifier>(),
255 // Enable network quality estimator.
256 false,
257 // Enable Public Key Pinning bypass for local trust anchors.
258 true,
259 // Certificate verifier cache data.
260 "");
261
262 net::URLRequestContextBuilder builder;
263 net::NetLog net_log;
264 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
265 // Set a ProxyConfigService to avoid DCHECK failure when building.
266 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30267 std::make_unique<net::ProxyConfigServiceFixed>(
Yixin Wang10f477ed2017-11-21 04:20:20268 net::ProxyConfig::CreateDirect()));
269 std::unique_ptr<net::URLRequestContext> context(builder.Build());
270 const net::HttpNetworkSession::Params* params =
271 context->GetNetworkSessionParams();
272
273 EXPECT_TRUE(
274 base::ContainsKey(params->quic_host_whitelist, "www.example.com"));
275 EXPECT_TRUE(
276 base::ContainsKey(params->quic_host_whitelist, "www.example.org"));
277}
278
Yixin Wang983875152017-11-21 20:30:13279TEST(URLRequestContextConfigTest, SetQuicMaxTimeBeforeCryptoHandshake) {
280 base::test::ScopedTaskEnvironment scoped_task_environment_(
281 base::test::ScopedTaskEnvironment::MainThreadType::IO);
282
283 URLRequestContextConfig config(
284 // Enable QUIC.
285 true,
286 // QUIC User Agent ID.
287 "Default QUIC User Agent ID",
288 // Enable SPDY.
289 true,
290 // Enable Brotli.
291 false,
292 // Type of http cache.
293 URLRequestContextConfig::HttpCacheType::DISK,
294 // Max size of http cache in bytes.
295 1024000,
296 // Disable caching for HTTP responses. Other information may be stored in
297 // the cache.
298 false,
299 // Storage path for http cache and cookie storage.
300 "/data/data/org.chromium.net/app_cronet_test/test_storage",
301 // User-Agent request header field.
302 "fake agent",
303 // JSON encoded experimental options.
304 "{\"QUIC\":{\"max_time_before_crypto_handshake_seconds\":7,"
305 "\"max_idle_time_before_crypto_handshake_seconds\":11}}",
306 // MockCertVerifier to use for testing purposes.
307 std::unique_ptr<net::CertVerifier>(),
308 // Enable network quality estimator.
309 false,
310 // Enable Public Key Pinning bypass for local trust anchors.
311 true,
312 // Certificate verifier cache data.
313 "");
314
315 net::URLRequestContextBuilder builder;
316 net::NetLog net_log;
317 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
318 // Set a ProxyConfigService to avoid DCHECK failure when building.
319 builder.set_proxy_config_service(
Lily Houghtonef028852017-12-06 20:52:30320 std::make_unique<net::ProxyConfigServiceFixed>(
Yixin Wang983875152017-11-21 20:30:13321 net::ProxyConfig::CreateDirect()));
322 std::unique_ptr<net::URLRequestContext> context(builder.Build());
323 const net::HttpNetworkSession::Params* params =
324 context->GetNetworkSessionParams();
325
326 EXPECT_EQ(7, params->quic_max_time_before_crypto_handshake_seconds);
327 EXPECT_EQ(11, params->quic_max_idle_time_before_crypto_handshake_seconds);
328}
329
Yixin Wang1875fdb2017-12-06 02:26:49330TEST(URLURLRequestContextConfigTest, SetQuicConnectionOptions) {
331 base::test::ScopedTaskEnvironment scoped_task_environment_(
332 base::test::ScopedTaskEnvironment::MainThreadType::IO);
333
334 URLRequestContextConfig config(
335 // Enable QUIC.
336 true,
337 // QUIC User Agent ID.
338 "Default QUIC User Agent ID",
339 // Enable SPDY.
340 true,
341 // Enable Brotli.
342 false,
343 // Type of http cache.
344 URLRequestContextConfig::HttpCacheType::DISK,
345 // Max size of http cache in bytes.
346 1024000,
347 // Disable caching for HTTP responses. Other information may be stored in
348 // the cache.
349 false,
350 // Storage path for http cache and cookie storage.
351 "/data/data/org.chromium.net/app_cronet_test/test_storage",
352 // User-Agent request header field.
353 "fake agent",
354 // JSON encoded experimental options.
355 "{\"QUIC\":{\"connection_options\":\"TIME,TBBR,REJ\","
356 "\"client_connection_options\":\"TBBR,1RTT\"}}",
357 // MockCertVerifier to use for testing purposes.
358 std::unique_ptr<net::CertVerifier>(),
359 // Enable network quality estimator.
360 false,
361 // Enable Public Key Pinning bypass for local trust anchors.
362 true,
363 // Certificate verifier cache data.
364 "");
365
366 net::URLRequestContextBuilder builder;
367 net::NetLog net_log;
368 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
369 // Set a ProxyConfigService to avoid DCHECK failure when building.
370 builder.set_proxy_config_service(
Gyuyoung Kim6afb5082018-01-19 13:35:57371 std::make_unique<net::ProxyConfigServiceFixed>(
Yixin Wang1875fdb2017-12-06 02:26:49372 net::ProxyConfig::CreateDirect()));
373 std::unique_ptr<net::URLRequestContext> context(builder.Build());
374 const net::HttpNetworkSession::Params* params =
375 context->GetNetworkSessionParams();
376
377 net::QuicTagVector connection_options;
378 connection_options.push_back(net::kTIME);
379 connection_options.push_back(net::kTBBR);
380 connection_options.push_back(net::kREJ);
381 EXPECT_EQ(connection_options, params->quic_connection_options);
382
383 net::QuicTagVector client_connection_options;
384 client_connection_options.push_back(net::kTBBR);
385 client_connection_options.push_back(net::k1RTT);
386 EXPECT_EQ(client_connection_options, params->quic_client_connection_options);
387}
388
juliatuttle50d9c4b2016-08-23 22:49:19389// See stale_host_resolver_unittest.cc for test of StaleDNS options.
390
xunjielif24ee5f2015-11-23 18:05:26391} // namespace cronet