Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Unified Diff: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java

Issue 1413303006: [Cronet] When connection is disconnected, InputStream#read should give an exception (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use @code Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
index 3507a493f7423e82f2a76ace5692c28e89ebc34d..e71e5439bcaa75b0d33bc71428022f6c943b9740 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
@@ -67,7 +67,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
assertEquals(200, urlConnection.getResponseCode());
assertEquals("OK", urlConnection.getResponseMessage());
- assertEquals("GET", getResponseAsString(urlConnection));
+ assertEquals("GET", TestUtil.getResponseAsString(urlConnection));
urlConnection.disconnect();
}
@@ -94,7 +94,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
out.write(data);
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- assertEquals(dataString, getResponseAsString(connection));
+ assertEquals(dataString, TestUtil.getResponseAsString(connection));
connection.disconnect();
}
}
@@ -134,8 +134,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
URL url = new URL(NativeTestServer.getFileURL("/success.txt"));
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
- assertEquals("this is a text file\n",
- getResponseAsString(urlConnection));
+ assertEquals("this is a text file\n", TestUtil.getResponseAsString(urlConnection));
// After shutting down the server, the server should not be handling
// new requests.
NativeTestServer.shutdownNativeTestServer();
@@ -229,7 +228,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
urlConnection.disconnect();
assertEquals(200, urlConnection.getResponseCode());
assertEquals("OK", urlConnection.getResponseMessage());
- assertEquals("GET", getResponseAsString(urlConnection));
+ assertEquals("GET", TestUtil.getResponseAsString(urlConnection));
}
@SmallTest
@@ -267,7 +266,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
assertEquals(200, urlConnection.getResponseCode());
assertEquals("OK", urlConnection.getResponseMessage());
- assertEquals("GET", getResponseAsString(urlConnection));
+ assertEquals("GET", TestUtil.getResponseAsString(urlConnection));
// Disconnect multiple times should be fine.
for (int i = 0; i < 10; i++) {
urlConnection.disconnect();
@@ -298,7 +297,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
// Check the request headers echoed back by the server.
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- String headers = getResponseAsString(connection);
+ String headers = TestUtil.getResponseAsString(connection);
List<String> fooHeaderValues =
getRequestHeaderValues(headers, "foo-header");
List<String> barHeaderValues =
@@ -370,7 +369,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
// Check the request headers echoed back by the server.
assertEquals(200, conn.getResponseCode());
assertEquals("OK", conn.getResponseMessage());
- String headers = getResponseAsString(conn);
+ String headers = TestUtil.getResponseAsString(conn);
List<String> actualValues1 =
getRequestHeaderValues(headers, "same-capitalization");
assertEquals(1, actualValues1.size());
@@ -401,7 +400,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
// Check the request headers echoed back by the server.
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- String headers = getResponseAsString(connection);
+ String headers = TestUtil.getResponseAsString(connection);
List<String> actualValues =
getRequestHeaderValues(headers, "Header-nAme");
assertEquals(1, actualValues.size());
@@ -529,15 +528,17 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
@SmallTest
@Feature({"Cronet"})
- @OnlyRunCronetHttpURLConnection
+ @CompareDefaultWithCronet
public void testInputStreamReadOneByte() throws Exception {
- String data = "MyBigFunkyData";
- int dataLength = data.length();
- int repeatCount = 100000;
- MockUrlRequestJobFactory.setUp();
- URL url = new URL(MockUrlRequestJobFactory.getMockUrlForData(data, repeatCount));
- HttpURLConnection connection =
- (HttpURLConnection) url.openConnection();
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ // Make the server echo a large request body, so it exceeds the internal
+ // read buffer.
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ byte[] largeData = TestUtil.getLargeData();
+ connection.setFixedLengthStreamingMode(largeData.length);
+ connection.getOutputStream().write(largeData);
InputStream in = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
@@ -547,13 +548,10 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
// All data has been read. Try reading beyond what is available should give -1.
assertEquals(-1, in.read());
- String responseData = new String(out.toByteArray());
- for (int i = 0; i < repeatCount; ++i) {
- assertEquals(data, responseData.substring(dataLength * i,
- dataLength * (i + 1)));
- }
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
+ String responseData = new String(out.toByteArray());
+ TestUtil.checkLargeData(responseData);
}
@SmallTest
@@ -673,6 +671,104 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
urlConnection.disconnect();
}
+ /**
+ * Makes sure that disconnect while reading from InputStream, the message
+ * loop does not block. Regression test for crbug.com/550605.
+ */
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testDisconnectWhileReadingDoesnotBlock() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ // Make the server echo a large request body, so it exceeds the internal
+ // read buffer.
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ byte[] largeData = TestUtil.getLargeData();
+ connection.setFixedLengthStreamingMode(largeData.length);
+ OutputStream out = connection.getOutputStream();
+ out.write(largeData);
+
+ InputStream in = connection.getInputStream();
+ // Read one byte and disconnect.
+ assertTrue(in.read() != 1);
+ connection.disconnect();
+ // Continue reading, and make sure the message loop will not block.
+ try {
+ int b = 0;
+ while (b != -1) {
+ b = in.read();
+ }
+ // The response body is big, the connection should be disconnected
+ // before EOF can be received.
+ fail();
+ } catch (IOException e) {
+ // Expected.
+ assertEquals("stream closed", e.getMessage());
+ }
+ // Read once more, and make sure exception is thrown.
+ try {
+ in.read();
+ fail();
+ } catch (IOException e) {
+ // Expected.
+ assertEquals("stream closed", e.getMessage());
+ }
+ }
+
+ /**
+ * Makes sure that {@link UrlRequest.Callback#onFailed} exception is
+ * propagated when calling read on the input stream.
+ */
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testServerHangsUp() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ // Make the server echo a large request body, so it exceeds the internal
+ // read buffer.
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ byte[] largeData = TestUtil.getLargeData();
+ connection.setFixedLengthStreamingMode(largeData.length);
+ OutputStream out = connection.getOutputStream();
+ out.write(largeData);
+
+ InputStream in = connection.getInputStream();
+ // Read one byte and shut down the server.
+ assertTrue(in.read() != 1);
+ NativeTestServer.shutdownNativeTestServer();
+ // Continue reading, and make sure the message loop will not block.
+ try {
+ int b = 0;
+ while (b != -1) {
+ b = in.read();
+ }
+ // Server closes the connection before EOF can be received.
+ fail();
+ } catch (IOException e) {
+ // Expected.
+ // Cronet gives a net::ERR_CONTENT_LENGTH_MISMATCH while the
+ // default implementation gives a java.net.ProtocolException with
+ // "unexpected end of stream" message.
+ }
+
+ // Read once more, and make sure exception is thrown.
+ try {
+ in.read();
+ fail();
+ } catch (IOException e) {
+ // Expected.
+ // Cronet gives a net::ERR_CONTENT_LENGTH_MISMATCH while the
+ // default implementation gives a java.net.ProtocolException with
+ // "unexpected end of stream" message.
+ }
+ // Spins up server to avoid crash when shutting it down in tearDown().
+ assertTrue(NativeTestServer.startNativeTestServer(getContext()));
+ }
+
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
@@ -685,7 +781,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
assertEquals("OK", connection.getResponseMessage());
assertEquals(NativeTestServer.getFileURL("/success.txt"),
connection.getURL().toString());
- assertEquals("this is a text file\n", getResponseAsString(connection));
+ assertEquals("this is a text file\n", TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -737,7 +833,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
assertEquals("OK", connection.getResponseMessage());
assertEquals(NativeTestServer.getFileURL("/success.txt"),
connection.getURL().toString());
- assertEquals("this is a text file\n", getResponseAsString(connection));
+ assertEquals("this is a text file\n", TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -901,8 +997,7 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
connection.setUseCaches(cacheSetting == CacheSetting.USE_CACHE);
if (outcome == ExpectedOutcome.SUCCESS) {
assertEquals(200, connection.getResponseCode());
- assertEquals("this is a cacheable file\n",
- getResponseAsString(connection));
+ assertEquals("this is a cacheable file\n", TestUtil.getResponseAsString(connection));
} else {
try {
connection.getResponseCode();
@@ -985,20 +1080,6 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
}
/**
- * 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();
- }
-
- /**
* Helper method to extract a list of header values with the give header
* name.
*/

Powered by Google App Engine
This is Rietveld 408576698