| 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 | 13 |
| 14 import java.io.File; | 14 import java.io.File; |
| 15 import java.io.FileInputStream; | 15 import java.io.FileInputStream; |
| 16 import java.io.FileNotFoundException; | 16 import java.io.FileNotFoundException; |
| 17 import java.io.IOException; | 17 import java.io.IOException; |
| 18 import java.util.HashMap; | 18 import java.util.HashMap; |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Tests making requests using QUIC. | 21 * Tests making requests using QUIC. |
| 22 */ | 22 */ |
| 23 public class QuicTest extends CronetTestBase { | 23 public class QuicTest extends CronetTestBase { |
| 24 private static final String TAG = "cr.QuicTest"; | 24 private static final String TAG = "cr.QuicTest"; |
| 25 private static final String[] CERTS_USED = {"quic_test.example.com.crt"}; | 25 private static final String[] CERTS_USED = {"quic_test.example.com.crt"}; |
| 26 private CronetTestActivity mActivity; | 26 private CronetTestFramework mTestFramework; |
| 27 | 27 |
| 28 @Override | 28 @Override |
| 29 protected void setUp() throws Exception { | 29 protected void setUp() throws Exception { |
| 30 super.setUp(); | 30 super.setUp(); |
| 31 // Load library first, since we need the Quic test server's URL. | 31 // Load library first, since we need the Quic test server's URL. |
| 32 System.loadLibrary("cronet_tests"); | 32 System.loadLibrary("cronet_tests"); |
| 33 QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext
()); | 33 QuicTestServer.startQuicTestServer(getContext()); |
| 34 | 34 |
| 35 CronetEngine.Builder builder = new CronetEngine.Builder(mActivity); | 35 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 36 builder.enableQUIC(true); | 36 builder.enableQUIC(true); |
| 37 builder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getSe
rverPort(), | 37 builder.addQuicHint(QuicTestServer.getServerHost(), QuicTestServer.getSe
rverPort(), |
| 38 QuicTestServer.getServerPort()); | 38 QuicTestServer.getServerPort()); |
| 39 builder.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF"); | 39 builder.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF"); |
| 40 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); | 40 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); |
| 41 | 41 |
| 42 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, builder.toStr
ing(), | 42 String[] commandLineArgs = {CronetTestFramework.CONFIG_KEY, builder.toSt
ring(), |
| 43 CronetTestActivity.CACHE_KEY, CronetTestActivity.CACHE_DISK_NO_H
TTP}; | 43 CronetTestFramework.CACHE_KEY, CronetTestFramework.CACHE_DISK_NO
_HTTP}; |
| 44 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLi
neArgs); | 44 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null,
commandLineArgs); |
| 45 } | 45 } |
| 46 | 46 |
| 47 @Override | 47 @Override |
| 48 protected void tearDown() throws Exception { | 48 protected void tearDown() throws Exception { |
| 49 QuicTestServer.shutdownQuicTestServer(); | 49 QuicTestServer.shutdownQuicTestServer(); |
| 50 super.tearDown(); | 50 super.tearDown(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 @SmallTest | 53 @SmallTest |
| 54 @Feature({"Cronet"}) | 54 @Feature({"Cronet"}) |
| 55 public void testQuicLoadUrl_LegacyAPI() throws Exception { | 55 public void testQuicLoadUrl_LegacyAPI() throws Exception { |
| 56 long urlRequestContextAdapter = ((ChromiumUrlRequestFactory) mActivity.m
RequestFactory) | 56 long urlRequestContextAdapter = ((ChromiumUrlRequestFactory) mTestFramew
ork.mRequestFactory) |
| 57 .getRequestContext() | 57 .getRequestContext() |
| 58 .getUrlRequestContextAdapter(); | 58 .getUrlRequestContextAdapter(); |
| 59 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, true
); | 59 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, true
); |
| 60 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 60 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 61 | 61 |
| 62 HashMap<String, String> headers = new HashMap<String, String>(); | 62 HashMap<String, String> headers = new HashMap<String, String>(); |
| 63 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 63 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 64 | 64 |
| 65 // Although the native stack races QUIC and SPDY for the first request, | 65 // Although the native stack races QUIC and SPDY for the first request, |
| 66 // since there is no http server running on the corresponding TCP port, | 66 // since there is no http server running on the corresponding TCP port, |
| 67 // QUIC will always succeed with a 200 (see | 67 // QUIC will always succeed with a 200 (see |
| 68 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 68 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 69 HttpUrlRequest request = mActivity.mRequestFactory.createRequest( | 69 HttpUrlRequest request = mTestFramework.mRequestFactory.createRequest( |
| 70 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); | 70 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); |
| 71 request.start(); | 71 request.start(); |
| 72 listener.blockForComplete(); | 72 listener.blockForComplete(); |
| 73 assertEquals(200, listener.mHttpStatusCode); | 73 assertEquals(200, listener.mHttpStatusCode); |
| 74 assertEquals( | 74 assertEquals( |
| 75 "This is a simple text file served by QUIC.\n", | 75 "This is a simple text file served by QUIC.\n", |
| 76 listener.mResponseAsString); | 76 listener.mResponseAsString); |
| 77 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); | 77 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); |
| 78 } | 78 } |
| 79 | 79 |
| 80 @LargeTest | 80 @LargeTest |
| 81 @Feature({"Cronet"}) | 81 @Feature({"Cronet"}) |
| 82 public void testQuicLoadUrl() throws Exception { | 82 public void testQuicLoadUrl() throws Exception { |
| 83 long urlRequestContextAdapter = | 83 long urlRequestContextAdapter = ((CronetUrlRequestContext) mTestFramewor
k.mCronetEngine) |
| 84 ((CronetUrlRequestContext) mActivity.mCronetEngine).getUrlReques
tContextAdapter(); | 84 .getUrlRequestContextAdapter(); |
| 85 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, fals
e); | 85 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, fals
e); |
| 86 | 86 |
| 87 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 87 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 88 TestUrlRequestListener listener = new TestUrlRequestListener(); | 88 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 89 | 89 |
| 90 // Although the native stack races QUIC and SPDY for the first request, | 90 // Although the native stack races QUIC and SPDY for the first request, |
| 91 // since there is no http server running on the corresponding TCP port, | 91 // since there is no http server running on the corresponding TCP port, |
| 92 // QUIC will always succeed with a 200 (see | 92 // QUIC will always succeed with a 200 (see |
| 93 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 93 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 94 UrlRequest.Builder requestBuilder = new UrlRequest.Builder( | 94 UrlRequest.Builder requestBuilder = new UrlRequest.Builder( |
| 95 quicURL, listener, listener.getExecutor(), mActivity.mCronetEngi
ne); | 95 quicURL, listener, listener.getExecutor(), mTestFramework.mCrone
tEngine); |
| 96 requestBuilder.build().start(); | 96 requestBuilder.build().start(); |
| 97 listener.blockForDone(); | 97 listener.blockForDone(); |
| 98 | 98 |
| 99 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 99 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 100 String expectedContent = "This is a simple text file served by QUIC.\n"; | 100 String expectedContent = "This is a simple text file served by QUIC.\n"; |
| 101 assertEquals(expectedContent, listener.mResponseAsString); | 101 assertEquals(expectedContent, listener.mResponseAsString); |
| 102 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc
ol()); | 102 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc
ol()); |
| 103 // The total received bytes should be larger than the content length, to
account for | 103 // The total received bytes should be larger than the content length, to
account for |
| 104 // headers. | 104 // headers. |
| 105 assertTrue(listener.mResponseInfo.getReceivedBytesCount() > expectedCont
ent.length()); | 105 assertTrue(listener.mResponseInfo.getReceivedBytesCount() > expectedCont
ent.length()); |
| 106 | 106 |
| 107 // This test takes a long time, since the update will only be scheduled | 107 // This test takes a long time, since the update will only be scheduled |
| 108 // after kUpdatePrefsDelayMs in http_server_properties_manager.cc. | 108 // after kUpdatePrefsDelayMs in http_server_properties_manager.cc. |
| 109 while (true) { | 109 while (true) { |
| 110 Log.i(TAG, "Still waiting for pref file update....."); | 110 Log.i(TAG, "Still waiting for pref file update....."); |
| 111 Thread.sleep(10000); | 111 Thread.sleep(10000); |
| 112 boolean contains = false; | 112 boolean contains = false; |
| 113 try { | 113 try { |
| 114 if (fileContainsString("local_prefs.json", "quic")) break; | 114 if (fileContainsString("local_prefs.json", "quic")) break; |
| 115 } catch (FileNotFoundException e) { | 115 } catch (FileNotFoundException e) { |
| 116 // Ignored this exception since the file will only be created wh
en updates are | 116 // Ignored this exception since the file will only be created wh
en updates are |
| 117 // flushed to the disk. | 117 // flushed to the disk. |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 assertTrue(fileContainsString("local_prefs.json", | 120 assertTrue(fileContainsString("local_prefs.json", |
| 121 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP
ort())); | 121 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP
ort())); |
| 122 mActivity.mCronetEngine.shutdown(); | 122 mTestFramework.mCronetEngine.shutdown(); |
| 123 | 123 |
| 124 // Make another request using a new context but with no QUIC hints. | 124 // Make another request using a new context but with no QUIC hints. |
| 125 CronetEngine.Builder builder = | 125 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 126 new CronetEngine.Builder(getInstrumentation().getTargetContext()
); | 126 builder.setStoragePath(mTestFramework.getTestStorage()); |
| 127 builder.setStoragePath(mActivity.getTestStorage()); | |
| 128 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); | 127 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, 1000 * 102
4); |
| 129 builder.enableQUIC(true); | 128 builder.enableQUIC(true); |
| 130 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); | 129 builder.setMockCertVerifierForTesting(MockCertVerifier.createMockCertVer
ifier(CERTS_USED)); |
| 131 CronetEngine newEngine = new CronetUrlRequestContext(builder); | 130 CronetEngine newEngine = new CronetUrlRequestContext(builder); |
| 132 long newUrlRequestContextAdapter = | 131 long newUrlRequestContextAdapter = |
| 133 ((CronetUrlRequestContext) newEngine).getUrlRequestContextAdapte
r(); | 132 ((CronetUrlRequestContext) newEngine).getUrlRequestContextAdapte
r(); |
| 134 NativeTestServer.registerHostResolverProc(newUrlRequestContextAdapter, f
alse); | 133 NativeTestServer.registerHostResolverProc(newUrlRequestContextAdapter, f
alse); |
| 135 TestUrlRequestListener listener2 = new TestUrlRequestListener(); | 134 TestUrlRequestListener listener2 = new TestUrlRequestListener(); |
| 136 requestBuilder = | 135 requestBuilder = |
| 137 new UrlRequest.Builder(quicURL, listener2, listener2.getExecutor
(), newEngine); | 136 new UrlRequest.Builder(quicURL, listener2, listener2.getExecutor
(), newEngine); |
| 138 requestBuilder.build().start(); | 137 requestBuilder.build().start(); |
| 139 listener2.blockForDone(); | 138 listener2.blockForDone(); |
| 140 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); | 139 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); |
| 141 assertEquals(expectedContent, listener2.mResponseAsString); | 140 assertEquals(expectedContent, listener2.mResponseAsString); |
| 142 assertEquals("quic/1+spdy/3", listener2.mResponseInfo.getNegotiatedProto
col()); | 141 assertEquals("quic/1+spdy/3", listener2.mResponseInfo.getNegotiatedProto
col()); |
| 143 // The total received bytes should be larger than the content length, to
account for | 142 // The total received bytes should be larger than the content length, to
account for |
| 144 // headers. | 143 // headers. |
| 145 assertTrue(listener2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); | 144 assertTrue(listener2.mResponseInfo.getReceivedBytesCount() > expectedCon
tent.length()); |
| 146 } | 145 } |
| 147 | 146 |
| 148 // Returns whether a file contains a particular string. | 147 // Returns whether a file contains a particular string. |
| 149 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") | 148 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
| 150 private boolean fileContainsString(String filename, String content) throws I
OException { | 149 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 151 File file = new File(mActivity.getTestStorage() + "/" + filename); | 150 File file = new File(mTestFramework.getTestStorage() + "/" + filename); |
| 152 FileInputStream fileInputStream = new FileInputStream(file); | 151 FileInputStream fileInputStream = new FileInputStream(file); |
| 153 byte[] data = new byte[(int) file.length()]; | 152 byte[] data = new byte[(int) file.length()]; |
| 154 fileInputStream.read(data); | 153 fileInputStream.read(data); |
| 155 fileInputStream.close(); | 154 fileInputStream.close(); |
| 156 return new String(data, "UTF-8").contains(content); | 155 return new String(data, "UTF-8").contains(content); |
| 157 } | 156 } |
| 158 } | 157 } |
| OLD | NEW |