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

Side by Side Diff: net/spdy/bidirectional_stream_spdy_impl_unittest.cc

Issue 2032733002: Do not crash on null stream in writing to bidirectional streams (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_crash
Patch Set: Address Andrei's comments round 2 Created 4 years, 6 months 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 unified diff | Download patch
« no previous file with comments | « net/spdy/bidirectional_stream_spdy_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/spdy/bidirectional_stream_spdy_impl.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/time/time.h"
16 #include "base/timer/mock_timer.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/test_data_directory.h"
19 #include "net/http/http_request_info.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_response_info.h"
22 #include "net/log/net_log.h"
23 #include "net/log/test_net_log.h"
24 #include "net/socket/socket_test_util.h"
25 #include "net/spdy/spdy_session.h"
26 #include "net/spdy/spdy_test_util_common.h"
27 #include "net/test/cert_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 namespace net {
31
32 namespace {
33
34 const char kBodyData[] = "Body data";
35 const size_t kBodyDataSize = arraysize(kBodyData);
36 // Size of the buffer to be allocated for each read.
37 const size_t kReadBufferSize = 4096;
38
39 class TestDelegateBase : public BidirectionalStreamImpl::Delegate {
40 public:
41 TestDelegateBase(base::WeakPtr<SpdySession> session,
42 IOBuffer* read_buf,
43 int read_buf_len)
44 : stream_(new BidirectionalStreamSpdyImpl(session)),
45 read_buf_(read_buf),
46 read_buf_len_(read_buf_len),
47 loop_(nullptr),
48 error_(OK),
49 bytes_read_(0),
50 on_data_read_count_(0),
51 on_data_sent_count_(0),
52 do_not_start_read_(false),
53 run_until_completion_(false),
54 not_expect_callback_(false),
55 on_failed_called_(false) {}
56
57 ~TestDelegateBase() override {}
58
59 void OnStreamReady(bool request_headers_sent) override {
60 CHECK(!on_failed_called_);
61 }
62
63 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override {
64 CHECK(!on_failed_called_);
65 CHECK(!not_expect_callback_);
66 response_headers_ = response_headers;
67 if (!do_not_start_read_)
68 StartOrContinueReading();
69 }
70
71 void OnDataRead(int bytes_read) override {
72 CHECK(!on_failed_called_);
73 CHECK(!not_expect_callback_);
74 on_data_read_count_++;
75 CHECK_GE(bytes_read, OK);
76 bytes_read_ += bytes_read;
77 data_received_.append(read_buf_->data(), bytes_read);
78 if (!do_not_start_read_)
79 StartOrContinueReading();
80 }
81
82 void OnDataSent() override {
83 CHECK(!on_failed_called_);
84 CHECK(!not_expect_callback_);
85 on_data_sent_count_++;
86 }
87
88 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override {
89 CHECK(!on_failed_called_);
90 trailers_ = trailers;
91 if (run_until_completion_)
92 loop_->Quit();
93 }
94
95 void OnFailed(int error) override {
96 CHECK(!on_failed_called_);
97 CHECK(!not_expect_callback_);
98 CHECK_NE(OK, error);
99 error_ = error;
100 on_failed_called_ = true;
101 if (run_until_completion_)
102 loop_->Quit();
103 }
104
105 void Start(const BidirectionalStreamRequestInfo* request,
106 const BoundNetLog& net_log) {
107 stream_->Start(request, net_log,
108 /*send_request_headers_automatically=*/false, this,
109 base::WrapUnique(new base::Timer(false, false)));
110 not_expect_callback_ = false;
111 }
112
113 void SendData(IOBuffer* data, int length, bool end_of_stream) {
114 not_expect_callback_ = true;
115 stream_->SendData(data, length, end_of_stream);
116 not_expect_callback_ = false;
117 }
118
119 void SendvData(const std::vector<scoped_refptr<IOBuffer>>& data,
120 const std::vector<int>& length,
121 bool end_of_stream) {
122 not_expect_callback_ = true;
123 stream_->SendvData(data, length, end_of_stream);
124 not_expect_callback_ = false;
125 }
126
127 // Sets whether the delegate should wait until the completion of the stream.
128 void SetRunUntilCompletion(bool run_until_completion) {
129 run_until_completion_ = run_until_completion;
130 loop_.reset(new base::RunLoop);
131 }
132
133 // Starts or continues read data from |stream_| until there is no more
134 // byte can be read synchronously.
135 void StartOrContinueReading() {
136 int rv = ReadData();
137 while (rv > 0) {
138 rv = ReadData();
139 }
140 if (run_until_completion_ && rv == 0)
141 loop_->Quit();
142 }
143
144 // Calls ReadData on the |stream_| and updates internal states.
145 int ReadData() {
146 int rv = stream_->ReadData(read_buf_.get(), read_buf_len_);
147 if (rv > 0) {
148 data_received_.append(read_buf_->data(), rv);
149 bytes_read_ += rv;
150 }
151 return rv;
152 }
153
154 NextProto GetProtocol() const { return stream_->GetProtocol(); }
155
156 int64_t GetTotalReceivedBytes() const {
157 return stream_->GetTotalReceivedBytes();
158 }
159
160 int64_t GetTotalSentBytes() const { return stream_->GetTotalSentBytes(); }
161
162 // Const getters for internal states.
163 const std::string& data_received() const { return data_received_; }
164 int bytes_read() const { return bytes_read_; }
165 int error() const { return error_; }
166 const SpdyHeaderBlock response_headers() const { return response_headers_; }
167 const SpdyHeaderBlock trailers() const { return trailers_; }
168 int on_data_read_count() const { return on_data_read_count_; }
169 int on_data_sent_count() const { return on_data_sent_count_; }
170 bool on_failed_called() const { return on_failed_called_; }
171
172 // Sets whether the delegate should automatically start reading.
173 void set_do_not_start_read(bool do_not_start_read) {
174 do_not_start_read_ = do_not_start_read;
175 }
176
177 // Cancels |stream_|.
178 void CancelStream() { stream_->Cancel(); }
179
180 private:
181 std::unique_ptr<BidirectionalStreamSpdyImpl> stream_;
182 scoped_refptr<IOBuffer> read_buf_;
183 int read_buf_len_;
184 std::string data_received_;
185 std::unique_ptr<base::RunLoop> loop_;
186 SpdyHeaderBlock response_headers_;
187 SpdyHeaderBlock trailers_;
188 int error_;
189 int bytes_read_;
190 int on_data_read_count_;
191 int on_data_sent_count_;
192 bool do_not_start_read_;
193 bool run_until_completion_;
194 bool not_expect_callback_;
195 bool on_failed_called_;
196
197 DISALLOW_COPY_AND_ASSIGN(TestDelegateBase);
198 };
199
200 } // namespace
201
202 class BidirectionalStreamSpdyImplTest : public testing::Test {
203 public:
204 BidirectionalStreamSpdyImplTest()
205 : spdy_util_(kProtoHTTP2, true),
206 session_deps_(kProtoHTTP2),
207 ssl_data_(SSLSocketDataProvider(ASYNC, OK)) {
208 ssl_data_.SetNextProto(kProtoHTTP2);
209 ssl_data_.cert = ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
210 }
211
212 protected:
213 void TearDown() override {
214 if (sequenced_data_) {
215 EXPECT_TRUE(sequenced_data_->AllReadDataConsumed());
216 EXPECT_TRUE(sequenced_data_->AllWriteDataConsumed());
217 }
218 }
219
220 // Initializes the session using SequencedSocketData.
221 void InitSession(MockRead* reads,
222 size_t reads_count,
223 MockWrite* writes,
224 size_t writes_count,
225 const SpdySessionKey& key) {
226 ASSERT_TRUE(ssl_data_.cert.get());
227 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data_);
228 sequenced_data_.reset(
229 new SequencedSocketData(reads, reads_count, writes, writes_count));
230 session_deps_.socket_factory->AddSocketDataProvider(sequenced_data_.get());
231 session_deps_.net_log = net_log_.bound().net_log();
232 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_);
233 session_ =
234 CreateSecureSpdySession(http_session_.get(), key, net_log_.bound());
235 }
236
237 BoundTestNetLog net_log_;
238 SpdyTestUtil spdy_util_;
239 SpdySessionDependencies session_deps_;
240 std::unique_ptr<SequencedSocketData> sequenced_data_;
241 std::unique_ptr<HttpNetworkSession> http_session_;
242 base::WeakPtr<SpdySession> session_;
243
244 private:
245 SSLSocketDataProvider ssl_data_;
246 };
247
248 TEST_F(BidirectionalStreamSpdyImplTest, SendDataAfterStreamFailed) {
249 std::unique_ptr<SpdySerializedFrame> req(spdy_util_.ConstructSpdyPost(
250 "https://www.example.org", 1, kBodyDataSize * 3, LOW, nullptr, 0));
251 std::unique_ptr<SpdySerializedFrame> rst(
252 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_PROTOCOL_ERROR));
253
254 MockWrite writes[] = {
255 CreateMockWrite(*req, 0), CreateMockWrite(*rst, 2),
256 };
257
258 const char* const kExtraHeaders[] = {"X-UpperCase", "yes"};
259 std::unique_ptr<SpdySerializedFrame> resp(
260 spdy_util_.ConstructSpdyGetSynReply(kExtraHeaders, 1, 1));
261
262 MockRead reads[] = {
263 CreateMockRead(*resp, 1), MockRead(ASYNC, 0, 3),
264 };
265
266 HostPortPair host_port_pair("www.example.org", 443);
267 SpdySessionKey key(host_port_pair, ProxyServer::Direct(),
268 PRIVACY_MODE_DISABLED);
269 InitSession(reads, arraysize(reads), writes, arraysize(writes), key);
270
271 BidirectionalStreamRequestInfo request_info;
272 request_info.method = "POST";
273 request_info.url = GURL("https://www.example.org/");
274 request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength,
275 base::SizeTToString(kBodyDataSize * 3));
276
277 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize));
278 std::unique_ptr<TestDelegateBase> delegate(
279 new TestDelegateBase(session_, read_buffer.get(), kReadBufferSize));
280 delegate->SetRunUntilCompletion(true);
281 delegate->Start(&request_info, net_log_.bound());
282 base::RunLoop().RunUntilIdle();
283
284 EXPECT_TRUE(delegate->on_failed_called());
285
286 // Try to send data after OnFailed(), should not get called back.
287 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer("dummy"));
288 delegate->SendData(buf.get(), buf->size(), false);
289 base::RunLoop().RunUntilIdle();
290
291 EXPECT_EQ(ERR_SPDY_PROTOCOL_ERROR, delegate->error());
292 EXPECT_EQ(0, delegate->on_data_read_count());
293 EXPECT_EQ(0, delegate->on_data_sent_count());
294 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol());
295 // BidirectionalStreamSpdyStreamJob does not count the bytes sent for |rst|
296 // because it is sent after SpdyStream::Delegate::OnClose is called.
297 EXPECT_EQ(CountWriteBytes(writes, 1), delegate->GetTotalSentBytes());
298 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
299 delegate->GetTotalReceivedBytes());
300 }
301
302 TEST_F(BidirectionalStreamSpdyImplTest, SendDataAfterCancelStream) {
303 BufferedSpdyFramer framer(spdy_util_.spdy_version());
304
305 std::unique_ptr<SpdySerializedFrame> req(spdy_util_.ConstructSpdyPost(
306 "https://www.example.org", 1, kBodyDataSize * 3, LOWEST, nullptr, 0));
307 std::unique_ptr<SpdySerializedFrame> data_frame(
308 framer.CreateDataFrame(1, kBodyData, kBodyDataSize, DATA_FLAG_NONE));
309 std::unique_ptr<SpdySerializedFrame> rst(
310 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL));
311
312 MockWrite writes[] = {
313 CreateMockWrite(*req, 0), CreateMockWrite(*data_frame, 3),
314 CreateMockWrite(*rst, 5),
315 };
316
317 std::unique_ptr<SpdySerializedFrame> resp(
318 spdy_util_.ConstructSpdyGetSynReply(nullptr, 0, 1));
319 std::unique_ptr<SpdySerializedFrame> response_body_frame(
320 spdy_util_.ConstructSpdyBodyFrame(1, false));
321
322 MockRead reads[] = {
323 CreateMockRead(*resp, 1),
324 MockRead(ASYNC, ERR_IO_PENDING, 2), // Force a pause.
325 MockRead(ASYNC, ERR_IO_PENDING, 4), // Force a pause.
326 MockRead(ASYNC, 0, 6),
327 };
328
329 HostPortPair host_port_pair("www.example.org", 443);
330 SpdySessionKey key(host_port_pair, ProxyServer::Direct(),
331 PRIVACY_MODE_DISABLED);
332 InitSession(reads, arraysize(reads), writes, arraysize(writes), key);
333
334 BidirectionalStreamRequestInfo request_info;
335 request_info.method = "POST";
336 request_info.url = GURL("https://www.example.org/");
337 request_info.priority = LOWEST;
338 request_info.extra_headers.SetHeader(net::HttpRequestHeaders::kContentLength,
339 base::SizeTToString(kBodyDataSize * 3));
340
341 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadBufferSize));
342 std::unique_ptr<TestDelegateBase> delegate(
343 new TestDelegateBase(session_, read_buffer.get(), kReadBufferSize));
344 delegate->set_do_not_start_read(true);
345 delegate->Start(&request_info, net_log_.bound());
346 // Send the request and receive response headers.
347 sequenced_data_->RunUntilPaused();
348 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol());
349
350 // Send a DATA frame.
351 scoped_refptr<StringIOBuffer> buf(
352 new StringIOBuffer(std::string(kBodyData, kBodyDataSize)));
353 delegate->SendData(buf.get(), buf->size(), false);
354 sequenced_data_->Resume();
355 base::RunLoop().RunUntilIdle();
356 // Cancel the stream.
357 delegate->CancelStream();
358 sequenced_data_->Resume();
359 base::RunLoop().RunUntilIdle();
360
361 // Try to send data after Cancel(), should not get called back.
362 delegate->SendData(buf.get(), buf->size(), false);
363 base::MessageLoop::current()->RunUntilIdle();
364 EXPECT_FALSE(delegate->on_failed_called());
365
366 EXPECT_EQ("200", delegate->response_headers().find(":status")->second);
367 EXPECT_EQ(0, delegate->on_data_read_count());
368 EXPECT_EQ(kProtoHTTP2, delegate->GetProtocol());
369 EXPECT_EQ(0, delegate->GetTotalSentBytes());
370 EXPECT_EQ(0, delegate->GetTotalReceivedBytes());
371 }
372
373 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/bidirectional_stream_spdy_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698