| 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.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 8 | 9 |
| 10 import org.chromium.base.Log; |
| 9 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
| 10 | 12 |
| 13 import java.io.File; |
| 14 import java.io.FileInputStream; |
| 15 import java.io.FileNotFoundException; |
| 16 import java.io.IOException; |
| 11 import java.util.HashMap; | 17 import java.util.HashMap; |
| 12 | 18 |
| 13 /** | 19 /** |
| 14 * Tests making requests using QUIC. | 20 * Tests making requests using QUIC. |
| 15 */ | 21 */ |
| 16 public class QuicTest extends CronetTestBase { | 22 public class QuicTest extends CronetTestBase { |
| 17 | 23 private static final String TAG = "cr.QuicTest"; |
| 18 private CronetTestActivity mActivity; | 24 private CronetTestActivity mActivity; |
| 19 | 25 |
| 20 @Override | 26 @Override |
| 21 protected void setUp() throws Exception { | 27 protected void setUp() throws Exception { |
| 22 super.setUp(); | 28 super.setUp(); |
| 23 // Load library first, since we need the Quic test server's URL. | 29 // Load library first, since we need the Quic test server's URL. |
| 24 System.loadLibrary("cronet_tests"); | 30 System.loadLibrary("cronet_tests"); |
| 25 QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext
()); | 31 QuicTestServer.startQuicTestServer(getInstrumentation().getTargetContext
()); |
| 26 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 32 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 27 config.enableQUIC(true); | 33 config.enableQUIC(true); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 56 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); | 62 quicURL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listen
er); |
| 57 request.start(); | 63 request.start(); |
| 58 listener.blockForComplete(); | 64 listener.blockForComplete(); |
| 59 assertEquals(200, listener.mHttpStatusCode); | 65 assertEquals(200, listener.mHttpStatusCode); |
| 60 assertEquals( | 66 assertEquals( |
| 61 "This is a simple text file served by QUIC.\n", | 67 "This is a simple text file served by QUIC.\n", |
| 62 listener.mResponseAsString); | 68 listener.mResponseAsString); |
| 63 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); | 69 assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol); |
| 64 } | 70 } |
| 65 | 71 |
| 66 @SmallTest | 72 @LargeTest |
| 67 @Feature({"Cronet"}) | 73 @Feature({"Cronet"}) |
| 68 public void testQuicLoadUrl() throws Exception { | 74 public void testQuicLoadUrl() throws Exception { |
| 69 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; | 75 String quicURL = QuicTestServer.getServerURL() + "/simple.txt"; |
| 70 TestUrlRequestListener listener = new TestUrlRequestListener(); | 76 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 71 | 77 |
| 72 // Although the native stack races QUIC and SPDY for the first request, | 78 // Although the native stack races QUIC and SPDY for the first request, |
| 73 // since there is no http server running on the corresponding TCP port, | 79 // since there is no http server running on the corresponding TCP port, |
| 74 // QUIC will always succeed with a 200 (see | 80 // QUIC will always succeed with a 200 (see |
| 75 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). | 81 // net::HttpStreamFactoryImpl::Request::OnStreamFailed). |
| 76 UrlRequest request = mActivity.mUrlRequestContext.createRequest( | 82 UrlRequest request = mActivity.mUrlRequestContext.createRequest( |
| 77 quicURL, listener, listener.getExecutor()); | 83 quicURL, listener, listener.getExecutor()); |
| 78 request.start(); | 84 request.start(); |
| 79 listener.blockForDone(); | 85 listener.blockForDone(); |
| 80 | 86 |
| 81 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 87 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 82 assertEquals("This is a simple text file served by QUIC.\n", listener.mR
esponseAsString); | 88 assertEquals("This is a simple text file served by QUIC.\n", listener.mR
esponseAsString); |
| 83 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc
ol()); | 89 assertEquals("quic/1+spdy/3", listener.mResponseInfo.getNegotiatedProtoc
ol()); |
| 90 |
| 91 // This test takes a long time, since the update will only be scheduled |
| 92 // after kUpdatePrefsDelayMs in http_server_properties_manager.cc. |
| 93 while (true) { |
| 94 Log.i(TAG, "Still waiting for pref file update....."); |
| 95 Thread.sleep(10000); |
| 96 boolean contains = false; |
| 97 try { |
| 98 contains = fileContainsString("local_prefs.json", "quic"); |
| 99 } catch (FileNotFoundException e) { |
| 100 // Ignored this exception since file will only be created when u
pdated is flushed to |
| 101 // disk. |
| 102 } |
| 103 if (contains) { |
| 104 break; |
| 105 } |
| 106 } |
| 107 assertTrue(fileContainsString("local_prefs.json", |
| 108 QuicTestServer.getServerHost() + ":" + QuicTestServer.getServerP
ort())); |
| 109 mActivity.mUrlRequestContext.shutdown(); |
| 110 |
| 111 // Make another request using a new context but with no quic hints. |
| 112 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 113 config.setStoragePath(mActivity.getTestStorage()); |
| 114 config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 1000 * 10
24); |
| 115 config.enableQUIC(true); |
| 116 CronetUrlRequestContext newContext = |
| 117 new CronetUrlRequestContext(getInstrumentation().getTargetContex
t(), config); |
| 118 TestUrlRequestListener listener2 = new TestUrlRequestListener(); |
| 119 UrlRequest request2 = newContext.createRequest(quicURL, listener2, liste
ner2.getExecutor()); |
| 120 request2.start(); |
| 121 listener2.blockForDone(); |
| 122 assertEquals(200, listener2.mResponseInfo.getHttpStatusCode()); |
| 123 assertEquals("This is a simple text file served by QUIC.\n", listener2.m
ResponseAsString); |
| 124 assertEquals("quic/1+spdy/3", listener2.mResponseInfo.getNegotiatedProto
col()); |
| 125 } |
| 126 |
| 127 // Returns whether a file contains a particular string. |
| 128 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 129 File file = new File(mActivity.getTestStorage() + "/" + filename); |
| 130 FileInputStream fileInputStream = new FileInputStream(file); |
| 131 byte[] data = new byte[(int) file.length()]; |
| 132 fileInputStream.read(data); |
| 133 fileInputStream.close(); |
| 134 return new String(data, "UTF-8").contains(content); |
| 84 } | 135 } |
| 85 } | 136 } |
| OLD | NEW |