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

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