blob: 4dbf887498480e1fea5379951a109d7a045c94bf [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
dchengfe3745e6242016-04-21 23:49:587#include "base/memory/ptr_util.h"
mmenke51629db12017-06-28 13:34:128#include "base/test/scoped_task_environment.h"
tfhef3618f2016-01-11 23:07:089#include "base/values.h"
pauljensen9041eb3c2015-12-09 12:29:0110#include "net/cert/cert_verifier.h"
xunjielif24ee5f2015-11-23 18:05:2611#include "net/http/http_network_session.h"
mikecironef22f9812016-10-04 03:40:1912#include "net/log/net_log.h"
13#include "net/log/net_log_with_source.h"
xunjielif24ee5f2015-11-23 18:05:2614#include "net/proxy/proxy_config.h"
15#include "net/proxy/proxy_config_service_fixed.h"
16#include "net/url_request/url_request_context.h"
17#include "net/url_request/url_request_context_builder.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace cronet {
21
mmenke51629db12017-06-28 13:34:1222TEST(URLRequestContextConfigTest, TestExperimentalOptionParsing) {
23 base::test::ScopedTaskEnvironment scoped_task_environment_(
24 base::test::ScopedTaskEnvironment::MainThreadType::IO);
25
pauljensen9041eb3c2015-12-09 12:29:0126 URLRequestContextConfig config(
27 // Enable QUIC.
28 true,
mefc5da5712016-02-09 20:14:2329 // QUIC User Agent ID.
30 "Default QUIC User Agent ID",
pauljensen9041eb3c2015-12-09 12:29:0131 // Enable SPDY.
32 true,
xunjieli186d2bf2017-04-18 13:45:4733 // Enable Brotli.
34 false,
pauljensen9041eb3c2015-12-09 12:29:0135 // Type of http cache.
36 URLRequestContextConfig::HttpCacheType::DISK,
37 // Max size of http cache in bytes.
38 1024000,
39 // Disable caching for HTTP responses. Other information may be stored in
40 // the cache.
41 false,
42 // Storage path for http cache and cookie storage.
43 "/data/data/org.chromium.net/app_cronet_test/test_storage",
44 // User-Agent request header field.
45 "fake agent",
46 // JSON encoded experimental options.
rtenneti6971c172016-01-15 20:12:1047 "{\"QUIC\":{\"max_server_configs_stored_in_properties\":2,"
mefc5da5712016-02-09 20:14:2348 "\"user_agent_id\":\"Custom QUIC UAID\","
rtenneti64e809d02015-12-11 00:26:2049 "\"idle_connection_timeout_seconds\":300,"
Jana Iyengar903dec22017-11-28 00:44:2350 "\"close_sessions_on_ip_change\":true,"
rtennetid073dd22016-08-04 01:58:3351 "\"race_cert_verification\":true,"
pauljensene92c4092015-12-09 19:13:4852 "\"connection_options\":\"TIME,TBBR,REJ\"},"
mgershaf2c12c2016-08-22 16:33:5453 "\"AsyncDNS\":{\"enable\":true},"
xunjielid67295e2017-03-16 21:05:4154 "\"UnknownOption\":{\"foo\":true},"
mgershaf2c12c2016-08-22 16:33:5455 "\"HostResolverRules\":{\"host_resolver_rules\":"
mgershb3fe8082017-02-28 20:09:2056 "\"MAP * 127.0.0.1\"},"
57 // See http://crbug.com/696569.
mgershaf9a9232017-04-13 20:19:0358 "\"disable_ipv6_on_wifi\":true}",
pauljensen9041eb3c2015-12-09 12:29:0159 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:3960 std::unique_ptr<net::CertVerifier>(),
61 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:0262 false,
63 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:2864 true,
65 // Certificate verifier cache data.
66 "");
xunjielif24ee5f2015-11-23 18:05:2667
xunjielif24ee5f2015-11-23 18:05:2668 net::URLRequestContextBuilder builder;
pauljensene92c4092015-12-09 19:13:4869 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:0270 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
xunjielid67295e2017-03-16 21:05:4171 EXPECT_FALSE(config.effective_experimental_options->HasKey("UnknownOption"));
xunjielif24ee5f2015-11-23 18:05:2672 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:1073 builder.set_proxy_config_service(
74 base::MakeUnique<net::ProxyConfigServiceFixed>(
75 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:5876 std::unique_ptr<net::URLRequestContext> context(builder.Build());
xunjielif24ee5f2015-11-23 18:05:2677 const net::HttpNetworkSession::Params* params =
78 context->GetNetworkSessionParams();
79 // Check Quic Connection options.
80 net::QuicTagVector quic_connection_options;
81 quic_connection_options.push_back(net::kTIME);
82 quic_connection_options.push_back(net::kTBBR);
83 quic_connection_options.push_back(net::kREJ);
84 EXPECT_EQ(quic_connection_options, params->quic_connection_options);
85
mefc5da5712016-02-09 20:14:2386 // Check Custom QUIC User Agent Id.
87 EXPECT_EQ("Custom QUIC UAID", params->quic_user_agent_id);
88
rtenneti6971c172016-01-15 20:12:1089 // Check max_server_configs_stored_in_properties.
90 EXPECT_EQ(2u, params->quic_max_server_configs_stored_in_properties);
xunjielif24ee5f2015-11-23 18:05:2691
rtenneti64e809d02015-12-11 00:26:2092 // Check idle_connection_timeout_seconds.
93 EXPECT_EQ(300, params->quic_idle_connection_timeout_seconds);
94
Jana Iyengar903dec22017-11-28 00:44:2395 EXPECT_TRUE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:3696 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change);
Zhongyi Shi64795622017-11-20 02:21:4997 EXPECT_FALSE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:2898 EXPECT_FALSE(params->quic_migrate_sessions_early_v2);
jrid26566952016-02-04 21:06:4299
rtennetid073dd22016-08-04 01:58:33100 // Check race_cert_verification.
101 EXPECT_TRUE(params->quic_race_cert_verification);
102
pauljensene92c4092015-12-09 19:13:48103 // Check AsyncDNS resolver is enabled.
tfhef3618f2016-01-11 23:07:08104 EXPECT_TRUE(context->host_resolver()->GetDnsConfigAsValue());
mgershaf2c12c2016-08-22 16:33:54105
mgershaf9a9232017-04-13 20:19:03106 // Check IPv6 is disabled when on wifi.
107 EXPECT_TRUE(context->host_resolver()->GetNoIPv6OnWifi());
mgershb3fe8082017-02-28 20:09:20108
mgershaf2c12c2016-08-22 16:33:54109 net::HostResolver::RequestInfo info(net::HostPortPair("abcde", 80));
110 net::AddressList addresses;
111 EXPECT_EQ(net::OK, context->host_resolver()->ResolveFromCache(
tfarina428341112016-09-22 13:38:20112 info, &addresses, net::NetLogWithSource()));
xunjielif24ee5f2015-11-23 18:05:26113}
114
mmenke51629db12017-06-28 13:34:12115TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationOptions) {
116 base::test::ScopedTaskEnvironment scoped_task_environment_(
117 base::test::ScopedTaskEnvironment::MainThreadType::IO);
118
jric69a88422016-02-12 19:48:36119 URLRequestContextConfig config(
120 // Enable QUIC.
121 true,
122 // QUIC User Agent ID.
123 "Default QUIC User Agent ID",
124 // Enable SPDY.
125 true,
xunjieli186d2bf2017-04-18 13:45:47126 // Enable Brotli.
127 false,
jric69a88422016-02-12 19:48:36128 // Type of http cache.
129 URLRequestContextConfig::HttpCacheType::DISK,
130 // Max size of http cache in bytes.
131 1024000,
132 // Disable caching for HTTP responses. Other information may be stored in
133 // the cache.
134 false,
135 // Storage path for http cache and cookie storage.
136 "/data/data/org.chromium.net/app_cronet_test/test_storage",
137 // User-Agent request header field.
138 "fake agent",
139 // JSON encoded experimental options.
jrie68df0e2016-02-12 22:01:42140 "{\"QUIC\":{\"migrate_sessions_on_network_change\":true,"
141 "\"migrate_sessions_early\":true}}",
jric69a88422016-02-12 19:48:36142 // MockCertVerifier to use for testing purposes.
tbansal7018e2a2016-06-25 00:40:39143 std::unique_ptr<net::CertVerifier>(),
144 // Enable network quality estimator.
kapishnikov385aa422016-07-01 20:53:02145 false,
146 // Enable Public Key Pinning bypass for local trust anchors.
rtenneti121f9fa2016-07-07 23:49:28147 true,
148 // Certificate verifier cache data.
149 "");
jric69a88422016-02-12 19:48:36150
151 net::URLRequestContextBuilder builder;
152 net::NetLog net_log;
David Benjamindc2f4b02017-07-27 23:59:02153 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
jric69a88422016-02-12 19:48:36154 // Set a ProxyConfigService to avoid DCHECK failure when building.
ricea85ec57952016-08-31 09:34:10155 builder.set_proxy_config_service(
156 base::MakeUnique<net::ProxyConfigServiceFixed>(
157 net::ProxyConfig::CreateDirect()));
dchengfe3745e6242016-04-21 23:49:58158 std::unique_ptr<net::URLRequestContext> context(builder.Build());
jric69a88422016-02-12 19:48:36159 const net::HttpNetworkSession::Params* params =
160 context->GetNetworkSessionParams();
161
Jana Iyengar903dec22017-11-28 00:44:23162 EXPECT_FALSE(params->quic_close_sessions_on_ip_change);
jric69a88422016-02-12 19:48:36163 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change);
jrie68df0e2016-02-12 22:01:42164 EXPECT_TRUE(params->quic_migrate_sessions_early);
jric69a88422016-02-12 19:48:36165}
166
Yixin Wang10f477ed2017-11-21 04:20:20167TEST(URLRequestContextConfigTest, SetQuicConnectionMigrationV2Options) {
Zhongyi Shi64795622017-11-20 02:21:49168 base::test::ScopedTaskEnvironment scoped_task_environment_(
169 base::test::ScopedTaskEnvironment::MainThreadType::IO);
170
171 URLRequestContextConfig config(
172 // Enable QUIC.
173 true,
174 // QUIC User Agent ID.
175 "Default QUIC User Agent ID",
176 // Enable SPDY.
177 true,
178 // Enable Brotli.
179 false,
180 // Type of http cache.
181 URLRequestContextConfig::HttpCacheType::DISK,
182 // Max size of http cache in bytes.
183 1024000,
184 // Disable caching for HTTP responses. Other information may be stored in
185 // the cache.
186 false,
187 // Storage path for http cache and cookie storage.
188 "/data/data/org.chromium.net/app_cronet_test/test_storage",
189 // User-Agent request header field.
190 "fake agent",
191 // JSON encoded experimental options.
Zhongyi Shif4683a32017-12-01 00:03:28192 "{\"QUIC\":{\"migrate_sessions_on_network_change_v2\":true,"
193 "\"migrate_sessions_early_v2\":true}}",
Zhongyi Shi64795622017-11-20 02:21:49194 // MockCertVerifier to use for testing purposes.
195 std::unique_ptr<net::CertVerifier>(),
196 // Enable network quality estimator.
197 false,
198 // Enable Public Key Pinning bypass for local trust anchors.
199 true,
200 // Certificate verifier cache data.
201 "");
202
203 net::URLRequestContextBuilder builder;
204 net::NetLog net_log;
205 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
206 // Set a ProxyConfigService to avoid DCHECK failure when building.
207 builder.set_proxy_config_service(
208 base::MakeUnique<net::ProxyConfigServiceFixed>(
209 net::ProxyConfig::CreateDirect()));
210 std::unique_ptr<net::URLRequestContext> context(builder.Build());
211 const net::HttpNetworkSession::Params* params =
212 context->GetNetworkSessionParams();
213
214 EXPECT_TRUE(params->quic_migrate_sessions_on_network_change_v2);
Zhongyi Shif4683a32017-12-01 00:03:28215 EXPECT_TRUE(params->quic_migrate_sessions_early_v2);
Zhongyi Shi64795622017-11-20 02:21:49216}
217
Yixin Wang10f477ed2017-11-21 04:20:20218TEST(URLRequestContextConfigTest, SetQuicHostWhitelist) {
219 base::test::ScopedTaskEnvironment scoped_task_environment_(
220 base::test::ScopedTaskEnvironment::MainThreadType::IO);
221
222 URLRequestContextConfig config(
223 // Enable QUIC.
224 true,
225 // QUIC User Agent ID.
226 "Default QUIC User Agent ID",
227 // Enable SPDY.
228 true,
229 // Enable Brotli.
230 false,
231 // Type of http cache.
232 URLRequestContextConfig::HttpCacheType::DISK,
233 // Max size of http cache in bytes.
234 1024000,
235 // Disable caching for HTTP responses. Other information may be stored in
236 // the cache.
237 false,
238 // Storage path for http cache and cookie storage.
239 "/data/data/org.chromium.net/app_cronet_test/test_storage",
240 // User-Agent request header field.
241 "fake agent",
242 // JSON encoded experimental options.
243 "{\"QUIC\":{\"host_whitelist\":\"www.example.com,www.example.org\"}}",
244 // MockCertVerifier to use for testing purposes.
245 std::unique_ptr<net::CertVerifier>(),
246 // Enable network quality estimator.
247 false,
248 // Enable Public Key Pinning bypass for local trust anchors.
249 true,
250 // Certificate verifier cache data.
251 "");
252
253 net::URLRequestContextBuilder builder;
254 net::NetLog net_log;
255 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
256 // Set a ProxyConfigService to avoid DCHECK failure when building.
257 builder.set_proxy_config_service(
258 base::MakeUnique<net::ProxyConfigServiceFixed>(
259 net::ProxyConfig::CreateDirect()));
260 std::unique_ptr<net::URLRequestContext> context(builder.Build());
261 const net::HttpNetworkSession::Params* params =
262 context->GetNetworkSessionParams();
263
264 EXPECT_TRUE(
265 base::ContainsKey(params->quic_host_whitelist, "www.example.com"));
266 EXPECT_TRUE(
267 base::ContainsKey(params->quic_host_whitelist, "www.example.org"));
268}
269
Yixin Wang983875152017-11-21 20:30:13270TEST(URLRequestContextConfigTest, SetQuicMaxTimeBeforeCryptoHandshake) {
271 base::test::ScopedTaskEnvironment scoped_task_environment_(
272 base::test::ScopedTaskEnvironment::MainThreadType::IO);
273
274 URLRequestContextConfig config(
275 // Enable QUIC.
276 true,
277 // QUIC User Agent ID.
278 "Default QUIC User Agent ID",
279 // Enable SPDY.
280 true,
281 // Enable Brotli.
282 false,
283 // Type of http cache.
284 URLRequestContextConfig::HttpCacheType::DISK,
285 // Max size of http cache in bytes.
286 1024000,
287 // Disable caching for HTTP responses. Other information may be stored in
288 // the cache.
289 false,
290 // Storage path for http cache and cookie storage.
291 "/data/data/org.chromium.net/app_cronet_test/test_storage",
292 // User-Agent request header field.
293 "fake agent",
294 // JSON encoded experimental options.
295 "{\"QUIC\":{\"max_time_before_crypto_handshake_seconds\":7,"
296 "\"max_idle_time_before_crypto_handshake_seconds\":11}}",
297 // MockCertVerifier to use for testing purposes.
298 std::unique_ptr<net::CertVerifier>(),
299 // Enable network quality estimator.
300 false,
301 // Enable Public Key Pinning bypass for local trust anchors.
302 true,
303 // Certificate verifier cache data.
304 "");
305
306 net::URLRequestContextBuilder builder;
307 net::NetLog net_log;
308 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
309 // Set a ProxyConfigService to avoid DCHECK failure when building.
310 builder.set_proxy_config_service(
311 base::MakeUnique<net::ProxyConfigServiceFixed>(
312 net::ProxyConfig::CreateDirect()));
313 std::unique_ptr<net::URLRequestContext> context(builder.Build());
314 const net::HttpNetworkSession::Params* params =
315 context->GetNetworkSessionParams();
316
317 EXPECT_EQ(7, params->quic_max_time_before_crypto_handshake_seconds);
318 EXPECT_EQ(11, params->quic_max_idle_time_before_crypto_handshake_seconds);
319}
320
Yixin Wang1875fdb2017-12-06 02:26:49321TEST(URLURLRequestContextConfigTest, SetQuicConnectionOptions) {
322 base::test::ScopedTaskEnvironment scoped_task_environment_(
323 base::test::ScopedTaskEnvironment::MainThreadType::IO);
324
325 URLRequestContextConfig config(
326 // Enable QUIC.
327 true,
328 // QUIC User Agent ID.
329 "Default QUIC User Agent ID",
330 // Enable SPDY.
331 true,
332 // Enable Brotli.
333 false,
334 // Type of http cache.
335 URLRequestContextConfig::HttpCacheType::DISK,
336 // Max size of http cache in bytes.
337 1024000,
338 // Disable caching for HTTP responses. Other information may be stored in
339 // the cache.
340 false,
341 // Storage path for http cache and cookie storage.
342 "/data/data/org.chromium.net/app_cronet_test/test_storage",
343 // User-Agent request header field.
344 "fake agent",
345 // JSON encoded experimental options.
346 "{\"QUIC\":{\"connection_options\":\"TIME,TBBR,REJ\","
347 "\"client_connection_options\":\"TBBR,1RTT\"}}",
348 // MockCertVerifier to use for testing purposes.
349 std::unique_ptr<net::CertVerifier>(),
350 // Enable network quality estimator.
351 false,
352 // Enable Public Key Pinning bypass for local trust anchors.
353 true,
354 // Certificate verifier cache data.
355 "");
356
357 net::URLRequestContextBuilder builder;
358 net::NetLog net_log;
359 config.ConfigureURLRequestContextBuilder(&builder, &net_log);
360 // Set a ProxyConfigService to avoid DCHECK failure when building.
361 builder.set_proxy_config_service(
362 base::MakeUnique<net::ProxyConfigServiceFixed>(
363 net::ProxyConfig::CreateDirect()));
364 std::unique_ptr<net::URLRequestContext> context(builder.Build());
365 const net::HttpNetworkSession::Params* params =
366 context->GetNetworkSessionParams();
367
368 net::QuicTagVector connection_options;
369 connection_options.push_back(net::kTIME);
370 connection_options.push_back(net::kTBBR);
371 connection_options.push_back(net::kREJ);
372 EXPECT_EQ(connection_options, params->quic_connection_options);
373
374 net::QuicTagVector client_connection_options;
375 client_connection_options.push_back(net::kTBBR);
376 client_connection_options.push_back(net::k1RTT);
377 EXPECT_EQ(client_connection_options, params->quic_client_connection_options);
378}
379
juliatuttle50d9c4b2016-08-23 22:49:19380// See stale_host_resolver_unittest.cc for test of StaleDNS options.
381
xunjielif24ee5f2015-11-23 18:05:26382} // namespace cronet