OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.support.test.filters.MediumTest; | 7 import android.support.test.filters.MediumTest; |
8 | 8 |
9 import org.json.JSONObject; | 9 import org.json.JSONObject; |
10 | 10 |
11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
12 import org.chromium.base.PathUtils; | 12 import org.chromium.base.PathUtils; |
13 import org.chromium.base.annotations.JNINamespace; | |
13 import org.chromium.base.test.util.Feature; | 14 import org.chromium.base.test.util.Feature; |
15 import org.chromium.net.impl.CronetUrlRequestContext; | |
16 import org.chromium.net.test.EmbeddedTestServer; | |
14 | 17 |
15 import java.io.File; | 18 import java.io.File; |
16 import java.io.FileInputStream; | 19 import java.io.FileInputStream; |
17 import java.io.FileNotFoundException; | 20 import java.io.FileNotFoundException; |
18 import java.io.IOException; | 21 import java.io.IOException; |
22 import java.net.URL; | |
19 | 23 |
20 /** | 24 /** |
21 * Tests for experimental options. | 25 * Tests for experimental options. |
22 */ | 26 */ |
27 @JNINamespace("cronet") | |
23 public class ExperimentalOptionsTest extends CronetTestBase { | 28 public class ExperimentalOptionsTest extends CronetTestBase { |
24 private static final String TAG = ExperimentalOptionsTest.class.getSimpleNam e(); | 29 private static final String TAG = ExperimentalOptionsTest.class.getSimpleNam e(); |
25 private ExperimentalCronetEngine.Builder mBuilder; | 30 private ExperimentalCronetEngine.Builder mBuilder; |
26 | 31 |
27 @Override | 32 @Override |
28 protected void setUp() throws Exception { | 33 protected void setUp() throws Exception { |
29 super.setUp(); | 34 super.setUp(); |
30 mBuilder = new ExperimentalCronetEngine.Builder(getContext()); | 35 mBuilder = new ExperimentalCronetEngine.Builder(getContext()); |
31 CronetTestUtil.setMockCertVerifierForTesting( | 36 CronetTestUtil.setMockCertVerifierForTesting( |
32 mBuilder, QuicTestServer.createMockCertVerifier()); | 37 mBuilder, QuicTestServer.createMockCertVerifier()); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 // Ignored this exception since the file will only be created when u pdates are | 133 // Ignored this exception since the file will only be created when u pdates are |
129 // flushed to the disk. | 134 // flushed to the disk. |
130 Log.i(TAG, "file not found"); | 135 Log.i(TAG, "file not found"); |
131 } finally { | 136 } finally { |
132 if (fileInputStream != null) { | 137 if (fileInputStream != null) { |
133 fileInputStream.close(); | 138 fileInputStream.close(); |
134 } | 139 } |
135 } | 140 } |
136 return false; | 141 return false; |
137 } | 142 } |
143 | |
144 @MediumTest | |
145 @Feature({"Cronet"}) | |
146 @OnlyRunNativeCronet | |
147 // Tests that basic Cronet functionality works when host cache persistence i s enabled, and that | |
148 // persistence works. | |
149 public void testHostCachePersistence() throws Exception { | |
150 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( getContext()); | |
151 | |
152 String realUrl = testServer.getURL("/echo?status=200"); | |
153 URL javaUrl = new URL(realUrl); | |
154 String realHost = javaUrl.getHost(); | |
155 int realPort = javaUrl.getPort(); | |
156 String testHost = "host-cache-test-host"; | |
157 String testUrl = new URL("http", testHost, realPort, javaUrl.getPath()). toString(); | |
158 | |
159 mBuilder.setStoragePath(getTestStorage(getContext())); | |
160 | |
161 // Set a short delay so the pref gets written quickly. | |
162 JSONObject staleDns = new JSONObject() | |
163 .put("enable", true) | |
164 .put("delay_ms", 0) | |
165 .put("allow_other_network", true) | |
166 .put("persist_to_disk", true) | |
167 .put("persist_delay_ms", 0); | |
168 JSONObject experimentalOptions = new JSONObject().put("StaleDNS", staleD ns); | |
169 mBuilder.setExperimentalOptions(experimentalOptions.toString()); | |
170 CronetUrlRequestContext context = (CronetUrlRequestContext) mBuilder.bui ld(); | |
171 | |
172 // Create a HostCache entry for "host-cache-test-host". | |
173 nativeWriteToHostCache(context.getUrlRequestContextAdapter(), realHost); | |
174 | |
175 // Do a request for the test URL to make sure it's cached. | |
176 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | |
177 UrlRequest.Builder builder = | |
178 context.newUrlRequestBuilder(testUrl, callback, callback.getExec utor()); | |
179 UrlRequest urlRequest = builder.build(); | |
180 urlRequest.start(); | |
181 callback.blockForDone(); | |
182 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | |
183 | |
184 // Shut down the context, persisting contents to disk, and build a new o ne. | |
185 context.shutdown(); | |
186 context = (CronetUrlRequestContext) mBuilder.build(); | |
187 | |
188 // Use the test URL without creating a new cache entry first. It should use the persisted | |
189 // one. | |
190 callback = new TestUrlRequestCallback(); | |
191 builder = context.newUrlRequestBuilder(testUrl, callback, callback.getEx ecutor()); | |
192 urlRequest = builder.build(); | |
193 urlRequest.start(); | |
194 callback.blockForDone(); | |
pauljensen
2017/06/27 11:27:11
is this racing the load of the host cache persiste
mgersh
2017/06/27 14:07:20
No, prefs loading is synchronous in Cronet.
| |
195 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | |
196 } | |
197 | |
198 // Sets a host cache entry with hostname "host-cache-test-host" and an Addre ssList containing | |
199 // the provided address. | |
200 private static native void nativeWriteToHostCache(long adapter, String addre ss); | |
138 } | 201 } |
OLD | NEW |