summaryrefslogtreecommitdiffstats
path: root/components/cronet/android/test/cronet_test_util.cc
blob: 07089a0fc4a6a9f23daba24325addeb98c91dbd2 (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cronet/android/test/cronet_test_util.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "components/cronet/android/cronet_url_request_context_adapter.h"
#include "components/cronet/android/test/native_test_server.h"
#include "components/cronet/android/url_request_context_adapter.h"
#include "jni/CronetTestUtil_jni.h"
#include "net/dns/host_resolver_impl.h"
#include "net/dns/mock_host_resolver.h"

namespace cronet {

const char kFakeSdchDomain[] = "fake.sdch.domain";
// This must match the certificate used
// (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and
// the file served (
// components/cronet/android/test/assets/test/quic_data/simple.txt).
const char kFakeQuicDomain[] = "test.example.com";

namespace {

// Install host resolver rules to map fake domains to |destination|, usually an
// IP address.
void RegisterHostResolverProcHelper(net::URLRequestContext* url_request_context,
                                    const std::string& destination) {
  net::HostResolverImpl* resolver =
      static_cast<net::HostResolverImpl*>(url_request_context->host_resolver());
  scoped_refptr<net::RuleBasedHostResolverProc> proc =
      new net::RuleBasedHostResolverProc(NULL);
  proc->AddRule(kFakeSdchDomain, destination);
  proc->AddRule(kFakeQuicDomain, destination);
  resolver->set_proc_params_for_test(
      net::HostResolverImpl::ProcTaskParams(proc.get(), 1u));
  JNIEnv* env = base::android::AttachCurrentThread();
  Java_CronetTestUtil_onHostResolverProcRegistered(env);
}

void RegisterHostResolverProcOnNetworkThread(
    CronetURLRequestContextAdapter* context_adapter,
    const std::string& destination) {
  RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(),
                                 destination);
}

// TODO(xunjieli): Delete this once legacy API is removed.
void RegisterHostResolverProcOnNetworkThreadLegacyAPI(
    URLRequestContextAdapter* context_adapter,
    const std::string& destination) {
  RegisterHostResolverProcHelper(context_adapter->GetURLRequestContext(),
                                 destination);
}

}  // namespace

void RegisterHostResolverProc(JNIEnv* env,
                              const JavaParamRef<jclass>& jcaller,
                              jlong jadapter,
                              jboolean jlegacy_api,
                              const JavaParamRef<jstring>& jdestination) {
  std::string destination(
      base::android::ConvertJavaStringToUTF8(env, jdestination));
  if (jlegacy_api == JNI_TRUE) {
    URLRequestContextAdapter* context_adapter =
        reinterpret_cast<URLRequestContextAdapter*>(jadapter);
    context_adapter->PostTaskToNetworkThread(
        FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThreadLegacyAPI,
                              base::Unretained(context_adapter), destination));
  } else {
    CronetURLRequestContextAdapter* context_adapter =
        reinterpret_cast<CronetURLRequestContextAdapter*>(jadapter);
    context_adapter->PostTaskToNetworkThread(
        FROM_HERE, base::Bind(&RegisterHostResolverProcOnNetworkThread,
                              base::Unretained(context_adapter), destination));
  }
}

bool RegisterCronetTestUtil(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace cronet