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

Side by Side Diff: net/websockets/websocket_channel_test.cc

Issue 2267233002: Change WebSocketChannel to pass data via IOBuffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make it const char* Created 4 years, 2 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/websockets/websocket_channel.cc ('k') | net/websockets/websocket_end_to_end_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "net/websockets/websocket_channel.h" 5 #include "net/websockets/websocket_channel.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <string.h> 9 #include <string.h>
10 10
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 151
152 // This typedef mainly exists to avoid having to repeat the "NOLINT" incantation 152 // This typedef mainly exists to avoid having to repeat the "NOLINT" incantation
153 // all over the place. 153 // all over the place.
154 typedef StrictMock< MockFunction<void(int)> > Checkpoint; // NOLINT 154 typedef StrictMock< MockFunction<void(int)> > Checkpoint; // NOLINT
155 155
156 // This mock is for testing expectations about how the EventInterface is used. 156 // This mock is for testing expectations about how the EventInterface is used.
157 class MockWebSocketEventInterface : public WebSocketEventInterface { 157 class MockWebSocketEventInterface : public WebSocketEventInterface {
158 public: 158 public:
159 MockWebSocketEventInterface() {} 159 MockWebSocketEventInterface() {}
160 160
161 ChannelState OnDataFrame(bool fin,
162 WebSocketMessageType type,
163 scoped_refptr<IOBuffer> buffer,
164 size_t buffer_size) override {
165 const char* data = buffer ? buffer->data() : nullptr;
166 return OnDataFrameVector(fin, type,
167 std::vector<char>(data, data + buffer_size));
168 }
169
161 MOCK_METHOD2(OnAddChannelResponse, 170 MOCK_METHOD2(OnAddChannelResponse,
162 ChannelState(const std::string&, 171 ChannelState(const std::string&,
163 const std::string&)); // NOLINT 172 const std::string&)); // NOLINT
164 MOCK_METHOD3(OnDataFrame, 173 MOCK_METHOD3(OnDataFrameVector,
165 ChannelState(bool, 174 ChannelState(bool,
166 WebSocketMessageType, 175 WebSocketMessageType,
167 const std::vector<char>&)); // NOLINT 176 const std::vector<char>&)); // NOLINT
168 MOCK_METHOD1(OnFlowControl, ChannelState(int64_t)); // NOLINT 177 MOCK_METHOD1(OnFlowControl, ChannelState(int64_t)); // NOLINT
169 MOCK_METHOD0(OnClosingHandshake, ChannelState(void)); // NOLINT 178 MOCK_METHOD0(OnClosingHandshake, ChannelState(void)); // NOLINT
170 MOCK_METHOD1(OnFailChannel, ChannelState(const std::string&)); // NOLINT 179 MOCK_METHOD1(OnFailChannel, ChannelState(const std::string&)); // NOLINT
171 MOCK_METHOD3(OnDropChannel, 180 MOCK_METHOD3(OnDropChannel,
172 ChannelState(bool, uint16_t, const std::string&)); // NOLINT 181 ChannelState(bool, uint16_t, const std::string&)); // NOLINT
173 182
174 // We can't use GMock with scoped_ptr. 183 // We can't use GMock with scoped_ptr.
175 ChannelState OnStartOpeningHandshake( 184 ChannelState OnStartOpeningHandshake(
176 std::unique_ptr<WebSocketHandshakeRequestInfo>) override { 185 std::unique_ptr<WebSocketHandshakeRequestInfo>) override {
177 OnStartOpeningHandshakeCalled(); 186 OnStartOpeningHandshakeCalled();
(...skipping 23 matching lines...) Expand all
201 210
202 // This fake EventInterface is for tests which need a WebSocketEventInterface 211 // This fake EventInterface is for tests which need a WebSocketEventInterface
203 // implementation but are not verifying how it is used. 212 // implementation but are not verifying how it is used.
204 class FakeWebSocketEventInterface : public WebSocketEventInterface { 213 class FakeWebSocketEventInterface : public WebSocketEventInterface {
205 ChannelState OnAddChannelResponse(const std::string& selected_protocol, 214 ChannelState OnAddChannelResponse(const std::string& selected_protocol,
206 const std::string& extensions) override { 215 const std::string& extensions) override {
207 return CHANNEL_ALIVE; 216 return CHANNEL_ALIVE;
208 } 217 }
209 ChannelState OnDataFrame(bool fin, 218 ChannelState OnDataFrame(bool fin,
210 WebSocketMessageType type, 219 WebSocketMessageType type,
211 const std::vector<char>& data) override { 220 scoped_refptr<IOBuffer> data,
221 size_t data_size) override {
212 return CHANNEL_ALIVE; 222 return CHANNEL_ALIVE;
213 } 223 }
214 ChannelState OnFlowControl(int64_t quota) override { return CHANNEL_ALIVE; } 224 ChannelState OnFlowControl(int64_t quota) override { return CHANNEL_ALIVE; }
215 ChannelState OnClosingHandshake() override { return CHANNEL_ALIVE; } 225 ChannelState OnClosingHandshake() override { return CHANNEL_ALIVE; }
216 ChannelState OnFailChannel(const std::string& message) override { 226 ChannelState OnFailChannel(const std::string& message) override {
217 return CHANNEL_DELETED; 227 return CHANNEL_DELETED;
218 } 228 }
219 ChannelState OnDropChannel(bool was_clean, 229 ChannelState OnDropChannel(bool was_clean,
220 uint16_t code, 230 uint16_t code,
221 const std::string& reason) override { 231 const std::string& reason) override {
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 736
727 GURL socket_url; 737 GURL socket_url;
728 std::unique_ptr<WebSocketHandshakeStreamCreateHelper> create_helper; 738 std::unique_ptr<WebSocketHandshakeStreamCreateHelper> create_helper;
729 url::Origin origin; 739 url::Origin origin;
730 GURL first_party_for_cookies; 740 GURL first_party_for_cookies;
731 URLRequestContext* url_request_context; 741 URLRequestContext* url_request_context;
732 NetLogWithSource net_log; 742 NetLogWithSource net_log;
733 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate; 743 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate;
734 }; 744 };
735 745
736 // Converts a std::string to a std::vector<char>. For test purposes, it is 746 std::vector<char> AsVector(const base::StringPiece& s) {
747 return std::vector<char>(s.begin(), s.end());
748 }
749
750 // Converts a base::StringPiece to a IOBuffer. For test purposes, it is
737 // convenient to be able to specify data as a string, but the 751 // convenient to be able to specify data as a string, but the
738 // WebSocketEventInterface requires the vector<char> type. 752 // WebSocketEventInterface requires the IOBuffer type.
739 std::vector<char> AsVector(const std::string& s) { 753 scoped_refptr<IOBuffer> AsIOBuffer(const base::StringPiece& s) {
740 return std::vector<char>(s.begin(), s.end()); 754 scoped_refptr<IOBuffer> buffer(new IOBuffer(s.size()));
755 std::copy(s.begin(), s.end(), buffer->data());
756 return buffer;
741 } 757 }
742 758
743 class FakeSSLErrorCallbacks 759 class FakeSSLErrorCallbacks
744 : public WebSocketEventInterface::SSLErrorCallbacks { 760 : public WebSocketEventInterface::SSLErrorCallbacks {
745 public: 761 public:
746 void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {} 762 void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {}
747 void ContinueSSLRequest() override {} 763 void ContinueSSLRequest() override {}
748 }; 764 };
749 765
750 // Base class for all test fixtures. 766 // Base class for all test fixtures.
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 WebSocketChannelDeletingTest* fixture) 891 WebSocketChannelDeletingTest* fixture)
876 : fixture_(fixture) {} 892 : fixture_(fixture) {}
877 893
878 ChannelState OnAddChannelResponse(const std::string& selected_protocol, 894 ChannelState OnAddChannelResponse(const std::string& selected_protocol,
879 const std::string& extensions) override { 895 const std::string& extensions) override {
880 return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE); 896 return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE);
881 } 897 }
882 898
883 ChannelState OnDataFrame(bool fin, 899 ChannelState OnDataFrame(bool fin,
884 WebSocketMessageType type, 900 WebSocketMessageType type,
885 const std::vector<char>& data) override { 901 scoped_refptr<IOBuffer> data,
902 size_t data_size) override {
886 return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME); 903 return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME);
887 } 904 }
888 905
889 ChannelState OnFlowControl(int64_t quota) override { 906 ChannelState OnFlowControl(int64_t quota) override {
890 return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL); 907 return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL);
891 } 908 }
892 909
893 ChannelState OnClosingHandshake() override { 910 ChannelState OnClosingHandshake() override {
894 return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE); 911 return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE);
895 } 912 }
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1100 EXPECT_EQ(nullptr, channel_.get()); 1117 EXPECT_EQ(nullptr, channel_.get());
1101 } 1118 }
1102 1119
1103 TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterSend) { 1120 TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterSend) {
1104 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1121 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1105 // Avoid deleting the channel yet. 1122 // Avoid deleting the channel yet.
1106 deleting_ = EVENT_ON_FAIL_CHANNEL | EVENT_ON_DROP_CHANNEL; 1123 deleting_ = EVENT_ON_FAIL_CHANNEL | EVENT_ON_DROP_CHANNEL;
1107 CreateChannelAndConnectSuccessfully(); 1124 CreateChannelAndConnectSuccessfully();
1108 ASSERT_TRUE(channel_); 1125 ASSERT_TRUE(channel_);
1109 deleting_ = EVENT_ON_FLOW_CONTROL; 1126 deleting_ = EVENT_ON_FLOW_CONTROL;
1110 channel_->SendFrame(true, 1127 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1111 WebSocketFrameHeader::kOpCodeText, 1128 AsIOBuffer(std::string(kDefaultInitialQuota, 'B')),
1112 std::vector<char>(kDefaultInitialQuota, 'B')); 1129 kDefaultInitialQuota);
1113 EXPECT_EQ(nullptr, channel_.get()); 1130 EXPECT_EQ(nullptr, channel_.get());
1114 } 1131 }
1115 1132
1116 TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeSync) { 1133 TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeSync) {
1117 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1134 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1118 new ReadableFakeWebSocketStream); 1135 new ReadableFakeWebSocketStream);
1119 static const InitFrame frames[] = { 1136 static const InitFrame frames[] = {
1120 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, 1137 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
1121 NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}}; 1138 NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}};
1122 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 1139 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
(...skipping 16 matching lines...) Expand all
1139 ASSERT_TRUE(channel_); 1156 ASSERT_TRUE(channel_);
1140 base::RunLoop().RunUntilIdle(); 1157 base::RunLoop().RunUntilIdle();
1141 EXPECT_EQ(nullptr, channel_.get()); 1158 EXPECT_EQ(nullptr, channel_.get());
1142 } 1159 }
1143 1160
1144 TEST_F(WebSocketChannelDeletingTest, OnDropChannelWriteError) { 1161 TEST_F(WebSocketChannelDeletingTest, OnDropChannelWriteError) {
1145 set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream)); 1162 set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream));
1146 deleting_ = EVENT_ON_DROP_CHANNEL; 1163 deleting_ = EVENT_ON_DROP_CHANNEL;
1147 CreateChannelAndConnectSuccessfully(); 1164 CreateChannelAndConnectSuccessfully();
1148 ASSERT_TRUE(channel_); 1165 ASSERT_TRUE(channel_);
1149 channel_->SendFrame( 1166 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1150 true, WebSocketFrameHeader::kOpCodeText, AsVector("this will fail")); 1167 AsIOBuffer("this will fail"), 14U);
1151 EXPECT_EQ(nullptr, channel_.get()); 1168 EXPECT_EQ(nullptr, channel_.get());
1152 } 1169 }
1153 1170
1154 TEST_F(WebSocketChannelDeletingTest, OnDropChannelReadError) { 1171 TEST_F(WebSocketChannelDeletingTest, OnDropChannelReadError) {
1155 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1172 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1156 new ReadableFakeWebSocketStream); 1173 new ReadableFakeWebSocketStream);
1157 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, 1174 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
1158 ERR_FAILED); 1175 ERR_FAILED);
1159 set_stream(std::move(stream)); 1176 set_stream(std::move(stream));
1160 deleting_ = EVENT_ON_DROP_CHANNEL; 1177 deleting_ = EVENT_ON_DROP_CHANNEL;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1202 base::Time())); 1219 base::Time()));
1203 base::RunLoop().RunUntilIdle(); 1220 base::RunLoop().RunUntilIdle();
1204 EXPECT_EQ(nullptr, channel_.get()); 1221 EXPECT_EQ(nullptr, channel_.get());
1205 } 1222 }
1206 1223
1207 TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) { 1224 TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) {
1208 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1225 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1209 deleting_ = EVENT_ON_FAIL_CHANNEL; 1226 deleting_ = EVENT_ON_FAIL_CHANNEL;
1210 CreateChannelAndConnectSuccessfully(); 1227 CreateChannelAndConnectSuccessfully();
1211 ASSERT_TRUE(channel_); 1228 ASSERT_TRUE(channel_);
1212 channel_->SendFrame(true, 1229 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1213 WebSocketFrameHeader::kOpCodeText, 1230 AsIOBuffer(std::string(kDefaultInitialQuota * 2, 'T')),
1214 std::vector<char>(kDefaultInitialQuota * 2, 'T')); 1231 kDefaultInitialQuota * 2);
1215 EXPECT_EQ(nullptr, channel_.get()); 1232 EXPECT_EQ(nullptr, channel_.get());
1216 } 1233 }
1217 1234
1218 TEST_F(WebSocketChannelDeletingTest, FailChannelInOnReadDone) { 1235 TEST_F(WebSocketChannelDeletingTest, FailChannelInOnReadDone) {
1219 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1236 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1220 new ReadableFakeWebSocketStream); 1237 new ReadableFakeWebSocketStream);
1221 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, 1238 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
1222 ERR_WS_PROTOCOL_ERROR); 1239 ERR_WS_PROTOCOL_ERROR);
1223 set_stream(std::move(stream)); 1240 set_stream(std::move(stream));
1224 deleting_ = EVENT_ON_FAIL_CHANNEL; 1241 deleting_ = EVENT_ON_FAIL_CHANNEL;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1389 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1406 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1390 new ReadableFakeWebSocketStream); 1407 new ReadableFakeWebSocketStream);
1391 static const InitFrame frames[] = { 1408 static const InitFrame frames[] = {
1392 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; 1409 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
1393 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 1410 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
1394 set_stream(std::move(stream)); 1411 set_stream(std::move(stream));
1395 { 1412 {
1396 InSequence s; 1413 InSequence s;
1397 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1414 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1398 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1415 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1399 EXPECT_CALL( 1416 EXPECT_CALL(*event_interface_,
1400 *event_interface_, 1417 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
1401 OnDataFrame( 1418 AsVector("HELLO")));
1402 true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO")));
1403 } 1419 }
1404 1420
1405 CreateChannelAndConnectSuccessfully(); 1421 CreateChannelAndConnectSuccessfully();
1406 } 1422 }
1407 1423
1408 // A remote server could accept the handshake, but then immediately send a 1424 // A remote server could accept the handshake, but then immediately send a
1409 // Close frame. 1425 // Close frame.
1410 TEST_F(WebSocketChannelEventInterfaceTest, CloseAfterHandshake) { 1426 TEST_F(WebSocketChannelEventInterfaceTest, CloseAfterHandshake) {
1411 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1427 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1412 new ReadableFakeWebSocketStream); 1428 new ReadableFakeWebSocketStream);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1458 // We use this checkpoint object to verify that the callback isn't called 1474 // We use this checkpoint object to verify that the callback isn't called
1459 // until we expect it to be. 1475 // until we expect it to be.
1460 Checkpoint checkpoint; 1476 Checkpoint checkpoint;
1461 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); 1477 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
1462 set_stream(std::move(stream)); 1478 set_stream(std::move(stream));
1463 { 1479 {
1464 InSequence s; 1480 InSequence s;
1465 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1481 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1466 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1482 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1467 EXPECT_CALL(checkpoint, Call(1)); 1483 EXPECT_CALL(checkpoint, Call(1));
1468 EXPECT_CALL( 1484 EXPECT_CALL(*event_interface_,
1469 *event_interface_, 1485 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
1470 OnDataFrame( 1486 AsVector("HELLO")));
1471 true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO")));
1472 EXPECT_CALL(checkpoint, Call(2)); 1487 EXPECT_CALL(checkpoint, Call(2));
1473 } 1488 }
1474 1489
1475 CreateChannelAndConnectSuccessfully(); 1490 CreateChannelAndConnectSuccessfully();
1476 checkpoint.Call(1); 1491 checkpoint.Call(1);
1477 base::RunLoop().RunUntilIdle(); 1492 base::RunLoop().RunUntilIdle();
1478 checkpoint.Call(2); 1493 checkpoint.Call(2);
1479 } 1494 }
1480 1495
1481 // Extra data can arrive while a read is being processed, resulting in the next 1496 // Extra data can arrive while a read is being processed, resulting in the next
1482 // read completing synchronously. 1497 // read completing synchronously.
1483 TEST_F(WebSocketChannelEventInterfaceTest, AsyncThenSyncRead) { 1498 TEST_F(WebSocketChannelEventInterfaceTest, AsyncThenSyncRead) {
1484 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1499 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1485 new ReadableFakeWebSocketStream); 1500 new ReadableFakeWebSocketStream);
1486 static const InitFrame frames1[] = { 1501 static const InitFrame frames1[] = {
1487 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; 1502 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}};
1488 static const InitFrame frames2[] = { 1503 static const InitFrame frames2[] = {
1489 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "WORLD"}}; 1504 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "WORLD"}};
1490 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); 1505 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
1491 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames2); 1506 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames2);
1492 set_stream(std::move(stream)); 1507 set_stream(std::move(stream));
1493 { 1508 {
1494 InSequence s; 1509 InSequence s;
1495 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1510 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1496 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1511 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1497 EXPECT_CALL( 1512 EXPECT_CALL(*event_interface_,
1498 *event_interface_, 1513 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
1499 OnDataFrame( 1514 AsVector("HELLO")));
1500 true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO"))); 1515 EXPECT_CALL(*event_interface_,
1501 EXPECT_CALL( 1516 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
1502 *event_interface_, 1517 AsVector("WORLD")));
1503 OnDataFrame(
1504 true, WebSocketFrameHeader::kOpCodeText, AsVector("WORLD")));
1505 } 1518 }
1506 1519
1507 CreateChannelAndConnectSuccessfully(); 1520 CreateChannelAndConnectSuccessfully();
1508 base::RunLoop().RunUntilIdle(); 1521 base::RunLoop().RunUntilIdle();
1509 } 1522 }
1510 1523
1511 // Data frames are delivered the same regardless of how many reads they arrive 1524 // Data frames are delivered the same regardless of how many reads they arrive
1512 // as. 1525 // as.
1513 TEST_F(WebSocketChannelEventInterfaceTest, FragmentedMessage) { 1526 TEST_F(WebSocketChannelEventInterfaceTest, FragmentedMessage) {
1514 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1527 std::unique_ptr<ReadableFakeWebSocketStream> stream(
(...skipping 14 matching lines...) Expand all
1529 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, 1542 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
1530 NOT_MASKED, "FRAMES"}}; 1543 NOT_MASKED, "FRAMES"}};
1531 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); 1544 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
1532 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2); 1545 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2);
1533 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3); 1546 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3);
1534 set_stream(std::move(stream)); 1547 set_stream(std::move(stream));
1535 { 1548 {
1536 InSequence s; 1549 InSequence s;
1537 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1550 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1538 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1551 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1552 EXPECT_CALL(*event_interface_,
1553 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
1554 AsVector("THREE")));
1539 EXPECT_CALL( 1555 EXPECT_CALL(
1540 *event_interface_, 1556 *event_interface_,
1541 OnDataFrame( 1557 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
1542 false, WebSocketFrameHeader::kOpCodeText, AsVector("THREE"))); 1558 AsVector(" ")));
1543 EXPECT_CALL( 1559 EXPECT_CALL(
1544 *event_interface_, 1560 *event_interface_,
1545 OnDataFrame( 1561 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
1546 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" "))); 1562 AsVector("SMALL")));
1547 EXPECT_CALL(*event_interface_,
1548 OnDataFrame(false,
1549 WebSocketFrameHeader::kOpCodeContinuation,
1550 AsVector("SMALL")));
1551 EXPECT_CALL( 1563 EXPECT_CALL(
1552 *event_interface_, 1564 *event_interface_,
1553 OnDataFrame( 1565 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
1554 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" "))); 1566 AsVector(" ")));
1555 EXPECT_CALL(*event_interface_, 1567 EXPECT_CALL(
1556 OnDataFrame(true, 1568 *event_interface_,
1557 WebSocketFrameHeader::kOpCodeContinuation, 1569 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
1558 AsVector("FRAMES"))); 1570 AsVector("FRAMES")));
1559 } 1571 }
1560 1572
1561 CreateChannelAndConnectSuccessfully(); 1573 CreateChannelAndConnectSuccessfully();
1562 base::RunLoop().RunUntilIdle(); 1574 base::RunLoop().RunUntilIdle();
1563 } 1575 }
1564 1576
1565 // A message can consist of one frame with null payload. 1577 // A message can consist of one frame with null payload.
1566 TEST_F(WebSocketChannelEventInterfaceTest, NullMessage) { 1578 TEST_F(WebSocketChannelEventInterfaceTest, NullMessage) {
1567 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1579 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1568 new ReadableFakeWebSocketStream); 1580 new ReadableFakeWebSocketStream);
1569 static const InitFrame frames[] = { 1581 static const InitFrame frames[] = {
1570 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr}}; 1582 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr}};
1571 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 1583 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
1572 set_stream(std::move(stream)); 1584 set_stream(std::move(stream));
1573 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1585 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1574 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1586 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1575 EXPECT_CALL( 1587 EXPECT_CALL(
1576 *event_interface_, 1588 *event_interface_,
1577 OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector(""))); 1589 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText, AsVector("")));
1578 CreateChannelAndConnectSuccessfully(); 1590 CreateChannelAndConnectSuccessfully();
1579 } 1591 }
1580 1592
1581 // Connection closed by the remote host without a closing handshake. 1593 // Connection closed by the remote host without a closing handshake.
1582 TEST_F(WebSocketChannelEventInterfaceTest, AsyncAbnormalClosure) { 1594 TEST_F(WebSocketChannelEventInterfaceTest, AsyncAbnormalClosure) {
1583 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1595 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1584 new ReadableFakeWebSocketStream); 1596 new ReadableFakeWebSocketStream);
1585 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, 1597 stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC,
1586 ERR_CONNECTION_CLOSED); 1598 ERR_CONNECTION_CLOSED);
1587 set_stream(std::move(stream)); 1599 set_stream(std::move(stream));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1676 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, 1688 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
1677 NOT_MASKED, "MESSAGE"}}; 1689 NOT_MASKED, "MESSAGE"}};
1678 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); 1690 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1);
1679 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2); 1691 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2);
1680 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3); 1692 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3);
1681 set_stream(std::move(stream)); 1693 set_stream(std::move(stream));
1682 { 1694 {
1683 InSequence s; 1695 InSequence s;
1684 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1696 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1685 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1697 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1698 EXPECT_CALL(*event_interface_,
1699 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
1700 AsVector("SPLIT ")));
1686 EXPECT_CALL( 1701 EXPECT_CALL(
1687 *event_interface_, 1702 *event_interface_,
1688 OnDataFrame( 1703 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
1689 false, WebSocketFrameHeader::kOpCodeText, AsVector("SPLIT "))); 1704 AsVector("MESSAGE")));
1690 EXPECT_CALL(*event_interface_,
1691 OnDataFrame(true,
1692 WebSocketFrameHeader::kOpCodeContinuation,
1693 AsVector("MESSAGE")));
1694 } 1705 }
1695 1706
1696 CreateChannelAndConnectSuccessfully(); 1707 CreateChannelAndConnectSuccessfully();
1697 base::RunLoop().RunUntilIdle(); 1708 base::RunLoop().RunUntilIdle();
1698 } 1709 }
1699 1710
1700 // It seems redundant to repeat the entirety of the above test, so just test a 1711 // It seems redundant to repeat the entirety of the above test, so just test a
1701 // Pong with null data. 1712 // Pong with null data.
1702 TEST_F(WebSocketChannelEventInterfaceTest, PongWithNullData) { 1713 TEST_F(WebSocketChannelEventInterfaceTest, PongWithNullData) {
1703 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1714 std::unique_ptr<ReadableFakeWebSocketStream> stream(
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1742 // for each one. 1753 // for each one.
1743 TEST_F(WebSocketChannelEventInterfaceTest, SmallWriteDoesntUpdateQuota) { 1754 TEST_F(WebSocketChannelEventInterfaceTest, SmallWriteDoesntUpdateQuota) {
1744 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1755 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1745 { 1756 {
1746 InSequence s; 1757 InSequence s;
1747 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1758 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1748 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1759 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1749 } 1760 }
1750 1761
1751 CreateChannelAndConnectSuccessfully(); 1762 CreateChannelAndConnectSuccessfully();
1752 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("B")); 1763 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsIOBuffer("B"),
1764 1U);
1753 } 1765 }
1754 1766
1755 // If we send enough to go below |send_quota_low_water_mark_| we should get our 1767 // If we send enough to go below |send_quota_low_water_mark_| we should get our
1756 // quota refreshed. 1768 // quota refreshed.
1757 TEST_F(WebSocketChannelEventInterfaceTest, LargeWriteUpdatesQuota) { 1769 TEST_F(WebSocketChannelEventInterfaceTest, LargeWriteUpdatesQuota) {
1758 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1770 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1759 // We use this checkpoint object to verify that the quota update comes after 1771 // We use this checkpoint object to verify that the quota update comes after
1760 // the write. 1772 // the write.
1761 Checkpoint checkpoint; 1773 Checkpoint checkpoint;
1762 { 1774 {
1763 InSequence s; 1775 InSequence s;
1764 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1776 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1765 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1777 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1766 EXPECT_CALL(checkpoint, Call(1)); 1778 EXPECT_CALL(checkpoint, Call(1));
1767 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1779 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1768 EXPECT_CALL(checkpoint, Call(2)); 1780 EXPECT_CALL(checkpoint, Call(2));
1769 } 1781 }
1770 1782
1771 CreateChannelAndConnectSuccessfully(); 1783 CreateChannelAndConnectSuccessfully();
1772 checkpoint.Call(1); 1784 checkpoint.Call(1);
1773 channel_->SendFrame(true, 1785 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1774 WebSocketFrameHeader::kOpCodeText, 1786 AsIOBuffer(std::string(kDefaultInitialQuota, 'B')),
1775 std::vector<char>(kDefaultInitialQuota, 'B')); 1787 kDefaultInitialQuota);
1776 checkpoint.Call(2); 1788 checkpoint.Call(2);
1777 } 1789 }
1778 1790
1779 // Verify that our quota actually is refreshed when we are told it is. 1791 // Verify that our quota actually is refreshed when we are told it is.
1780 TEST_F(WebSocketChannelEventInterfaceTest, QuotaReallyIsRefreshed) { 1792 TEST_F(WebSocketChannelEventInterfaceTest, QuotaReallyIsRefreshed) {
1781 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1793 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1782 Checkpoint checkpoint; 1794 Checkpoint checkpoint;
1783 { 1795 {
1784 InSequence s; 1796 InSequence s;
1785 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1797 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1786 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1798 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1787 EXPECT_CALL(checkpoint, Call(1)); 1799 EXPECT_CALL(checkpoint, Call(1));
1788 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1800 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1789 EXPECT_CALL(checkpoint, Call(2)); 1801 EXPECT_CALL(checkpoint, Call(2));
1790 // If quota was not really refreshed, we would get an OnDropChannel() 1802 // If quota was not really refreshed, we would get an OnDropChannel()
1791 // message. 1803 // message.
1792 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1804 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1793 EXPECT_CALL(checkpoint, Call(3)); 1805 EXPECT_CALL(checkpoint, Call(3));
1794 } 1806 }
1795 1807
1796 CreateChannelAndConnectSuccessfully(); 1808 CreateChannelAndConnectSuccessfully();
1797 checkpoint.Call(1); 1809 checkpoint.Call(1);
1798 channel_->SendFrame(true, 1810 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1799 WebSocketFrameHeader::kOpCodeText, 1811 AsIOBuffer(std::string(kDefaultQuotaRefreshTrigger, 'D')),
1800 std::vector<char>(kDefaultQuotaRefreshTrigger, 'D')); 1812 kDefaultQuotaRefreshTrigger);
1801 checkpoint.Call(2); 1813 checkpoint.Call(2);
1802 // We should have received more quota at this point. 1814 // We should have received more quota at this point.
1803 channel_->SendFrame(true, 1815 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1804 WebSocketFrameHeader::kOpCodeText, 1816 AsIOBuffer(std::string(kDefaultQuotaRefreshTrigger, 'E')),
1805 std::vector<char>(kDefaultQuotaRefreshTrigger, 'E')); 1817 kDefaultQuotaRefreshTrigger);
1806 checkpoint.Call(3); 1818 checkpoint.Call(3);
1807 } 1819 }
1808 1820
1809 // If we send more than the available quota then the connection will be closed 1821 // If we send more than the available quota then the connection will be closed
1810 // with an error. 1822 // with an error.
1811 TEST_F(WebSocketChannelEventInterfaceTest, WriteOverQuotaIsRejected) { 1823 TEST_F(WebSocketChannelEventInterfaceTest, WriteOverQuotaIsRejected) {
1812 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 1824 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
1813 { 1825 {
1814 InSequence s; 1826 InSequence s;
1815 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1827 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1816 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); 1828 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
1817 EXPECT_CALL(*event_interface_, OnFailChannel("Send quota exceeded")); 1829 EXPECT_CALL(*event_interface_, OnFailChannel("Send quota exceeded"));
1818 } 1830 }
1819 1831
1820 CreateChannelAndConnectSuccessfully(); 1832 CreateChannelAndConnectSuccessfully();
1821 channel_->SendFrame(true, 1833 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1822 WebSocketFrameHeader::kOpCodeText, 1834 AsIOBuffer(std::string(kDefaultInitialQuota + 1, 'C')),
1823 std::vector<char>(kDefaultInitialQuota + 1, 'C')); 1835 kDefaultInitialQuota + 1);
1824 } 1836 }
1825 1837
1826 // If a write fails, the channel is dropped. 1838 // If a write fails, the channel is dropped.
1827 TEST_F(WebSocketChannelEventInterfaceTest, FailedWrite) { 1839 TEST_F(WebSocketChannelEventInterfaceTest, FailedWrite) {
1828 set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream)); 1840 set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream));
1829 Checkpoint checkpoint; 1841 Checkpoint checkpoint;
1830 { 1842 {
1831 InSequence s; 1843 InSequence s;
1832 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1844 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1833 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1845 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1834 EXPECT_CALL(checkpoint, Call(1)); 1846 EXPECT_CALL(checkpoint, Call(1));
1835 EXPECT_CALL(*event_interface_, 1847 EXPECT_CALL(*event_interface_,
1836 OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)); 1848 OnDropChannel(false, kWebSocketErrorAbnormalClosure, _));
1837 EXPECT_CALL(checkpoint, Call(2)); 1849 EXPECT_CALL(checkpoint, Call(2));
1838 } 1850 }
1839 1851
1840 CreateChannelAndConnectSuccessfully(); 1852 CreateChannelAndConnectSuccessfully();
1841 checkpoint.Call(1); 1853 checkpoint.Call(1);
1842 1854
1843 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("H")); 1855 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsIOBuffer("H"),
1856 1U);
1844 checkpoint.Call(2); 1857 checkpoint.Call(2);
1845 } 1858 }
1846 1859
1847 // OnDropChannel() is called exactly once when StartClosingHandshake() is used. 1860 // OnDropChannel() is called exactly once when StartClosingHandshake() is used.
1848 TEST_F(WebSocketChannelEventInterfaceTest, SendCloseDropsChannel) { 1861 TEST_F(WebSocketChannelEventInterfaceTest, SendCloseDropsChannel) {
1849 set_stream(base::WrapUnique(new EchoeyFakeWebSocketStream)); 1862 set_stream(base::WrapUnique(new EchoeyFakeWebSocketStream));
1850 { 1863 {
1851 InSequence s; 1864 InSequence s;
1852 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1865 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1853 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1866 EXPECT_CALL(*event_interface_, OnFlowControl(_));
(...skipping 25 matching lines...) Expand all
1879 set_stream(base::WrapUnique(new ResetOnWriteFakeWebSocketStream)); 1892 set_stream(base::WrapUnique(new ResetOnWriteFakeWebSocketStream));
1880 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 1893 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
1881 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 1894 EXPECT_CALL(*event_interface_, OnFlowControl(_));
1882 1895
1883 EXPECT_CALL(*event_interface_, 1896 EXPECT_CALL(*event_interface_,
1884 OnDropChannel(false, kWebSocketErrorAbnormalClosure, "")) 1897 OnDropChannel(false, kWebSocketErrorAbnormalClosure, ""))
1885 .Times(1); 1898 .Times(1);
1886 1899
1887 CreateChannelAndConnectSuccessfully(); 1900 CreateChannelAndConnectSuccessfully();
1888 1901
1889 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("yt?")); 1902 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
1903 AsIOBuffer("yt?"), 3U);
1890 base::RunLoop().RunUntilIdle(); 1904 base::RunLoop().RunUntilIdle();
1891 } 1905 }
1892 1906
1893 // When the remote server sends a Close frame with an empty payload, 1907 // When the remote server sends a Close frame with an empty payload,
1894 // WebSocketChannel should report code 1005, kWebSocketErrorNoStatusReceived. 1908 // WebSocketChannel should report code 1005, kWebSocketErrorNoStatusReceived.
1895 TEST_F(WebSocketChannelEventInterfaceTest, CloseWithNoPayloadGivesStatus1005) { 1909 TEST_F(WebSocketChannelEventInterfaceTest, CloseWithNoPayloadGivesStatus1005) {
1896 std::unique_ptr<ReadableFakeWebSocketStream> stream( 1910 std::unique_ptr<ReadableFakeWebSocketStream> stream(
1897 new ReadableFakeWebSocketStream); 1911 new ReadableFakeWebSocketStream);
1898 static const InitFrame frames[] = { 1912 static const InitFrame frames[] = {
1899 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}}; 1913 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}};
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
2328 std::unique_ptr<ReadableFakeWebSocketStream> stream( 2342 std::unique_ptr<ReadableFakeWebSocketStream> stream(
2329 new ReadableFakeWebSocketStream); 2343 new ReadableFakeWebSocketStream);
2330 static const InitFrame frames[] = { 2344 static const InitFrame frames[] = {
2331 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; 2345 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
2332 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 2346 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
2333 set_stream(std::move(stream)); 2347 set_stream(std::move(stream));
2334 { 2348 {
2335 InSequence s; 2349 InSequence s;
2336 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2350 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2337 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2351 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2352 EXPECT_CALL(*event_interface_,
2353 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2354 AsVector("FO")));
2338 EXPECT_CALL( 2355 EXPECT_CALL(
2339 *event_interface_, 2356 *event_interface_,
2340 OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO"))); 2357 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2358 AsVector("U")));
2341 EXPECT_CALL( 2359 EXPECT_CALL(
2342 *event_interface_, 2360 *event_interface_,
2343 OnDataFrame( 2361 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
2344 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U"))); 2362 AsVector("R")));
2345 EXPECT_CALL(
2346 *event_interface_,
2347 OnDataFrame(
2348 true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R")));
2349 } 2363 }
2350 2364
2351 CreateChannelAndConnectWithQuota(2); 2365 CreateChannelAndConnectWithQuota(2);
2352 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); 2366 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1));
2353 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); 2367 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1));
2354 } 2368 }
2355 2369
2356 // The code path for async messages is slightly different, so test it 2370 // The code path for async messages is slightly different, so test it
2357 // separately. 2371 // separately.
2358 TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitAsync) { 2372 TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitAsync) {
2359 std::unique_ptr<ReadableFakeWebSocketStream> stream( 2373 std::unique_ptr<ReadableFakeWebSocketStream> stream(
2360 new ReadableFakeWebSocketStream); 2374 new ReadableFakeWebSocketStream);
2361 static const InitFrame frames[] = { 2375 static const InitFrame frames[] = {
2362 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; 2376 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}};
2363 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); 2377 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames);
2364 set_stream(std::move(stream)); 2378 set_stream(std::move(stream));
2365 Checkpoint checkpoint; 2379 Checkpoint checkpoint;
2366 { 2380 {
2367 InSequence s; 2381 InSequence s;
2368 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2382 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2369 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2383 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2370 EXPECT_CALL(checkpoint, Call(1)); 2384 EXPECT_CALL(checkpoint, Call(1));
2371 EXPECT_CALL( 2385 EXPECT_CALL(*event_interface_,
2372 *event_interface_, 2386 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2373 OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO"))); 2387 AsVector("FO")));
2374 EXPECT_CALL(checkpoint, Call(2)); 2388 EXPECT_CALL(checkpoint, Call(2));
2375 EXPECT_CALL( 2389 EXPECT_CALL(
2376 *event_interface_, 2390 *event_interface_,
2377 OnDataFrame( 2391 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2378 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U"))); 2392 AsVector("U")));
2379 EXPECT_CALL(checkpoint, Call(3)); 2393 EXPECT_CALL(checkpoint, Call(3));
2380 EXPECT_CALL( 2394 EXPECT_CALL(
2381 *event_interface_, 2395 *event_interface_,
2382 OnDataFrame( 2396 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
2383 true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R"))); 2397 AsVector("R")));
2384 } 2398 }
2385 2399
2386 CreateChannelAndConnectWithQuota(2); 2400 CreateChannelAndConnectWithQuota(2);
2387 checkpoint.Call(1); 2401 checkpoint.Call(1);
2388 base::RunLoop().RunUntilIdle(); 2402 base::RunLoop().RunUntilIdle();
2389 checkpoint.Call(2); 2403 checkpoint.Call(2);
2390 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); 2404 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1));
2391 checkpoint.Call(3); 2405 checkpoint.Call(3);
2392 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); 2406 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1));
2393 } 2407 }
(...skipping 13 matching lines...) Expand all
2407 NOT_MASKED, "SECOND FRAME IS 26 BYTES. "}, 2421 NOT_MASKED, "SECOND FRAME IS 26 BYTES. "},
2408 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, 2422 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
2409 NOT_MASKED, "FINAL FRAME IS 24 BYTES."}}; 2423 NOT_MASKED, "FINAL FRAME IS 24 BYTES."}};
2410 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 2424 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
2411 set_stream(std::move(stream)); 2425 set_stream(std::move(stream));
2412 { 2426 {
2413 InSequence s; 2427 InSequence s;
2414 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2428 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2415 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2429 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2416 EXPECT_CALL(*event_interface_, 2430 EXPECT_CALL(*event_interface_,
2417 OnDataFrame(false, 2431 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2418 WebSocketFrameHeader::kOpCodeText, 2432 AsVector("FIRST FRAME IS")));
2419 AsVector("FIRST FRAME IS"))); 2433 EXPECT_CALL(
2420 EXPECT_CALL(*event_interface_, 2434 *event_interface_,
2421 OnDataFrame(false, 2435 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2422 WebSocketFrameHeader::kOpCodeContinuation, 2436 AsVector(" 25 BYTES. ")));
2423 AsVector(" 25 BYTES. "))); 2437 EXPECT_CALL(
2424 EXPECT_CALL(*event_interface_, 2438 *event_interface_,
2425 OnDataFrame(false, 2439 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2426 WebSocketFrameHeader::kOpCodeContinuation, 2440 AsVector("SECOND FRAME IS 26 BYTES. ")));
2427 AsVector("SECOND FRAME IS 26 BYTES. "))); 2441 EXPECT_CALL(
2428 EXPECT_CALL(*event_interface_, 2442 *event_interface_,
2429 OnDataFrame(false, 2443 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2430 WebSocketFrameHeader::kOpCodeContinuation, 2444 AsVector("FINAL ")));
2431 AsVector("FINAL "))); 2445 EXPECT_CALL(
2432 EXPECT_CALL(*event_interface_, 2446 *event_interface_,
2433 OnDataFrame(true, 2447 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
2434 WebSocketFrameHeader::kOpCodeContinuation, 2448 AsVector("FRAME IS 24 BYTES.")));
2435 AsVector("FRAME IS 24 BYTES.")));
2436 } 2449 }
2437 CreateChannelAndConnectWithQuota(14); 2450 CreateChannelAndConnectWithQuota(14);
2438 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(43)); 2451 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(43));
2439 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(32)); 2452 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(32));
2440 } 2453 }
2441 2454
2442 // An empty message handled when we are out of quota must not be delivered 2455 // An empty message handled when we are out of quota must not be delivered
2443 // out-of-order with respect to other messages. 2456 // out-of-order with respect to other messages.
2444 TEST_F(WebSocketChannelFlowControlTest, EmptyMessageNoQuota) { 2457 TEST_F(WebSocketChannelFlowControlTest, EmptyMessageNoQuota) {
2445 std::unique_ptr<ReadableFakeWebSocketStream> stream( 2458 std::unique_ptr<ReadableFakeWebSocketStream> stream(
2446 new ReadableFakeWebSocketStream); 2459 new ReadableFakeWebSocketStream);
2447 static const InitFrame frames[] = { 2460 static const InitFrame frames[] = {
2448 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, 2461 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED,
2449 "FIRST MESSAGE"}, 2462 "FIRST MESSAGE"},
2450 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr}, 2463 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr},
2451 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, 2464 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED,
2452 "THIRD MESSAGE"}}; 2465 "THIRD MESSAGE"}};
2453 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 2466 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
2454 set_stream(std::move(stream)); 2467 set_stream(std::move(stream));
2455 { 2468 {
2456 InSequence s; 2469 InSequence s;
2457 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2470 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2458 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2471 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2459 EXPECT_CALL(*event_interface_, 2472 EXPECT_CALL(*event_interface_,
2460 OnDataFrame(false, 2473 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2461 WebSocketFrameHeader::kOpCodeText, 2474 AsVector("FIRST ")));
2462 AsVector("FIRST "))); 2475 EXPECT_CALL(
2476 *event_interface_,
2477 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
2478 AsVector("MESSAGE")));
2463 EXPECT_CALL(*event_interface_, 2479 EXPECT_CALL(*event_interface_,
2464 OnDataFrame(true, 2480 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
2465 WebSocketFrameHeader::kOpCodeContinuation, 2481 AsVector("")));
2466 AsVector("MESSAGE")));
2467 EXPECT_CALL(*event_interface_, 2482 EXPECT_CALL(*event_interface_,
2468 OnDataFrame(true, 2483 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText,
2469 WebSocketFrameHeader::kOpCodeText, 2484 AsVector("THIRD MESSAGE")));
2470 AsVector("")));
2471 EXPECT_CALL(*event_interface_,
2472 OnDataFrame(true,
2473 WebSocketFrameHeader::kOpCodeText,
2474 AsVector("THIRD MESSAGE")));
2475 } 2485 }
2476 2486
2477 CreateChannelAndConnectWithQuota(6); 2487 CreateChannelAndConnectWithQuota(6);
2478 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(128)); 2488 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(128));
2479 } 2489 }
2480 2490
2481 // A close frame should not overtake data frames. 2491 // A close frame should not overtake data frames.
2482 TEST_F(WebSocketChannelFlowControlTest, CloseFrameShouldNotOvertakeDataFrames) { 2492 TEST_F(WebSocketChannelFlowControlTest, CloseFrameShouldNotOvertakeDataFrames) {
2483 std::unique_ptr<ReadableFakeWebSocketStream> stream( 2493 std::unique_ptr<ReadableFakeWebSocketStream> stream(
2484 new ReadableFakeWebSocketStream); 2494 new ReadableFakeWebSocketStream);
2485 static const InitFrame frames[] = { 2495 static const InitFrame frames[] = {
2486 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, 2496 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED,
2487 "FIRST "}, 2497 "FIRST "},
2488 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, 2498 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED,
2489 "MESSAGE"}, 2499 "MESSAGE"},
2490 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, 2500 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED,
2491 "SECOND "}, 2501 "SECOND "},
2492 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, 2502 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED,
2493 CLOSE_DATA(NORMAL_CLOSURE, "GOOD BYE")}, 2503 CLOSE_DATA(NORMAL_CLOSURE, "GOOD BYE")},
2494 }; 2504 };
2495 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 2505 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
2496 set_stream(std::move(stream)); 2506 set_stream(std::move(stream));
2497 Checkpoint checkpoint; 2507 Checkpoint checkpoint;
2498 InSequence s; 2508 InSequence s;
2499 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2509 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2500 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2510 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2501 EXPECT_CALL(*event_interface_, 2511 EXPECT_CALL(*event_interface_,
2502 OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, 2512 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2503 AsVector("FIRST "))); 2513 AsVector("FIRST ")));
2504 EXPECT_CALL(checkpoint, Call(1)); 2514 EXPECT_CALL(checkpoint, Call(1));
2505 EXPECT_CALL(*event_interface_, 2515 EXPECT_CALL(
2506 OnDataFrame(false, WebSocketFrameHeader::kOpCodeContinuation, 2516 *event_interface_,
2507 AsVector("MESSAG"))); 2517 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2518 AsVector("MESSAG")));
2508 EXPECT_CALL(checkpoint, Call(2)); 2519 EXPECT_CALL(checkpoint, Call(2));
2509 EXPECT_CALL(*event_interface_, 2520 EXPECT_CALL(*event_interface_,
2510 OnDataFrame(true, WebSocketFrameHeader::kOpCodeContinuation, 2521 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeContinuation,
2511 AsVector("E"))); 2522 AsVector("E")));
2523 EXPECT_CALL(*event_interface_,
2524 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeText,
2525 AsVector("SECON")));
2526 EXPECT_CALL(checkpoint, Call(3));
2512 EXPECT_CALL( 2527 EXPECT_CALL(
2513 *event_interface_, 2528 *event_interface_,
2514 OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("SECON"))); 2529 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeContinuation,
2515 EXPECT_CALL(checkpoint, Call(3)); 2530 AsVector("D ")));
2516 EXPECT_CALL(*event_interface_,
2517 OnDataFrame(false, WebSocketFrameHeader::kOpCodeContinuation,
2518 AsVector("D ")));
2519 EXPECT_CALL(*event_interface_, OnClosingHandshake()); 2531 EXPECT_CALL(*event_interface_, OnClosingHandshake());
2520 EXPECT_CALL(checkpoint, Call(4)); 2532 EXPECT_CALL(checkpoint, Call(4));
2521 2533
2522 CreateChannelAndConnectWithQuota(6); 2534 CreateChannelAndConnectWithQuota(6);
2523 checkpoint.Call(1); 2535 checkpoint.Call(1);
2524 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); 2536 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6));
2525 checkpoint.Call(2); 2537 checkpoint.Call(2);
2526 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); 2538 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6));
2527 checkpoint.Call(3); 2539 checkpoint.Call(3);
2528 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); 2540 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6));
2529 checkpoint.Call(4); 2541 checkpoint.Call(4);
2530 } 2542 }
2531 2543
2532 // RFC6455 5.1 "a client MUST mask all frames that it sends to the server". 2544 // RFC6455 5.1 "a client MUST mask all frames that it sends to the server".
2533 // WebSocketChannel actually only sets the mask bit in the header, it doesn't 2545 // WebSocketChannel actually only sets the mask bit in the header, it doesn't
2534 // perform masking itself (not all transports actually use masking). 2546 // perform masking itself (not all transports actually use masking).
2535 TEST_F(WebSocketChannelStreamTest, SentFramesAreMasked) { 2547 TEST_F(WebSocketChannelStreamTest, SentFramesAreMasked) {
2536 static const InitFrame expected[] = { 2548 static const InitFrame expected[] = {
2537 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, 2549 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
2538 MASKED, "NEEDS MASKING"}}; 2550 MASKED, "NEEDS MASKING"}};
2539 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 2551 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
2540 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 2552 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
2541 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); 2553 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
2542 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) 2554 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
2543 .WillOnce(Return(OK)); 2555 .WillOnce(Return(OK));
2544 2556
2545 CreateChannelAndConnectSuccessfully(); 2557 CreateChannelAndConnectSuccessfully();
2546 channel_->SendFrame( 2558 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2547 true, WebSocketFrameHeader::kOpCodeText, AsVector("NEEDS MASKING")); 2559 AsIOBuffer("NEEDS MASKING"), 13U);
2548 } 2560 }
2549 2561
2550 // RFC6455 5.5.1 "The application MUST NOT send any more data frames after 2562 // RFC6455 5.5.1 "The application MUST NOT send any more data frames after
2551 // sending a Close frame." 2563 // sending a Close frame."
2552 TEST_F(WebSocketChannelStreamTest, NothingIsSentAfterClose) { 2564 TEST_F(WebSocketChannelStreamTest, NothingIsSentAfterClose) {
2553 static const InitFrame expected[] = { 2565 static const InitFrame expected[] = {
2554 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, 2566 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
2555 MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}}; 2567 MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}};
2556 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 2568 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
2557 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 2569 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
2558 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); 2570 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
2559 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) 2571 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
2560 .WillOnce(Return(OK)); 2572 .WillOnce(Return(OK));
2561 2573
2562 CreateChannelAndConnectSuccessfully(); 2574 CreateChannelAndConnectSuccessfully();
2563 ASSERT_EQ(CHANNEL_ALIVE, channel_->StartClosingHandshake(1000, "Success")); 2575 ASSERT_EQ(CHANNEL_ALIVE, channel_->StartClosingHandshake(1000, "Success"));
2564 channel_->SendFrame( 2576 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2565 true, WebSocketFrameHeader::kOpCodeText, AsVector("SHOULD BE IGNORED")); 2577 AsIOBuffer("SHOULD BE IGNORED"), 18U);
2566 } 2578 }
2567 2579
2568 // RFC6455 5.5.1 "If an endpoint receives a Close frame and did not previously 2580 // RFC6455 5.5.1 "If an endpoint receives a Close frame and did not previously
2569 // send a Close frame, the endpoint MUST send a Close frame in response." 2581 // send a Close frame, the endpoint MUST send a Close frame in response."
2570 TEST_F(WebSocketChannelStreamTest, CloseIsEchoedBack) { 2582 TEST_F(WebSocketChannelStreamTest, CloseIsEchoedBack) {
2571 static const InitFrame frames[] = { 2583 static const InitFrame frames[] = {
2572 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, 2584 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
2573 NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}}; 2585 NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}};
2574 static const InitFrame expected[] = { 2586 static const InitFrame expected[] = {
2575 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, 2587 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
2796 2808
2797 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) 2809 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
2798 .WillOnce(Return(OK)); 2810 .WillOnce(Return(OK));
2799 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) 2811 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
2800 .WillOnce(Return(OK)); 2812 .WillOnce(Return(OK));
2801 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected3), _)) 2813 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected3), _))
2802 .WillOnce(Return(OK)); 2814 .WillOnce(Return(OK));
2803 } 2815 }
2804 2816
2805 CreateChannelAndConnectSuccessfully(); 2817 CreateChannelAndConnectSuccessfully();
2806 channel_->SendFrame( 2818 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
2807 false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello ")); 2819 AsIOBuffer("Hello "), 6U);
2808 *read_frames = CreateFrameVector(frames); 2820 *read_frames = CreateFrameVector(frames);
2809 read_callback.Run(OK); 2821 read_callback.Run(OK);
2810 channel_->SendFrame( 2822 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeContinuation,
2811 true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("World")); 2823 AsIOBuffer("World"), 5U);
2812 } 2824 }
2813 2825
2814 // WriteFrames() may not be called until the previous write has completed. 2826 // WriteFrames() may not be called until the previous write has completed.
2815 // WebSocketChannel must buffer writes that happen in the meantime. 2827 // WebSocketChannel must buffer writes that happen in the meantime.
2816 TEST_F(WebSocketChannelStreamTest, WriteFramesOneAtATime) { 2828 TEST_F(WebSocketChannelStreamTest, WriteFramesOneAtATime) {
2817 static const InitFrame expected1[] = { 2829 static const InitFrame expected1[] = {
2818 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}}; 2830 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}};
2819 static const InitFrame expected2[] = { 2831 static const InitFrame expected2[] = {
2820 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "World"}}; 2832 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "World"}};
2821 CompletionCallback write_callback; 2833 CompletionCallback write_callback;
2822 Checkpoint checkpoint; 2834 Checkpoint checkpoint;
2823 2835
2824 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 2836 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
2825 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 2837 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
2826 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); 2838 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
2827 { 2839 {
2828 InSequence s; 2840 InSequence s;
2829 EXPECT_CALL(checkpoint, Call(1)); 2841 EXPECT_CALL(checkpoint, Call(1));
2830 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) 2842 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
2831 .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING))); 2843 .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING)));
2832 EXPECT_CALL(checkpoint, Call(2)); 2844 EXPECT_CALL(checkpoint, Call(2));
2833 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) 2845 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
2834 .WillOnce(Return(ERR_IO_PENDING)); 2846 .WillOnce(Return(ERR_IO_PENDING));
2835 EXPECT_CALL(checkpoint, Call(3)); 2847 EXPECT_CALL(checkpoint, Call(3));
2836 } 2848 }
2837 2849
2838 CreateChannelAndConnectSuccessfully(); 2850 CreateChannelAndConnectSuccessfully();
2839 checkpoint.Call(1); 2851 checkpoint.Call(1);
2840 channel_->SendFrame( 2852 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
2841 false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello ")); 2853 AsIOBuffer("Hello "), 6U);
2842 channel_->SendFrame( 2854 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2843 true, WebSocketFrameHeader::kOpCodeText, AsVector("World")); 2855 AsIOBuffer("World"), 5U);
2844 checkpoint.Call(2); 2856 checkpoint.Call(2);
2845 write_callback.Run(OK); 2857 write_callback.Run(OK);
2846 checkpoint.Call(3); 2858 checkpoint.Call(3);
2847 } 2859 }
2848 2860
2849 // WebSocketChannel must buffer frames while it is waiting for a write to 2861 // WebSocketChannel must buffer frames while it is waiting for a write to
2850 // complete, and then send them in a single batch. The batching behaviour is 2862 // complete, and then send them in a single batch. The batching behaviour is
2851 // important to get good throughput in the "many small messages" case. 2863 // important to get good throughput in the "many small messages" case.
2852 TEST_F(WebSocketChannelStreamTest, WaitingMessagesAreBatched) { 2864 TEST_F(WebSocketChannelStreamTest, WaitingMessagesAreBatched) {
2853 static const char input_letters[] = "Hello"; 2865 static const char input_letters[] = "Hello";
(...skipping 12 matching lines...) Expand all
2866 { 2878 {
2867 InSequence s; 2879 InSequence s;
2868 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) 2880 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _))
2869 .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING))); 2881 .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING)));
2870 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) 2882 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _))
2871 .WillOnce(Return(ERR_IO_PENDING)); 2883 .WillOnce(Return(ERR_IO_PENDING));
2872 } 2884 }
2873 2885
2874 CreateChannelAndConnectSuccessfully(); 2886 CreateChannelAndConnectSuccessfully();
2875 for (size_t i = 0; i < strlen(input_letters); ++i) { 2887 for (size_t i = 0; i < strlen(input_letters); ++i) {
2876 channel_->SendFrame(true, 2888 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2877 WebSocketFrameHeader::kOpCodeText, 2889 AsIOBuffer(std::string(1, input_letters[i])), 1U);
2878 std::vector<char>(1, input_letters[i]));
2879 } 2890 }
2880 write_callback.Run(OK); 2891 write_callback.Run(OK);
2881 } 2892 }
2882 2893
2883 // When the renderer sends more on a channel than it has quota for, we send the 2894 // When the renderer sends more on a channel than it has quota for, we send the
2884 // remote server a kWebSocketErrorGoingAway error code. 2895 // remote server a kWebSocketErrorGoingAway error code.
2885 TEST_F(WebSocketChannelStreamTest, SendGoingAwayOnRendererQuotaExceeded) { 2896 TEST_F(WebSocketChannelStreamTest, SendGoingAwayOnRendererQuotaExceeded) {
2886 static const InitFrame expected[] = { 2897 static const InitFrame expected[] = {
2887 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, 2898 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose,
2888 MASKED, CLOSE_DATA(GOING_AWAY, "")}}; 2899 MASKED, CLOSE_DATA(GOING_AWAY, "")}};
2889 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 2900 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
2890 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 2901 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
2891 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); 2902 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
2892 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) 2903 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
2893 .WillOnce(Return(OK)); 2904 .WillOnce(Return(OK));
2894 EXPECT_CALL(*mock_stream_, Close()); 2905 EXPECT_CALL(*mock_stream_, Close());
2895 2906
2896 CreateChannelAndConnectSuccessfully(); 2907 CreateChannelAndConnectSuccessfully();
2897 channel_->SendFrame(true, 2908 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2898 WebSocketFrameHeader::kOpCodeText, 2909 AsIOBuffer(std::string(kDefaultInitialQuota + 1, 'C')),
2899 std::vector<char>(kDefaultInitialQuota + 1, 'C')); 2910 kDefaultInitialQuota + 1);
2900 } 2911 }
2901 2912
2902 // For convenience, most of these tests use Text frames. However, the WebSocket 2913 // For convenience, most of these tests use Text frames. However, the WebSocket
2903 // protocol also has Binary frames and those need to be 8-bit clean. For the 2914 // protocol also has Binary frames and those need to be 8-bit clean. For the
2904 // sake of completeness, this test verifies that they are. 2915 // sake of completeness, this test verifies that they are.
2905 TEST_F(WebSocketChannelStreamTest, WrittenBinaryFramesAre8BitClean) { 2916 TEST_F(WebSocketChannelStreamTest, WrittenBinaryFramesAre8BitClean) {
2906 std::vector<std::unique_ptr<WebSocketFrame>>* frames = nullptr; 2917 std::vector<std::unique_ptr<WebSocketFrame>>* frames = nullptr;
2907 2918
2908 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 2919 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
2909 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 2920 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
2910 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); 2921 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING));
2911 EXPECT_CALL(*mock_stream_, WriteFrames(_, _)) 2922 EXPECT_CALL(*mock_stream_, WriteFrames(_, _))
2912 .WillOnce(DoAll(SaveArg<0>(&frames), Return(ERR_IO_PENDING))); 2923 .WillOnce(DoAll(SaveArg<0>(&frames), Return(ERR_IO_PENDING)));
2913 2924
2914 CreateChannelAndConnectSuccessfully(); 2925 CreateChannelAndConnectSuccessfully();
2915 channel_->SendFrame( 2926 channel_->SendFrame(
2916 true, 2927 true, WebSocketFrameHeader::kOpCodeBinary,
2917 WebSocketFrameHeader::kOpCodeBinary, 2928 AsIOBuffer(std::string(kBinaryBlob, kBinaryBlob + kBinaryBlobSize)),
2918 std::vector<char>(kBinaryBlob, kBinaryBlob + kBinaryBlobSize)); 2929 kBinaryBlobSize);
2919 ASSERT_TRUE(frames != nullptr); 2930 ASSERT_TRUE(frames != nullptr);
2920 ASSERT_EQ(1U, frames->size()); 2931 ASSERT_EQ(1U, frames->size());
2921 const WebSocketFrame* out_frame = (*frames)[0].get(); 2932 const WebSocketFrame* out_frame = (*frames)[0].get();
2922 EXPECT_EQ(kBinaryBlobSize, out_frame->header.payload_length); 2933 EXPECT_EQ(kBinaryBlobSize, out_frame->header.payload_length);
2923 ASSERT_TRUE(out_frame->data.get()); 2934 ASSERT_TRUE(out_frame->data.get());
2924 EXPECT_EQ(0, memcmp(kBinaryBlob, out_frame->data->data(), kBinaryBlobSize)); 2935 EXPECT_EQ(0, memcmp(kBinaryBlob, out_frame->data->data(), kBinaryBlobSize));
2925 } 2936 }
2926 2937
2927 // Test the read path for 8-bit cleanliness as well. 2938 // Test the read path for 8-bit cleanliness as well.
2928 TEST_F(WebSocketChannelEventInterfaceTest, ReadBinaryFramesAre8BitClean) { 2939 TEST_F(WebSocketChannelEventInterfaceTest, ReadBinaryFramesAre8BitClean) {
2929 std::unique_ptr<WebSocketFrame> frame( 2940 std::unique_ptr<WebSocketFrame> frame(
2930 new WebSocketFrame(WebSocketFrameHeader::kOpCodeBinary)); 2941 new WebSocketFrame(WebSocketFrameHeader::kOpCodeBinary));
2931 WebSocketFrameHeader& frame_header = frame->header; 2942 WebSocketFrameHeader& frame_header = frame->header;
2932 frame_header.final = true; 2943 frame_header.final = true;
2933 frame_header.payload_length = kBinaryBlobSize; 2944 frame_header.payload_length = kBinaryBlobSize;
2934 frame->data = new IOBuffer(kBinaryBlobSize); 2945 frame->data = new IOBuffer(kBinaryBlobSize);
2935 memcpy(frame->data->data(), kBinaryBlob, kBinaryBlobSize); 2946 memcpy(frame->data->data(), kBinaryBlob, kBinaryBlobSize);
2936 std::vector<std::unique_ptr<WebSocketFrame>> frames; 2947 std::vector<std::unique_ptr<WebSocketFrame>> frames;
2937 frames.push_back(std::move(frame)); 2948 frames.push_back(std::move(frame));
2938 std::unique_ptr<ReadableFakeWebSocketStream> stream( 2949 std::unique_ptr<ReadableFakeWebSocketStream> stream(
2939 new ReadableFakeWebSocketStream); 2950 new ReadableFakeWebSocketStream);
2940 stream->PrepareRawReadFrames(ReadableFakeWebSocketStream::SYNC, OK, 2951 stream->PrepareRawReadFrames(ReadableFakeWebSocketStream::SYNC, OK,
2941 std::move(frames)); 2952 std::move(frames));
2942 set_stream(std::move(stream)); 2953 set_stream(std::move(stream));
2943 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 2954 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
2944 EXPECT_CALL(*event_interface_, OnFlowControl(_)); 2955 EXPECT_CALL(*event_interface_, OnFlowControl(_));
2945 EXPECT_CALL(*event_interface_, 2956 EXPECT_CALL(
2946 OnDataFrame(true, 2957 *event_interface_,
2947 WebSocketFrameHeader::kOpCodeBinary, 2958 OnDataFrameVector(
2948 std::vector<char>(kBinaryBlob, 2959 true, WebSocketFrameHeader::kOpCodeBinary,
2949 kBinaryBlob + kBinaryBlobSize))); 2960 std::vector<char>(kBinaryBlob, kBinaryBlob + kBinaryBlobSize)));
2950 2961
2951 CreateChannelAndConnectSuccessfully(); 2962 CreateChannelAndConnectSuccessfully();
2952 } 2963 }
2953 2964
2954 // Invalid UTF-8 is not permitted in Text frames. 2965 // Invalid UTF-8 is not permitted in Text frames.
2955 TEST_F(WebSocketChannelSendUtf8Test, InvalidUtf8Rejected) { 2966 TEST_F(WebSocketChannelSendUtf8Test, InvalidUtf8Rejected) {
2956 EXPECT_CALL( 2967 EXPECT_CALL(
2957 *event_interface_, 2968 *event_interface_,
2958 OnFailChannel("Browser sent a text frame containing invalid UTF-8")); 2969 OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
2959 2970
2960 CreateChannelAndConnectSuccessfully(); 2971 CreateChannelAndConnectSuccessfully();
2961 2972
2962 channel_->SendFrame( 2973 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2963 true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff")); 2974 AsIOBuffer("\xff"), 1U);
2964 } 2975 }
2965 2976
2966 // A Text message cannot end with a partial UTF-8 character. 2977 // A Text message cannot end with a partial UTF-8 character.
2967 TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInFinalFrame) { 2978 TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInFinalFrame) {
2968 EXPECT_CALL( 2979 EXPECT_CALL(
2969 *event_interface_, 2980 *event_interface_,
2970 OnFailChannel("Browser sent a text frame containing invalid UTF-8")); 2981 OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
2971 2982
2972 CreateChannelAndConnectSuccessfully(); 2983 CreateChannelAndConnectSuccessfully();
2973 2984
2974 channel_->SendFrame( 2985 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
2975 true, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2")); 2986 AsIOBuffer("\xc2"), 1U);
2976 } 2987 }
2977 2988
2978 // A non-final Text frame may end with a partial UTF-8 character (compare to 2989 // A non-final Text frame may end with a partial UTF-8 character (compare to
2979 // previous test). 2990 // previous test).
2980 TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInNonFinalFrame) { 2991 TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInNonFinalFrame) {
2981 CreateChannelAndConnectSuccessfully(); 2992 CreateChannelAndConnectSuccessfully();
2982 2993
2983 channel_->SendFrame( 2994 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
2984 false, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2")); 2995 AsIOBuffer("\xc2"), 1U);
2985 } 2996 }
2986 2997
2987 // UTF-8 parsing context must be retained between frames. 2998 // UTF-8 parsing context must be retained between frames.
2988 TEST_F(WebSocketChannelSendUtf8Test, ValidCharacterSplitBetweenFrames) { 2999 TEST_F(WebSocketChannelSendUtf8Test, ValidCharacterSplitBetweenFrames) {
2989 CreateChannelAndConnectSuccessfully(); 3000 CreateChannelAndConnectSuccessfully();
2990 3001
2991 channel_->SendFrame( 3002 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
2992 false, WebSocketFrameHeader::kOpCodeText, AsVector("\xf1")); 3003 AsIOBuffer("\xf1"), 1U);
2993 channel_->SendFrame(true, 3004 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeContinuation,
2994 WebSocketFrameHeader::kOpCodeContinuation, 3005 AsIOBuffer("\x80\xa0\xbf"), 3U);
2995 AsVector("\x80\xa0\xbf"));
2996 } 3006 }
2997 3007
2998 // Similarly, an invalid character should be detected even if split. 3008 // Similarly, an invalid character should be detected even if split.
2999 TEST_F(WebSocketChannelSendUtf8Test, InvalidCharacterSplit) { 3009 TEST_F(WebSocketChannelSendUtf8Test, InvalidCharacterSplit) {
3000 EXPECT_CALL( 3010 EXPECT_CALL(
3001 *event_interface_, 3011 *event_interface_,
3002 OnFailChannel("Browser sent a text frame containing invalid UTF-8")); 3012 OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
3003 3013
3004 CreateChannelAndConnectSuccessfully(); 3014 CreateChannelAndConnectSuccessfully();
3005 3015
3006 channel_->SendFrame( 3016 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
3007 false, WebSocketFrameHeader::kOpCodeText, AsVector("\xe1")); 3017 AsIOBuffer("\xe1"), 1U);
3008 channel_->SendFrame(true, 3018 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeContinuation,
3009 WebSocketFrameHeader::kOpCodeContinuation, 3019 AsIOBuffer("\x80\xa0\xbf"), 3U);
3010 AsVector("\x80\xa0\xbf"));
3011 } 3020 }
3012 3021
3013 // An invalid character must be detected in continuation frames. 3022 // An invalid character must be detected in continuation frames.
3014 TEST_F(WebSocketChannelSendUtf8Test, InvalidByteInContinuation) { 3023 TEST_F(WebSocketChannelSendUtf8Test, InvalidByteInContinuation) {
3015 EXPECT_CALL( 3024 EXPECT_CALL(
3016 *event_interface_, 3025 *event_interface_,
3017 OnFailChannel("Browser sent a text frame containing invalid UTF-8")); 3026 OnFailChannel("Browser sent a text frame containing invalid UTF-8"));
3018 3027
3019 CreateChannelAndConnectSuccessfully(); 3028 CreateChannelAndConnectSuccessfully();
3020 3029
3021 channel_->SendFrame( 3030 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeText,
3022 false, WebSocketFrameHeader::kOpCodeText, AsVector("foo")); 3031 AsIOBuffer("foo"), 3U);
3023 channel_->SendFrame( 3032 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeContinuation,
3024 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar")); 3033 AsIOBuffer("bar"), 3U);
3025 channel_->SendFrame( 3034 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeContinuation,
3026 true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff")); 3035 AsIOBuffer("\xff"), 1U);
3027 } 3036 }
3028 3037
3029 // However, continuation frames of a Binary frame will not be tested for UTF-8 3038 // However, continuation frames of a Binary frame will not be tested for UTF-8
3030 // validity. 3039 // validity.
3031 TEST_F(WebSocketChannelSendUtf8Test, BinaryContinuationNotChecked) { 3040 TEST_F(WebSocketChannelSendUtf8Test, BinaryContinuationNotChecked) {
3032 CreateChannelAndConnectSuccessfully(); 3041 CreateChannelAndConnectSuccessfully();
3033 3042
3034 channel_->SendFrame( 3043 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeBinary,
3035 false, WebSocketFrameHeader::kOpCodeBinary, AsVector("foo")); 3044 AsIOBuffer("foo"), 3U);
3036 channel_->SendFrame( 3045 channel_->SendFrame(false, WebSocketFrameHeader::kOpCodeContinuation,
3037 false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar")); 3046 AsIOBuffer("bar"), 3U);
3038 channel_->SendFrame( 3047 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeContinuation,
3039 true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff")); 3048 AsIOBuffer("\xff"), 1U);
3040 } 3049 }
3041 3050
3042 // Multiple text messages can be validated without the validation state getting 3051 // Multiple text messages can be validated without the validation state getting
3043 // confused. 3052 // confused.
3044 TEST_F(WebSocketChannelSendUtf8Test, ValidateMultipleTextMessages) { 3053 TEST_F(WebSocketChannelSendUtf8Test, ValidateMultipleTextMessages) {
3045 CreateChannelAndConnectSuccessfully(); 3054 CreateChannelAndConnectSuccessfully();
3046 3055
3047 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("foo")); 3056 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
3048 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("bar")); 3057 AsIOBuffer("foo"), 3U);
3058 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
3059 AsIOBuffer("bar"), 3U);
3049 } 3060 }
3050 3061
3051 // UTF-8 validation is enforced on received Text frames. 3062 // UTF-8 validation is enforced on received Text frames.
3052 TEST_F(WebSocketChannelEventInterfaceTest, ReceivedInvalidUtf8) { 3063 TEST_F(WebSocketChannelEventInterfaceTest, ReceivedInvalidUtf8) {
3053 std::unique_ptr<ReadableFakeWebSocketStream> stream( 3064 std::unique_ptr<ReadableFakeWebSocketStream> stream(
3054 new ReadableFakeWebSocketStream); 3065 new ReadableFakeWebSocketStream);
3055 static const InitFrame frames[] = { 3066 static const InitFrame frames[] = {
3056 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}}; 3067 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}};
3057 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 3068 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
3058 set_stream(std::move(stream)); 3069 set_stream(std::move(stream));
(...skipping 15 matching lines...) Expand all
3074 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); 3085 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber());
3075 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); 3086 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber());
3076 EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) 3087 EXPECT_CALL(*mock_stream_, ReadFrames(_, _))
3077 .WillRepeatedly(Return(ERR_IO_PENDING)); 3088 .WillRepeatedly(Return(ERR_IO_PENDING));
3078 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) 3089 EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _))
3079 .WillOnce(Return(OK)); 3090 .WillOnce(Return(OK));
3080 EXPECT_CALL(*mock_stream_, Close()).Times(1); 3091 EXPECT_CALL(*mock_stream_, Close()).Times(1);
3081 3092
3082 CreateChannelAndConnectSuccessfully(); 3093 CreateChannelAndConnectSuccessfully();
3083 3094
3084 channel_->SendFrame( 3095 channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText,
3085 true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff")); 3096 AsIOBuffer("\xff"), 1U);
3086 } 3097 }
3087 3098
3088 // The rest of the tests for receiving invalid UTF-8 test the communication with 3099 // The rest of the tests for receiving invalid UTF-8 test the communication with
3089 // the server. Since there is only one code path, it would be redundant to 3100 // the server. Since there is only one code path, it would be redundant to
3090 // perform the same tests on the EventInterface as well. 3101 // perform the same tests on the EventInterface as well.
3091 3102
3092 // If invalid UTF-8 is received in a Text frame, the connection is failed. 3103 // If invalid UTF-8 is received in a Text frame, the connection is failed.
3093 TEST_F(WebSocketChannelReceiveUtf8Test, InvalidTextFrameRejected) { 3104 TEST_F(WebSocketChannelReceiveUtf8Test, InvalidTextFrameRejected) {
3094 static const InitFrame frames[] = { 3105 static const InitFrame frames[] = {
3095 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}}; 3106 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}};
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
3244 static const InitFrame frames[] = { 3255 static const InitFrame frames[] = {
3245 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary, 3256 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary,
3246 NOT_MASKED, "frame1"}, 3257 NOT_MASKED, "frame1"},
3247 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, 3258 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText,
3248 NOT_MASKED, "frame2"}}; 3259 NOT_MASKED, "frame2"}};
3249 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 3260 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
3250 set_stream(std::move(stream)); 3261 set_stream(std::move(stream));
3251 3262
3252 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 3263 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
3253 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); 3264 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
3254 EXPECT_CALL( 3265 EXPECT_CALL(*event_interface_,
3255 *event_interface_, 3266 OnDataFrameVector(false, WebSocketFrameHeader::kOpCodeBinary,
3256 OnDataFrame( 3267 AsVector("frame1")));
3257 false, WebSocketFrameHeader::kOpCodeBinary, AsVector("frame1")));
3258 EXPECT_CALL( 3268 EXPECT_CALL(
3259 *event_interface_, 3269 *event_interface_,
3260 OnFailChannel( 3270 OnFailChannel(
3261 "Received start of new message but previous message is unfinished.")); 3271 "Received start of new message but previous message is unfinished."));
3262 3272
3263 CreateChannelAndConnectSuccessfully(); 3273 CreateChannelAndConnectSuccessfully();
3264 } 3274 }
3265 3275
3266 // A new message cannot start with a Continuation frame. 3276 // A new message cannot start with a Continuation frame.
3267 TEST_F(WebSocketChannelEventInterfaceTest, MessageStartingWithContinuation) { 3277 TEST_F(WebSocketChannelEventInterfaceTest, MessageStartingWithContinuation) {
(...skipping 23 matching lines...) Expand all
3291 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, 3301 {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation,
3292 NOT_MASKED, ""}, 3302 NOT_MASKED, ""},
3293 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}}; 3303 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}};
3294 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); 3304 stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames);
3295 set_stream(std::move(stream)); 3305 set_stream(std::move(stream));
3296 3306
3297 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); 3307 EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _));
3298 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); 3308 EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota));
3299 EXPECT_CALL( 3309 EXPECT_CALL(
3300 *event_interface_, 3310 *event_interface_,
3301 OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector(""))); 3311 OnDataFrameVector(true, WebSocketFrameHeader::kOpCodeText, AsVector("")));
3302 3312
3303 CreateChannelAndConnectSuccessfully(); 3313 CreateChannelAndConnectSuccessfully();
3304 } 3314 }
3305 3315
3306 // Calls to OnSSLCertificateError() must be passed through to the event 3316 // Calls to OnSSLCertificateError() must be passed through to the event
3307 // interface with the correct URL attached. 3317 // interface with the correct URL attached.
3308 TEST_F(WebSocketChannelEventInterfaceTest, OnSSLCertificateErrorCalled) { 3318 TEST_F(WebSocketChannelEventInterfaceTest, OnSSLCertificateErrorCalled) {
3309 const GURL wss_url("wss://example.com/sslerror"); 3319 const GURL wss_url("wss://example.com/sslerror");
3310 connect_data_.socket_url = wss_url; 3320 connect_data_.socket_url = wss_url;
3311 const SSLInfo ssl_info; 3321 const SSLInfo ssl_info;
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
3507 TEST_F(WebSocketChannelTest, CurrentSendQuotaUpdated) { 3517 TEST_F(WebSocketChannelTest, CurrentSendQuotaUpdated) {
3508 const int kMessageSize = 5; 3518 const int kMessageSize = 5;
3509 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); 3519 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream));
3510 CreateChannelAndConnectSuccessfully(); 3520 CreateChannelAndConnectSuccessfully();
3511 3521
3512 int initial_send_quota = channel_->current_send_quota(); 3522 int initial_send_quota = channel_->current_send_quota();
3513 EXPECT_GE(initial_send_quota, kMessageSize); 3523 EXPECT_GE(initial_send_quota, kMessageSize);
3514 3524
3515 channel_->SendFrame( 3525 channel_->SendFrame(
3516 true, WebSocketFrameHeader::kOpCodeText, 3526 true, WebSocketFrameHeader::kOpCodeText,
3517 std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); 3527 AsIOBuffer(std::string(static_cast<size_t>(kMessageSize), 'a')),
3528 static_cast<size_t>(kMessageSize));
3518 int new_send_quota = channel_->current_send_quota(); 3529 int new_send_quota = channel_->current_send_quota();
3519 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); 3530 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota);
3520 } 3531 }
3521 3532
3522 } // namespace 3533 } // namespace
3523 } // namespace net 3534 } // namespace net
OLDNEW
« no previous file with comments | « net/websockets/websocket_channel.cc ('k') | net/websockets/websocket_end_to_end_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698