1 // Copyright 2016 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 package org.chromium.net.smoke;
6 
7 import android.content.Context;
8 
9 import org.json.JSONObject;
10 
11 import org.chromium.net.ExperimentalCronetEngine;
12 
13 import java.io.File;
14 
15 /**
16  * Provides support for tests, so they can be run in different environments against different
17  * servers. It contains methods, which behavior can be different in different testing environments.
18  * The concrete implementation of this interface is determined dynamically at runtime by reading
19  * the value of |TestSupportImplClass| from the Android string resource file.
20  */
21 public interface TestSupport {
22     enum Protocol {
23         HTTP1,
24         HTTP2,
25         QUIC,
26     }
27 
28     /**
29      * Creates a new test server that supports a given {@code protocol}.
30      *
31      * @param context context.
32      * @param protocol protocol that should be supported by the server.
33      * @return an instance of the server.
34      *
35      * @throws UnsupportedOperationException if the implementation of this interface
36      *                                       does not support a given {@code protocol}.
37      */
createTestServer(Context context, Protocol protocol)38     TestServer createTestServer(Context context, Protocol protocol);
39 
40     /**
41      * This method is called at the end of a test run if the netlog is available. An implementer
42      * of {@link TestSupport} can use it to process the result netlog; e.g., to copy the netlog
43      * to a directory where all test logs are collected. This method is optional and can be no-op.
44      *
45      * @param file the netlog file.
46      */
processNetLog(Context context, File file)47     void processNetLog(Context context, File file);
48 
49     /**
50      * Adds host resolver rules to a given experimental option JSON file.
51      * This method is optional.
52      *
53      * @param experimentalOptionsJson experimental options.
54      */
addHostResolverRules(JSONObject experimentalOptionsJson)55     void addHostResolverRules(JSONObject experimentalOptionsJson);
56 
57     /**
58      * Installs mock certificate verifier for a given {@code builder}.
59      * This method is optional.
60      *
61      * @param builder that should have the verifier installed.
62      */
installMockCertVerifierForTesting(ExperimentalCronetEngine.Builder builder)63     void installMockCertVerifierForTesting(ExperimentalCronetEngine.Builder builder);
64 
65     /**
66      * Loads a native library that is required for testing if any required.
67      */
loadTestNativeLibrary()68     void loadTestNativeLibrary();
69 
70     /**
71      * A test server.
72      */
73     interface TestServer {
74         /**
75          * Starts the server.
76          *
77          * @return true if the server started successfully.
78          */
start()79         boolean start();
80 
81         /**
82          * Shuts down the server.
83          */
shutdown()84         void shutdown();
85 
86         /**
87          * Return a URL that can be used by the test code to receive a successful response.
88          *
89          * @return the URL as a string.
90          */
getSuccessURL()91         String getSuccessURL();
92     }
93 }
94