1 // Copyright 2016 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 package org.chromium.net.smoke;
6 
7 import android.content.Context;
8 
9 import org.json.JSONObject;
10 
11 import org.chromium.base.Log;
12 import org.chromium.net.CronetTestUtil;
13 import org.chromium.net.ExperimentalCronetEngine;
14 
15 /** Provides support for tests that depend on QUIC and HTTP2 servers. */
16 class ChromiumNativeTestSupport extends ChromiumPlatformOnlyTestSupport {
17     private static final String TAG = ChromiumNativeTestSupport.class.getSimpleName();
18 
19     @Override
createTestServer(Context context, Protocol protocol)20     public TestServer createTestServer(Context context, Protocol protocol) {
21         switch (protocol) {
22             case QUIC:
23                 return new QuicTestServer(context);
24             case HTTP2:
25                 return new Http2TestServer(context);
26             case HTTP1:
27                 return super.createTestServer(context, protocol);
28             default:
29                 throw new RuntimeException("Unknown server protocol: " + protocol);
30         }
31     }
32 
33     @Override
addHostResolverRules(JSONObject experimentalOptionsJson)34     public void addHostResolverRules(JSONObject experimentalOptionsJson) {
35         try {
36             JSONObject hostResolverParams = CronetTestUtil.generateHostResolverRules();
37             experimentalOptionsJson.put("HostResolverRules", hostResolverParams);
38         } catch (Exception e) {
39             throw new RuntimeException(e);
40         }
41     }
42 
43     @Override
installMockCertVerifierForTesting(ExperimentalCronetEngine.Builder builder)44     public void installMockCertVerifierForTesting(ExperimentalCronetEngine.Builder builder) {
45         CronetTestUtil.setMockCertVerifierForTesting(
46                 builder, org.chromium.net.QuicTestServer.createMockCertVerifier());
47     }
48 
49     @Override
loadTestNativeLibrary()50     public void loadTestNativeLibrary() {
51         System.loadLibrary("cronet_tests");
52     }
53 
54     private static class QuicTestServer implements TestServer {
55         private final Context mContext;
56 
QuicTestServer(Context context)57         QuicTestServer(Context context) {
58             mContext = context;
59         }
60 
61         @Override
start()62         public boolean start() {
63             org.chromium.net.QuicTestServer.startQuicTestServer(mContext);
64             return true;
65         }
66 
67         @Override
shutdown()68         public void shutdown() {
69             org.chromium.net.QuicTestServer.shutdownQuicTestServer();
70         }
71 
72         @Override
getSuccessURL()73         public String getSuccessURL() {
74             return org.chromium.net.QuicTestServer.getServerURL() + "/simple.txt";
75         }
76     }
77 
78     private static class Http2TestServer implements TestServer {
79         private final Context mContext;
80 
Http2TestServer(Context context)81         Http2TestServer(Context context) {
82             mContext = context;
83         }
84 
85         @Override
start()86         public boolean start() {
87             try {
88                 return org.chromium.net.Http2TestServer.startHttp2TestServer(mContext);
89             } catch (Exception e) {
90                 Log.e(TAG, "Exception during Http2TestServer start", e);
91                 return false;
92             }
93         }
94 
95         @Override
shutdown()96         public void shutdown() {
97             try {
98                 org.chromium.net.Http2TestServer.shutdownHttp2TestServer();
99             } catch (Exception e) {
100                 Log.e(TAG, "Exception during Http2TestServer shutdown", e);
101             }
102         }
103 
104         @Override
getSuccessURL()105         public String getSuccessURL() {
106             return org.chromium.net.Http2TestServer.getEchoMethodUrl();
107         }
108     }
109 }
110