| 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; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 super.tearDown(); | 59 super.tearDown(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 @SmallTest | 62 @SmallTest |
| 63 @Feature({"Cronet"}) | 63 @Feature({"Cronet"}) |
| 64 @SuppressWarnings("deprecation") | 64 @SuppressWarnings("deprecation") |
| 65 public void testQuicLoadUrl_LegacyAPI() throws Exception { | 65 public void testQuicLoadUrl_LegacyAPI() throws Exception { |
| 66 String[] commandLineArgs = { | 66 String[] commandLineArgs = { |
| 67 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar
yInitType.LEGACY}; | 67 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar
yInitType.LEGACY}; |
| 68 mTestFramework = new CronetTestFramework(null, commandLineArgs, getConte
xt(), mBuilder); | 68 mTestFramework = new CronetTestFramework(null, commandLineArgs, getConte
xt(), mBuilder); |
| 69 | 69 registerHostResolver(mTestFramework, true); |
| 70 long urlRequestContextAdapter = ((ChromiumUrlRequestFactory) mTestFramew
ork.mRequestFactory) | |
| 71 .getRequestContext() | |
| 72 .getUrlRequestContextAdapter(); | |
| 73 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, true
); | |
| 74 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 70 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 75 | 71 |
| 76 HashMap<String, String> headers = new HashMap<String, String>(); | 72 HashMap<String, String> headers = new HashMap<String, String>(); |
| 77 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 73 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 78 | 74 |
| 79 // Although the native stack races QUIC and SPDY for the first request, | 75 // Although the native stack races QUIC and SPDY for the first request, |
| 80 // since there is no http server running on the corresponding TCP port, | 76 // since there is no http server running on the corresponding TCP port, |
| 81 // QUIC will always succeed with a 200 (see | 77 // QUIC will always succeed with a 200 (see |
| 82 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 78 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 83 HttpUrlRequest request = mTestFramework.mRequestFactory.createRequest( | 79 HttpUrlRequest request = mTestFramework.mRequestFactory.createRequest( |
| 84 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); | 80 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); |
| 85 request.start(); | 81 request.start(); |
| 86 listener.blockForComplete(); | 82 listener.blockForComplete(); |
| 87 assertEquals(200, listener.mHttpStatusCode); | 83 assertEquals(200, listener.mHttpStatusCode); |
| 88 assertEquals( | 84 assertEquals( |
| 89 "This is a simple text file served by QUIC.\n", | 85 "This is a simple text file served by QUIC.\n", |
| 90 listener.mResponseAsString); | 86 listener.mResponseAsString); |
| 91 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); | 87 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); |
| 92 } | 88 } |
| 93 | 89 |
| 94 @LargeTest | 90 @LargeTest |
| 95 @Feature({"Cronet"}) | 91 @Feature({"Cronet"}) |
| 96 public void testQuicLoadUrl() throws Exception { | 92 public void testQuicLoadUrl() throws Exception { |
| 97 String[] commandLineArgs = { | 93 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, mBuilder); |
| 98 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar
yInitType.CRONET}; | 94 registerHostResolver(mTestFramework); |
| 99 mTestFramework = new CronetTestFramework(null, commandLineArgs, getConte
xt(), mBuilder); | |
| 100 | |
| 101 long urlRequestContextAdapter = ((CronetUrlRequestContext) mTestFramewor
k.mCronetEngine) | |
| 102 .getUrlRequestContextAdapter(); | |
| 103 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, fals
e); | |
| 104 | |
| 105 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 95 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 106 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 96 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 107 | 97 |
| 108 // Although the native stack races QUIC and SPDY for the first request, | 98 // Although the native stack races QUIC and SPDY for the first request, |
| 109 // since there is no http server running on the corresponding TCP port, | 99 // since there is no http server running on the corresponding TCP port, |
| 110 // QUIC will always succeed with a 200 (see | 100 // QUIC will always succeed with a 200 (see |
| 111 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 101 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 112 UrlRequest.Builder requestBuilder = new UrlRequest.Builder( | 102 UrlRequest.Builder requestBuilder = new UrlRequest.Builder( |
| 113 quicURL, callback, callback.getExecutor(), mTestFramework.mCrone
tEngine); | 103 quicURL, callback, callback.getExecutor(), mTestFramework.mCrone
tEngine); |
| 114 requestBuilder.build().start(); | 104 requestBuilder.build().start(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 138 assertTrue(fileContainsString("local_prefs.json", | 128 assertTrue(fileContainsString("local_prefs.json", |
| 139 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP
ort())); | 129 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP
ort())); |
| 140 mTestFramework.mCronetEngine.shutdown(); | 130 mTestFramework.mCronetEngine.shutdown(); |
| 141 | 131 |
| 142 // Make another request using a new context but with no QUIC hints. | 132 // Make another request using a new context but with no QUIC hints. |
| 143 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 133 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 144 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; | 134 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; |
| 145 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); | 135 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); |
| 146 builder.enableQUIC(true); | 136 builder.enableQUIC(true); |
| 147 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); | 137 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); |
| 148 CronetEngine newEngine = new CronetUrlRequestContext(builder); | 138 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n
ull, builder); |
| 149 long newUrlRequestContextAdapter = | 139 registerHostResolver(mTestFramework); |
| 150 ((CronetUrlRequestContext) newEngine).getUrlRequestContextAdapte
r(); | |
| 151 NativeTestServer.registerHostResolverProc(newUrlRequestContextAdapter, f
alse); | |
| 152 TestUrlRequestCallback callback2 = new TestUrlRequestCallback(); | 140 TestUrlRequestCallback callback2 = new TestUrlRequestCallback(); |
| 153 requestBuilder = | 141 requestBuilder = new UrlRequest.Builder( |
| 154 new UrlRequest.Builder(quicURL, callback2, callback2.getExecutor
(), newEngine); | 142 quicURL, callback2, callback2.getExecutor(), mTestFramework.mCro
netEngine); |
| 155 requestBuilder.build().start(); | 143 requestBuilder.build().start(); |
| 156 callback2.blockForDone(); | 144 callback2.blockForDone(); |
| 157 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); | 145 assertEquals(200, callback2.mResponseInfo.getHttpStatusCode()); |
| 158 assertEquals(expectedContent, callback2.mResponseAsString); | 146 assertEquals(expectedContent, callback2.mResponseAsString); |
| 159 assertEquals("quic/1+spdy/3", callback2.mResponseInfo.getNegotiatedProto
col()); | 147 assertEquals("quic/1+spdy/3", callback2.mResponseInfo.getNegotiatedProto
col()); |
| 160 // The total received bytes should be larger than the content length, to
account for | 148 // The total received bytes should be larger than the content length, to
account for |
| 161 // headers. | 149 // headers. |
| 162 assertTrue(callback2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); | 150 assertTrue(callback2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); |
| 163 } | 151 } |
| 164 | 152 |
| 165 // Returns whether a file contains a particular string. | 153 // Returns whether a file contains a particular string. |
| 166 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") | 154 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
| 167 private boolean fileContainsString(String filename, String content) throws I
OException { | 155 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 168 File file = new File(CronetTestFramework.getTestStorage(getContext()) +
"/" + filename); | 156 File file = new File(CronetTestFramework.getTestStorage(getContext()) +
"/" + filename); |
| 169 FileInputStream fileInputStream = new FileInputStream(file); | 157 FileInputStream fileInputStream = new FileInputStream(file); |
| 170 byte[] data = new byte[(int) file.length()]; | 158 byte[] data = new byte[(int) file.length()]; |
| 171 fileInputStream.read(data); | 159 fileInputStream.read(data); |
| 172 fileInputStream.close(); | 160 fileInputStream.close(); |
| 173 return new String(data, "UTF-8").contains(content); | 161 return new String(data, "UTF-8").contains(content); |
| 174 } | 162 } |
| 175 } | 163 } |
| OLD | NEW |