Index: net/spdy/spdy_http_stream_unittest.cc |
diff --git a/net/spdy/spdy_http_stream_unittest.cc b/net/spdy/spdy_http_stream_unittest.cc |
index a6d4a12abb1c652b04ae657ca56f87e73508d9c7..abd1579a0b7fdf038fbfecdda0af1919b45224a1 100644 |
--- a/net/spdy/spdy_http_stream_unittest.cc |
+++ b/net/spdy/spdy_http_stream_unittest.cc |
@@ -75,6 +75,39 @@ void TestLoadTimingNotReused(const HttpStream& stream) { |
ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
} |
+class ReadErrorUploadDataStream : public UploadDataStream { |
+ public: |
+ enum class FailureMode { SYNC, ASYNC }; |
+ |
+ explicit ReadErrorUploadDataStream(FailureMode mode) |
+ : UploadDataStream(true, 0), async_(mode), weak_factory_(this) {} |
+ |
+ private: |
+ void CompleteRead() { UploadDataStream::OnReadCompleted(ERR_FAILED); } |
+ |
+ // UploadDataStream implementation: |
+ int InitInternal() override { return OK; } |
+ |
+ int ReadInternal(IOBuffer* buf, int buf_len) override { |
+ if (async_ == FailureMode::ASYNC) { |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&ReadErrorUploadDataStream::CompleteRead, |
+ weak_factory_.GetWeakPtr()), |
+ base::TimeDelta::FromSeconds(1)); |
Bence
2016/06/10 17:39:17
Is there a way to avoid time delay in tests? In g
maksims (do not use this acc)
2016/06/20 08:14:24
Done.
|
+ return ERR_IO_PENDING; |
+ } |
+ return ERR_FAILED; |
+ } |
+ |
+ void ResetInternal() override {} |
+ |
+ const FailureMode async_; |
+ |
+ base::WeakPtrFactory<ReadErrorUploadDataStream> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ReadErrorUploadDataStream); |
+}; |
+ |
} // namespace |
class SpdyHttpStreamTest : public testing::Test, |
@@ -99,7 +132,7 @@ class SpdyHttpStreamTest : public testing::Test, |
} |
void TearDown() override { |
- crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
+ crypto::ECSignatureCreator::SetFactoryForTesting(nullptr); |
base::MessageLoop::current()->RunUntilIdle(); |
EXPECT_TRUE(sequenced_data_->AllReadDataConsumed()); |
EXPECT_TRUE(sequenced_data_->AllWriteDataConsumed()); |
@@ -151,7 +184,7 @@ TEST_P(SpdyHttpStreamTest, GetUploadProgressBeforeInitialization) { |
HostPortPair host_port_pair("www.example.org", 80); |
SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
PRIVACY_MODE_DISABLED); |
- InitSession(reads, arraysize(reads), NULL, 0, key); |
+ InitSession(reads, arraysize(reads), nullptr, 0, key); |
SpdyHttpStream stream(session_, false); |
UploadProgress progress = stream.GetUploadProgress(); |
@@ -169,7 +202,7 @@ TEST_P(SpdyHttpStreamTest, SendRequest) { |
CreateMockWrite(*req.get(), 0), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); |
+ spdy_util_.ConstructSpdyGetSynReply(nullptr, 0, 1)); |
MockRead reads[] = { |
CreateMockRead(*resp, 1), MockRead(SYNCHRONOUS, 0, 2) // EOF |
}; |
@@ -234,11 +267,11 @@ TEST_P(SpdyHttpStreamTest, LoadTimingTwoRequests) { |
CreateMockWrite(*req2, 1), |
}; |
std::unique_ptr<SpdySerializedFrame> resp1( |
- spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); |
+ spdy_util_.ConstructSpdyGetSynReply(nullptr, 0, 1)); |
std::unique_ptr<SpdySerializedFrame> body1( |
spdy_util_.ConstructSpdyBodyFrame(1, "", 0, true)); |
std::unique_ptr<SpdySerializedFrame> resp2( |
- spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 3)); |
+ spdy_util_.ConstructSpdyGetSynReply(nullptr, 0, 3)); |
std::unique_ptr<SpdySerializedFrame> body2( |
spdy_util_.ConstructSpdyBodyFrame(3, "", 0, true)); |
MockRead reads[] = { |
@@ -330,7 +363,7 @@ TEST_P(SpdyHttpStreamTest, SendChunkedPost) { |
BufferedSpdyFramer framer(spdy_util_.spdy_version()); |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> body( |
framer.CreateDataFrame(1, kUploadData, kUploadDataSize, DATA_FLAG_FIN)); |
MockWrite writes[] = { |
@@ -339,7 +372,7 @@ TEST_P(SpdyHttpStreamTest, SendChunkedPost) { |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
MockRead reads[] = { |
CreateMockRead(*resp, 2), |
CreateMockRead(*body, 3), |
@@ -395,7 +428,7 @@ TEST_P(SpdyHttpStreamTest, ConnectionClosedDuringChunkedPost) { |
BufferedSpdyFramer framer(spdy_util_.spdy_version()); |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> body( |
framer.CreateDataFrame(1, kUploadData, kUploadDataSize, DATA_FLAG_NONE)); |
MockWrite writes[] = { |
@@ -404,7 +437,7 @@ TEST_P(SpdyHttpStreamTest, ConnectionClosedDuringChunkedPost) { |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
MockRead reads[] = { |
MockRead(ASYNC, ERR_CONNECTION_CLOSED, 2) // Server hangs up early. |
}; |
@@ -438,6 +471,8 @@ TEST_P(SpdyHttpStreamTest, ConnectionClosedDuringChunkedPost) { |
http_stream.SendRequest(headers, &response, callback.callback())); |
EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
+ // Server closes the connection. Thus, callback now returns |
+ // "CONNECTION_CLOSED" error. |
EXPECT_EQ(OK, callback.WaitForResult()); |
maksims (do not use this acc)
2016/06/03 11:39:08
As I said this returns OK instead of ERR_CONNECTIO
|
EXPECT_EQ(static_cast<int64_t>(req->size() + body->size()), |
@@ -466,7 +501,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPost) { |
const char kUploadData1[] = "12345678"; |
const int kUploadData1Size = arraysize(kUploadData1)-1; |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> chunk1( |
spdy_util_.ConstructSpdyBodyFrame(1, false)); |
std::unique_ptr<SpdySerializedFrame> chunk2(spdy_util_.ConstructSpdyBodyFrame( |
@@ -480,7 +515,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPost) { |
CreateMockWrite(*chunk3, 3), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
MockRead reads[] = { |
CreateMockRead(*resp, 4), |
CreateMockRead(*chunk1, 5), |
@@ -570,7 +605,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPost) { |
// frame when uploading a chunked data stream. |
TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithEmptyFinalDataFrame) { |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> chunk1( |
spdy_util_.ConstructSpdyBodyFrame(1, false)); |
std::unique_ptr<SpdySerializedFrame> chunk2( |
@@ -581,7 +616,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithEmptyFinalDataFrame) { |
CreateMockWrite(*chunk2, 2), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
MockRead reads[] = { |
CreateMockRead(*resp, 3), |
CreateMockRead(*chunk1, 4), |
@@ -629,7 +664,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithEmptyFinalDataFrame) { |
EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); |
// Now end the stream with an empty data frame and the FIN set. |
- upload_stream.AppendData(NULL, 0, true); |
+ upload_stream.AppendData(nullptr, 0, true); |
// Finish writing the final frame, and perform all reads. |
base::RunLoop().RunUntilIdle(); |
@@ -663,7 +698,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithEmptyFinalDataFrame) { |
// payload. Unclear if this is a case worth supporting. |
TEST_P(SpdyHttpStreamTest, ChunkedPostWithEmptyPayload) { |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> chunk( |
spdy_util_.ConstructSpdyBodyFrame(1, "", 0, true)); |
MockWrite writes[] = { |
@@ -671,7 +706,7 @@ TEST_P(SpdyHttpStreamTest, ChunkedPostWithEmptyPayload) { |
CreateMockWrite(*chunk, 1), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
MockRead reads[] = { |
CreateMockRead(*resp, 2), |
CreateMockRead(*chunk, 3), |
@@ -741,7 +776,7 @@ TEST_P(SpdyHttpStreamTest, SpdyURLTest) { |
CreateMockWrite(*req.get(), 0), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); |
+ spdy_util_.ConstructSpdyGetSynReply(nullptr, 0, 1)); |
MockRead reads[] = { |
CreateMockRead(*resp, 1), MockRead(SYNCHRONOUS, 0, 2) // EOF |
}; |
@@ -785,7 +820,7 @@ TEST_P(SpdyHttpStreamTest, SpdyURLTest) { |
// made available is handled correctly. |
TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
std::unique_ptr<SpdySerializedFrame> req( |
- spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> chunk1( |
spdy_util_.ConstructSpdyBodyFrame(1, true)); |
MockWrite writes[] = { |
@@ -793,7 +828,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
CreateMockWrite(*chunk1, 1), |
}; |
std::unique_ptr<SpdySerializedFrame> resp( |
- spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
std::unique_ptr<SpdySerializedFrame> window_update( |
spdy_util_.ConstructSpdyWindowUpdate(1, kUploadDataSize)); |
MockRead reads[] = { |
@@ -836,8 +871,6 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
// Complete the initial request write and first chunk. |
base::RunLoop().RunUntilIdle(); |
- ASSERT_TRUE(callback.have_result()); |
- EXPECT_EQ(OK, callback.WaitForResult()); |
EXPECT_EQ(static_cast<int64_t>(req->size()), |
http_stream->GetTotalSentBytes()); |
@@ -846,11 +879,14 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
upload_stream.AppendData(kUploadData, kUploadDataSize, true); |
// Verify that the window size has decreased. |
- ASSERT_TRUE(http_stream->stream() != NULL); |
+ ASSERT_TRUE(http_stream->stream() != nullptr); |
EXPECT_NE(static_cast<int>( |
SpdySession::GetDefaultInitialWindowSize(session_->protocol())), |
http_stream->stream()->send_window_size()); |
+ ASSERT_TRUE(callback.have_result()); |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
maksims (do not use this acc)
2016/06/03 11:39:08
Due to new behavior, the callback should be read o
|
+ |
// Read window update. |
base::RunLoop().RunUntilIdle(); |
@@ -860,7 +896,7 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); |
// Verify the window update. |
- ASSERT_TRUE(http_stream->stream() != NULL); |
+ ASSERT_TRUE(http_stream->stream() != nullptr); |
EXPECT_EQ(static_cast<int>( |
SpdySession::GetDefaultInitialWindowSize(session_->protocol())), |
http_stream->stream()->send_window_size()); |
@@ -888,6 +924,113 @@ TEST_P(SpdyHttpStreamTest, DelayedSendChunkedPostWithWindowUpdate) { |
ASSERT_EQ(200, response.headers->response_code()); |
} |
+TEST_P(SpdyHttpStreamTest, DataReadErrorSynchronous) { |
+ std::unique_ptr<SpdySerializedFrame> req( |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
+ |
+ // Server receives RST_STREAM_INTERNAL_ERROR on clients' internal failure. |
+ // The failure is a reading error in this case caused by |
+ // UploadDataStream::Read() |
+ std::unique_ptr<SpdySerializedFrame> rst_frame( |
+ spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_INTERNAL_ERROR)); |
+ |
+ MockWrite writes[] = { |
+ CreateMockWrite(*req, 0, SYNCHRONOUS), // Request |
+ CreateMockWrite(*rst_frame, 1, SYNCHRONOUS) // Reset frame |
+ }; |
+ |
+ std::unique_ptr<SpdySerializedFrame> resp( |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
+ |
+ MockRead reads[] = { |
+ CreateMockRead(*resp, 2), |
+ MockRead(SYNCHRONOUS, 0, 3), |
+ }; |
+ |
+ HostPortPair host_port_pair("www.example.org", 80); |
+ SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
+ PRIVACY_MODE_DISABLED); |
+ InitSession(reads, arraysize(reads), writes, arraysize(writes), key); |
+ EXPECT_EQ(spdy_util_.spdy_version(), session_->GetProtocolVersion()); |
+ ReadErrorUploadDataStream upload_data_stream( |
+ ReadErrorUploadDataStream::FailureMode::SYNC); |
+ ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); |
+ |
+ HttpRequestInfo request; |
+ request.method = "POST"; |
+ request.url = GURL("http://www.example.org/"); |
+ request.upload_data_stream = &upload_data_stream; |
+ |
+ TestCompletionCallback callback; |
+ HttpResponseInfo response; |
+ HttpRequestHeaders headers; |
+ BoundNetLog net_log; |
+ SpdyHttpStream http_stream(session_, true); |
+ ASSERT_EQ(OK, http_stream.InitializeStream(&request, DEFAULT_PRIORITY, |
+ net_log, CompletionCallback())); |
+ |
+ int result = http_stream.SendRequest(headers, &response, callback.callback()); |
+ EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); |
+ |
+ // Because the server has not closed the connection yet, there shouldn't be |
+ // a stream but a session in the pool |
+ EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
+} |
+ |
+TEST_P(SpdyHttpStreamTest, DataReadErrorAsynchronous) { |
+ std::unique_ptr<SpdySerializedFrame> req( |
+ spdy_util_.ConstructChunkedSpdyPost(nullptr, 0)); |
+ |
+ // Server receives RST_STREAM_INTERNAL_ERROR on clients' internal failure. |
+ // The failure is a reading error in this case caused by |
+ // UploadDataStream::Read() |
+ std::unique_ptr<SpdySerializedFrame> rst_frame( |
+ spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_INTERNAL_ERROR)); |
+ |
+ MockWrite writes[] = { |
+ CreateMockWrite(*req, 0), // Request |
+ CreateMockWrite(*rst_frame, 1) // Reset frame |
+ }; |
+ |
+ std::unique_ptr<SpdySerializedFrame> resp( |
+ spdy_util_.ConstructSpdyPostSynReply(nullptr, 0)); |
+ |
+ MockRead reads[] = { |
+ MockRead(ASYNC, 0, 2), |
+ }; |
+ |
+ HostPortPair host_port_pair("www.example.org", 80); |
+ SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
+ PRIVACY_MODE_DISABLED); |
+ InitSession(reads, arraysize(reads), writes, arraysize(writes), key); |
+ EXPECT_EQ(spdy_util_.spdy_version(), session_->GetProtocolVersion()); |
+ |
+ ReadErrorUploadDataStream upload_data_stream( |
+ ReadErrorUploadDataStream::FailureMode::ASYNC); |
+ ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); |
+ |
+ HttpRequestInfo request; |
+ request.method = "POST"; |
+ request.url = GURL("http://www.example.org/"); |
+ request.upload_data_stream = &upload_data_stream; |
+ |
+ TestCompletionCallback callback; |
+ HttpResponseInfo response; |
+ HttpRequestHeaders headers; |
+ BoundNetLog net_log; |
+ SpdyHttpStream http_stream(session_, true); |
+ ASSERT_EQ(OK, http_stream.InitializeStream(&request, DEFAULT_PRIORITY, |
+ net_log, CompletionCallback())); |
+ |
+ int result = http_stream.SendRequest(headers, &response, callback.callback()); |
+ EXPECT_EQ(ERR_IO_PENDING, result); |
+ EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); |
+ |
+ // Because the server has closed the connection, there shouldn't be a session |
+ // in the pool anymore. |
+ EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
+} |
+ |
// TODO(willchan): Write a longer test for SpdyStream that exercises all |
// methods. |