summaryrefslogtreecommitdiff
path: root/components/cronet/android/test/src/org/chromium/net/QuicTestServer.java
blob: fd6e6fe5ec27ed6fd5fa5542fc738a19c9de374d (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
// Copyright 2015 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.content.Context;

import org.jni_zero.JNINamespace;
import org.jni_zero.NativeMethods;

import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.UrlUtils;

/**
 * Wrapper class to start a Quic test server.
 */
@JNINamespace("cronet")
public final class QuicTestServer {
    private static final String TAG = QuicTestServer.class.getSimpleName();

    private static final String CERT_USED = "quic-chain.pem";
    private static final String KEY_USED = "quic-leaf-cert.key";
    private static final String[] CERTS_USED = {CERT_USED};

    private static boolean sServerRunning;

    /*
     * Starts the server.
     */
    public static void startQuicTestServer(Context context) {
        if (sServerRunning) {
            throw new IllegalStateException("Quic server is already running");
        }
        TestFilesInstaller.installIfNeeded(context);
        QuicTestServerJni.get().startQuicTestServer(
                TestFilesInstaller.getInstalledPath(context), UrlUtils.getIsolatedTestRoot());
        sServerRunning = true;
    }

    /**
     * Shuts down the server. No-op if the server is already shut down.
     */
    public static void shutdownQuicTestServer() {
        if (!sServerRunning) {
            return;
        }
        QuicTestServerJni.get().shutdownQuicTestServer();
        sServerRunning = false;
    }

    public static String getServerURL() {
        return "https://" + getServerHost() + ":" + getServerPort();
    }

    public static String getServerHost() {
        return CronetTestUtil.QUIC_FAKE_HOST;
    }

    public static int getServerPort() {
        return QuicTestServerJni.get().getServerPort();
    }

    public static void delayResponse(String path, int delayInSeconds) {
        QuicTestServerJni.get().delayResponse(path, delayInSeconds);
    }

    public static final String getServerCert() {
        return CERT_USED;
    }

    public static final String getServerCertKey() {
        return KEY_USED;
    }

    public static long createMockCertVerifier() {
        TestFilesInstaller.installIfNeeded(ContextUtils.getApplicationContext());
        return MockCertVerifier.createMockCertVerifier(CERTS_USED, true);
    }

    @NativeMethods("cronet_tests")
    interface Natives {
        /*
         * Runs a quic test server synchronously.
         */
        void startQuicTestServer(String filePath, String testDataDir);

        /*
         * Shutdowns the quic test-server synchronously.
         *
         * Calling this without calling startQuicTestServer first will lead to unexpected
         * behavior if not compiled in debug mode.
         */
        void shutdownQuicTestServer();
        int getServerPort();

        /*
         * Responses for path will be delayed by delayInSeconds.
         *
         * Ideally this wouldn't take a delay. Instead, it should provide a synchronization
         * mechanism that allows the caller to unblock the request. This would require changes all
         * the way down to QUICHE though.
         */
        void delayResponse(String path, int delayInSeconds);
    }
}