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

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

Powered by Google App Engine
This is Rietveld 408576698