Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f3b1d5e1ee90d8b04501dcc987f981ad4cd7d00a |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java |
| @@ -0,0 +1,343 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net.urlconnection; |
| + |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.net.CronetTestActivity; |
| +import org.chromium.net.CronetTestBase; |
| +import org.chromium.net.NativeTestServer; |
| + |
| +import java.io.ByteArrayOutputStream; |
| +import java.io.InputStream; |
| +import java.io.OutputStream; |
| +import java.net.HttpURLConnection; |
| +import java.net.ProtocolException; |
| +import java.net.URL; |
| + |
| +/** |
| + * Tests {@code getOutputStream} when {@code setFixedLengthStreamingMode} is |
| + * enabled. |
| + * Tests annotated with {@code CompareDefaultWithCronet} will run once with the |
| + * default HttpURLConnection implementation and then with Cronet's |
| + * HttpURLConnection implementation. Tests annotated with |
| + * {@code OnlyRunCronetHttpURLConnection} only run Cronet's implementation. |
| + * See {@link CronetTestBase#runTest()} for details. |
| + */ |
| +public class CronetFixedModeOutputStreamTest extends CronetTestBase { |
| + private static final String UPLOAD_DATA_STRING = "Nifty upload data!"; |
| + private static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes(); |
| + private static final int REPEAT_COUNT = 100000; |
| + private CronetTestActivity mActivity; |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + mActivity = launchCronetTestApp(); |
| + assertTrue(NativeTestServer.startNativeTestServer( |
| + getInstrumentation().getTargetContext())); |
| + } |
| + |
| + @Override |
| + protected void tearDown() throws Exception { |
| + NativeTestServer.shutdownNativeTestServer(); |
| + super.tearDown(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testFixedLengthStreamingModeZeroContentLength() |
|
mmenke
2015/04/01 17:41:05
Didn't you say that with fix length streaming mode
xunjieli
2015/04/01 19:04:55
Done.
|
| + throws Exception { |
| + // Check content length is set. |
| + URL echoLength = new URL(NativeTestServer.getEchoHeaderURL("Content-Length")); |
| + HttpURLConnection connection1 = |
| + (HttpURLConnection) echoLength.openConnection(); |
| + connection1.setDoOutput(true); |
| + connection1.setRequestMethod("POST"); |
| + connection1.setFixedLengthStreamingMode(0); |
| + assertEquals(200, connection1.getResponseCode()); |
| + assertEquals("OK", connection1.getResponseMessage()); |
| + assertEquals("0", getResponseAsString(connection1)); |
| + connection1.disconnect(); |
| + |
| + // Check body is empty. |
| + URL echoBody = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection2 = |
| + (HttpURLConnection) echoBody.openConnection(); |
| + connection2.setDoOutput(true); |
| + connection2.setRequestMethod("POST"); |
| + connection2.setFixedLengthStreamingMode(0); |
| + assertEquals(200, connection2.getResponseCode()); |
| + assertEquals("OK", connection2.getResponseMessage()); |
| + assertEquals("", getResponseAsString(connection2)); |
| + connection2.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testWriteLessThanContentLength() |
| + throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + // Set a content length that's 1 byte more. |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length + 1); |
| + OutputStream out = connection.getOutputStream(); |
| + out.write(UPLOAD_DATA); |
| + try { |
| + connection.getResponseCode(); |
| + fail(); |
| + } catch (ProtocolException e) { |
| + // Expected. |
| + } |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testWriteMoreThanContentLength() |
| + throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + // Set a content length that's 1 byte short. |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1); |
| + OutputStream out = connection.getOutputStream(); |
| + try { |
| + out.write(UPLOAD_DATA); |
| + fail(); |
| + } catch (ProtocolException e) { |
| + // Expected. |
| + assertEquals("expected " + (UPLOAD_DATA.length - 1) |
| + + " bytes but received " |
| + + UPLOAD_DATA.length, e.getMessage()); |
| + } |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testWriteMoreThanContentLengthWriteOneByte() |
| + throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + // Set a content length that's 1 byte short. |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1); |
| + OutputStream out = connection.getOutputStream(); |
| + for (int i = 0; i < UPLOAD_DATA.length - 1; i++) { |
| + out.write(UPLOAD_DATA[i]); |
| + } |
| + try { |
| + // Try upload an extra byte. |
| + out.write(UPLOAD_DATA[UPLOAD_DATA.length - 1]); |
| + fail(); |
| + } catch (ProtocolException e) { |
| + // Expected. |
| + assertEquals("expected 0 bytes but received 1", e.getMessage()); |
| + } |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testFixedLengthStreamingMode() throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); |
| + OutputStream out = connection.getOutputStream(); |
| + out.write(UPLOAD_DATA); |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("OK", connection.getResponseMessage()); |
| + assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection)); |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testFixedLengthStreamingModeWriteOneByte() |
| + throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); |
| + OutputStream out = connection.getOutputStream(); |
| + for (int i = 0; i < UPLOAD_DATA.length; i++) { |
| + // Write one byte at a time. |
| + out.write(UPLOAD_DATA[i]); |
| + } |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("OK", connection.getResponseMessage()); |
| + assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection)); |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testFixedLengthStreamingModeLargeData() throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + byte[] largeData = getLargeData(); |
| + connection.setFixedLengthStreamingMode(largeData.length); |
| + OutputStream out = connection.getOutputStream(); |
| + int totalBytesWritten = 0; |
| + // Number of bytes to write each time. It is doubled each time |
| + // to make sure that the implementation can handle large writes. |
| + int bytesToWrite = 683; |
| + while (totalBytesWritten < largeData.length) { |
| + if (bytesToWrite > largeData.length - totalBytesWritten) { |
| + // Do not write out of bound. |
| + bytesToWrite = largeData.length - totalBytesWritten; |
| + } |
| + out.write(largeData, totalBytesWritten, bytesToWrite); |
| + totalBytesWritten += bytesToWrite; |
| + bytesToWrite *= 2; |
| + } |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("OK", connection.getResponseMessage()); |
| + checkLargeData(getResponseAsString(connection)); |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testFixedLengthStreamingModeLargeDataWriteOneByte() |
| + throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + byte[] largeData = getLargeData(); |
| + connection.setFixedLengthStreamingMode(largeData.length); |
| + OutputStream out = connection.getOutputStream(); |
| + for (int i = 0; i < largeData.length; i++) { |
| + // Write one byte at a time. |
| + out.write(largeData[i]); |
| + } |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("OK", connection.getResponseMessage()); |
| + checkLargeData(getResponseAsString(connection)); |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @OnlyRunCronetHttpURLConnection |
| + public void testLargeDataMoreThanNativeBufferSize() |
| + throws Exception { |
| + // Set an internal buffer of size larger than the buffer size used |
| + // in network stack internally. |
| + // Normal stream uses 16384, QUIC uses 14520, and SPDY uses 2852. |
| + CronetFixedModeOutputStream.setDefaultBufferLengthForTesting(17384); |
| + testFixedLengthStreamingModeLargeDataWriteOneByte(); |
| + testFixedLengthStreamingModeLargeData(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @CompareDefaultWithCronet |
| + public void testOneMassiveWrite() throws Exception { |
| + URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + byte[] largeData = getLargeData(); |
| + connection.setFixedLengthStreamingMode(largeData.length); |
| + OutputStream out = connection.getOutputStream(); |
| + // Write everything at one go, so the data is larger than the buffer |
| + // used in CronetFixedModeOutputStream. |
| + out.write(largeData); |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("OK", connection.getResponseMessage()); |
| + checkLargeData(getResponseAsString(connection)); |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @OnlyRunCronetHttpURLConnection |
| + public void testRewind() throws Exception { |
| + // Post preserving redirect should fail. |
| + URL url = new URL(NativeTestServer.getRedirectToEchoBody()); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setDoOutput(true); |
| + connection.setRequestMethod("POST"); |
| + connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); |
| + try { |
| + OutputStream out = connection.getOutputStream(); |
| + out.write(UPLOAD_DATA); |
| + } catch (IllegalStateException e) { |
| + assertEquals("Rewind is not supported by CronetFixedModeOutputStream.", |
| + e.getMessage()); |
| + } |
| + connection.disconnect(); |
| + } |
| + |
| + /** |
| + * Helper method to extract response body as a string for testing. |
| + */ |
| + private String getResponseAsString(HttpURLConnection connection) |
| + throws Exception { |
| + InputStream in = connection.getInputStream(); |
| + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| + int b; |
| + while ((b = in.read()) != -1) { |
| + out.write(b); |
| + } |
| + return out.toString(); |
| + } |
| + |
| + /** |
| + * Produces a byte array that contains {@code REPEAT_COUNT} of |
| + * {@code UPLOAD_DATA_STRING}. |
| + */ |
| + private byte[] getLargeData() { |
| + byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length]; |
| + for (int i = 0; i < REPEAT_COUNT; i++) { |
| + for (int j = 0; j < UPLOAD_DATA.length; j++) { |
| + largeData[i * UPLOAD_DATA.length + j] = UPLOAD_DATA[j]; |
| + } |
| + } |
| + return largeData; |
| + } |
| + |
| + /** |
| + * Helper function to check whether {@code data} is a concatenation of |
| + * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings. |
| + */ |
| + private void checkLargeData(String data) { |
| + for (int i = 0; i < REPEAT_COUNT; i++) { |
| + assertEquals(UPLOAD_DATA_STRING, data.substring( |
| + UPLOAD_DATA_STRING.length() * i, |
| + UPLOAD_DATA_STRING.length() * (i + 1))); |
| + } |
| + } |
| +} |