1 // Copyright 2017 The Chromium Authors
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 <jni.h>
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/functional/bind.h"
11 #include "base/time/time.h"
12 #include "components/cronet/android/cronet_tests_jni_headers/ExperimentalOptionsTest_jni.h"
13 #include "components/cronet/android/test/cronet_test_util.h"
14 #include "components/cronet/url_request_context_config.h"
15 #include "net/base/address_family.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/network_isolation_key.h"
19 #include "net/dns/host_cache.h"
20 #include "net/dns/host_resolver.h"
21 #include "net/dns/public/dns_query_type.h"
22 #include "net/dns/public/host_resolver_source.h"
23 #include "net/url_request/url_request_context.h"
24
25 using base::android::JavaParamRef;
26
27 namespace cronet {
28
29 namespace {
WriteToHostCacheOnNetworkThread(jlong jcontext_adapter,const std::string & address_string)30 void WriteToHostCacheOnNetworkThread(jlong jcontext_adapter,
31 const std::string& address_string) {
32 net::URLRequestContext* context =
33 TestUtil::GetURLRequestContext(jcontext_adapter);
34 net::HostCache* cache = context->host_resolver()->GetHostCache();
35 const std::string hostname = "host-cache-test-host";
36
37 // Create multiple keys to ensure the test works in a variety of network
38 // conditions.
39 net::HostCache::Key key1(hostname, net::DnsQueryType::UNSPECIFIED, 0,
40 net::HostResolverSource::ANY,
41 net::NetworkAnonymizationKey());
42 net::HostCache::Key key2(hostname, net::DnsQueryType::A,
43 net::HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6,
44 net::HostResolverSource::ANY,
45 net::NetworkAnonymizationKey());
46
47 net::IPAddress address;
48 CHECK(address.AssignFromIPLiteral(address_string));
49 net::HostCache::Entry entry(net::OK, {{address, 0}}, /*aliases=*/{hostname},
50 net::HostCache::Entry::SOURCE_UNKNOWN);
51 cache->Set(key1, entry, base::TimeTicks::Now(), base::Seconds(1));
52 cache->Set(key2, entry, base::TimeTicks::Now(), base::Seconds(1));
53 }
54 } // namespace
55
JNI_ExperimentalOptionsTest_WriteToHostCache(JNIEnv * env,jlong jcontext_adapter,const JavaParamRef<jstring> & jaddress)56 static void JNI_ExperimentalOptionsTest_WriteToHostCache(
57 JNIEnv* env,
58 jlong jcontext_adapter,
59 const JavaParamRef<jstring>& jaddress) {
60 TestUtil::RunAfterContextInit(
61 jcontext_adapter,
62 base::BindOnce(&WriteToHostCacheOnNetworkThread, jcontext_adapter,
63 base::android::ConvertJavaStringToUTF8(env, jaddress)));
64 }
65
66 static jboolean
JNI_ExperimentalOptionsTest_ExperimentalOptionsParsingIsAllowedToFail(JNIEnv * env)67 JNI_ExperimentalOptionsTest_ExperimentalOptionsParsingIsAllowedToFail(
68 JNIEnv* env) {
69 return URLRequestContextConfig::ExperimentalOptionsParsingIsAllowedToFail();
70 }
71
72 } // namespace cronet
73