OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.test.suitebuilder.annotation.LargeTest; | 7 import android.test.suitebuilder.annotation.LargeTest; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
11 import org.chromium.base.annotations.SuppressFBWarnings; | 11 import org.chromium.base.annotations.SuppressFBWarnings; |
12 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
13 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; | 13 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; |
14 import org.json.JSONObject; | 14 import org.json.JSONObject; |
15 | 15 |
16 import java.io.File; | 16 import java.io.File; |
17 import java.io.FileInputStream; | 17 import java.io.FileInputStream; |
18 import java.io.FileNotFoundException; | 18 import java.io.FileNotFoundException; |
19 import java.io.IOException; | 19 import java.io.IOException; |
20 import java.util.HashMap; | 20 import java.util.HashMap; |
21 | 21 |
22 /** | 22 /** |
23 * Tests making requests using QUIC. | 23 * Tests making requests using QUIC. |
24 */ | 24 */ |
25 public class QuicTest extends CronetTestBase { | 25 public class QuicTest extends CronetTestBase { |
26 private static final String TAG = "cr.QuicTest"; | 26 private static final String TAG = "cr.QuicTest"; |
27 private static final String[] CERTS_USED = {"quic_test.example.com.crt"}; | |
28 private CronetTestFramework mTestFramework; | 27 private CronetTestFramework mTestFramework; |
29 private CronetEngine.Builder mBuilder; | 28 private CronetEngine.Builder mBuilder; |
30 | 29 |
31 @Override | 30 @Override |
32 protected void setUp() throws Exception { | 31 protected void setUp() throws Exception { |
33 super.setUp(); | 32 super.setUp(); |
34 // Load library first, since we need the Quic test server's URL. | 33 // Load library first, since we need the Quic test server's URL. |
35 System.loadLibrary("cronet_tests"); | 34 System.loadLibrary("cronet_tests"); |
36 QuicTestServer.startQuicTestServer(getContext()); | 35 QuicTestServer.startQuicTestServer(getContext()); |
37 | 36 |
38 mBuilder = new CronetEngine.Builder(getContext()); | 37 mBuilder = new CronetEngine.Builder(getContext()); |
39 mBuilder.enableQUIC(true); | 38 mBuilder.enableQUIC(true); |
40 mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getS
erverPort(), | 39 mBuilder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getS
erverPort(), |
41 QuicTestServer.getServerPort()); | 40 QuicTestServer.getServerPort()); |
42 | 41 |
43 JSONObject quicParams = new JSONObject() | 42 JSONObject quicParams = new JSONObject() |
44 .put("connection_options", "PACE,IW10,FO
O,DEADBEEF") | 43 .put("connection_options", "PACE,IW10,FO
O,DEADBEEF") |
45 .put("host_whitelist", "test.example.com
") | 44 .put("host_whitelist", "test.example.com
") |
46 .put("max_server_configs_stored_in_prope
rties", 2) | 45 .put("max_server_configs_stored_in_prope
rties", 2) |
47 .put("delay_tcp_race", true) | 46 .put("delay_tcp_race", true) |
48 .put("max_number_of_lossy_connections",
10) | 47 .put("max_number_of_lossy_connections",
10) |
49 .put("packet_loss_threshold", 0.5) | 48 .put("packet_loss_threshold", 0.5) |
50 .put("idle_connection_timeout_seconds",
300); | 49 .put("idle_connection_timeout_seconds",
300); |
51 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams
); | 50 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams
); |
52 mBuilder.setExperimentalOptions(experimentalOptions.toString()); | 51 mBuilder.setExperimentalOptions(experimentalOptions.toString()); |
53 | 52 |
54 mBuilder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVe
rifier(CERTS_USED)); | 53 mBuilder.setMockCertVerifierForTesting(QuicTestServer.createMockCertVeri
fier()); |
55 mBuilder.setStoragePath(CronetTestFramework.getTestStorage(getContext())
); | 54 mBuilder.setStoragePath(CronetTestFramework.getTestStorage(getContext())
); |
56 mBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1
000 * 1024); | 55 mBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 1
000 * 1024); |
57 } | 56 } |
58 | 57 |
59 @Override | 58 @Override |
60 protected void tearDown() throws Exception { | 59 protected void tearDown() throws Exception { |
61 QuicTestServer.shutdownQuicTestServer(); | 60 QuicTestServer.shutdownQuicTestServer(); |
62 super.tearDown(); | 61 super.tearDown(); |
63 } | 62 } |
64 | 63 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 mTestFramework.mCronetEngine.shutdown(); | 133 mTestFramework.mCronetEngine.shutdown(); |
135 | 134 |
136 // Make another request using a new context but with no QUIC hints. | 135 // Make another request using a new context but with no QUIC hints. |
137 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 136 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
138 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; | 137 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; |
139 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); | 138 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); |
140 builder.enableQUIC(true); | 139 builder.enableQUIC(true); |
141 JSONObject quicParams = new JSONObject().put("host_whitelist", "test.exa
mple.com"); | 140 JSONObject quicParams = new JSONObject().put("host_whitelist", "test.exa
mple.com"); |
142 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams
); | 141 JSONObject experimentalOptions = new JSONObject().put("QUIC", quicParams
); |
143 builder.setExperimentalOptions(experimentalOptions.toString()); | 142 builder.setExperimentalOptions(experimentalOptions.toString()); |
144 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); | 143 builder.setMockCertVerifierForTesting(QuicTestServer.createMockCertVerif
ier()); |
145 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, builder); | 144 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, builder); |
146 registerHostResolver(mTestFramework); | 145 registerHostResolver(mTestFramework); |
147 TestUrlRequestCallback callback2 = new TestUrlRequestCallback(); | 146 TestUrlRequestCallback callback2 = new TestUrlRequestCallback(); |
148 requestBuilder = new UrlRequest.Builder( | 147 requestBuilder = new UrlRequest.Builder( |
149 quicURL, callback2, callback2.getExecutor(), mTestFramework.mCro
netEngine); | 148 quicURL, callback2, callback2.getExecutor(), mTestFramework.mCro
netEngine); |
150 requestBuilder.build().start(); | 149 requestBuilder.build().start(); |
151 callback2.blockForDone(); | 150 callback2.blockForDone(); |
152 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); | 151 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); |
153 assertEquals(expectedContent, callback2.mResponseAsString); | 152 assertEquals(expectedContent, callback2.mResponseAsString); |
154 assertEquals("quic/1+spdy/3", callback2.mResponseInfo.getNegotiatedProto
col()); | 153 assertEquals("quic/1+spdy/3", callback2.mResponseInfo.getNegotiatedProto
col()); |
155 // The total received bytes should be larger than the content length, to
account for | 154 // The total received bytes should be larger than the content length, to
account for |
156 // headers. | 155 // headers. |
157 assertTrue(callback2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); | 156 assertTrue(callback2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); |
158 } | 157 } |
159 | 158 |
160 // Returns whether a file contains a particular string. | 159 // Returns whether a file contains a particular string. |
161 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") | 160 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
162 private boolean fileContainsString(String filename, String content) throws I
OException { | 161 private boolean fileContainsString(String filename, String content) throws I
OException { |
163 File file = new File(CronetTestFramework.getTestStorage(getContext()) +
"/" + filename); | 162 File file = new File(CronetTestFramework.getTestStorage(getContext()) +
"/" + filename); |
164 FileInputStream fileInputStream = new FileInputStream(file); | 163 FileInputStream fileInputStream = new FileInputStream(file); |
165 byte[] data = new byte[(int) file.length()]; | 164 byte[] data = new byte[(int) file.length()]; |
166 fileInputStream.read(data); | 165 fileInputStream.read(data); |
167 fileInputStream.close(); | 166 fileInputStream.close(); |
168 return new String(data, "UTF-8").contains(content); | 167 return new String(data, "UTF-8").contains(content); |
169 } | 168 } |
170 } | 169 } |
OLD | NEW |