summaryrefslogtreecommitdiff
path: root/components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java
blob: e6954c871113babf87b66b55d6acfd153303c370 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.net;

import android.net.Network;
import android.net.http.ExperimentalHttpEngine;
import android.net.http.HttpEngine;
import android.net.http.UrlRequest;

import org.json.JSONException;
import org.json.JSONObject;

import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.net.impl.CronetEngineBuilderImpl;
import org.chromium.net.impl.CronetUrlRequest;
import org.chromium.net.impl.CronetUrlRequestContext;

/**
 * Utilities for Cronet testing
 */
@JNINamespace("cronet")
public class CronetTestUtil {
    // QUIC test domain must match the certificate used
    // (quic-chain.pem and quic-leaf-cert.key), and the file served (
    // components/cronet/android/test/assets/test/quic_data/simple.txt).
    static final String QUIC_FAKE_HOST = "test.example.com";
    private static final String[] TEST_DOMAINS = {QUIC_FAKE_HOST};
    private static final String LOOPBACK_ADDRESS = "127.0.0.1";

    /**
     * Generates rules for customized DNS mapping for testing hostnames used by test servers,
     * namely:
     * <ul>
     * <li>{@link QuicTestServer#getServerHost}</li>
     * </ul>
     * Maps the test hostnames to 127.0.0.1.
     */
    public static JSONObject generateHostResolverRules() throws JSONException {
        return generateHostResolverRules(LOOPBACK_ADDRESS);
    }

    /**
     * Generates rules for customized DNS mapping for testing hostnames used by test servers,
     * namely:
     * <ul>
     * <li>{@link QuicTestServer#getServerHost}</li>
     * </ul>
     * @param destination host to map to
     */
    public static JSONObject generateHostResolverRules(String destination) throws JSONException {
        StringBuilder rules = new StringBuilder();
        for (String domain : TEST_DOMAINS) {
            rules.append("MAP " + domain + " " + destination + ",");
        }
        return new JSONObject().put("host_resolver_rules", rules);
    }

    /**
     * Prepare {@code cronetEngine}'s network thread so libcronet_test code can run on it.
     */
    public static class NetworkThreadTestConnector {
        private final CronetUrlRequestContext mRequestContext;

        public NetworkThreadTestConnector(HttpEngine cronetEngine) {
            mRequestContext = (CronetUrlRequestContext) cronetEngine;
            CronetTestUtilJni.get().prepareNetworkThread(
                    mRequestContext.getUrlRequestContextAdapter());
        }

        public void shutdown() {
            CronetTestUtilJni.get().cleanupNetworkThread(
                    mRequestContext.getUrlRequestContextAdapter());
        }
    }

    /**
     * Returns the value of load flags in |urlRequest|.
     * @param urlRequest is the UrlRequest object of interest.
     */
    public static int getLoadFlags(UrlRequest urlRequest) {
        return CronetTestUtilJni.get().getLoadFlags(
                ((CronetUrlRequest) urlRequest).getUrlRequestAdapterForTesting());
    }

    public static boolean doesURLRequestContextExistForTesting(
            HttpEngine engine, Network network) {
        CronetUrlRequestContext context = (CronetUrlRequestContext) engine;
        return CronetTestUtilJni.get().uRLRequestContextExistsForTesting(
                context.getUrlRequestContextAdapter(), network.getNetworkHandle());
    }

    public static void setMockCertVerifierForTesting(
            ExperimentalHttpEngine.Builder builder, long mockCertVerifier) {
        getCronetEngineBuilderImpl(builder).setMockCertVerifierForTesting(mockCertVerifier);
    }

    public static CronetEngineBuilderImpl getCronetEngineBuilderImpl(
            ExperimentalHttpEngine.Builder builder) {
        return (CronetEngineBuilderImpl) builder.getBuilderDelegate();
    }

    /**
     * Returns whether the device supports calling nativeGetTaggedBytes().
     */
    public static boolean nativeCanGetTaggedBytes() {
        return CronetTestUtilJni.get().canGetTaggedBytes();
    }

    /**
     * Query the system to find out how many bytes were received with tag
     * {@code expectedTag} for our UID.
     * @param expectedTag the tag to query for.
     * @return the count of received bytes.
     */
    public static long nativeGetTaggedBytes(int expectedTag) {
        return CronetTestUtilJni.get().getTaggedBytes(expectedTag);
    }

    @NativeMethods("cronet_tests")
    interface Natives {
        boolean canGetTaggedBytes();
        long getTaggedBytes(int expectedTag);
        int getLoadFlags(long urlRequestAdapter);
        void prepareNetworkThread(long contextAdapter);
        void cleanupNetworkThread(long contextAdapter);
        boolean uRLRequestContextExistsForTesting(long contextAdapter, long networkHandle);
    }
}