OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // Tests for WebSocketBasicStream. Note that we do not attempt to verify that | 5 // Tests for WebSocketBasicStream. Note that we do not attempt to verify that |
6 // frame parsing itself functions correctly, as that is covered by the | 6 // frame parsing itself functions correctly, as that is covered by the |
7 // WebSocketFrameParser tests. | 7 // WebSocketFrameParser tests. |
8 | 8 |
9 #include "net/websockets/websocket_basic_stream.h" | 9 #include "net/websockets/websocket_basic_stream.h" |
10 | 10 |
| 11 #include <stdint.h> |
11 #include <string.h> // for memcpy() and memset(). | 12 #include <string.h> // for memcpy() and memset(). |
12 | 13 |
13 #include <string> | 14 #include <string> |
14 | 15 |
15 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
16 #include "base/big_endian.h" | 17 #include "base/big_endian.h" |
17 #include "base/port.h" | |
18 #include "net/base/test_completion_callback.h" | 18 #include "net/base/test_completion_callback.h" |
19 #include "net/log/test_net_log.h" | 19 #include "net/log/test_net_log.h" |
20 #include "net/socket/socket_test_util.h" | 20 #include "net/socket/socket_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 namespace { | 24 namespace { |
25 | 25 |
26 #define WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(name, value) \ | 26 #define WEBSOCKET_BASIC_STREAM_TEST_DEFINE_CONSTANT(name, value) \ |
27 const char k##name[] = value; \ | 27 const char k##name[] = value; \ |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 265 |
266 TEST_F(WebSocketBasicStreamSocketTest, ConstructionWorks) { | 266 TEST_F(WebSocketBasicStreamSocketTest, ConstructionWorks) { |
267 CreateNullStream(); | 267 CreateNullStream(); |
268 } | 268 } |
269 | 269 |
270 TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncReadWorks) { | 270 TEST_F(WebSocketBasicStreamSocketSingleReadTest, SyncReadWorks) { |
271 CreateRead(MockRead(SYNCHRONOUS, kSampleFrame, kSampleFrameSize)); | 271 CreateRead(MockRead(SYNCHRONOUS, kSampleFrame, kSampleFrameSize)); |
272 int result = stream_->ReadFrames(&frames_, cb_.callback()); | 272 int result = stream_->ReadFrames(&frames_, cb_.callback()); |
273 EXPECT_EQ(OK, result); | 273 EXPECT_EQ(OK, result); |
274 ASSERT_EQ(1U, frames_.size()); | 274 ASSERT_EQ(1U, frames_.size()); |
275 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 275 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
276 EXPECT_TRUE(frames_[0]->header.final); | 276 EXPECT_TRUE(frames_[0]->header.final); |
277 } | 277 } |
278 | 278 |
279 TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncReadWorks) { | 279 TEST_F(WebSocketBasicStreamSocketSingleReadTest, AsyncReadWorks) { |
280 CreateRead(MockRead(ASYNC, kSampleFrame, kSampleFrameSize)); | 280 CreateRead(MockRead(ASYNC, kSampleFrame, kSampleFrameSize)); |
281 int result = stream_->ReadFrames(&frames_, cb_.callback()); | 281 int result = stream_->ReadFrames(&frames_, cb_.callback()); |
282 ASSERT_EQ(ERR_IO_PENDING, result); | 282 ASSERT_EQ(ERR_IO_PENDING, result); |
283 EXPECT_EQ(OK, cb_.WaitForResult()); | 283 EXPECT_EQ(OK, cb_.WaitForResult()); |
284 ASSERT_EQ(1U, frames_.size()); | 284 ASSERT_EQ(1U, frames_.size()); |
285 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 285 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
286 // Don't repeat all the tests from SyncReadWorks; just enough to be sure the | 286 // Don't repeat all the tests from SyncReadWorks; just enough to be sure the |
287 // frame was really read. | 287 // frame was really read. |
288 } | 288 } |
289 | 289 |
290 // ReadFrames will not return a frame whose header has not been wholly received. | 290 // ReadFrames will not return a frame whose header has not been wholly received. |
291 TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedSync) { | 291 TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedSync) { |
292 CreateChunkedRead( | 292 CreateChunkedRead( |
293 SYNCHRONOUS, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG); | 293 SYNCHRONOUS, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG); |
294 int result = stream_->ReadFrames(&frames_, cb_.callback()); | 294 int result = stream_->ReadFrames(&frames_, cb_.callback()); |
295 EXPECT_EQ(OK, result); | 295 EXPECT_EQ(OK, result); |
296 ASSERT_EQ(1U, frames_.size()); | 296 ASSERT_EQ(1U, frames_.size()); |
297 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 297 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
298 } | 298 } |
299 | 299 |
300 // The same behaviour applies to asynchronous reads. | 300 // The same behaviour applies to asynchronous reads. |
301 TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedAsync) { | 301 TEST_F(WebSocketBasicStreamSocketChunkedReadTest, HeaderFragmentedAsync) { |
302 CreateChunkedRead( | 302 CreateChunkedRead( |
303 ASYNC, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG); | 303 ASYNC, kSampleFrame, kSampleFrameSize, 1, 2, LAST_FRAME_BIG); |
304 int result = stream_->ReadFrames(&frames_, cb_.callback()); | 304 int result = stream_->ReadFrames(&frames_, cb_.callback()); |
305 ASSERT_EQ(ERR_IO_PENDING, result); | 305 ASSERT_EQ(ERR_IO_PENDING, result); |
306 EXPECT_EQ(OK, cb_.WaitForResult()); | 306 EXPECT_EQ(OK, cb_.WaitForResult()); |
307 ASSERT_EQ(1U, frames_.size()); | 307 ASSERT_EQ(1U, frames_.size()); |
308 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 308 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
309 } | 309 } |
310 | 310 |
311 // If it receives an incomplete header in a synchronous call, then has to wait | 311 // If it receives an incomplete header in a synchronous call, then has to wait |
312 // for the rest of the frame, ReadFrames will return ERR_IO_PENDING. | 312 // for the rest of the frame, ReadFrames will return ERR_IO_PENDING. |
313 TEST_F(WebSocketBasicStreamSocketTest, HeaderFragmentedSyncAsync) { | 313 TEST_F(WebSocketBasicStreamSocketTest, HeaderFragmentedSyncAsync) { |
314 MockRead reads[] = {MockRead(SYNCHRONOUS, kSampleFrame, 1), | 314 MockRead reads[] = {MockRead(SYNCHRONOUS, kSampleFrame, 1), |
315 MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1)}; | 315 MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1)}; |
316 CreateReadOnly(reads); | 316 CreateReadOnly(reads); |
317 int result = stream_->ReadFrames(&frames_, cb_.callback()); | 317 int result = stream_->ReadFrames(&frames_, cb_.callback()); |
318 ASSERT_EQ(ERR_IO_PENDING, result); | 318 ASSERT_EQ(ERR_IO_PENDING, result); |
319 EXPECT_EQ(OK, cb_.WaitForResult()); | 319 EXPECT_EQ(OK, cb_.WaitForResult()); |
320 ASSERT_EQ(1U, frames_.size()); | 320 ASSERT_EQ(1U, frames_.size()); |
321 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 321 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
322 } | 322 } |
323 | 323 |
324 // An extended header should also return ERR_IO_PENDING if it is not completely | 324 // An extended header should also return ERR_IO_PENDING if it is not completely |
325 // received. | 325 // received. |
326 TEST_F(WebSocketBasicStreamSocketTest, FragmentedLargeHeader) { | 326 TEST_F(WebSocketBasicStreamSocketTest, FragmentedLargeHeader) { |
327 MockRead reads[] = { | 327 MockRead reads[] = { |
328 MockRead(SYNCHRONOUS, kPartialLargeFrame, kLargeFrameHeaderSize - 1), | 328 MockRead(SYNCHRONOUS, kPartialLargeFrame, kLargeFrameHeaderSize - 1), |
329 MockRead(SYNCHRONOUS, ERR_IO_PENDING)}; | 329 MockRead(SYNCHRONOUS, ERR_IO_PENDING)}; |
330 CreateReadOnly(reads); | 330 CreateReadOnly(reads); |
331 EXPECT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback())); | 331 EXPECT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback())); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 | 631 |
632 // If there was a frame read at the same time as the response headers (and the | 632 // If there was a frame read at the same time as the response headers (and the |
633 // handshake succeeded), then we should parse it. | 633 // handshake succeeded), then we should parse it. |
634 TEST_F(WebSocketBasicStreamSocketTest, HttpReadBufferIsUsed) { | 634 TEST_F(WebSocketBasicStreamSocketTest, HttpReadBufferIsUsed) { |
635 SetHttpReadBuffer(kSampleFrame, kSampleFrameSize); | 635 SetHttpReadBuffer(kSampleFrame, kSampleFrameSize); |
636 CreateNullStream(); | 636 CreateNullStream(); |
637 | 637 |
638 EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback())); | 638 EXPECT_EQ(OK, stream_->ReadFrames(&frames_, cb_.callback())); |
639 ASSERT_EQ(1U, frames_.size()); | 639 ASSERT_EQ(1U, frames_.size()); |
640 ASSERT_TRUE(frames_[0]->data.get()); | 640 ASSERT_TRUE(frames_[0]->data.get()); |
641 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 641 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
642 } | 642 } |
643 | 643 |
644 // Check that a frame whose header partially arrived at the end of the response | 644 // Check that a frame whose header partially arrived at the end of the response |
645 // headers works correctly. | 645 // headers works correctly. |
646 TEST_F(WebSocketBasicStreamSocketSingleReadTest, | 646 TEST_F(WebSocketBasicStreamSocketSingleReadTest, |
647 PartialFrameHeaderInHttpResponse) { | 647 PartialFrameHeaderInHttpResponse) { |
648 SetHttpReadBuffer(kSampleFrame, 1); | 648 SetHttpReadBuffer(kSampleFrame, 1); |
649 CreateRead(MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1)); | 649 CreateRead(MockRead(ASYNC, kSampleFrame + 1, kSampleFrameSize - 1)); |
650 | 650 |
651 ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback())); | 651 ASSERT_EQ(ERR_IO_PENDING, stream_->ReadFrames(&frames_, cb_.callback())); |
652 EXPECT_EQ(OK, cb_.WaitForResult()); | 652 EXPECT_EQ(OK, cb_.WaitForResult()); |
653 ASSERT_EQ(1U, frames_.size()); | 653 ASSERT_EQ(1U, frames_.size()); |
654 ASSERT_TRUE(frames_[0]->data.get()); | 654 ASSERT_TRUE(frames_[0]->data.get()); |
655 EXPECT_EQ(GG_UINT64_C(6), frames_[0]->header.payload_length); | 655 EXPECT_EQ(UINT64_C(6), frames_[0]->header.payload_length); |
656 EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode); | 656 EXPECT_EQ(WebSocketFrameHeader::kOpCodeText, frames_[0]->header.opcode); |
657 } | 657 } |
658 | 658 |
659 // Check that a control frame which partially arrives at the end of the response | 659 // Check that a control frame which partially arrives at the end of the response |
660 // headers works correctly. | 660 // headers works correctly. |
661 TEST_F(WebSocketBasicStreamSocketSingleReadTest, | 661 TEST_F(WebSocketBasicStreamSocketSingleReadTest, |
662 PartialControlFrameInHttpResponse) { | 662 PartialControlFrameInHttpResponse) { |
663 const size_t kPartialFrameBytes = 3; | 663 const size_t kPartialFrameBytes = 3; |
664 SetHttpReadBuffer(kCloseFrame, kPartialFrameBytes); | 664 SetHttpReadBuffer(kCloseFrame, kPartialFrameBytes); |
665 CreateRead(MockRead(ASYNC, | 665 CreateRead(MockRead(ASYNC, |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 | 934 |
935 TEST_F(WebSocketBasicStreamSocketTest, GetSubProtocolWorks) { | 935 TEST_F(WebSocketBasicStreamSocketTest, GetSubProtocolWorks) { |
936 sub_protocol_ = "cyberchat"; | 936 sub_protocol_ = "cyberchat"; |
937 CreateNullStream(); | 937 CreateNullStream(); |
938 | 938 |
939 EXPECT_EQ("cyberchat", stream_->GetSubProtocol()); | 939 EXPECT_EQ("cyberchat", stream_->GetSubProtocol()); |
940 } | 940 } |
941 | 941 |
942 } // namespace | 942 } // namespace |
943 } // namespace net | 943 } // namespace net |
OLD | NEW |