[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/websockets/websocket_channel.h" |
| 6 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 7 | #include <limits.h> |
ricea | 5858a505 | 2016-01-06 04:57:38 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 9 | #include <string.h> |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 10 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 11 | #include <iostream> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 12 | #include <iterator> |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 13 | #include <memory> |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 14 | #include <string> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 15 | #include <utility> |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | #include "base/bind.h" |
| 19 | #include "base/bind_helpers.h" |
| 20 | #include "base/callback.h" |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 21 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 22 | #include "base/macros.h" |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 24 | #include "base/memory/weak_ptr.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 25 | #include "base/message_loop/message_loop.h" |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 26 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 27 | #include "base/single_thread_task_runner.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 29 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 30 | #include "net/base/net_errors.h" |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 33 | #include "net/url_request/url_request_context.h" |
| 34 | #include "net/websockets/websocket_errors.h" |
| 35 | #include "net/websockets/websocket_event_interface.h" |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 36 | #include "net/websockets/websocket_handshake_request_info.h" |
| 37 | #include "net/websockets/websocket_handshake_response_info.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 38 | #include "net/websockets/websocket_mux.h" |
| 39 | #include "testing/gmock/include/gmock/gmock.h" |
| 40 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 15fbdb4 | 2013-07-20 00:09:38 | [diff] [blame] | 41 | #include "url/gurl.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 42 | #include "url/origin.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 43 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 44 | // Hacky macros to construct the body of a Close message from a code and a |
| 45 | // string, while ensuring the result is a compile-time constant string. |
| 46 | // Use like CLOSE_DATA(NORMAL_CLOSURE, "Explanation String") |
| 47 | #define CLOSE_DATA(code, string) WEBSOCKET_CLOSE_CODE_AS_STRING_##code string |
| 48 | #define WEBSOCKET_CLOSE_CODE_AS_STRING_NORMAL_CLOSURE "\x03\xe8" |
| 49 | #define WEBSOCKET_CLOSE_CODE_AS_STRING_GOING_AWAY "\x03\xe9" |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 50 | #define WEBSOCKET_CLOSE_CODE_AS_STRING_PROTOCOL_ERROR "\x03\xea" |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 51 | #define WEBSOCKET_CLOSE_CODE_AS_STRING_ABNORMAL_CLOSURE "\x03\xee" |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 52 | #define WEBSOCKET_CLOSE_CODE_AS_STRING_SERVER_ERROR "\x03\xf3" |
| 53 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 54 | namespace net { |
| 55 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 56 | // Printing helpers to allow GoogleMock to print frames. These are explicitly |
| 57 | // designed to look like the static initialisation format we use in these |
| 58 | // tests. They have to live in the net namespace in order to be found by |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 59 | // GoogleMock; a nested anonymous namespace will not work. |
| 60 | |
| 61 | std::ostream& operator<<(std::ostream& os, const WebSocketFrameHeader& header) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 62 | return os << (header.final ? "FINAL_FRAME" : "NOT_FINAL_FRAME") << ", " |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 63 | << header.opcode << ", " |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 64 | << (header.masked ? "MASKED" : "NOT_MASKED"); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 67 | std::ostream& operator<<(std::ostream& os, const WebSocketFrame& frame) { |
| 68 | os << "{" << frame.header << ", "; |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 69 | if (frame.data.get()) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 70 | return os << "\"" << base::StringPiece(frame.data->data(), |
| 71 | frame.header.payload_length) |
| 72 | << "\"}"; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 73 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 74 | return os << "NULL}"; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 75 | } |
| 76 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 77 | std::ostream& operator<<( |
| 78 | std::ostream& os, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 79 | const std::vector<std::unique_ptr<WebSocketFrame>>& frames) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 80 | os << "{"; |
| 81 | bool first = true; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 82 | for (const auto& frame : frames) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 83 | if (!first) { |
| 84 | os << ",\n"; |
| 85 | } else { |
| 86 | first = false; |
| 87 | } |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 88 | os << *frame; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 89 | } |
| 90 | return os << "}"; |
| 91 | } |
| 92 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 93 | std::ostream& operator<<( |
| 94 | std::ostream& os, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 95 | const std::vector<std::unique_ptr<WebSocketFrame>>* vector) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 96 | return os << '&' << *vector; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | namespace { |
| 100 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 101 | using ::base::TimeDelta; |
| 102 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 103 | using ::testing::AnyNumber; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 104 | using ::testing::DefaultValue; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 105 | using ::testing::InSequence; |
| 106 | using ::testing::MockFunction; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 107 | using ::testing::NotNull; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 108 | using ::testing::Return; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 109 | using ::testing::SaveArg; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 110 | using ::testing::StrictMock; |
| 111 | using ::testing::_; |
| 112 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 113 | // A selection of characters that have traditionally been mangled in some |
| 114 | // environment or other, for testing 8-bit cleanliness. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 115 | const char kBinaryBlob[] = {'\n', '\r', // BACKWARDS CRNL |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 116 | '\0', // nul |
| 117 | '\x7F', // DEL |
| 118 | '\x80', '\xFF', // NOT VALID UTF-8 |
| 119 | '\x1A', // Control-Z, EOF on DOS |
| 120 | '\x03', // Control-C |
| 121 | '\x04', // EOT, special for Unix terms |
| 122 | '\x1B', // ESC, often special |
| 123 | '\b', // backspace |
| 124 | '\'', // single-quote, special in PHP |
| 125 | }; |
| 126 | const size_t kBinaryBlobSize = arraysize(kBinaryBlob); |
| 127 | |
| 128 | // The amount of quota a new connection gets by default. |
| 129 | // TODO(ricea): If kDefaultSendQuotaHighWaterMark changes, then this value will |
| 130 | // need to be updated. |
| 131 | const size_t kDefaultInitialQuota = 1 << 17; |
| 132 | // The amount of bytes we need to send after the initial connection to trigger a |
| 133 | // quota refresh. TODO(ricea): Change this if kDefaultSendQuotaHighWaterMark or |
| 134 | // kDefaultSendQuotaLowWaterMark change. |
| 135 | const size_t kDefaultQuotaRefreshTrigger = (1 << 16) + 1; |
| 136 | |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 137 | const int kVeryBigTimeoutMillis = 60 * 60 * 24 * 1000; |
| 138 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 139 | // TestTimeouts::tiny_timeout() is 100ms! I could run halfway around the world |
| 140 | // in that time! I would like my tests to run a bit quicker. |
| 141 | const int kVeryTinyTimeoutMillis = 1; |
| 142 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 143 | // Enough quota to pass any test. |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 144 | const int64_t kPlentyOfQuota = INT_MAX; |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 145 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 146 | typedef WebSocketEventInterface::ChannelState ChannelState; |
| 147 | const ChannelState CHANNEL_ALIVE = WebSocketEventInterface::CHANNEL_ALIVE; |
| 148 | const ChannelState CHANNEL_DELETED = WebSocketEventInterface::CHANNEL_DELETED; |
| 149 | |
| 150 | // This typedef mainly exists to avoid having to repeat the "NOLINT" incantation |
| 151 | // all over the place. |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 152 | typedef StrictMock< MockFunction<void(int)> > Checkpoint; // NOLINT |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 153 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 154 | // This mock is for testing expectations about how the EventInterface is used. |
| 155 | class MockWebSocketEventInterface : public WebSocketEventInterface { |
| 156 | public: |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 157 | MockWebSocketEventInterface() {} |
| 158 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 159 | MOCK_METHOD2(OnAddChannelResponse, |
| 160 | ChannelState(const std::string&, |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 161 | const std::string&)); // NOLINT |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 162 | MOCK_METHOD3(OnDataFrame, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 163 | ChannelState(bool, |
| 164 | WebSocketMessageType, |
| 165 | const std::vector<char>&)); // NOLINT |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 166 | MOCK_METHOD1(OnFlowControl, ChannelState(int64_t)); // NOLINT |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 167 | MOCK_METHOD0(OnClosingHandshake, ChannelState(void)); // NOLINT |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 168 | MOCK_METHOD1(OnFailChannel, ChannelState(const std::string&)); // NOLINT |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 169 | MOCK_METHOD3(OnDropChannel, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 170 | ChannelState(bool, uint16_t, const std::string&)); // NOLINT |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 171 | |
| 172 | // We can't use GMock with scoped_ptr. |
| 173 | ChannelState OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 174 | std::unique_ptr<WebSocketHandshakeRequestInfo>) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 175 | OnStartOpeningHandshakeCalled(); |
| 176 | return CHANNEL_ALIVE; |
| 177 | } |
| 178 | ChannelState OnFinishOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 179 | std::unique_ptr<WebSocketHandshakeResponseInfo>) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 180 | OnFinishOpeningHandshakeCalled(); |
| 181 | return CHANNEL_ALIVE; |
| 182 | } |
nick | d3f30d02 | 2015-04-23 10:18:37 | [diff] [blame] | 183 | ChannelState OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 184 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 185 | const GURL& url, |
| 186 | const SSLInfo& ssl_info, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 187 | bool fatal) override { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 188 | OnSSLCertificateErrorCalled( |
| 189 | ssl_error_callbacks.get(), url, ssl_info, fatal); |
| 190 | return CHANNEL_ALIVE; |
| 191 | } |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 192 | |
| 193 | MOCK_METHOD0(OnStartOpeningHandshakeCalled, void()); // NOLINT |
| 194 | MOCK_METHOD0(OnFinishOpeningHandshakeCalled, void()); // NOLINT |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 195 | MOCK_METHOD4( |
| 196 | OnSSLCertificateErrorCalled, |
| 197 | void(SSLErrorCallbacks*, const GURL&, const SSLInfo&, bool)); // NOLINT |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | // This fake EventInterface is for tests which need a WebSocketEventInterface |
| 201 | // implementation but are not verifying how it is used. |
| 202 | class FakeWebSocketEventInterface : public WebSocketEventInterface { |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 203 | ChannelState OnAddChannelResponse(const std::string& selected_protocol, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 204 | const std::string& extensions) override { |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 205 | return CHANNEL_ALIVE; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 206 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 207 | ChannelState OnDataFrame(bool fin, |
| 208 | WebSocketMessageType type, |
| 209 | const std::vector<char>& data) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 210 | return CHANNEL_ALIVE; |
| 211 | } |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 212 | ChannelState OnFlowControl(int64_t quota) override { return CHANNEL_ALIVE; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 213 | ChannelState OnClosingHandshake() override { return CHANNEL_ALIVE; } |
| 214 | ChannelState OnFailChannel(const std::string& message) override { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 215 | return CHANNEL_DELETED; |
| 216 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 217 | ChannelState OnDropChannel(bool was_clean, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 218 | uint16_t code, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 219 | const std::string& reason) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 220 | return CHANNEL_DELETED; |
| 221 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 222 | ChannelState OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 223 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 224 | return CHANNEL_ALIVE; |
| 225 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 226 | ChannelState OnFinishOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 227 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 228 | return CHANNEL_ALIVE; |
| 229 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 230 | ChannelState OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 231 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 232 | const GURL& url, |
| 233 | const SSLInfo& ssl_info, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 234 | bool fatal) override { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 235 | return CHANNEL_ALIVE; |
| 236 | } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | // This fake WebSocketStream is for tests that require a WebSocketStream but are |
| 240 | // not testing the way it is used. It has minimal functionality to return |
| 241 | // the |protocol| and |extensions| that it was constructed with. |
| 242 | class FakeWebSocketStream : public WebSocketStream { |
| 243 | public: |
| 244 | // Constructs with empty protocol and extensions. |
| 245 | FakeWebSocketStream() {} |
| 246 | |
| 247 | // Constructs with specified protocol and extensions. |
| 248 | FakeWebSocketStream(const std::string& protocol, |
| 249 | const std::string& extensions) |
| 250 | : protocol_(protocol), extensions_(extensions) {} |
| 251 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 252 | int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 253 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 254 | return ERR_IO_PENDING; |
| 255 | } |
| 256 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 257 | int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 258 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 259 | return ERR_IO_PENDING; |
| 260 | } |
| 261 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 262 | void Close() override {} |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 263 | |
| 264 | // Returns the string passed to the constructor. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 265 | std::string GetSubProtocol() const override { return protocol_; } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 266 | |
| 267 | // Returns the string passed to the constructor. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 268 | std::string GetExtensions() const override { return extensions_; } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 269 | |
| 270 | private: |
| 271 | // The string to return from GetSubProtocol(). |
| 272 | std::string protocol_; |
| 273 | |
| 274 | // The string to return from GetExtensions(). |
| 275 | std::string extensions_; |
| 276 | }; |
| 277 | |
| 278 | // To make the static initialisers easier to read, we use enums rather than |
| 279 | // bools. |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 280 | enum IsFinal { NOT_FINAL_FRAME, FINAL_FRAME }; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 281 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 282 | enum IsMasked { NOT_MASKED, MASKED }; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 283 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 284 | // This is used to initialise a WebSocketFrame but is statically initialisable. |
| 285 | struct InitFrame { |
| 286 | IsFinal final; |
| 287 | // Reserved fields omitted for now. Add them if you need them. |
| 288 | WebSocketFrameHeader::OpCode opcode; |
| 289 | IsMasked masked; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 290 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 291 | // Will be used to create the IOBuffer member. Can be null for null data. Is a |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 292 | // nul-terminated string for ease-of-use. |header.payload_length| is |
| 293 | // initialised from |strlen(data)|. This means it is not 8-bit clean, but this |
| 294 | // is not an issue for test data. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 295 | const char* const data; |
| 296 | }; |
| 297 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 298 | // For GoogleMock |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 299 | std::ostream& operator<<(std::ostream& os, const InitFrame& frame) { |
| 300 | os << "{" << (frame.final == FINAL_FRAME ? "FINAL_FRAME" : "NOT_FINAL_FRAME") |
| 301 | << ", " << frame.opcode << ", " |
| 302 | << (frame.masked == MASKED ? "MASKED" : "NOT_MASKED") << ", "; |
| 303 | if (frame.data) { |
| 304 | return os << "\"" << frame.data << "\"}"; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 305 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 306 | return os << "NULL}"; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | template <size_t N> |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 310 | std::ostream& operator<<(std::ostream& os, const InitFrame (&frames)[N]) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 311 | os << "{"; |
| 312 | bool first = true; |
| 313 | for (size_t i = 0; i < N; ++i) { |
| 314 | if (!first) { |
| 315 | os << ",\n"; |
| 316 | } else { |
| 317 | first = false; |
| 318 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 319 | os << frames[i]; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 320 | } |
| 321 | return os << "}"; |
| 322 | } |
| 323 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 324 | // Convert a const array of InitFrame structs to the format used at |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 325 | // runtime. Templated on the size of the array to save typing. |
| 326 | template <size_t N> |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 327 | std::vector<std::unique_ptr<WebSocketFrame>> CreateFrameVector( |
| 328 | const InitFrame (&source_frames)[N]) { |
| 329 | std::vector<std::unique_ptr<WebSocketFrame>> result_frames; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 330 | result_frames.reserve(N); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 331 | for (size_t i = 0; i < N; ++i) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 332 | const InitFrame& source_frame = source_frames[i]; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 333 | std::unique_ptr<WebSocketFrame> result_frame( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 334 | new WebSocketFrame(source_frame.opcode)); |
| 335 | size_t frame_length = source_frame.data ? strlen(source_frame.data) : 0; |
| 336 | WebSocketFrameHeader& result_header = result_frame->header; |
| 337 | result_header.final = (source_frame.final == FINAL_FRAME); |
| 338 | result_header.masked = (source_frame.masked == MASKED); |
| 339 | result_header.payload_length = frame_length; |
| 340 | if (source_frame.data) { |
| 341 | result_frame->data = new IOBuffer(frame_length); |
| 342 | memcpy(result_frame->data->data(), source_frame.data, frame_length); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 343 | } |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 344 | result_frames.push_back(std::move(result_frame)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 345 | } |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 346 | return result_frames; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // A GoogleMock action which can be used to respond to call to ReadFrames with |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 350 | // some frames. Use like ReadFrames(_, _).WillOnce(ReturnFrames(&frames)); |
| 351 | // |frames| is an array of InitFrame. |frames| needs to be passed by pointer |
| 352 | // because otherwise it will be treated as a pointer and the array size |
| 353 | // information will be lost. |
| 354 | ACTION_P(ReturnFrames, source_frames) { |
| 355 | *arg0 = CreateFrameVector(*source_frames); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 356 | return OK; |
| 357 | } |
| 358 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 359 | // The implementation of a GoogleMock matcher which can be used to compare a |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 360 | // std::vector<std::unique_ptr<WebSocketFrame>>* against an expectation defined |
| 361 | // as an |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 362 | // array of InitFrame objects. Although it is possible to compose built-in |
| 363 | // GoogleMock matchers to check the contents of a WebSocketFrame, the results |
| 364 | // are so unreadable that it is better to use this matcher. |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 365 | template <size_t N> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 366 | class EqualsFramesMatcher : public ::testing::MatcherInterface< |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 367 | std::vector<std::unique_ptr<WebSocketFrame>>*> { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 368 | public: |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 369 | EqualsFramesMatcher(const InitFrame (*expect_frames)[N]) |
| 370 | : expect_frames_(expect_frames) {} |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 371 | |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 372 | virtual bool MatchAndExplain( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 373 | std::vector<std::unique_ptr<WebSocketFrame>>* actual_frames, |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 374 | ::testing::MatchResultListener* listener) const { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 375 | if (actual_frames->size() != N) { |
| 376 | *listener << "the vector size is " << actual_frames->size(); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | for (size_t i = 0; i < N; ++i) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 380 | const WebSocketFrame& actual_frame = *(*actual_frames)[i]; |
| 381 | const InitFrame& expected_frame = (*expect_frames_)[i]; |
| 382 | if (actual_frame.header.final != (expected_frame.final == FINAL_FRAME)) { |
| 383 | *listener << "the frame is marked as " |
| 384 | << (actual_frame.header.final ? "" : "not ") << "final"; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 385 | return false; |
| 386 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 387 | if (actual_frame.header.opcode != expected_frame.opcode) { |
| 388 | *listener << "the opcode is " << actual_frame.header.opcode; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 389 | return false; |
| 390 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 391 | if (actual_frame.header.masked != (expected_frame.masked == MASKED)) { |
| 392 | *listener << "the frame is " |
| 393 | << (actual_frame.header.masked ? "masked" : "not masked"); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 394 | return false; |
| 395 | } |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 396 | const size_t expected_length = |
| 397 | expected_frame.data ? strlen(expected_frame.data) : 0; |
| 398 | if (actual_frame.header.payload_length != expected_length) { |
| 399 | *listener << "the payload length is " |
| 400 | << actual_frame.header.payload_length; |
| 401 | return false; |
| 402 | } |
| 403 | if (expected_length != 0 && |
| 404 | memcmp(actual_frame.data->data(), |
| 405 | expected_frame.data, |
| 406 | actual_frame.header.payload_length) != 0) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 407 | *listener << "the data content differs"; |
| 408 | return false; |
| 409 | } |
| 410 | } |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | virtual void DescribeTo(std::ostream* os) const { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 415 | *os << "matches " << *expect_frames_; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | virtual void DescribeNegationTo(std::ostream* os) const { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 419 | *os << "does not match " << *expect_frames_; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | private: |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 423 | const InitFrame (*expect_frames_)[N]; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 424 | }; |
| 425 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 426 | // The definition of EqualsFrames GoogleMock matcher. Unlike the ReturnFrames |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 427 | // action, this can take the array by reference. |
| 428 | template <size_t N> |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 429 | ::testing::Matcher<std::vector<std::unique_ptr<WebSocketFrame>>*> EqualsFrames( |
| 430 | const InitFrame (&frames)[N]) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 431 | return ::testing::MakeMatcher(new EqualsFramesMatcher<N>(&frames)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 432 | } |
| 433 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 434 | // A GoogleMock action to run a Closure. |
| 435 | ACTION_P(InvokeClosure, closure) { closure.Run(); } |
| 436 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 437 | // A GoogleMock action to run a Closure and return CHANNEL_DELETED. |
| 438 | ACTION_P(InvokeClosureReturnDeleted, closure) { |
| 439 | closure.Run(); |
| 440 | return WebSocketEventInterface::CHANNEL_DELETED; |
| 441 | } |
| 442 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 443 | // A FakeWebSocketStream whose ReadFrames() function returns data. |
| 444 | class ReadableFakeWebSocketStream : public FakeWebSocketStream { |
| 445 | public: |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 446 | enum IsSync { SYNC, ASYNC }; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 447 | |
| 448 | // After constructing the object, call PrepareReadFrames() once for each |
| 449 | // time you wish it to return from the test. |
| 450 | ReadableFakeWebSocketStream() : index_(0), read_frames_pending_(false) {} |
| 451 | |
| 452 | // Check that all the prepared responses have been consumed. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 453 | ~ReadableFakeWebSocketStream() override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 454 | CHECK(index_ >= responses_.size()); |
| 455 | CHECK(!read_frames_pending_); |
| 456 | } |
| 457 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 458 | // Prepares a fake response. Fake responses will be returned from ReadFrames() |
| 459 | // in the same order they were prepared with PrepareReadFrames() and |
| 460 | // PrepareReadFramesError(). If |async| is ASYNC, then ReadFrames() will |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 461 | // return ERR_IO_PENDING and the callback will be scheduled to run on the |
| 462 | // message loop. This requires the test case to run the message loop. If |
| 463 | // |async| is SYNC, the response will be returned synchronously. |error| is |
| 464 | // returned directly from ReadFrames() in the synchronous case, or passed to |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 465 | // the callback in the asynchronous case. |frames| will be converted to a |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 466 | // std::vector<std::unique_ptr<WebSocketFrame>> and copied to the pointer that |
| 467 | // was |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 468 | // passed to ReadFrames(). |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 469 | template <size_t N> |
| 470 | void PrepareReadFrames(IsSync async, |
| 471 | int error, |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 472 | const InitFrame (&frames)[N]) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 473 | responses_.push_back(base::WrapUnique( |
| 474 | new Response(async, error, CreateFrameVector(frames)))); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 477 | // An alternate version of PrepareReadFrames for when we need to construct |
| 478 | // the frames manually. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 479 | void PrepareRawReadFrames( |
| 480 | IsSync async, |
| 481 | int error, |
| 482 | std::vector<std::unique_ptr<WebSocketFrame>> frames) { |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 483 | responses_.push_back( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 484 | base::WrapUnique(new Response(async, error, std::move(frames)))); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 487 | // Prepares a fake error response (ie. there is no data). |
| 488 | void PrepareReadFramesError(IsSync async, int error) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 489 | responses_.push_back(base::WrapUnique(new Response( |
| 490 | async, error, std::vector<std::unique_ptr<WebSocketFrame>>()))); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 491 | } |
| 492 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 493 | int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 494 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 495 | CHECK(!read_frames_pending_); |
| 496 | if (index_ >= responses_.size()) |
| 497 | return ERR_IO_PENDING; |
| 498 | if (responses_[index_]->async == ASYNC) { |
| 499 | read_frames_pending_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 500 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 501 | FROM_HERE, base::Bind(&ReadableFakeWebSocketStream::DoCallback, |
| 502 | base::Unretained(this), frames, callback)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 503 | return ERR_IO_PENDING; |
| 504 | } else { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 505 | frames->swap(responses_[index_]->frames); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 506 | return responses_[index_++]->error; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private: |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 511 | void DoCallback(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 512 | const CompletionCallback& callback) { |
| 513 | read_frames_pending_ = false; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 514 | frames->swap(responses_[index_]->frames); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 515 | callback.Run(responses_[index_++]->error); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | struct Response { |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 520 | Response(IsSync async, |
| 521 | int error, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 522 | std::vector<std::unique_ptr<WebSocketFrame>> frames) |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 523 | : async(async), error(error), frames(std::move(frames)) {} |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 524 | |
| 525 | IsSync async; |
| 526 | int error; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 527 | std::vector<std::unique_ptr<WebSocketFrame>> frames; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 528 | |
| 529 | private: |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 530 | // Bad things will happen if we attempt to copy or assign |frames|. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 531 | DISALLOW_COPY_AND_ASSIGN(Response); |
| 532 | }; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 533 | std::vector<std::unique_ptr<Response>> responses_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 534 | |
| 535 | // The index into the responses_ array of the next response to be returned. |
| 536 | size_t index_; |
| 537 | |
| 538 | // True when an async response from ReadFrames() is pending. This only applies |
| 539 | // to "real" async responses. Once all the prepared responses have been |
| 540 | // returned, ReadFrames() returns ERR_IO_PENDING but read_frames_pending_ is |
| 541 | // not set to true. |
| 542 | bool read_frames_pending_; |
| 543 | }; |
| 544 | |
| 545 | // A FakeWebSocketStream where writes always complete successfully and |
| 546 | // synchronously. |
| 547 | class WriteableFakeWebSocketStream : public FakeWebSocketStream { |
| 548 | public: |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 549 | int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 550 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 551 | return OK; |
| 552 | } |
| 553 | }; |
| 554 | |
| 555 | // A FakeWebSocketStream where writes always fail. |
| 556 | class UnWriteableFakeWebSocketStream : public FakeWebSocketStream { |
| 557 | public: |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 558 | int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 559 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 560 | return ERR_CONNECTION_RESET; |
| 561 | } |
| 562 | }; |
| 563 | |
| 564 | // A FakeWebSocketStream which echoes any frames written back. Clears the |
| 565 | // "masked" header bit, but makes no other checks for validity. Tests using this |
| 566 | // must run the MessageLoop to receive the callback(s). If a message with opcode |
| 567 | // Close is echoed, then an ERR_CONNECTION_CLOSED is returned in the next |
| 568 | // callback. The test must do something to cause WriteFrames() to be called, |
| 569 | // otherwise the ReadFrames() callback will never be called. |
| 570 | class EchoeyFakeWebSocketStream : public FakeWebSocketStream { |
| 571 | public: |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 572 | EchoeyFakeWebSocketStream() : read_frames_(nullptr), done_(false) {} |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 573 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 574 | int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 575 | const CompletionCallback& callback) override { |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 576 | stored_frames_.insert(stored_frames_.end(), |
| 577 | std::make_move_iterator(frames->begin()), |
| 578 | std::make_move_iterator(frames->end())); |
| 579 | frames->clear(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 580 | // Users of WebSocketStream will not expect the ReadFrames() callback to be |
| 581 | // called from within WriteFrames(), so post it to the message loop instead. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 582 | PostCallback(); |
| 583 | return OK; |
| 584 | } |
| 585 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 586 | int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 587 | const CompletionCallback& callback) override { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 588 | read_callback_ = callback; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 589 | read_frames_ = frames; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 590 | if (done_) |
| 591 | PostCallback(); |
| 592 | return ERR_IO_PENDING; |
| 593 | } |
| 594 | |
| 595 | private: |
| 596 | void PostCallback() { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 597 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 598 | FROM_HERE, base::Bind(&EchoeyFakeWebSocketStream::DoCallback, |
| 599 | base::Unretained(this))); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void DoCallback() { |
| 603 | if (done_) { |
| 604 | read_callback_.Run(ERR_CONNECTION_CLOSED); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 605 | } else if (!stored_frames_.empty()) { |
| 606 | done_ = MoveFrames(read_frames_); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 607 | read_frames_ = nullptr; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 608 | read_callback_.Run(OK); |
| 609 | } |
| 610 | } |
| 611 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 612 | // Copy the frames stored in stored_frames_ to |out|, while clearing the |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 613 | // "masked" header bit. Returns true if a Close Frame was seen, false |
| 614 | // otherwise. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 615 | bool MoveFrames(std::vector<std::unique_ptr<WebSocketFrame>>* out) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 616 | bool seen_close = false; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 617 | *out = std::move(stored_frames_); |
| 618 | for (const auto& frame : *out) { |
| 619 | WebSocketFrameHeader& header = frame->header; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 620 | header.masked = false; |
| 621 | if (header.opcode == WebSocketFrameHeader::kOpCodeClose) |
| 622 | seen_close = true; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 623 | } |
| 624 | return seen_close; |
| 625 | } |
| 626 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 627 | std::vector<std::unique_ptr<WebSocketFrame>> stored_frames_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 628 | CompletionCallback read_callback_; |
| 629 | // Owned by the caller of ReadFrames(). |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 630 | std::vector<std::unique_ptr<WebSocketFrame>>* read_frames_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 631 | // True if we should close the connection. |
| 632 | bool done_; |
| 633 | }; |
| 634 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 635 | // A FakeWebSocketStream where writes trigger a connection reset. |
| 636 | // This differs from UnWriteableFakeWebSocketStream in that it is asynchronous |
| 637 | // and triggers ReadFrames to return a reset as well. Tests using this need to |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 638 | // run the message loop. There are two tricky parts here: |
| 639 | // 1. Calling the write callback may call Close(), after which the read callback |
| 640 | // should not be called. |
| 641 | // 2. Calling either callback may delete the stream altogether. |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 642 | class ResetOnWriteFakeWebSocketStream : public FakeWebSocketStream { |
| 643 | public: |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 644 | ResetOnWriteFakeWebSocketStream() : closed_(false), weak_ptr_factory_(this) {} |
| 645 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 646 | int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 647 | const CompletionCallback& callback) override { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 648 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 649 | FROM_HERE, |
| 650 | base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 651 | weak_ptr_factory_.GetWeakPtr(), callback, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 652 | ERR_CONNECTION_RESET)); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 653 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 654 | FROM_HERE, |
| 655 | base::Bind(&ResetOnWriteFakeWebSocketStream::CallCallbackUnlessClosed, |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 656 | weak_ptr_factory_.GetWeakPtr(), read_callback_, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 657 | ERR_CONNECTION_RESET)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 658 | return ERR_IO_PENDING; |
| 659 | } |
| 660 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 661 | int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 662 | const CompletionCallback& callback) override { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 663 | read_callback_ = callback; |
| 664 | return ERR_IO_PENDING; |
| 665 | } |
| 666 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 667 | void Close() override { closed_ = true; } |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 668 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 669 | private: |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 670 | void CallCallbackUnlessClosed(const CompletionCallback& callback, int value) { |
| 671 | if (!closed_) |
| 672 | callback.Run(value); |
| 673 | } |
| 674 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 675 | CompletionCallback read_callback_; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 676 | bool closed_; |
| 677 | // An IO error can result in the socket being deleted, so we use weak pointers |
| 678 | // to ensure correct behaviour in that case. |
| 679 | base::WeakPtrFactory<ResetOnWriteFakeWebSocketStream> weak_ptr_factory_; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 680 | }; |
| 681 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 682 | // This mock is for verifying that WebSocket protocol semantics are obeyed (to |
| 683 | // the extent that they are implemented in WebSocketCommon). |
| 684 | class MockWebSocketStream : public WebSocketStream { |
| 685 | public: |
| 686 | MOCK_METHOD2(ReadFrames, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 687 | int(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 688 | const CompletionCallback& callback)); |
| 689 | MOCK_METHOD2(WriteFrames, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 690 | int(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 691 | const CompletionCallback& callback)); |
| 692 | MOCK_METHOD0(Close, void()); |
| 693 | MOCK_CONST_METHOD0(GetSubProtocol, std::string()); |
| 694 | MOCK_CONST_METHOD0(GetExtensions, std::string()); |
| 695 | MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 696 | }; |
| 697 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 698 | struct ArgumentCopyingWebSocketStreamCreator { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 699 | std::unique_ptr<WebSocketStreamRequest> Create( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 700 | const GURL& socket_url, |
| 701 | const std::vector<std::string>& requested_subprotocols, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 702 | const url::Origin& origin, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 703 | URLRequestContext* url_request_context, |
| 704 | const BoundNetLog& net_log, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 705 | std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 706 | this->socket_url = socket_url; |
| 707 | this->requested_subprotocols = requested_subprotocols; |
| 708 | this->origin = origin; |
| 709 | this->url_request_context = url_request_context; |
| 710 | this->net_log = net_log; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 711 | this->connect_delegate = std::move(connect_delegate); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 712 | return base::WrapUnique(new WebSocketStreamRequest); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | GURL socket_url; |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 716 | url::Origin origin; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 717 | std::vector<std::string> requested_subprotocols; |
| 718 | URLRequestContext* url_request_context; |
| 719 | BoundNetLog net_log; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 720 | std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 721 | }; |
| 722 | |
| 723 | // Converts a std::string to a std::vector<char>. For test purposes, it is |
| 724 | // convenient to be able to specify data as a string, but the |
| 725 | // WebSocketEventInterface requires the vector<char> type. |
| 726 | std::vector<char> AsVector(const std::string& s) { |
| 727 | return std::vector<char>(s.begin(), s.end()); |
| 728 | } |
| 729 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 730 | class FakeSSLErrorCallbacks |
| 731 | : public WebSocketEventInterface::SSLErrorCallbacks { |
| 732 | public: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 733 | void CancelSSLRequest(int error, const SSLInfo* ssl_info) override {} |
| 734 | void ContinueSSLRequest() override {} |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 735 | }; |
| 736 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 737 | // Base class for all test fixtures. |
| 738 | class WebSocketChannelTest : public ::testing::Test { |
| 739 | protected: |
| 740 | WebSocketChannelTest() : stream_(new FakeWebSocketStream) {} |
| 741 | |
| 742 | // Creates a new WebSocketChannel and connects it, using the settings stored |
| 743 | // in |connect_data_|. |
| 744 | void CreateChannelAndConnect() { |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 745 | channel_.reset(new WebSocketChannel(CreateEventInterface(), |
| 746 | &connect_data_.url_request_context)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 747 | channel_->SendAddChannelRequestForTesting( |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 748 | connect_data_.socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 749 | connect_data_.requested_subprotocols, |
| 750 | connect_data_.origin, |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 751 | base::Bind(&ArgumentCopyingWebSocketStreamCreator::Create, |
| 752 | base::Unretained(&connect_data_.creator))); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // Same as CreateChannelAndConnect(), but calls the on_success callback as |
| 756 | // well. This method is virtual so that subclasses can also set the stream. |
| 757 | virtual void CreateChannelAndConnectSuccessfully() { |
| 758 | CreateChannelAndConnect(); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 759 | // Most tests aren't concerned with flow control from the renderer, so allow |
| 760 | // MAX_INT quota units. |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 761 | EXPECT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(kPlentyOfQuota)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 762 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // Returns a WebSocketEventInterface to be passed to the WebSocketChannel. |
| 766 | // This implementation returns a newly-created fake. Subclasses may return a |
| 767 | // mock instead. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 768 | virtual std::unique_ptr<WebSocketEventInterface> CreateEventInterface() { |
| 769 | return base::WrapUnique(new FakeWebSocketEventInterface); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // This method serves no other purpose than to provide a nice syntax for |
| 773 | // assigning to stream_. class T must be a subclass of WebSocketStream or you |
| 774 | // will have unpleasant compile errors. |
| 775 | template <class T> |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 776 | void set_stream(std::unique_ptr<T> stream) { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 777 | stream_ = std::move(stream); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // A struct containing the data that will be used to connect the channel. |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 781 | // Grouped for readability. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 782 | struct ConnectData { |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 783 | ConnectData() : socket_url("ws://ws/"), origin(GURL("http://ws")) {} |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 784 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 785 | // URLRequestContext object. |
| 786 | URLRequestContext url_request_context; |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 787 | |
| 788 | // URL to (pretend to) connect to. |
| 789 | GURL socket_url; |
| 790 | // Requested protocols for the request. |
| 791 | std::vector<std::string> requested_subprotocols; |
| 792 | // Origin of the request |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 793 | url::Origin origin; |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 794 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 795 | // A fake WebSocketStreamCreator that just records its arguments. |
| 796 | ArgumentCopyingWebSocketStreamCreator creator; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 797 | }; |
| 798 | ConnectData connect_data_; |
| 799 | |
| 800 | // The channel we are testing. Not initialised until SetChannel() is called. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 801 | std::unique_ptr<WebSocketChannel> channel_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 802 | |
| 803 | // A mock or fake stream for tests that need one. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 804 | std::unique_ptr<WebSocketStream> stream_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 805 | }; |
| 806 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 807 | // enum of WebSocketEventInterface calls. These are intended to be or'd together |
| 808 | // in order to instruct WebSocketChannelDeletingTest when it should fail. |
| 809 | enum EventInterfaceCall { |
| 810 | EVENT_ON_ADD_CHANNEL_RESPONSE = 0x1, |
| 811 | EVENT_ON_DATA_FRAME = 0x2, |
| 812 | EVENT_ON_FLOW_CONTROL = 0x4, |
| 813 | EVENT_ON_CLOSING_HANDSHAKE = 0x8, |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 814 | EVENT_ON_FAIL_CHANNEL = 0x10, |
| 815 | EVENT_ON_DROP_CHANNEL = 0x20, |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 816 | EVENT_ON_START_OPENING_HANDSHAKE = 0x40, |
| 817 | EVENT_ON_FINISH_OPENING_HANDSHAKE = 0x80, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 818 | EVENT_ON_SSL_CERTIFICATE_ERROR = 0x100, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 819 | }; |
| 820 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 821 | class WebSocketChannelDeletingTest : public WebSocketChannelTest { |
| 822 | public: |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 823 | ChannelState DeleteIfDeleting(EventInterfaceCall call) { |
| 824 | if (deleting_ & call) { |
| 825 | channel_.reset(); |
| 826 | return CHANNEL_DELETED; |
| 827 | } else { |
| 828 | return CHANNEL_ALIVE; |
| 829 | } |
| 830 | } |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 831 | |
| 832 | protected: |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 833 | WebSocketChannelDeletingTest() |
| 834 | : deleting_(EVENT_ON_ADD_CHANNEL_RESPONSE | EVENT_ON_DATA_FRAME | |
| 835 | EVENT_ON_FLOW_CONTROL | |
| 836 | EVENT_ON_CLOSING_HANDSHAKE | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 837 | EVENT_ON_FAIL_CHANNEL | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 838 | EVENT_ON_DROP_CHANNEL | |
| 839 | EVENT_ON_START_OPENING_HANDSHAKE | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 840 | EVENT_ON_FINISH_OPENING_HANDSHAKE | |
| 841 | EVENT_ON_SSL_CERTIFICATE_ERROR) {} |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 842 | // Create a ChannelDeletingFakeWebSocketEventInterface. Defined out-of-line to |
| 843 | // avoid circular dependency. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 844 | std::unique_ptr<WebSocketEventInterface> CreateEventInterface() override; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 845 | |
| 846 | // Tests can set deleting_ to a bitmap of EventInterfaceCall members that they |
| 847 | // want to cause Channel deletion. The default is for all calls to cause |
| 848 | // deletion. |
| 849 | int deleting_; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | // A FakeWebSocketEventInterface that deletes the WebSocketChannel on failure to |
| 853 | // connect. |
| 854 | class ChannelDeletingFakeWebSocketEventInterface |
| 855 | : public FakeWebSocketEventInterface { |
| 856 | public: |
| 857 | ChannelDeletingFakeWebSocketEventInterface( |
| 858 | WebSocketChannelDeletingTest* fixture) |
| 859 | : fixture_(fixture) {} |
| 860 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 861 | ChannelState OnAddChannelResponse(const std::string& selected_protocol, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 862 | const std::string& extensions) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 863 | return fixture_->DeleteIfDeleting(EVENT_ON_ADD_CHANNEL_RESPONSE); |
| 864 | } |
| 865 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 866 | ChannelState OnDataFrame(bool fin, |
| 867 | WebSocketMessageType type, |
| 868 | const std::vector<char>& data) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 869 | return fixture_->DeleteIfDeleting(EVENT_ON_DATA_FRAME); |
| 870 | } |
| 871 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 872 | ChannelState OnFlowControl(int64_t quota) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 873 | return fixture_->DeleteIfDeleting(EVENT_ON_FLOW_CONTROL); |
| 874 | } |
| 875 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 876 | ChannelState OnClosingHandshake() override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 877 | return fixture_->DeleteIfDeleting(EVENT_ON_CLOSING_HANDSHAKE); |
| 878 | } |
| 879 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 880 | ChannelState OnFailChannel(const std::string& message) override { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 881 | return fixture_->DeleteIfDeleting(EVENT_ON_FAIL_CHANNEL); |
| 882 | } |
| 883 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 884 | ChannelState OnDropChannel(bool was_clean, |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 885 | uint16_t code, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 886 | const std::string& reason) override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 887 | return fixture_->DeleteIfDeleting(EVENT_ON_DROP_CHANNEL); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 888 | } |
| 889 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 890 | ChannelState OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 891 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 892 | return fixture_->DeleteIfDeleting(EVENT_ON_START_OPENING_HANDSHAKE); |
| 893 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 894 | ChannelState OnFinishOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 895 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) override { |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 896 | return fixture_->DeleteIfDeleting(EVENT_ON_FINISH_OPENING_HANDSHAKE); |
| 897 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 898 | ChannelState OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 899 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 900 | const GURL& url, |
| 901 | const SSLInfo& ssl_info, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 902 | bool fatal) override { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 903 | return fixture_->DeleteIfDeleting(EVENT_ON_SSL_CERTIFICATE_ERROR); |
| 904 | } |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 905 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 906 | private: |
| 907 | // A pointer to the test fixture. Owned by the test harness; this object will |
| 908 | // be deleted before it is. |
| 909 | WebSocketChannelDeletingTest* fixture_; |
| 910 | }; |
| 911 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 912 | std::unique_ptr<WebSocketEventInterface> |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 913 | WebSocketChannelDeletingTest::CreateEventInterface() { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 914 | return base::WrapUnique(new ChannelDeletingFakeWebSocketEventInterface(this)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 915 | } |
| 916 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 917 | // Base class for tests which verify that EventInterface methods are called |
| 918 | // appropriately. |
| 919 | class WebSocketChannelEventInterfaceTest : public WebSocketChannelTest { |
| 920 | protected: |
| 921 | WebSocketChannelEventInterfaceTest() |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 922 | : event_interface_(new StrictMock<MockWebSocketEventInterface>) { |
| 923 | DefaultValue<ChannelState>::Set(CHANNEL_ALIVE); |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 924 | ON_CALL(*event_interface_, OnDropChannel(_, _, _)) |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 925 | .WillByDefault(Return(CHANNEL_DELETED)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 926 | ON_CALL(*event_interface_, OnFailChannel(_)) |
| 927 | .WillByDefault(Return(CHANNEL_DELETED)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 928 | } |
| 929 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 930 | ~WebSocketChannelEventInterfaceTest() override { |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 931 | DefaultValue<ChannelState>::Clear(); |
| 932 | } |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 933 | |
| 934 | // Tests using this fixture must set expectations on the event_interface_ mock |
| 935 | // object before calling CreateChannelAndConnect() or |
| 936 | // CreateChannelAndConnectSuccessfully(). This will only work once per test |
| 937 | // case, but once should be enough. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 938 | std::unique_ptr<WebSocketEventInterface> CreateEventInterface() override { |
| 939 | return base::WrapUnique(event_interface_.release()); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 940 | } |
| 941 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 942 | std::unique_ptr<MockWebSocketEventInterface> event_interface_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 943 | }; |
| 944 | |
| 945 | // Base class for tests which verify that WebSocketStream methods are called |
| 946 | // appropriately by using a MockWebSocketStream. |
| 947 | class WebSocketChannelStreamTest : public WebSocketChannelTest { |
| 948 | protected: |
| 949 | WebSocketChannelStreamTest() |
| 950 | : mock_stream_(new StrictMock<MockWebSocketStream>) {} |
| 951 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 952 | void CreateChannelAndConnectSuccessfully() override { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 953 | set_stream(std::move(mock_stream_)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 954 | WebSocketChannelTest::CreateChannelAndConnectSuccessfully(); |
| 955 | } |
| 956 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 957 | std::unique_ptr<MockWebSocketStream> mock_stream_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 958 | }; |
| 959 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 960 | // Fixture for tests which test UTF-8 validation of sent Text frames via the |
| 961 | // EventInterface. |
| 962 | class WebSocketChannelSendUtf8Test |
| 963 | : public WebSocketChannelEventInterfaceTest { |
| 964 | public: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 965 | void SetUp() override { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 966 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 967 | // For the purpose of the tests using this fixture, it doesn't matter |
| 968 | // whether these methods are called or not. |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 969 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)) |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 970 | .Times(AnyNumber()); |
| 971 | EXPECT_CALL(*event_interface_, OnFlowControl(_)) |
| 972 | .Times(AnyNumber()); |
| 973 | } |
| 974 | }; |
| 975 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 976 | // Fixture for tests which test use of receive quota from the renderer. |
| 977 | class WebSocketChannelFlowControlTest |
| 978 | : public WebSocketChannelEventInterfaceTest { |
| 979 | protected: |
| 980 | // Tests using this fixture should use CreateChannelAndConnectWithQuota() |
| 981 | // instead of CreateChannelAndConnectSuccessfully(). |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 982 | void CreateChannelAndConnectWithQuota(int64_t quota) { |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 983 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 984 | EXPECT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(quota)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 985 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | virtual void CreateChannelAndConnectSuccesfully() { NOTREACHED(); } |
| 989 | }; |
| 990 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 991 | // Fixture for tests which test UTF-8 validation of received Text frames using a |
| 992 | // mock WebSocketStream. |
| 993 | class WebSocketChannelReceiveUtf8Test : public WebSocketChannelStreamTest { |
| 994 | public: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 995 | void SetUp() override { |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 996 | // For the purpose of the tests using this fixture, it doesn't matter |
| 997 | // whether these methods are called or not. |
| 998 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 999 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 1000 | } |
| 1001 | }; |
| 1002 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 1003 | // Simple test that everything that should be passed to the creator function is |
| 1004 | // passed to the creator function. |
| 1005 | TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 1006 | connect_data_.socket_url = GURL("ws://example.com/test"); |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 1007 | connect_data_.origin = url::Origin(GURL("http://example.com")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1008 | connect_data_.requested_subprotocols.push_back("Sinbad"); |
| 1009 | |
| 1010 | CreateChannelAndConnect(); |
| 1011 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 1012 | const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 1013 | |
| 1014 | EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); |
| 1015 | |
| 1016 | EXPECT_EQ(connect_data_.socket_url, actual.socket_url); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1017 | EXPECT_EQ(connect_data_.requested_subprotocols, |
[email protected] | 72dd6fda | 2013-10-17 16:21:11 | [diff] [blame] | 1018 | actual.requested_subprotocols); |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 1019 | EXPECT_EQ(connect_data_.origin.Serialize(), actual.origin.Serialize()); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1020 | } |
| 1021 | |
[email protected] | abda70d | 2013-12-12 07:53:54 | [diff] [blame] | 1022 | // Verify that calling SendFlowControl before the connection is established does |
| 1023 | // not cause a crash. |
| 1024 | TEST_F(WebSocketChannelTest, SendFlowControlDuringHandshakeOkay) { |
| 1025 | CreateChannelAndConnect(); |
| 1026 | ASSERT_TRUE(channel_); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 1027 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(65536)); |
[email protected] | abda70d | 2013-12-12 07:53:54 | [diff] [blame] | 1028 | } |
| 1029 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1030 | // Any WebSocketEventInterface methods can delete the WebSocketChannel and |
| 1031 | // return CHANNEL_DELETED. The WebSocketChannelDeletingTests are intended to |
| 1032 | // verify that there are no use-after-free bugs when this happens. Problems will |
| 1033 | // probably only be found when running under Address Sanitizer or a similar |
| 1034 | // tool. |
| 1035 | TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseFail) { |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1036 | CreateChannelAndConnect(); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1037 | EXPECT_TRUE(channel_); |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 1038 | connect_data_.creator.connect_delegate->OnFailure("bye"); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1039 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1040 | } |
| 1041 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1042 | // Deletion is possible (due to IPC failure) even if the connect succeeds. |
| 1043 | TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseSuccess) { |
| 1044 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1045 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | TEST_F(WebSocketChannelDeletingTest, OnDataFrameSync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1049 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1050 | new ReadableFakeWebSocketStream); |
| 1051 | static const InitFrame frames[] = { |
| 1052 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1053 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1054 | set_stream(std::move(stream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1055 | deleting_ = EVENT_ON_DATA_FRAME; |
| 1056 | |
| 1057 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1058 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | TEST_F(WebSocketChannelDeletingTest, OnDataFrameAsync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1062 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1063 | new ReadableFakeWebSocketStream); |
| 1064 | static const InitFrame frames[] = { |
| 1065 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1066 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1067 | set_stream(std::move(stream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1068 | deleting_ = EVENT_ON_DATA_FRAME; |
| 1069 | |
| 1070 | CreateChannelAndConnectSuccessfully(); |
| 1071 | EXPECT_TRUE(channel_); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1072 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1073 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterConnect) { |
| 1077 | deleting_ = EVENT_ON_FLOW_CONTROL; |
| 1078 | |
| 1079 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1080 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | TEST_F(WebSocketChannelDeletingTest, OnFlowControlAfterSend) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1084 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1085 | // Avoid deleting the channel yet. |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 1086 | deleting_ = EVENT_ON_FAIL_CHANNEL | EVENT_ON_DROP_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1087 | CreateChannelAndConnectSuccessfully(); |
| 1088 | ASSERT_TRUE(channel_); |
| 1089 | deleting_ = EVENT_ON_FLOW_CONTROL; |
| 1090 | channel_->SendFrame(true, |
| 1091 | WebSocketFrameHeader::kOpCodeText, |
| 1092 | std::vector<char>(kDefaultInitialQuota, 'B')); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1093 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeSync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1097 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1098 | new ReadableFakeWebSocketStream); |
| 1099 | static const InitFrame frames[] = { |
| 1100 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 1101 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}}; |
| 1102 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1103 | set_stream(std::move(stream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1104 | deleting_ = EVENT_ON_CLOSING_HANDSHAKE; |
| 1105 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1106 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | TEST_F(WebSocketChannelDeletingTest, OnClosingHandshakeAsync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1110 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1111 | new ReadableFakeWebSocketStream); |
| 1112 | static const InitFrame frames[] = { |
| 1113 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 1114 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}}; |
| 1115 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1116 | set_stream(std::move(stream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1117 | deleting_ = EVENT_ON_CLOSING_HANDSHAKE; |
| 1118 | CreateChannelAndConnectSuccessfully(); |
| 1119 | ASSERT_TRUE(channel_); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1120 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1121 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST_F(WebSocketChannelDeletingTest, OnDropChannelWriteError) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1125 | set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1126 | deleting_ = EVENT_ON_DROP_CHANNEL; |
| 1127 | CreateChannelAndConnectSuccessfully(); |
| 1128 | ASSERT_TRUE(channel_); |
| 1129 | channel_->SendFrame( |
| 1130 | true, WebSocketFrameHeader::kOpCodeText, AsVector("this will fail")); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1131 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | TEST_F(WebSocketChannelDeletingTest, OnDropChannelReadError) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1135 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1136 | new ReadableFakeWebSocketStream); |
| 1137 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, |
| 1138 | ERR_FAILED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1139 | set_stream(std::move(stream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1140 | deleting_ = EVENT_ON_DROP_CHANNEL; |
| 1141 | CreateChannelAndConnectSuccessfully(); |
| 1142 | ASSERT_TRUE(channel_); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1143 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1144 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1145 | } |
| 1146 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1147 | TEST_F(WebSocketChannelDeletingTest, OnNotifyStartOpeningHandshakeError) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1148 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1149 | new ReadableFakeWebSocketStream); |
| 1150 | static const InitFrame frames[] = { |
| 1151 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1152 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1153 | set_stream(std::move(stream)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1154 | deleting_ = EVENT_ON_START_OPENING_HANDSHAKE; |
| 1155 | |
| 1156 | CreateChannelAndConnectSuccessfully(); |
| 1157 | ASSERT_TRUE(channel_); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1158 | channel_->OnStartOpeningHandshake( |
| 1159 | std::unique_ptr<WebSocketHandshakeRequestInfo>( |
| 1160 | new WebSocketHandshakeRequestInfo(GURL("http://www.example.com/"), |
| 1161 | base::Time()))); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1162 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1163 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | TEST_F(WebSocketChannelDeletingTest, OnNotifyFinishOpeningHandshakeError) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1167 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1168 | new ReadableFakeWebSocketStream); |
| 1169 | static const InitFrame frames[] = { |
| 1170 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1171 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1172 | set_stream(std::move(stream)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1173 | deleting_ = EVENT_ON_FINISH_OPENING_HANDSHAKE; |
| 1174 | |
| 1175 | CreateChannelAndConnectSuccessfully(); |
| 1176 | ASSERT_TRUE(channel_); |
| 1177 | scoped_refptr<HttpResponseHeaders> response_headers( |
| 1178 | new HttpResponseHeaders("")); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1179 | channel_->OnFinishOpeningHandshake( |
| 1180 | base::WrapUnique(new WebSocketHandshakeResponseInfo( |
| 1181 | GURL("http://www.example.com/"), 200, "OK", response_headers, |
| 1182 | base::Time()))); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1183 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1184 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1185 | } |
| 1186 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1187 | TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1188 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1189 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1190 | CreateChannelAndConnectSuccessfully(); |
| 1191 | ASSERT_TRUE(channel_); |
| 1192 | channel_->SendFrame(true, |
| 1193 | WebSocketFrameHeader::kOpCodeText, |
| 1194 | std::vector<char>(kDefaultInitialQuota * 2, 'T')); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1195 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | TEST_F(WebSocketChannelDeletingTest, FailChannelInOnReadDone) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1199 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1200 | new ReadableFakeWebSocketStream); |
| 1201 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, |
| 1202 | ERR_WS_PROTOCOL_ERROR); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1203 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1204 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1205 | CreateChannelAndConnectSuccessfully(); |
| 1206 | ASSERT_TRUE(channel_); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1207 | base::RunLoop().RunUntilIdle(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1208 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToMaskedFrame) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1212 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1213 | new ReadableFakeWebSocketStream); |
| 1214 | static const InitFrame frames[] = { |
| 1215 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"}}; |
| 1216 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1217 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1218 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1219 | |
| 1220 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1221 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToBadControlFrame) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1225 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1226 | new ReadableFakeWebSocketStream); |
| 1227 | static const InitFrame frames[] = { |
[email protected] | bae4842 | 2014-03-05 15:07:25 | [diff] [blame] | 1228 | {FINAL_FRAME, 0xF, NOT_MASKED, ""}}; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1229 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1230 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1231 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1232 | |
| 1233 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1234 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1235 | } |
| 1236 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1237 | // Version of above test with null data. |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1238 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToBadControlFrameNull) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1239 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1240 | new ReadableFakeWebSocketStream); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1241 | static const InitFrame frames[] = {{FINAL_FRAME, 0xF, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1242 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1243 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1244 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1245 | |
| 1246 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1247 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1248 | } |
| 1249 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1250 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToPongAfterClose) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1251 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1252 | new ReadableFakeWebSocketStream); |
| 1253 | static const InitFrame frames[] = { |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1254 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, |
| 1255 | CLOSE_DATA(NORMAL_CLOSURE, "Success")}, |
| 1256 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, ""}}; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1257 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1258 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1259 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1260 | |
| 1261 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1262 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1263 | } |
| 1264 | |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1265 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToPongAfterCloseNull) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1266 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1267 | new ReadableFakeWebSocketStream); |
| 1268 | static const InitFrame frames[] = { |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1269 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, |
| 1270 | CLOSE_DATA(NORMAL_CLOSURE, "Success")}, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1271 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1272 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1273 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1274 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1275 | |
| 1276 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1277 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1278 | } |
| 1279 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1280 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToUnknownOpCode) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1281 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1282 | new ReadableFakeWebSocketStream); |
| 1283 | static const InitFrame frames[] = {{FINAL_FRAME, 0x7, NOT_MASKED, ""}}; |
| 1284 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1285 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1286 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1287 | |
| 1288 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1289 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1290 | } |
| 1291 | |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1292 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueToUnknownOpCodeNull) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1293 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1294 | new ReadableFakeWebSocketStream); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1295 | static const InitFrame frames[] = {{FINAL_FRAME, 0x7, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1296 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1297 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1298 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
| 1299 | |
| 1300 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1301 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | TEST_F(WebSocketChannelDeletingTest, FailChannelDueInvalidCloseReason) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1305 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1306 | new ReadableFakeWebSocketStream); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 1307 | static const InitFrame frames[] = { |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1308 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 1309 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}}; |
| 1310 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1311 | set_stream(std::move(stream)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1312 | deleting_ = EVENT_ON_FAIL_CHANNEL; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1313 | |
| 1314 | CreateChannelAndConnectSuccessfully(); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1315 | EXPECT_EQ(nullptr, channel_.get()); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1316 | } |
| 1317 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1318 | TEST_F(WebSocketChannelEventInterfaceTest, ConnectSuccessReported) { |
| 1319 | // false means success. |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1320 | EXPECT_CALL(*event_interface_, OnAddChannelResponse("", "")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1321 | // OnFlowControl is always called immediately after connect to provide initial |
| 1322 | // quota to the renderer. |
| 1323 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1324 | |
| 1325 | CreateChannelAndConnect(); |
| 1326 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1327 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | TEST_F(WebSocketChannelEventInterfaceTest, ConnectFailureReported) { |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 1331 | EXPECT_CALL(*event_interface_, OnFailChannel("hello")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1332 | |
| 1333 | CreateChannelAndConnect(); |
| 1334 | |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 1335 | connect_data_.creator.connect_delegate->OnFailure("hello"); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1336 | } |
| 1337 | |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 1338 | TEST_F(WebSocketChannelEventInterfaceTest, NonWebSocketSchemeRejected) { |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1339 | EXPECT_CALL(*event_interface_, OnFailChannel("Invalid scheme")); |
[email protected] | 53deacb | 2013-11-22 14:02:43 | [diff] [blame] | 1340 | connect_data_.socket_url = GURL("http://www.google.com/"); |
| 1341 | CreateChannelAndConnect(); |
| 1342 | } |
| 1343 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1344 | TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) { |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1345 | EXPECT_CALL(*event_interface_, OnAddChannelResponse("Bob", "")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1346 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1347 | |
| 1348 | CreateChannelAndConnect(); |
| 1349 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 1350 | connect_data_.creator.connect_delegate->OnSuccess( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1351 | base::WrapUnique(new FakeWebSocketStream("Bob", ""))); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1352 | } |
| 1353 | |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 1354 | TEST_F(WebSocketChannelEventInterfaceTest, ExtensionsPassed) { |
| 1355 | EXPECT_CALL(*event_interface_, |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1356 | OnAddChannelResponse("", "extension1, extension2")); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 1357 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1358 | |
| 1359 | CreateChannelAndConnect(); |
| 1360 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1361 | connect_data_.creator.connect_delegate->OnSuccess( |
| 1362 | base::WrapUnique(new FakeWebSocketStream("", "extension1, extension2"))); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 1363 | } |
| 1364 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1365 | // The first frames from the server can arrive together with the handshake, in |
| 1366 | // which case they will be available as soon as ReadFrames() is called the first |
| 1367 | // time. |
| 1368 | TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1369 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1370 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1371 | static const InitFrame frames[] = { |
| 1372 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1373 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1374 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1375 | { |
| 1376 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1377 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1378 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1379 | EXPECT_CALL( |
| 1380 | *event_interface_, |
| 1381 | OnDataFrame( |
| 1382 | true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO"))); |
| 1383 | } |
| 1384 | |
| 1385 | CreateChannelAndConnectSuccessfully(); |
| 1386 | } |
| 1387 | |
| 1388 | // A remote server could accept the handshake, but then immediately send a |
| 1389 | // Close frame. |
| 1390 | TEST_F(WebSocketChannelEventInterfaceTest, CloseAfterHandshake) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1391 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1392 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1393 | static const InitFrame frames[] = { |
| 1394 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 1395 | NOT_MASKED, CLOSE_DATA(SERVER_ERROR, "Internal Server Error")}}; |
| 1396 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1397 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 1398 | ERR_CONNECTION_CLOSED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1399 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1400 | { |
| 1401 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1402 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1403 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1404 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1405 | EXPECT_CALL( |
| 1406 | *event_interface_, |
| 1407 | OnDropChannel( |
| 1408 | true, kWebSocketErrorInternalServerError, "Internal Server Error")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | CreateChannelAndConnectSuccessfully(); |
| 1412 | } |
| 1413 | |
| 1414 | // A remote server could close the connection immediately after sending the |
| 1415 | // handshake response (most likely a bug in the server). |
| 1416 | TEST_F(WebSocketChannelEventInterfaceTest, ConnectionCloseAfterHandshake) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1417 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1418 | new ReadableFakeWebSocketStream); |
| 1419 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 1420 | ERR_CONNECTION_CLOSED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1421 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1422 | { |
| 1423 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1424 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1425 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1426 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1427 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | CreateChannelAndConnectSuccessfully(); |
| 1431 | } |
| 1432 | |
| 1433 | TEST_F(WebSocketChannelEventInterfaceTest, NormalAsyncRead) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1434 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1435 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1436 | static const InitFrame frames[] = { |
| 1437 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1438 | // We use this checkpoint object to verify that the callback isn't called |
| 1439 | // until we expect it to be. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1440 | Checkpoint checkpoint; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1441 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1442 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1443 | { |
| 1444 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1445 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1446 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1447 | EXPECT_CALL(checkpoint, Call(1)); |
| 1448 | EXPECT_CALL( |
| 1449 | *event_interface_, |
| 1450 | OnDataFrame( |
| 1451 | true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO"))); |
| 1452 | EXPECT_CALL(checkpoint, Call(2)); |
| 1453 | } |
| 1454 | |
| 1455 | CreateChannelAndConnectSuccessfully(); |
| 1456 | checkpoint.Call(1); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1457 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1458 | checkpoint.Call(2); |
| 1459 | } |
| 1460 | |
| 1461 | // Extra data can arrive while a read is being processed, resulting in the next |
| 1462 | // read completing synchronously. |
| 1463 | TEST_F(WebSocketChannelEventInterfaceTest, AsyncThenSyncRead) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1464 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1465 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1466 | static const InitFrame frames1[] = { |
| 1467 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
| 1468 | static const InitFrame frames2[] = { |
| 1469 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "WORLD"}}; |
| 1470 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); |
| 1471 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames2); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1472 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1473 | { |
| 1474 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1475 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1476 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1477 | EXPECT_CALL( |
| 1478 | *event_interface_, |
| 1479 | OnDataFrame( |
| 1480 | true, WebSocketFrameHeader::kOpCodeText, AsVector("HELLO"))); |
| 1481 | EXPECT_CALL( |
| 1482 | *event_interface_, |
| 1483 | OnDataFrame( |
| 1484 | true, WebSocketFrameHeader::kOpCodeText, AsVector("WORLD"))); |
| 1485 | } |
| 1486 | |
| 1487 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1488 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1489 | } |
| 1490 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1491 | // Data frames are delivered the same regardless of how many reads they arrive |
| 1492 | // as. |
| 1493 | TEST_F(WebSocketChannelEventInterfaceTest, FragmentedMessage) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1494 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1495 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1496 | // Here we have one message which arrived in five frames split across three |
| 1497 | // reads. It may have been reframed on arrival, but this class doesn't care |
| 1498 | // about that. |
| 1499 | static const InitFrame frames1[] = { |
| 1500 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "THREE"}, |
| 1501 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 1502 | NOT_MASKED, " "}}; |
| 1503 | static const InitFrame frames2[] = { |
| 1504 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 1505 | NOT_MASKED, "SMALL"}}; |
| 1506 | static const InitFrame frames3[] = { |
| 1507 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 1508 | NOT_MASKED, " "}, |
| 1509 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 1510 | NOT_MASKED, "FRAMES"}}; |
| 1511 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); |
| 1512 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2); |
| 1513 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1514 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1515 | { |
| 1516 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1517 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1518 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1519 | EXPECT_CALL( |
| 1520 | *event_interface_, |
| 1521 | OnDataFrame( |
| 1522 | false, WebSocketFrameHeader::kOpCodeText, AsVector("THREE"))); |
| 1523 | EXPECT_CALL( |
| 1524 | *event_interface_, |
| 1525 | OnDataFrame( |
| 1526 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" "))); |
| 1527 | EXPECT_CALL(*event_interface_, |
| 1528 | OnDataFrame(false, |
| 1529 | WebSocketFrameHeader::kOpCodeContinuation, |
| 1530 | AsVector("SMALL"))); |
| 1531 | EXPECT_CALL( |
| 1532 | *event_interface_, |
| 1533 | OnDataFrame( |
| 1534 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector(" "))); |
| 1535 | EXPECT_CALL(*event_interface_, |
| 1536 | OnDataFrame(true, |
| 1537 | WebSocketFrameHeader::kOpCodeContinuation, |
| 1538 | AsVector("FRAMES"))); |
| 1539 | } |
| 1540 | |
| 1541 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1542 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1543 | } |
| 1544 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1545 | // A message can consist of one frame with null payload. |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1546 | TEST_F(WebSocketChannelEventInterfaceTest, NullMessage) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1547 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1548 | new ReadableFakeWebSocketStream); |
| 1549 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1550 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1551 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1552 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1553 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1554 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1555 | EXPECT_CALL( |
| 1556 | *event_interface_, |
| 1557 | OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector(""))); |
| 1558 | CreateChannelAndConnectSuccessfully(); |
| 1559 | } |
| 1560 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1561 | // Connection closed by the remote host without a closing handshake. |
| 1562 | TEST_F(WebSocketChannelEventInterfaceTest, AsyncAbnormalClosure) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1563 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1564 | new ReadableFakeWebSocketStream); |
| 1565 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, |
| 1566 | ERR_CONNECTION_CLOSED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1567 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1568 | { |
| 1569 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1570 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1571 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1572 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1573 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1577 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | // A connection reset should produce the same event as an unexpected closure. |
| 1581 | TEST_F(WebSocketChannelEventInterfaceTest, ConnectionReset) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1582 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1583 | new ReadableFakeWebSocketStream); |
| 1584 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, |
| 1585 | ERR_CONNECTION_RESET); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1586 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1587 | { |
| 1588 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1589 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1590 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1591 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1592 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1596 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1597 | } |
| 1598 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1599 | // RFC6455 5.1 "A client MUST close a connection if it detects a masked frame." |
| 1600 | TEST_F(WebSocketChannelEventInterfaceTest, MaskedFramesAreRejected) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1601 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1602 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1603 | static const InitFrame frames[] = { |
| 1604 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"}}; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1605 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1606 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1607 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1608 | { |
| 1609 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1610 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1611 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1612 | EXPECT_CALL( |
| 1613 | *event_interface_, |
| 1614 | OnFailChannel( |
| 1615 | "A server must not mask any frames that it sends to the client.")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1619 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | // RFC6455 5.2 "If an unknown opcode is received, the receiving endpoint MUST |
| 1623 | // _Fail the WebSocket Connection_." |
| 1624 | TEST_F(WebSocketChannelEventInterfaceTest, UnknownOpCodeIsRejected) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1625 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1626 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1627 | static const InitFrame frames[] = {{FINAL_FRAME, 4, NOT_MASKED, "HELLO"}}; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1628 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1629 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1630 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1631 | { |
| 1632 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1633 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1634 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1635 | EXPECT_CALL(*event_interface_, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1636 | OnFailChannel("Unrecognized frame opcode: 4")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1640 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | // RFC6455 5.4 "Control frames ... MAY be injected in the middle of a |
| 1644 | // fragmented message." |
| 1645 | TEST_F(WebSocketChannelEventInterfaceTest, ControlFrameInDataMessage) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1646 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1647 | new ReadableFakeWebSocketStream); |
| 1648 | // We have one message of type Text split into two frames. In the middle is a |
| 1649 | // control message of type Pong. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1650 | static const InitFrame frames1[] = { |
| 1651 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, |
| 1652 | NOT_MASKED, "SPLIT "}}; |
| 1653 | static const InitFrame frames2[] = { |
| 1654 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, ""}}; |
| 1655 | static const InitFrame frames3[] = { |
| 1656 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 1657 | NOT_MASKED, "MESSAGE"}}; |
| 1658 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames1); |
| 1659 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames2); |
| 1660 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames3); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1661 | set_stream(std::move(stream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1662 | { |
| 1663 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1664 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1665 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1666 | EXPECT_CALL( |
| 1667 | *event_interface_, |
| 1668 | OnDataFrame( |
| 1669 | false, WebSocketFrameHeader::kOpCodeText, AsVector("SPLIT "))); |
| 1670 | EXPECT_CALL(*event_interface_, |
| 1671 | OnDataFrame(true, |
| 1672 | WebSocketFrameHeader::kOpCodeContinuation, |
| 1673 | AsVector("MESSAGE"))); |
| 1674 | } |
| 1675 | |
| 1676 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1677 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1678 | } |
| 1679 | |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1680 | // It seems redundant to repeat the entirety of the above test, so just test a |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1681 | // Pong with null data. |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1682 | TEST_F(WebSocketChannelEventInterfaceTest, PongWithNullData) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1683 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1684 | new ReadableFakeWebSocketStream); |
| 1685 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1686 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1687 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1688 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1689 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1690 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1691 | |
| 1692 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1693 | base::RunLoop().RunUntilIdle(); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1694 | } |
| 1695 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1696 | // If a frame has an invalid header, then the connection is closed and |
| 1697 | // subsequent frames must not trigger events. |
| 1698 | TEST_F(WebSocketChannelEventInterfaceTest, FrameAfterInvalidFrame) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1699 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1700 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1701 | static const InitFrame frames[] = { |
| 1702 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "HELLO"}, |
| 1703 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, " WORLD"}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1704 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1705 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1706 | set_stream(std::move(stream)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1707 | { |
| 1708 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1709 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1710 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1711 | EXPECT_CALL( |
| 1712 | *event_interface_, |
| 1713 | OnFailChannel( |
| 1714 | "A server must not mask any frames that it sends to the client.")); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1718 | base::RunLoop().RunUntilIdle(); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1719 | } |
| 1720 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1721 | // If the renderer sends lots of small writes, we don't want to update the quota |
| 1722 | // for each one. |
| 1723 | TEST_F(WebSocketChannelEventInterfaceTest, SmallWriteDoesntUpdateQuota) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1724 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1725 | { |
| 1726 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1727 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1728 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1729 | } |
| 1730 | |
| 1731 | CreateChannelAndConnectSuccessfully(); |
| 1732 | channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("B")); |
| 1733 | } |
| 1734 | |
ricea | 10bf0e1 | 2015-12-14 11:18:29 | [diff] [blame] | 1735 | // If we send enough to go below |send_quota_low_water_mark_| we should get our |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1736 | // quota refreshed. |
| 1737 | TEST_F(WebSocketChannelEventInterfaceTest, LargeWriteUpdatesQuota) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1738 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1739 | // We use this checkpoint object to verify that the quota update comes after |
| 1740 | // the write. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1741 | Checkpoint checkpoint; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1742 | { |
| 1743 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1744 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1745 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1746 | EXPECT_CALL(checkpoint, Call(1)); |
| 1747 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1748 | EXPECT_CALL(checkpoint, Call(2)); |
| 1749 | } |
| 1750 | |
| 1751 | CreateChannelAndConnectSuccessfully(); |
| 1752 | checkpoint.Call(1); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1753 | channel_->SendFrame(true, |
| 1754 | WebSocketFrameHeader::kOpCodeText, |
| 1755 | std::vector<char>(kDefaultInitialQuota, 'B')); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1756 | checkpoint.Call(2); |
| 1757 | } |
| 1758 | |
| 1759 | // Verify that our quota actually is refreshed when we are told it is. |
| 1760 | TEST_F(WebSocketChannelEventInterfaceTest, QuotaReallyIsRefreshed) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1761 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1762 | Checkpoint checkpoint; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1763 | { |
| 1764 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1765 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1766 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1767 | EXPECT_CALL(checkpoint, Call(1)); |
| 1768 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1769 | EXPECT_CALL(checkpoint, Call(2)); |
| 1770 | // If quota was not really refreshed, we would get an OnDropChannel() |
| 1771 | // message. |
| 1772 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1773 | EXPECT_CALL(checkpoint, Call(3)); |
| 1774 | } |
| 1775 | |
| 1776 | CreateChannelAndConnectSuccessfully(); |
| 1777 | checkpoint.Call(1); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1778 | channel_->SendFrame(true, |
| 1779 | WebSocketFrameHeader::kOpCodeText, |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1780 | std::vector<char>(kDefaultQuotaRefreshTrigger, 'D')); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1781 | checkpoint.Call(2); |
| 1782 | // We should have received more quota at this point. |
| 1783 | channel_->SendFrame(true, |
| 1784 | WebSocketFrameHeader::kOpCodeText, |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1785 | std::vector<char>(kDefaultQuotaRefreshTrigger, 'E')); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1786 | checkpoint.Call(3); |
| 1787 | } |
| 1788 | |
| 1789 | // If we send more than the available quota then the connection will be closed |
| 1790 | // with an error. |
| 1791 | TEST_F(WebSocketChannelEventInterfaceTest, WriteOverQuotaIsRejected) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1792 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1793 | { |
| 1794 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1795 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1796 | EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1797 | EXPECT_CALL(*event_interface_, OnFailChannel("Send quota exceeded")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | CreateChannelAndConnectSuccessfully(); |
| 1801 | channel_->SendFrame(true, |
| 1802 | WebSocketFrameHeader::kOpCodeText, |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1803 | std::vector<char>(kDefaultInitialQuota + 1, 'C')); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // If a write fails, the channel is dropped. |
| 1807 | TEST_F(WebSocketChannelEventInterfaceTest, FailedWrite) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1808 | set_stream(base::WrapUnique(new UnWriteableFakeWebSocketStream)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 1809 | Checkpoint checkpoint; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1810 | { |
| 1811 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1812 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1813 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1814 | EXPECT_CALL(checkpoint, Call(1)); |
| 1815 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1816 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1817 | EXPECT_CALL(checkpoint, Call(2)); |
| 1818 | } |
| 1819 | |
| 1820 | CreateChannelAndConnectSuccessfully(); |
| 1821 | checkpoint.Call(1); |
| 1822 | |
| 1823 | channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("H")); |
| 1824 | checkpoint.Call(2); |
| 1825 | } |
| 1826 | |
| 1827 | // OnDropChannel() is called exactly once when StartClosingHandshake() is used. |
| 1828 | TEST_F(WebSocketChannelEventInterfaceTest, SendCloseDropsChannel) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1829 | set_stream(base::WrapUnique(new EchoeyFakeWebSocketStream)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1830 | { |
| 1831 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1832 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1833 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1834 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1835 | OnDropChannel(true, kWebSocketNormalClosure, "Fred")); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | CreateChannelAndConnectSuccessfully(); |
| 1839 | |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 1840 | ASSERT_EQ(CHANNEL_ALIVE, |
| 1841 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "Fred")); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1842 | base::RunLoop().RunUntilIdle(); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 1843 | } |
| 1844 | |
[email protected] | 6dfd8b3 | 2014-02-05 11:24:49 | [diff] [blame] | 1845 | // StartClosingHandshake() also works before connection completes, and calls |
| 1846 | // OnDropChannel. |
| 1847 | TEST_F(WebSocketChannelEventInterfaceTest, CloseDuringConnection) { |
| 1848 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1849 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, "")); |
[email protected] | 6dfd8b3 | 2014-02-05 11:24:49 | [diff] [blame] | 1850 | |
| 1851 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 1852 | ASSERT_EQ(CHANNEL_DELETED, |
| 1853 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "Joe")); |
[email protected] | 6dfd8b3 | 2014-02-05 11:24:49 | [diff] [blame] | 1854 | } |
| 1855 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1856 | // OnDropChannel() is only called once when a write() on the socket triggers a |
| 1857 | // connection reset. |
| 1858 | TEST_F(WebSocketChannelEventInterfaceTest, OnDropChannelCalledOnce) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1859 | set_stream(base::WrapUnique(new ResetOnWriteFakeWebSocketStream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1860 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1861 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1862 | |
| 1863 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1864 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, "")) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1865 | .Times(1); |
| 1866 | |
| 1867 | CreateChannelAndConnectSuccessfully(); |
| 1868 | |
| 1869 | channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("yt?")); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1870 | base::RunLoop().RunUntilIdle(); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | // When the remote server sends a Close frame with an empty payload, |
| 1874 | // WebSocketChannel should report code 1005, kWebSocketErrorNoStatusReceived. |
| 1875 | TEST_F(WebSocketChannelEventInterfaceTest, CloseWithNoPayloadGivesStatus1005) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1876 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1877 | new ReadableFakeWebSocketStream); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1878 | static const InitFrame frames[] = { |
| 1879 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}}; |
| 1880 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1881 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 1882 | ERR_CONNECTION_CLOSED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1883 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1884 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1885 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1886 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
| 1887 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1888 | OnDropChannel(true, kWebSocketErrorNoStatusReceived, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 1889 | |
| 1890 | CreateChannelAndConnectSuccessfully(); |
| 1891 | } |
| 1892 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1893 | // A version of the above test with null payload. |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1894 | TEST_F(WebSocketChannelEventInterfaceTest, |
| 1895 | CloseWithNullPayloadGivesStatus1005) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1896 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1897 | new ReadableFakeWebSocketStream); |
| 1898 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1899 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1900 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
| 1901 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 1902 | ERR_CONNECTION_CLOSED); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1903 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1904 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1905 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1906 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
| 1907 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 1908 | OnDropChannel(true, kWebSocketErrorNoStatusReceived, _)); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 1909 | |
| 1910 | CreateChannelAndConnectSuccessfully(); |
| 1911 | } |
| 1912 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1913 | // If ReadFrames() returns ERR_WS_PROTOCOL_ERROR, then the connection must be |
| 1914 | // failed. |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1915 | TEST_F(WebSocketChannelEventInterfaceTest, SyncProtocolErrorGivesStatus1002) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1916 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1917 | new ReadableFakeWebSocketStream); |
| 1918 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 1919 | ERR_WS_PROTOCOL_ERROR); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1920 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1921 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1922 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1923 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1924 | EXPECT_CALL(*event_interface_, OnFailChannel("Invalid frame header")); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1925 | |
| 1926 | CreateChannelAndConnectSuccessfully(); |
| 1927 | } |
| 1928 | |
| 1929 | // Async version of above test. |
| 1930 | TEST_F(WebSocketChannelEventInterfaceTest, AsyncProtocolErrorGivesStatus1002) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1931 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1932 | new ReadableFakeWebSocketStream); |
| 1933 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::ASYNC, |
| 1934 | ERR_WS_PROTOCOL_ERROR); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1935 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1936 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1937 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1938 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 1939 | EXPECT_CALL(*event_interface_, OnFailChannel("Invalid frame header")); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1940 | |
| 1941 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1942 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 1943 | } |
| 1944 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1945 | TEST_F(WebSocketChannelEventInterfaceTest, StartHandshakeRequest) { |
| 1946 | { |
| 1947 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1948 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1949 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1950 | EXPECT_CALL(*event_interface_, OnStartOpeningHandshakeCalled()); |
| 1951 | } |
| 1952 | |
| 1953 | CreateChannelAndConnectSuccessfully(); |
| 1954 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1955 | std::unique_ptr<WebSocketHandshakeRequestInfo> request_info( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1956 | new WebSocketHandshakeRequestInfo(GURL("ws://www.example.com/"), |
| 1957 | base::Time())); |
| 1958 | connect_data_.creator.connect_delegate->OnStartOpeningHandshake( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1959 | std::move(request_info)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1960 | |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1961 | base::RunLoop().RunUntilIdle(); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | TEST_F(WebSocketChannelEventInterfaceTest, FinishHandshakeRequest) { |
| 1965 | { |
| 1966 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 1967 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1968 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 1969 | EXPECT_CALL(*event_interface_, OnFinishOpeningHandshakeCalled()); |
| 1970 | } |
| 1971 | |
| 1972 | CreateChannelAndConnectSuccessfully(); |
| 1973 | |
| 1974 | scoped_refptr<HttpResponseHeaders> response_headers( |
| 1975 | new HttpResponseHeaders("")); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1976 | std::unique_ptr<WebSocketHandshakeResponseInfo> response_info( |
| 1977 | new WebSocketHandshakeResponseInfo(GURL("ws://www.example.com/"), 200, |
| 1978 | "OK", response_headers, base::Time())); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1979 | connect_data_.creator.connect_delegate->OnFinishOpeningHandshake( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 1980 | std::move(response_info)); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 1981 | base::RunLoop().RunUntilIdle(); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | TEST_F(WebSocketChannelEventInterfaceTest, FailJustAfterHandshake) { |
| 1985 | { |
| 1986 | InSequence s; |
| 1987 | EXPECT_CALL(*event_interface_, OnStartOpeningHandshakeCalled()); |
| 1988 | EXPECT_CALL(*event_interface_, OnFinishOpeningHandshakeCalled()); |
| 1989 | EXPECT_CALL(*event_interface_, OnFailChannel("bye")); |
| 1990 | } |
| 1991 | |
| 1992 | CreateChannelAndConnect(); |
| 1993 | |
| 1994 | WebSocketStream::ConnectDelegate* connect_delegate = |
| 1995 | connect_data_.creator.connect_delegate.get(); |
| 1996 | GURL url("ws://www.example.com/"); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 1997 | std::unique_ptr<WebSocketHandshakeRequestInfo> request_info( |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 1998 | new WebSocketHandshakeRequestInfo(url, base::Time())); |
| 1999 | scoped_refptr<HttpResponseHeaders> response_headers( |
| 2000 | new HttpResponseHeaders("")); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2001 | std::unique_ptr<WebSocketHandshakeResponseInfo> response_info( |
| 2002 | new WebSocketHandshakeResponseInfo(url, 200, "OK", response_headers, |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 2003 | base::Time())); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2004 | connect_delegate->OnStartOpeningHandshake(std::move(request_info)); |
| 2005 | connect_delegate->OnFinishOpeningHandshake(std::move(response_info)); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 2006 | |
| 2007 | connect_delegate->OnFailure("bye"); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 2008 | base::RunLoop().RunUntilIdle(); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 2009 | } |
| 2010 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2011 | // Any frame after close is invalid. This test uses a Text frame. See also |
| 2012 | // test "PingAfterCloseIfRejected". |
| 2013 | TEST_F(WebSocketChannelEventInterfaceTest, DataAfterCloseIsRejected) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2014 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2015 | new ReadableFakeWebSocketStream); |
| 2016 | static const InitFrame frames[] = { |
| 2017 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, |
| 2018 | CLOSE_DATA(NORMAL_CLOSURE, "OK")}, |
| 2019 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "Payload"}}; |
| 2020 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2021 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2022 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2023 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2024 | |
| 2025 | { |
| 2026 | InSequence s; |
| 2027 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
| 2028 | EXPECT_CALL(*event_interface_, |
| 2029 | OnFailChannel("Data frame received after close")); |
| 2030 | } |
| 2031 | |
| 2032 | CreateChannelAndConnectSuccessfully(); |
| 2033 | } |
| 2034 | |
| 2035 | // A Close frame with a one-byte payload elicits a specific console error |
| 2036 | // message. |
| 2037 | TEST_F(WebSocketChannelEventInterfaceTest, OneByteClosePayloadMessage) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2038 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2039 | new ReadableFakeWebSocketStream); |
| 2040 | static const InitFrame frames[] = { |
| 2041 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, "\x03"}}; |
| 2042 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2043 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2044 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2045 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2046 | EXPECT_CALL( |
| 2047 | *event_interface_, |
| 2048 | OnFailChannel( |
| 2049 | "Received a broken close frame containing an invalid size body.")); |
| 2050 | |
| 2051 | CreateChannelAndConnectSuccessfully(); |
| 2052 | } |
| 2053 | |
| 2054 | // A Close frame with a reserved status code also elicits a specific console |
| 2055 | // error message. |
| 2056 | TEST_F(WebSocketChannelEventInterfaceTest, ClosePayloadReservedStatusMessage) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2057 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2058 | new ReadableFakeWebSocketStream); |
| 2059 | static const InitFrame frames[] = { |
| 2060 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2061 | NOT_MASKED, CLOSE_DATA(ABNORMAL_CLOSURE, "Not valid on wire")}}; |
| 2062 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2063 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2064 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2065 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2066 | EXPECT_CALL( |
| 2067 | *event_interface_, |
| 2068 | OnFailChannel( |
| 2069 | "Received a broken close frame containing a reserved status code.")); |
| 2070 | |
| 2071 | CreateChannelAndConnectSuccessfully(); |
| 2072 | } |
| 2073 | |
| 2074 | // A Close frame with invalid UTF-8 also elicits a specific console error |
| 2075 | // message. |
| 2076 | TEST_F(WebSocketChannelEventInterfaceTest, ClosePayloadInvalidReason) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2077 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2078 | new ReadableFakeWebSocketStream); |
| 2079 | static const InitFrame frames[] = { |
| 2080 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2081 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}}; |
| 2082 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2083 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2084 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2085 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2086 | EXPECT_CALL( |
| 2087 | *event_interface_, |
| 2088 | OnFailChannel( |
| 2089 | "Received a broken close frame containing invalid UTF-8.")); |
| 2090 | |
| 2091 | CreateChannelAndConnectSuccessfully(); |
| 2092 | } |
| 2093 | |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 2094 | // The reserved bits must all be clear on received frames. Extensions should |
| 2095 | // clear the bits when they are set correctly before passing on the frame. |
| 2096 | TEST_F(WebSocketChannelEventInterfaceTest, ReservedBitsMustNotBeSet) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2097 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 2098 | new ReadableFakeWebSocketStream); |
| 2099 | static const InitFrame frames[] = { |
| 2100 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, |
| 2101 | NOT_MASKED, "sakana"}}; |
| 2102 | // It is not worth adding support for reserved bits to InitFrame just for this |
| 2103 | // one test, so set the bit manually. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2104 | std::vector<std::unique_ptr<WebSocketFrame>> raw_frames = |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 2105 | CreateFrameVector(frames); |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 2106 | raw_frames[0]->header.reserved1 = true; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 2107 | stream->PrepareRawReadFrames(ReadableFakeWebSocketStream::SYNC, OK, |
| 2108 | std::move(raw_frames)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2109 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2110 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 2111 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
[email protected] | c46c261 | 2014-02-28 17:08:02 | [diff] [blame] | 2112 | EXPECT_CALL(*event_interface_, |
| 2113 | OnFailChannel( |
| 2114 | "One or more reserved bits are on: reserved1 = 1, " |
| 2115 | "reserved2 = 0, reserved3 = 0")); |
[email protected] | 658d767 | 2014-02-26 13:11:35 | [diff] [blame] | 2116 | |
| 2117 | CreateChannelAndConnectSuccessfully(); |
| 2118 | } |
| 2119 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2120 | // The closing handshake times out and sends an OnDropChannel event if no |
| 2121 | // response to the client Close message is received. |
| 2122 | TEST_F(WebSocketChannelEventInterfaceTest, |
| 2123 | ClientInitiatedClosingHandshakeTimesOut) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2124 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2125 | new ReadableFakeWebSocketStream); |
| 2126 | stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, |
| 2127 | ERR_IO_PENDING); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2128 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2129 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2130 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2131 | // This checkpoint object verifies that the OnDropChannel message comes after |
| 2132 | // the timeout. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2133 | Checkpoint checkpoint; |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2134 | TestClosure completion; |
| 2135 | { |
| 2136 | InSequence s; |
| 2137 | EXPECT_CALL(checkpoint, Call(1)); |
| 2138 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 2139 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)) |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2140 | .WillOnce(InvokeClosureReturnDeleted(completion.closure())); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2141 | } |
| 2142 | CreateChannelAndConnectSuccessfully(); |
| 2143 | // OneShotTimer is not very friendly to testing; there is no apparent way to |
| 2144 | // set an expectation on it. Instead the tests need to infer that the timeout |
| 2145 | // was fired by the behaviour of the WebSocketChannel object. |
| 2146 | channel_->SetClosingHandshakeTimeoutForTesting( |
| 2147 | TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 2148 | channel_->SetUnderlyingConnectionCloseTimeoutForTesting( |
| 2149 | TimeDelta::FromMilliseconds(kVeryBigTimeoutMillis)); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2150 | ASSERT_EQ(CHANNEL_ALIVE, |
| 2151 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "")); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2152 | checkpoint.Call(1); |
| 2153 | completion.WaitForResult(); |
| 2154 | } |
| 2155 | |
| 2156 | // The closing handshake times out and sends an OnDropChannel event if a Close |
| 2157 | // message is received but the connection isn't closed by the remote host. |
| 2158 | TEST_F(WebSocketChannelEventInterfaceTest, |
| 2159 | ServerInitiatedClosingHandshakeTimesOut) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2160 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2161 | new ReadableFakeWebSocketStream); |
| 2162 | static const InitFrame frames[] = { |
| 2163 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2164 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 2165 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2166 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2167 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2168 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2169 | Checkpoint checkpoint; |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2170 | TestClosure completion; |
| 2171 | { |
| 2172 | InSequence s; |
| 2173 | EXPECT_CALL(checkpoint, Call(1)); |
| 2174 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
| 2175 | EXPECT_CALL(*event_interface_, |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 2176 | OnDropChannel(false, kWebSocketErrorAbnormalClosure, _)) |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2177 | .WillOnce(InvokeClosureReturnDeleted(completion.closure())); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2178 | } |
| 2179 | CreateChannelAndConnectSuccessfully(); |
| 2180 | channel_->SetClosingHandshakeTimeoutForTesting( |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 2181 | TimeDelta::FromMilliseconds(kVeryBigTimeoutMillis)); |
| 2182 | channel_->SetUnderlyingConnectionCloseTimeoutForTesting( |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 2183 | TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
| 2184 | checkpoint.Call(1); |
| 2185 | completion.WaitForResult(); |
| 2186 | } |
| 2187 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2188 | // The renderer should provide us with some quota immediately, and then |
| 2189 | // WebSocketChannel calls ReadFrames as soon as the stream is available. |
| 2190 | TEST_F(WebSocketChannelStreamTest, FlowControlEarly) { |
| 2191 | Checkpoint checkpoint; |
| 2192 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2193 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2194 | { |
| 2195 | InSequence s; |
| 2196 | EXPECT_CALL(checkpoint, Call(1)); |
| 2197 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2198 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2199 | EXPECT_CALL(checkpoint, Call(2)); |
| 2200 | } |
| 2201 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2202 | set_stream(std::move(mock_stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2203 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2204 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(kPlentyOfQuota)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2205 | checkpoint.Call(1); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2206 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2207 | checkpoint.Call(2); |
| 2208 | } |
| 2209 | |
| 2210 | // If for some reason the connect succeeds before the renderer sends us quota, |
| 2211 | // we shouldn't call ReadFrames() immediately. |
| 2212 | // TODO(ricea): Actually we should call ReadFrames() with a small limit so we |
| 2213 | // can still handle control frames. This should be done once we have any API to |
| 2214 | // expose quota to the lower levels. |
| 2215 | TEST_F(WebSocketChannelStreamTest, FlowControlLate) { |
| 2216 | Checkpoint checkpoint; |
| 2217 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2218 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2219 | { |
| 2220 | InSequence s; |
| 2221 | EXPECT_CALL(checkpoint, Call(1)); |
| 2222 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2223 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2224 | EXPECT_CALL(checkpoint, Call(2)); |
| 2225 | } |
| 2226 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2227 | set_stream(std::move(mock_stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2228 | CreateChannelAndConnect(); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2229 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2230 | checkpoint.Call(1); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2231 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(kPlentyOfQuota)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2232 | checkpoint.Call(2); |
| 2233 | } |
| 2234 | |
| 2235 | // We should stop calling ReadFrames() when all quota is used. |
| 2236 | TEST_F(WebSocketChannelStreamTest, FlowControlStopsReadFrames) { |
| 2237 | static const InitFrame frames[] = { |
| 2238 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; |
| 2239 | |
| 2240 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2241 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2242 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2243 | .WillOnce(ReturnFrames(&frames)); |
| 2244 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2245 | set_stream(std::move(mock_stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2246 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2247 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(4)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2248 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | // Providing extra quota causes ReadFrames() to be called again. |
| 2252 | TEST_F(WebSocketChannelStreamTest, FlowControlStartsWithMoreQuota) { |
| 2253 | static const InitFrame frames[] = { |
| 2254 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; |
| 2255 | Checkpoint checkpoint; |
| 2256 | |
| 2257 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2258 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2259 | { |
| 2260 | InSequence s; |
| 2261 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2262 | .WillOnce(ReturnFrames(&frames)); |
| 2263 | EXPECT_CALL(checkpoint, Call(1)); |
| 2264 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2265 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2266 | } |
| 2267 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2268 | set_stream(std::move(mock_stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2269 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2270 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(4)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2271 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2272 | checkpoint.Call(1); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2273 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(4)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | // ReadFrames() isn't called again until all pending data has been passed to |
| 2277 | // the renderer. |
| 2278 | TEST_F(WebSocketChannelStreamTest, ReadFramesNotCalledUntilQuotaAvailable) { |
| 2279 | static const InitFrame frames[] = { |
| 2280 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; |
| 2281 | Checkpoint checkpoint; |
| 2282 | |
| 2283 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2284 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2285 | { |
| 2286 | InSequence s; |
| 2287 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2288 | .WillOnce(ReturnFrames(&frames)); |
| 2289 | EXPECT_CALL(checkpoint, Call(1)); |
| 2290 | EXPECT_CALL(checkpoint, Call(2)); |
| 2291 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2292 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2293 | } |
| 2294 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2295 | set_stream(std::move(mock_stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2296 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2297 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(2)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2298 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2299 | checkpoint.Call(1); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2300 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(2)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2301 | checkpoint.Call(2); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2302 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(2)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | // A message that needs to be split into frames to fit within quota should |
| 2306 | // maintain correct semantics. |
| 2307 | TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitSync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2308 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2309 | new ReadableFakeWebSocketStream); |
| 2310 | static const InitFrame frames[] = { |
| 2311 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; |
| 2312 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2313 | set_stream(std::move(stream)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2314 | { |
| 2315 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2316 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2317 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2318 | EXPECT_CALL( |
| 2319 | *event_interface_, |
| 2320 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO"))); |
| 2321 | EXPECT_CALL( |
| 2322 | *event_interface_, |
| 2323 | OnDataFrame( |
| 2324 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U"))); |
| 2325 | EXPECT_CALL( |
| 2326 | *event_interface_, |
| 2327 | OnDataFrame( |
| 2328 | true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R"))); |
| 2329 | } |
| 2330 | |
| 2331 | CreateChannelAndConnectWithQuota(2); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2332 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); |
| 2333 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | // The code path for async messages is slightly different, so test it |
| 2337 | // separately. |
| 2338 | TEST_F(WebSocketChannelFlowControlTest, SingleFrameMessageSplitAsync) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2339 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2340 | new ReadableFakeWebSocketStream); |
| 2341 | static const InitFrame frames[] = { |
| 2342 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "FOUR"}}; |
| 2343 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2344 | set_stream(std::move(stream)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2345 | Checkpoint checkpoint; |
| 2346 | { |
| 2347 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2348 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2349 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2350 | EXPECT_CALL(checkpoint, Call(1)); |
| 2351 | EXPECT_CALL( |
| 2352 | *event_interface_, |
| 2353 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("FO"))); |
| 2354 | EXPECT_CALL(checkpoint, Call(2)); |
| 2355 | EXPECT_CALL( |
| 2356 | *event_interface_, |
| 2357 | OnDataFrame( |
| 2358 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("U"))); |
| 2359 | EXPECT_CALL(checkpoint, Call(3)); |
| 2360 | EXPECT_CALL( |
| 2361 | *event_interface_, |
| 2362 | OnDataFrame( |
| 2363 | true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("R"))); |
| 2364 | } |
| 2365 | |
| 2366 | CreateChannelAndConnectWithQuota(2); |
| 2367 | checkpoint.Call(1); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 2368 | base::RunLoop().RunUntilIdle(); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2369 | checkpoint.Call(2); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2370 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2371 | checkpoint.Call(3); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2372 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(1)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | // A message split into multiple frames which is further split due to quota |
| 2376 | // restrictions should stil be correct. |
| 2377 | // TODO(ricea): The message ends up split into more frames than are strictly |
| 2378 | // necessary. The complexity/performance tradeoffs here need further |
| 2379 | // examination. |
| 2380 | TEST_F(WebSocketChannelFlowControlTest, MultipleFrameSplit) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2381 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2382 | new ReadableFakeWebSocketStream); |
| 2383 | static const InitFrame frames[] = { |
| 2384 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, |
| 2385 | NOT_MASKED, "FIRST FRAME IS 25 BYTES. "}, |
| 2386 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 2387 | NOT_MASKED, "SECOND FRAME IS 26 BYTES. "}, |
| 2388 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 2389 | NOT_MASKED, "FINAL FRAME IS 24 BYTES."}}; |
| 2390 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2391 | set_stream(std::move(stream)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2392 | { |
| 2393 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2394 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2395 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2396 | EXPECT_CALL(*event_interface_, |
| 2397 | OnDataFrame(false, |
| 2398 | WebSocketFrameHeader::kOpCodeText, |
| 2399 | AsVector("FIRST FRAME IS"))); |
| 2400 | EXPECT_CALL(*event_interface_, |
| 2401 | OnDataFrame(false, |
| 2402 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2403 | AsVector(" 25 BYTES. "))); |
| 2404 | EXPECT_CALL(*event_interface_, |
| 2405 | OnDataFrame(false, |
| 2406 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2407 | AsVector("SECOND FRAME IS 26 BYTES. "))); |
| 2408 | EXPECT_CALL(*event_interface_, |
| 2409 | OnDataFrame(false, |
| 2410 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2411 | AsVector("FINAL "))); |
| 2412 | EXPECT_CALL(*event_interface_, |
| 2413 | OnDataFrame(true, |
| 2414 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2415 | AsVector("FRAME IS 24 BYTES."))); |
| 2416 | } |
| 2417 | CreateChannelAndConnectWithQuota(14); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2418 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(43)); |
| 2419 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(32)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | // An empty message handled when we are out of quota must not be delivered |
| 2423 | // out-of-order with respect to other messages. |
| 2424 | TEST_F(WebSocketChannelFlowControlTest, EmptyMessageNoQuota) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2425 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2426 | new ReadableFakeWebSocketStream); |
| 2427 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2428 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, |
| 2429 | "FIRST MESSAGE"}, |
| 2430 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, nullptr}, |
| 2431 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, |
| 2432 | "THIRD MESSAGE"}}; |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2433 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2434 | set_stream(std::move(stream)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2435 | { |
| 2436 | InSequence s; |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2437 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2438 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2439 | EXPECT_CALL(*event_interface_, |
| 2440 | OnDataFrame(false, |
| 2441 | WebSocketFrameHeader::kOpCodeText, |
| 2442 | AsVector("FIRST "))); |
| 2443 | EXPECT_CALL(*event_interface_, |
| 2444 | OnDataFrame(true, |
| 2445 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2446 | AsVector("MESSAGE"))); |
| 2447 | EXPECT_CALL(*event_interface_, |
| 2448 | OnDataFrame(true, |
| 2449 | WebSocketFrameHeader::kOpCodeText, |
| 2450 | AsVector(""))); |
| 2451 | EXPECT_CALL(*event_interface_, |
| 2452 | OnDataFrame(true, |
| 2453 | WebSocketFrameHeader::kOpCodeText, |
| 2454 | AsVector("THIRD MESSAGE"))); |
| 2455 | } |
| 2456 | |
| 2457 | CreateChannelAndConnectWithQuota(6); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2458 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(128)); |
| 2459 | } |
| 2460 | |
| 2461 | // A close frame should not overtake data frames. |
| 2462 | TEST_F(WebSocketChannelFlowControlTest, CloseFrameShouldNotOvertakeDataFrames) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2463 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2464 | new ReadableFakeWebSocketStream); |
| 2465 | static const InitFrame frames[] = { |
| 2466 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, |
| 2467 | "FIRST "}, |
| 2468 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, |
| 2469 | "MESSAGE"}, |
| 2470 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, |
| 2471 | "SECOND "}, |
| 2472 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, |
| 2473 | CLOSE_DATA(NORMAL_CLOSURE, "GOOD BYE")}, |
| 2474 | }; |
| 2475 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
| 2476 | set_stream(std::move(stream)); |
| 2477 | Checkpoint checkpoint; |
| 2478 | InSequence s; |
| 2479 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
| 2480 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2481 | EXPECT_CALL(*event_interface_, |
| 2482 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, |
| 2483 | AsVector("FIRST "))); |
| 2484 | EXPECT_CALL(checkpoint, Call(1)); |
| 2485 | EXPECT_CALL(*event_interface_, |
| 2486 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeContinuation, |
| 2487 | AsVector("MESSAG"))); |
| 2488 | EXPECT_CALL(checkpoint, Call(2)); |
| 2489 | EXPECT_CALL(*event_interface_, |
| 2490 | OnDataFrame(true, WebSocketFrameHeader::kOpCodeContinuation, |
| 2491 | AsVector("E"))); |
| 2492 | EXPECT_CALL( |
| 2493 | *event_interface_, |
| 2494 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeText, AsVector("SECON"))); |
| 2495 | EXPECT_CALL(checkpoint, Call(3)); |
| 2496 | EXPECT_CALL(*event_interface_, |
| 2497 | OnDataFrame(false, WebSocketFrameHeader::kOpCodeContinuation, |
| 2498 | AsVector("D "))); |
| 2499 | EXPECT_CALL(*event_interface_, OnClosingHandshake()); |
| 2500 | EXPECT_CALL(checkpoint, Call(4)); |
| 2501 | |
| 2502 | CreateChannelAndConnectWithQuota(6); |
| 2503 | checkpoint.Call(1); |
| 2504 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); |
| 2505 | checkpoint.Call(2); |
| 2506 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); |
| 2507 | checkpoint.Call(3); |
| 2508 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(6)); |
| 2509 | checkpoint.Call(4); |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2510 | } |
| 2511 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 2512 | // RFC6455 5.1 "a client MUST mask all frames that it sends to the server". |
| 2513 | // WebSocketChannel actually only sets the mask bit in the header, it doesn't |
| 2514 | // perform masking itself (not all transports actually use masking). |
| 2515 | TEST_F(WebSocketChannelStreamTest, SentFramesAreMasked) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2516 | static const InitFrame expected[] = { |
| 2517 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, |
| 2518 | MASKED, "NEEDS MASKING"}}; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 2519 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2520 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 2521 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2522 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2523 | .WillOnce(Return(OK)); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 2524 | |
| 2525 | CreateChannelAndConnectSuccessfully(); |
| 2526 | channel_->SendFrame( |
| 2527 | true, WebSocketFrameHeader::kOpCodeText, AsVector("NEEDS MASKING")); |
| 2528 | } |
| 2529 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2530 | // RFC6455 5.5.1 "The application MUST NOT send any more data frames after |
| 2531 | // sending a Close frame." |
| 2532 | TEST_F(WebSocketChannelStreamTest, NothingIsSentAfterClose) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2533 | static const InitFrame expected[] = { |
| 2534 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2535 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Success")}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2536 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2537 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2538 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2539 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2540 | .WillOnce(Return(OK)); |
| 2541 | |
| 2542 | CreateChannelAndConnectSuccessfully(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2543 | ASSERT_EQ(CHANNEL_ALIVE, channel_->StartClosingHandshake(1000, "Success")); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2544 | channel_->SendFrame( |
| 2545 | true, WebSocketFrameHeader::kOpCodeText, AsVector("SHOULD BE IGNORED")); |
| 2546 | } |
| 2547 | |
| 2548 | // RFC6455 5.5.1 "If an endpoint receives a Close frame and did not previously |
| 2549 | // send a Close frame, the endpoint MUST send a Close frame in response." |
| 2550 | TEST_F(WebSocketChannelStreamTest, CloseIsEchoedBack) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2551 | static const InitFrame frames[] = { |
| 2552 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2553 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}}; |
| 2554 | static const InitFrame expected[] = { |
| 2555 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2556 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2557 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2558 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2559 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2560 | .WillOnce(ReturnFrames(&frames)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2561 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2562 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2563 | .WillOnce(Return(OK)); |
| 2564 | |
| 2565 | CreateChannelAndConnectSuccessfully(); |
| 2566 | } |
| 2567 | |
| 2568 | // The converse of the above case; after sending a Close frame, we should not |
| 2569 | // send another one. |
| 2570 | TEST_F(WebSocketChannelStreamTest, CloseOnlySentOnce) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2571 | static const InitFrame expected[] = { |
| 2572 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2573 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}}; |
| 2574 | static const InitFrame frames_init[] = { |
| 2575 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2576 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "Close")}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2577 | |
| 2578 | // We store the parameters that were passed to ReadFrames() so that we can |
| 2579 | // call them explicitly later. |
| 2580 | CompletionCallback read_callback; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2581 | std::vector<std::unique_ptr<WebSocketFrame>>* frames = nullptr; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2582 | |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2583 | // These are not interesting. |
| 2584 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 2585 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 2586 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2587 | // Use a checkpoint to make the ordering of events clearer. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2588 | Checkpoint checkpoint; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2589 | { |
| 2590 | InSequence s; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2591 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2592 | .WillOnce(DoAll(SaveArg<0>(&frames), |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2593 | SaveArg<1>(&read_callback), |
| 2594 | Return(ERR_IO_PENDING))); |
| 2595 | EXPECT_CALL(checkpoint, Call(1)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2596 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2597 | .WillOnce(Return(OK)); |
| 2598 | EXPECT_CALL(checkpoint, Call(2)); |
| 2599 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2600 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2601 | EXPECT_CALL(checkpoint, Call(3)); |
| 2602 | // WriteFrames() must not be called again. GoogleMock will ensure that the |
| 2603 | // test fails if it is. |
| 2604 | } |
| 2605 | |
| 2606 | CreateChannelAndConnectSuccessfully(); |
| 2607 | checkpoint.Call(1); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2608 | ASSERT_EQ(CHANNEL_ALIVE, |
| 2609 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "Close")); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2610 | checkpoint.Call(2); |
| 2611 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2612 | *frames = CreateFrameVector(frames_init); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2613 | read_callback.Run(OK); |
| 2614 | checkpoint.Call(3); |
| 2615 | } |
| 2616 | |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2617 | // Invalid close status codes should not be sent on the network. |
| 2618 | TEST_F(WebSocketChannelStreamTest, InvalidCloseStatusCodeNotSent) { |
| 2619 | static const InitFrame expected[] = { |
| 2620 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2621 | MASKED, CLOSE_DATA(SERVER_ERROR, "")}}; |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2622 | |
| 2623 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2624 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2625 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2626 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2627 | |
| 2628 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)); |
| 2629 | |
| 2630 | CreateChannelAndConnectSuccessfully(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2631 | ASSERT_EQ(CHANNEL_ALIVE, channel_->StartClosingHandshake(999, "")); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | // A Close frame with a reason longer than 123 bytes cannot be sent on the |
| 2635 | // network. |
| 2636 | TEST_F(WebSocketChannelStreamTest, LongCloseReasonNotSent) { |
| 2637 | static const InitFrame expected[] = { |
| 2638 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2639 | MASKED, CLOSE_DATA(SERVER_ERROR, "")}}; |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2640 | |
| 2641 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2642 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2643 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2644 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2645 | |
| 2646 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)); |
| 2647 | |
| 2648 | CreateChannelAndConnectSuccessfully(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 2649 | ASSERT_EQ(CHANNEL_ALIVE, |
| 2650 | channel_->StartClosingHandshake(1000, std::string(124, 'A'))); |
[email protected] | 3de6509 | 2013-10-24 09:39:44 | [diff] [blame] | 2651 | } |
| 2652 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2653 | // We generate code 1005, kWebSocketErrorNoStatusReceived, when there is no |
| 2654 | // status in the Close message from the other side. Code 1005 is not allowed to |
| 2655 | // appear on the wire, so we should not echo it back. See test |
| 2656 | // CloseWithNoPayloadGivesStatus1005, above, for confirmation that code 1005 is |
| 2657 | // correctly generated internally. |
| 2658 | TEST_F(WebSocketChannelStreamTest, Code1005IsNotEchoed) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2659 | static const InitFrame frames[] = { |
| 2660 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, ""}}; |
| 2661 | static const InitFrame expected[] = { |
| 2662 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, ""}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2663 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2664 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2665 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2666 | .WillOnce(ReturnFrames(&frames)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2667 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2668 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2669 | .WillOnce(Return(OK)); |
| 2670 | |
| 2671 | CreateChannelAndConnectSuccessfully(); |
| 2672 | } |
| 2673 | |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2674 | TEST_F(WebSocketChannelStreamTest, Code1005IsNotEchoedNull) { |
| 2675 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2676 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2677 | static const InitFrame expected[] = { |
| 2678 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, ""}}; |
| 2679 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2680 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2681 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2682 | .WillOnce(ReturnFrames(&frames)) |
| 2683 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 2684 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 2685 | .WillOnce(Return(OK)); |
| 2686 | |
| 2687 | CreateChannelAndConnectSuccessfully(); |
| 2688 | } |
| 2689 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2690 | // Receiving an invalid UTF-8 payload in a Close frame causes us to fail the |
| 2691 | // connection. |
| 2692 | TEST_F(WebSocketChannelStreamTest, CloseFrameInvalidUtf8) { |
| 2693 | static const InitFrame frames[] = { |
| 2694 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2695 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "\xFF")}}; |
| 2696 | static const InitFrame expected[] = { |
| 2697 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 2698 | MASKED, CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in Close frame")}}; |
| 2699 | |
| 2700 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2701 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2702 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2703 | .WillOnce(ReturnFrames(&frames)) |
| 2704 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 2705 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 2706 | .WillOnce(Return(OK)); |
| 2707 | EXPECT_CALL(*mock_stream_, Close()); |
| 2708 | |
| 2709 | CreateChannelAndConnectSuccessfully(); |
| 2710 | } |
| 2711 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2712 | // RFC6455 5.5.2 "Upon receipt of a Ping frame, an endpoint MUST send a Pong |
| 2713 | // frame in response" |
| 2714 | // 5.5.3 "A Pong frame sent in response to a Ping frame must have identical |
| 2715 | // "Application data" as found in the message body of the Ping frame being |
| 2716 | // replied to." |
| 2717 | TEST_F(WebSocketChannelStreamTest, PingRepliedWithPong) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2718 | static const InitFrame frames[] = { |
| 2719 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePing, |
| 2720 | NOT_MASKED, "Application data"}}; |
| 2721 | static const InitFrame expected[] = { |
| 2722 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, |
| 2723 | MASKED, "Application data"}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2724 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2725 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2726 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2727 | .WillOnce(ReturnFrames(&frames)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2728 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2729 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2730 | .WillOnce(Return(OK)); |
| 2731 | |
| 2732 | CreateChannelAndConnectSuccessfully(); |
| 2733 | } |
| 2734 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2735 | // A ping with a null payload should be responded to with a Pong with a null |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2736 | // payload. |
[email protected] | 403ee6e | 2014-01-27 10:10:44 | [diff] [blame] | 2737 | TEST_F(WebSocketChannelStreamTest, NullPingRepliedWithNullPong) { |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2738 | static const InitFrame frames[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2739 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePing, NOT_MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2740 | static const InitFrame expected[] = { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2741 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, MASKED, nullptr}}; |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2742 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2743 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 00f4daf | 2013-11-12 13:56:41 | [diff] [blame] | 2744 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 2745 | .WillOnce(ReturnFrames(&frames)) |
| 2746 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 2747 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 2748 | .WillOnce(Return(OK)); |
| 2749 | |
| 2750 | CreateChannelAndConnectSuccessfully(); |
| 2751 | } |
| 2752 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2753 | TEST_F(WebSocketChannelStreamTest, PongInTheMiddleOfDataMessage) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2754 | static const InitFrame frames[] = { |
| 2755 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePing, |
| 2756 | NOT_MASKED, "Application data"}}; |
| 2757 | static const InitFrame expected1[] = { |
| 2758 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}}; |
| 2759 | static const InitFrame expected2[] = { |
| 2760 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePong, |
| 2761 | MASKED, "Application data"}}; |
| 2762 | static const InitFrame expected3[] = { |
| 2763 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 2764 | MASKED, "World"}}; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2765 | std::vector<std::unique_ptr<WebSocketFrame>>* read_frames; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2766 | CompletionCallback read_callback; |
| 2767 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2768 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2769 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2770 | .WillOnce(DoAll(SaveArg<0>(&read_frames), |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2771 | SaveArg<1>(&read_callback), |
| 2772 | Return(ERR_IO_PENDING))) |
| 2773 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 2774 | { |
| 2775 | InSequence s; |
| 2776 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2777 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2778 | .WillOnce(Return(OK)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2779 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2780 | .WillOnce(Return(OK)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2781 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected3), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2782 | .WillOnce(Return(OK)); |
| 2783 | } |
| 2784 | |
| 2785 | CreateChannelAndConnectSuccessfully(); |
| 2786 | channel_->SendFrame( |
| 2787 | false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello ")); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2788 | *read_frames = CreateFrameVector(frames); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2789 | read_callback.Run(OK); |
| 2790 | channel_->SendFrame( |
| 2791 | true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("World")); |
| 2792 | } |
| 2793 | |
| 2794 | // WriteFrames() may not be called until the previous write has completed. |
| 2795 | // WebSocketChannel must buffer writes that happen in the meantime. |
| 2796 | TEST_F(WebSocketChannelStreamTest, WriteFramesOneAtATime) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2797 | static const InitFrame expected1[] = { |
| 2798 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "Hello "}}; |
| 2799 | static const InitFrame expected2[] = { |
| 2800 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "World"}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2801 | CompletionCallback write_callback; |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 2802 | Checkpoint checkpoint; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2803 | |
| 2804 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2805 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2806 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
| 2807 | { |
| 2808 | InSequence s; |
| 2809 | EXPECT_CALL(checkpoint, Call(1)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2810 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2811 | .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING))); |
| 2812 | EXPECT_CALL(checkpoint, Call(2)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2813 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2814 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2815 | EXPECT_CALL(checkpoint, Call(3)); |
| 2816 | } |
| 2817 | |
| 2818 | CreateChannelAndConnectSuccessfully(); |
| 2819 | checkpoint.Call(1); |
| 2820 | channel_->SendFrame( |
| 2821 | false, WebSocketFrameHeader::kOpCodeText, AsVector("Hello ")); |
| 2822 | channel_->SendFrame( |
| 2823 | true, WebSocketFrameHeader::kOpCodeText, AsVector("World")); |
| 2824 | checkpoint.Call(2); |
| 2825 | write_callback.Run(OK); |
| 2826 | checkpoint.Call(3); |
| 2827 | } |
| 2828 | |
| 2829 | // WebSocketChannel must buffer frames while it is waiting for a write to |
| 2830 | // complete, and then send them in a single batch. The batching behaviour is |
| 2831 | // important to get good throughput in the "many small messages" case. |
| 2832 | TEST_F(WebSocketChannelStreamTest, WaitingMessagesAreBatched) { |
| 2833 | static const char input_letters[] = "Hello"; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2834 | static const InitFrame expected1[] = { |
| 2835 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "H"}}; |
| 2836 | static const InitFrame expected2[] = { |
| 2837 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "e"}, |
| 2838 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "l"}, |
| 2839 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "l"}, |
| 2840 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, MASKED, "o"}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2841 | CompletionCallback write_callback; |
| 2842 | |
| 2843 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2844 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2845 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
| 2846 | { |
| 2847 | InSequence s; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2848 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected1), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2849 | .WillOnce(DoAll(SaveArg<1>(&write_callback), Return(ERR_IO_PENDING))); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2850 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected2), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2851 | .WillOnce(Return(ERR_IO_PENDING)); |
| 2852 | } |
| 2853 | |
| 2854 | CreateChannelAndConnectSuccessfully(); |
| 2855 | for (size_t i = 0; i < strlen(input_letters); ++i) { |
| 2856 | channel_->SendFrame(true, |
| 2857 | WebSocketFrameHeader::kOpCodeText, |
| 2858 | std::vector<char>(1, input_letters[i])); |
| 2859 | } |
| 2860 | write_callback.Run(OK); |
| 2861 | } |
| 2862 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 2863 | // When the renderer sends more on a channel than it has quota for, we send the |
| 2864 | // remote server a kWebSocketErrorGoingAway error code. |
| 2865 | TEST_F(WebSocketChannelStreamTest, SendGoingAwayOnRendererQuotaExceeded) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2866 | static const InitFrame expected[] = { |
| 2867 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 2868 | MASKED, CLOSE_DATA(GOING_AWAY, "")}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2869 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2870 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2871 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2872 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2873 | .WillOnce(Return(OK)); |
| 2874 | EXPECT_CALL(*mock_stream_, Close()); |
| 2875 | |
| 2876 | CreateChannelAndConnectSuccessfully(); |
| 2877 | channel_->SendFrame(true, |
| 2878 | WebSocketFrameHeader::kOpCodeText, |
| 2879 | std::vector<char>(kDefaultInitialQuota + 1, 'C')); |
| 2880 | } |
| 2881 | |
| 2882 | // For convenience, most of these tests use Text frames. However, the WebSocket |
| 2883 | // protocol also has Binary frames and those need to be 8-bit clean. For the |
| 2884 | // sake of completeness, this test verifies that they are. |
| 2885 | TEST_F(WebSocketChannelStreamTest, WrittenBinaryFramesAre8BitClean) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2886 | std::vector<std::unique_ptr<WebSocketFrame>>* frames = nullptr; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2887 | |
| 2888 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 2889 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2890 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)).WillOnce(Return(ERR_IO_PENDING)); |
| 2891 | EXPECT_CALL(*mock_stream_, WriteFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2892 | .WillOnce(DoAll(SaveArg<0>(&frames), Return(ERR_IO_PENDING))); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2893 | |
| 2894 | CreateChannelAndConnectSuccessfully(); |
| 2895 | channel_->SendFrame( |
| 2896 | true, |
| 2897 | WebSocketFrameHeader::kOpCodeBinary, |
| 2898 | std::vector<char>(kBinaryBlob, kBinaryBlob + kBinaryBlobSize)); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2899 | ASSERT_TRUE(frames != nullptr); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2900 | ASSERT_EQ(1U, frames->size()); |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 2901 | const WebSocketFrame* out_frame = (*frames)[0].get(); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2902 | EXPECT_EQ(kBinaryBlobSize, out_frame->header.payload_length); |
dcheng | b206dc41 | 2014-08-26 19:46:23 | [diff] [blame] | 2903 | ASSERT_TRUE(out_frame->data.get()); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2904 | EXPECT_EQ(0, memcmp(kBinaryBlob, out_frame->data->data(), kBinaryBlobSize)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | // Test the read path for 8-bit cleanliness as well. |
| 2908 | TEST_F(WebSocketChannelEventInterfaceTest, ReadBinaryFramesAre8BitClean) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2909 | std::unique_ptr<WebSocketFrame> frame( |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 2910 | new WebSocketFrame(WebSocketFrameHeader::kOpCodeBinary)); |
| 2911 | WebSocketFrameHeader& frame_header = frame->header; |
| 2912 | frame_header.final = true; |
| 2913 | frame_header.payload_length = kBinaryBlobSize; |
| 2914 | frame->data = new IOBuffer(kBinaryBlobSize); |
| 2915 | memcpy(frame->data->data(), kBinaryBlob, kBinaryBlobSize); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2916 | std::vector<std::unique_ptr<WebSocketFrame>> frames; |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2917 | frames.push_back(std::move(frame)); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 2918 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2919 | new ReadableFakeWebSocketStream); |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 2920 | stream->PrepareRawReadFrames(ReadableFakeWebSocketStream::SYNC, OK, |
| 2921 | std::move(frames)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 2922 | set_stream(std::move(stream)); |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 2923 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 2924 | EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
| 2925 | EXPECT_CALL(*event_interface_, |
| 2926 | OnDataFrame(true, |
| 2927 | WebSocketFrameHeader::kOpCodeBinary, |
| 2928 | std::vector<char>(kBinaryBlob, |
| 2929 | kBinaryBlob + kBinaryBlobSize))); |
| 2930 | |
| 2931 | CreateChannelAndConnectSuccessfully(); |
| 2932 | } |
| 2933 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 2934 | // Invalid UTF-8 is not permitted in Text frames. |
| 2935 | TEST_F(WebSocketChannelSendUtf8Test, InvalidUtf8Rejected) { |
| 2936 | EXPECT_CALL( |
| 2937 | *event_interface_, |
| 2938 | OnFailChannel("Browser sent a text frame containing invalid UTF-8")); |
| 2939 | |
| 2940 | CreateChannelAndConnectSuccessfully(); |
| 2941 | |
| 2942 | channel_->SendFrame( |
| 2943 | true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff")); |
| 2944 | } |
| 2945 | |
| 2946 | // A Text message cannot end with a partial UTF-8 character. |
| 2947 | TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInFinalFrame) { |
| 2948 | EXPECT_CALL( |
| 2949 | *event_interface_, |
| 2950 | OnFailChannel("Browser sent a text frame containing invalid UTF-8")); |
| 2951 | |
| 2952 | CreateChannelAndConnectSuccessfully(); |
| 2953 | |
| 2954 | channel_->SendFrame( |
| 2955 | true, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2")); |
| 2956 | } |
| 2957 | |
| 2958 | // A non-final Text frame may end with a partial UTF-8 character (compare to |
| 2959 | // previous test). |
| 2960 | TEST_F(WebSocketChannelSendUtf8Test, IncompleteCharacterInNonFinalFrame) { |
| 2961 | CreateChannelAndConnectSuccessfully(); |
| 2962 | |
| 2963 | channel_->SendFrame( |
| 2964 | false, WebSocketFrameHeader::kOpCodeText, AsVector("\xc2")); |
| 2965 | } |
| 2966 | |
| 2967 | // UTF-8 parsing context must be retained between frames. |
| 2968 | TEST_F(WebSocketChannelSendUtf8Test, ValidCharacterSplitBetweenFrames) { |
| 2969 | CreateChannelAndConnectSuccessfully(); |
| 2970 | |
| 2971 | channel_->SendFrame( |
| 2972 | false, WebSocketFrameHeader::kOpCodeText, AsVector("\xf1")); |
| 2973 | channel_->SendFrame(true, |
| 2974 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2975 | AsVector("\x80\xa0\xbf")); |
| 2976 | } |
| 2977 | |
| 2978 | // Similarly, an invalid character should be detected even if split. |
| 2979 | TEST_F(WebSocketChannelSendUtf8Test, InvalidCharacterSplit) { |
| 2980 | EXPECT_CALL( |
| 2981 | *event_interface_, |
| 2982 | OnFailChannel("Browser sent a text frame containing invalid UTF-8")); |
| 2983 | |
| 2984 | CreateChannelAndConnectSuccessfully(); |
| 2985 | |
| 2986 | channel_->SendFrame( |
| 2987 | false, WebSocketFrameHeader::kOpCodeText, AsVector("\xe1")); |
| 2988 | channel_->SendFrame(true, |
| 2989 | WebSocketFrameHeader::kOpCodeContinuation, |
| 2990 | AsVector("\x80\xa0\xbf")); |
| 2991 | } |
| 2992 | |
| 2993 | // An invalid character must be detected in continuation frames. |
| 2994 | TEST_F(WebSocketChannelSendUtf8Test, InvalidByteInContinuation) { |
| 2995 | EXPECT_CALL( |
| 2996 | *event_interface_, |
| 2997 | OnFailChannel("Browser sent a text frame containing invalid UTF-8")); |
| 2998 | |
| 2999 | CreateChannelAndConnectSuccessfully(); |
| 3000 | |
| 3001 | channel_->SendFrame( |
| 3002 | false, WebSocketFrameHeader::kOpCodeText, AsVector("foo")); |
| 3003 | channel_->SendFrame( |
| 3004 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar")); |
| 3005 | channel_->SendFrame( |
| 3006 | true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff")); |
| 3007 | } |
| 3008 | |
| 3009 | // However, continuation frames of a Binary frame will not be tested for UTF-8 |
| 3010 | // validity. |
| 3011 | TEST_F(WebSocketChannelSendUtf8Test, BinaryContinuationNotChecked) { |
| 3012 | CreateChannelAndConnectSuccessfully(); |
| 3013 | |
| 3014 | channel_->SendFrame( |
| 3015 | false, WebSocketFrameHeader::kOpCodeBinary, AsVector("foo")); |
| 3016 | channel_->SendFrame( |
| 3017 | false, WebSocketFrameHeader::kOpCodeContinuation, AsVector("bar")); |
| 3018 | channel_->SendFrame( |
| 3019 | true, WebSocketFrameHeader::kOpCodeContinuation, AsVector("\xff")); |
| 3020 | } |
| 3021 | |
| 3022 | // Multiple text messages can be validated without the validation state getting |
| 3023 | // confused. |
| 3024 | TEST_F(WebSocketChannelSendUtf8Test, ValidateMultipleTextMessages) { |
| 3025 | CreateChannelAndConnectSuccessfully(); |
| 3026 | |
| 3027 | channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("foo")); |
| 3028 | channel_->SendFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector("bar")); |
| 3029 | } |
| 3030 | |
| 3031 | // UTF-8 validation is enforced on received Text frames. |
| 3032 | TEST_F(WebSocketChannelEventInterfaceTest, ReceivedInvalidUtf8) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3033 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 3034 | new ReadableFakeWebSocketStream); |
| 3035 | static const InitFrame frames[] = { |
| 3036 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}}; |
| 3037 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3038 | set_stream(std::move(stream)); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 3039 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 3040 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 3041 | EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); |
| 3042 | EXPECT_CALL(*event_interface_, |
| 3043 | OnFailChannel("Could not decode a text frame as UTF-8.")); |
| 3044 | |
| 3045 | CreateChannelAndConnectSuccessfully(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame^] | 3046 | base::RunLoop().RunUntilIdle(); |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | // Invalid UTF-8 is not sent over the network. |
| 3050 | TEST_F(WebSocketChannelStreamTest, InvalidUtf8TextFrameNotSent) { |
| 3051 | static const InitFrame expected[] = {{FINAL_FRAME, |
| 3052 | WebSocketFrameHeader::kOpCodeClose, |
| 3053 | MASKED, CLOSE_DATA(GOING_AWAY, "")}}; |
| 3054 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 3055 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 3056 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3057 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3058 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3059 | .WillOnce(Return(OK)); |
| 3060 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3061 | |
| 3062 | CreateChannelAndConnectSuccessfully(); |
| 3063 | |
| 3064 | channel_->SendFrame( |
| 3065 | true, WebSocketFrameHeader::kOpCodeText, AsVector("\xff")); |
| 3066 | } |
| 3067 | |
| 3068 | // The rest of the tests for receiving invalid UTF-8 test the communication with |
| 3069 | // the server. Since there is only one code path, it would be redundant to |
| 3070 | // perform the same tests on the EventInterface as well. |
| 3071 | |
| 3072 | // If invalid UTF-8 is received in a Text frame, the connection is failed. |
| 3073 | TEST_F(WebSocketChannelReceiveUtf8Test, InvalidTextFrameRejected) { |
| 3074 | static const InitFrame frames[] = { |
| 3075 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xff"}}; |
| 3076 | static const InitFrame expected[] = { |
| 3077 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, |
| 3078 | CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}}; |
| 3079 | { |
| 3080 | InSequence s; |
| 3081 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3082 | .WillOnce(ReturnFrames(&frames)) |
| 3083 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3084 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3085 | .WillOnce(Return(OK)); |
| 3086 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3087 | } |
| 3088 | |
| 3089 | CreateChannelAndConnectSuccessfully(); |
| 3090 | } |
| 3091 | |
| 3092 | // A received Text message is not permitted to end with a partial UTF-8 |
| 3093 | // character. |
| 3094 | TEST_F(WebSocketChannelReceiveUtf8Test, IncompleteCharacterReceived) { |
| 3095 | static const InitFrame frames[] = { |
| 3096 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"}}; |
| 3097 | static const InitFrame expected[] = { |
| 3098 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, |
| 3099 | CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}}; |
| 3100 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3101 | .WillOnce(ReturnFrames(&frames)) |
| 3102 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3103 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3104 | .WillOnce(Return(OK)); |
| 3105 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3106 | |
| 3107 | CreateChannelAndConnectSuccessfully(); |
| 3108 | } |
| 3109 | |
| 3110 | // However, a non-final Text frame may end with a partial UTF-8 character. |
| 3111 | TEST_F(WebSocketChannelReceiveUtf8Test, IncompleteCharacterIncompleteMessage) { |
| 3112 | static const InitFrame frames[] = { |
| 3113 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"}}; |
| 3114 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3115 | .WillOnce(ReturnFrames(&frames)) |
| 3116 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3117 | |
| 3118 | CreateChannelAndConnectSuccessfully(); |
| 3119 | } |
| 3120 | |
| 3121 | // However, it will become an error if it is followed by an empty final frame. |
| 3122 | TEST_F(WebSocketChannelReceiveUtf8Test, TricksyIncompleteCharacter) { |
| 3123 | static const InitFrame frames[] = { |
| 3124 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xc2"}, |
| 3125 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}}; |
| 3126 | static const InitFrame expected[] = { |
| 3127 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, |
| 3128 | CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}}; |
| 3129 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3130 | .WillOnce(ReturnFrames(&frames)) |
| 3131 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3132 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3133 | .WillOnce(Return(OK)); |
| 3134 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3135 | |
| 3136 | CreateChannelAndConnectSuccessfully(); |
| 3137 | } |
| 3138 | |
| 3139 | // UTF-8 parsing context must be retained between received frames of the same |
| 3140 | // message. |
| 3141 | TEST_F(WebSocketChannelReceiveUtf8Test, ReceivedParsingContextRetained) { |
| 3142 | static const InitFrame frames[] = { |
| 3143 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xf1"}, |
| 3144 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3145 | NOT_MASKED, "\x80\xa0\xbf"}}; |
| 3146 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3147 | .WillOnce(ReturnFrames(&frames)) |
| 3148 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3149 | |
| 3150 | CreateChannelAndConnectSuccessfully(); |
| 3151 | } |
| 3152 | |
| 3153 | // An invalid character must be detected even if split between frames. |
| 3154 | TEST_F(WebSocketChannelReceiveUtf8Test, SplitInvalidCharacterReceived) { |
| 3155 | static const InitFrame frames[] = { |
| 3156 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "\xe1"}, |
| 3157 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3158 | NOT_MASKED, "\x80\xa0\xbf"}}; |
| 3159 | static const InitFrame expected[] = { |
| 3160 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, |
| 3161 | CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}}; |
| 3162 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3163 | .WillOnce(ReturnFrames(&frames)) |
| 3164 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3165 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3166 | .WillOnce(Return(OK)); |
| 3167 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3168 | |
| 3169 | CreateChannelAndConnectSuccessfully(); |
| 3170 | } |
| 3171 | |
| 3172 | // An invalid character received in a continuation frame must be detected. |
| 3173 | TEST_F(WebSocketChannelReceiveUtf8Test, InvalidReceivedIncontinuation) { |
| 3174 | static const InitFrame frames[] = { |
| 3175 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "foo"}, |
| 3176 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3177 | NOT_MASKED, "bar"}, |
| 3178 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3179 | NOT_MASKED, "\xff"}}; |
| 3180 | static const InitFrame expected[] = { |
| 3181 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, MASKED, |
| 3182 | CLOSE_DATA(PROTOCOL_ERROR, "Invalid UTF-8 in text frame")}}; |
| 3183 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3184 | .WillOnce(ReturnFrames(&frames)) |
| 3185 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3186 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3187 | .WillOnce(Return(OK)); |
| 3188 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3189 | |
| 3190 | CreateChannelAndConnectSuccessfully(); |
| 3191 | } |
| 3192 | |
| 3193 | // Continuations of binary frames must not be tested for UTF-8 validity. |
| 3194 | TEST_F(WebSocketChannelReceiveUtf8Test, ReceivedBinaryNotUtf8Tested) { |
| 3195 | static const InitFrame frames[] = { |
| 3196 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary, NOT_MASKED, "foo"}, |
| 3197 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3198 | NOT_MASKED, "bar"}, |
| 3199 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3200 | NOT_MASKED, "\xff"}}; |
| 3201 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3202 | .WillOnce(ReturnFrames(&frames)) |
| 3203 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3204 | |
| 3205 | CreateChannelAndConnectSuccessfully(); |
| 3206 | } |
| 3207 | |
| 3208 | // Multiple Text messages can be validated. |
| 3209 | TEST_F(WebSocketChannelReceiveUtf8Test, ValidateMultipleReceived) { |
| 3210 | static const InitFrame frames[] = { |
| 3211 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "foo"}, |
| 3212 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "bar"}}; |
| 3213 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3214 | .WillOnce(ReturnFrames(&frames)) |
| 3215 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3216 | |
| 3217 | CreateChannelAndConnectSuccessfully(); |
| 3218 | } |
| 3219 | |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3220 | // A new data message cannot start in the middle of another data message. |
| 3221 | TEST_F(WebSocketChannelEventInterfaceTest, BogusContinuation) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3222 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3223 | new ReadableFakeWebSocketStream); |
| 3224 | static const InitFrame frames[] = { |
| 3225 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeBinary, |
| 3226 | NOT_MASKED, "frame1"}, |
| 3227 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, |
| 3228 | NOT_MASKED, "frame2"}}; |
| 3229 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3230 | set_stream(std::move(stream)); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3231 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 3232 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3233 | EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); |
| 3234 | EXPECT_CALL( |
| 3235 | *event_interface_, |
| 3236 | OnDataFrame( |
| 3237 | false, WebSocketFrameHeader::kOpCodeBinary, AsVector("frame1"))); |
| 3238 | EXPECT_CALL( |
| 3239 | *event_interface_, |
| 3240 | OnFailChannel( |
| 3241 | "Received start of new message but previous message is unfinished.")); |
| 3242 | |
| 3243 | CreateChannelAndConnectSuccessfully(); |
| 3244 | } |
| 3245 | |
| 3246 | // A new message cannot start with a Continuation frame. |
| 3247 | TEST_F(WebSocketChannelEventInterfaceTest, MessageStartingWithContinuation) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3248 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3249 | new ReadableFakeWebSocketStream); |
| 3250 | static const InitFrame frames[] = { |
| 3251 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3252 | NOT_MASKED, "continuation"}}; |
| 3253 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3254 | set_stream(std::move(stream)); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3255 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 3256 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 3257 | EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); |
| 3258 | EXPECT_CALL(*event_interface_, |
| 3259 | OnFailChannel("Received unexpected continuation frame.")); |
| 3260 | |
| 3261 | CreateChannelAndConnectSuccessfully(); |
| 3262 | } |
| 3263 | |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 3264 | // A frame passed to the renderer must be either non-empty or have the final bit |
| 3265 | // set. |
| 3266 | TEST_F(WebSocketChannelEventInterfaceTest, DataFramesNonEmptyOrFinal) { |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3267 | std::unique_ptr<ReadableFakeWebSocketStream> stream( |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 3268 | new ReadableFakeWebSocketStream); |
| 3269 | static const InitFrame frames[] = { |
| 3270 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, ""}, |
| 3271 | {NOT_FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, |
| 3272 | NOT_MASKED, ""}, |
| 3273 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeContinuation, NOT_MASKED, ""}}; |
| 3274 | stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3275 | set_stream(std::move(stream)); |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 3276 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 3277 | EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 3278 | EXPECT_CALL(*event_interface_, OnFlowControl(kDefaultInitialQuota)); |
| 3279 | EXPECT_CALL( |
| 3280 | *event_interface_, |
| 3281 | OnDataFrame(true, WebSocketFrameHeader::kOpCodeText, AsVector(""))); |
| 3282 | |
| 3283 | CreateChannelAndConnectSuccessfully(); |
| 3284 | } |
| 3285 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 3286 | // Calls to OnSSLCertificateError() must be passed through to the event |
| 3287 | // interface with the correct URL attached. |
| 3288 | TEST_F(WebSocketChannelEventInterfaceTest, OnSSLCertificateErrorCalled) { |
| 3289 | const GURL wss_url("wss://example.com/sslerror"); |
| 3290 | connect_data_.socket_url = wss_url; |
| 3291 | const SSLInfo ssl_info; |
| 3292 | const bool fatal = true; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3293 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> fake_callbacks( |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 3294 | new FakeSSLErrorCallbacks); |
| 3295 | |
| 3296 | EXPECT_CALL(*event_interface_, |
| 3297 | OnSSLCertificateErrorCalled(NotNull(), wss_url, _, fatal)); |
| 3298 | |
| 3299 | CreateChannelAndConnect(); |
| 3300 | connect_data_.creator.connect_delegate->OnSSLCertificateError( |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3301 | std::move(fake_callbacks), ssl_info, fatal); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 3302 | } |
| 3303 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 3304 | // If we receive another frame after Close, it is not valid. It is not |
| 3305 | // completely clear what behaviour is required from the standard in this case, |
| 3306 | // but the current implementation fails the connection. Since a Close has |
| 3307 | // already been sent, this just means closing the connection. |
| 3308 | TEST_F(WebSocketChannelStreamTest, PingAfterCloseIsRejected) { |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 3309 | static const InitFrame frames[] = { |
| 3310 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3311 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}, |
| 3312 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodePing, |
| 3313 | NOT_MASKED, "Ping body"}}; |
| 3314 | static const InitFrame expected[] = { |
| 3315 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3316 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 3317 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 3318 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 3319 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 3320 | .WillOnce(ReturnFrames(&frames)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 3321 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3322 | { |
| 3323 | // We only need to verify the relative order of WriteFrames() and |
| 3324 | // Close(). The current implementation calls WriteFrames() for the Close |
| 3325 | // frame before calling ReadFrames() again, but that is an implementation |
| 3326 | // detail and better not to consider required behaviour. |
| 3327 | InSequence s; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 3328 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 3329 | .WillOnce(Return(OK)); |
| 3330 | EXPECT_CALL(*mock_stream_, Close()).Times(1); |
| 3331 | } |
| 3332 | |
| 3333 | CreateChannelAndConnectSuccessfully(); |
| 3334 | } |
| 3335 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 3336 | // A protocol error from the remote server should result in a close frame with |
| 3337 | // status 1002, followed by the connection closing. |
| 3338 | TEST_F(WebSocketChannelStreamTest, ProtocolError) { |
| 3339 | static const InitFrame expected[] = { |
| 3340 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3341 | MASKED, CLOSE_DATA(PROTOCOL_ERROR, "WebSocket Protocol Error")}}; |
| 3342 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 3343 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 3344 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3345 | .WillOnce(Return(ERR_WS_PROTOCOL_ERROR)); |
| 3346 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3347 | .WillOnce(Return(OK)); |
| 3348 | EXPECT_CALL(*mock_stream_, Close()); |
| 3349 | |
| 3350 | CreateChannelAndConnectSuccessfully(); |
| 3351 | } |
| 3352 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3353 | // Set the closing handshake timeout to a very tiny value before connecting. |
| 3354 | class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest { |
| 3355 | protected: |
| 3356 | WebSocketChannelStreamTimeoutTest() {} |
| 3357 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3358 | void CreateChannelAndConnectSuccessfully() override { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3359 | set_stream(std::move(mock_stream_)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3360 | CreateChannelAndConnect(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 3361 | ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(kPlentyOfQuota)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3362 | channel_->SetClosingHandshakeTimeoutForTesting( |
| 3363 | TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
tyoshino | d4d1d30 | 2014-11-07 04:31:16 | [diff] [blame] | 3364 | channel_->SetUnderlyingConnectionCloseTimeoutForTesting( |
| 3365 | TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3366 | connect_data_.creator.connect_delegate->OnSuccess(std::move(stream_)); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3367 | } |
| 3368 | }; |
| 3369 | |
| 3370 | // In this case the server initiates the closing handshake with a Close |
| 3371 | // message. WebSocketChannel responds with a matching Close message, and waits |
| 3372 | // for the server to close the TCP/IP connection. The server never closes the |
| 3373 | // connection, so the closing handshake times out and WebSocketChannel closes |
| 3374 | // the connection itself. |
| 3375 | TEST_F(WebSocketChannelStreamTimeoutTest, ServerInitiatedCloseTimesOut) { |
| 3376 | static const InitFrame frames[] = { |
| 3377 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3378 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 3379 | static const InitFrame expected[] = { |
| 3380 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3381 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 3382 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 3383 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3384 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3385 | .WillOnce(ReturnFrames(&frames)) |
| 3386 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 3387 | Checkpoint checkpoint; |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3388 | TestClosure completion; |
| 3389 | { |
| 3390 | InSequence s; |
| 3391 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3392 | .WillOnce(Return(OK)); |
| 3393 | EXPECT_CALL(checkpoint, Call(1)); |
| 3394 | EXPECT_CALL(*mock_stream_, Close()) |
| 3395 | .WillOnce(InvokeClosure(completion.closure())); |
| 3396 | } |
| 3397 | |
| 3398 | CreateChannelAndConnectSuccessfully(); |
| 3399 | checkpoint.Call(1); |
| 3400 | completion.WaitForResult(); |
| 3401 | } |
| 3402 | |
| 3403 | // In this case the client initiates the closing handshake by sending a Close |
| 3404 | // message. WebSocketChannel waits for a Close message in response from the |
| 3405 | // server. The server never responds to the Close message, so the closing |
| 3406 | // handshake times out and WebSocketChannel closes the connection. |
| 3407 | TEST_F(WebSocketChannelStreamTimeoutTest, ClientInitiatedCloseTimesOut) { |
| 3408 | static const InitFrame expected[] = { |
| 3409 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3410 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 3411 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 3412 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3413 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3414 | .WillRepeatedly(Return(ERR_IO_PENDING)); |
| 3415 | TestClosure completion; |
| 3416 | { |
| 3417 | InSequence s; |
| 3418 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3419 | .WillOnce(Return(OK)); |
| 3420 | EXPECT_CALL(*mock_stream_, Close()) |
| 3421 | .WillOnce(InvokeClosure(completion.closure())); |
| 3422 | } |
| 3423 | |
| 3424 | CreateChannelAndConnectSuccessfully(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 3425 | ASSERT_EQ(CHANNEL_ALIVE, |
| 3426 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK")); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3427 | completion.WaitForResult(); |
| 3428 | } |
| 3429 | |
| 3430 | // In this case the client initiates the closing handshake and the server |
| 3431 | // responds with a matching Close message. WebSocketChannel waits for the server |
| 3432 | // to close the TCP/IP connection, but it never does. The closing handshake |
| 3433 | // times out and WebSocketChannel closes the connection. |
| 3434 | TEST_F(WebSocketChannelStreamTimeoutTest, ConnectionCloseTimesOut) { |
| 3435 | static const InitFrame expected[] = { |
| 3436 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3437 | MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 3438 | static const InitFrame frames[] = { |
| 3439 | {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, |
| 3440 | NOT_MASKED, CLOSE_DATA(NORMAL_CLOSURE, "OK")}}; |
| 3441 | EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
[email protected] | 6c5d9f6 | 2014-01-27 15:05:21 | [diff] [blame] | 3442 | EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3443 | TestClosure completion; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3444 | std::vector<std::unique_ptr<WebSocketFrame>>* read_frames = nullptr; |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3445 | CompletionCallback read_callback; |
| 3446 | { |
| 3447 | InSequence s; |
| 3448 | // Copy the arguments to ReadFrames so that the test can call the callback |
| 3449 | // after it has send the close message. |
| 3450 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3451 | .WillOnce(DoAll(SaveArg<0>(&read_frames), |
| 3452 | SaveArg<1>(&read_callback), |
| 3453 | Return(ERR_IO_PENDING))); |
| 3454 | // The first real event that happens is the client sending the Close |
| 3455 | // message. |
| 3456 | EXPECT_CALL(*mock_stream_, WriteFrames(EqualsFrames(expected), _)) |
| 3457 | .WillOnce(Return(OK)); |
| 3458 | // The |read_frames| callback is called (from this test case) at this |
| 3459 | // point. ReadFrames is called again by WebSocketChannel, waiting for |
| 3460 | // ERR_CONNECTION_CLOSED. |
| 3461 | EXPECT_CALL(*mock_stream_, ReadFrames(_, _)) |
| 3462 | .WillOnce(Return(ERR_IO_PENDING)); |
| 3463 | // The timeout happens and so WebSocketChannel closes the stream. |
| 3464 | EXPECT_CALL(*mock_stream_, Close()) |
| 3465 | .WillOnce(InvokeClosure(completion.closure())); |
| 3466 | } |
| 3467 | |
| 3468 | CreateChannelAndConnectSuccessfully(); |
yhirano | d2727df | 2016-04-11 05:32:49 | [diff] [blame] | 3469 | ASSERT_EQ(CHANNEL_ALIVE, |
| 3470 | channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK")); |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 3471 | ASSERT_TRUE(read_frames); |
| 3472 | // Provide the "Close" message from the server. |
| 3473 | *read_frames = CreateFrameVector(frames); |
| 3474 | read_callback.Run(OK); |
| 3475 | completion.WaitForResult(); |
| 3476 | } |
| 3477 | |
ricea | 5858a505 | 2016-01-06 04:57:38 | [diff] [blame] | 3478 | // Verify that current_send_quota() returns a non-zero value for a newly |
| 3479 | // connected channel. |
| 3480 | TEST_F(WebSocketChannelTest, CurrentSendQuotaNonZero) { |
| 3481 | CreateChannelAndConnectSuccessfully(); |
| 3482 | EXPECT_GT(channel_->current_send_quota(), 0); |
| 3483 | } |
| 3484 | |
| 3485 | // Verify that current_send_quota() is updated when SendFrame() is called. |
| 3486 | TEST_F(WebSocketChannelTest, CurrentSendQuotaUpdated) { |
| 3487 | const int kMessageSize = 5; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 3488 | set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
ricea | 5858a505 | 2016-01-06 04:57:38 | [diff] [blame] | 3489 | CreateChannelAndConnectSuccessfully(); |
| 3490 | |
| 3491 | int initial_send_quota = channel_->current_send_quota(); |
| 3492 | EXPECT_GE(initial_send_quota, kMessageSize); |
| 3493 | |
| 3494 | channel_->SendFrame( |
| 3495 | true, WebSocketFrameHeader::kOpCodeText, |
| 3496 | std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); |
| 3497 | int new_send_quota = channel_->current_send_quota(); |
| 3498 | EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); |
| 3499 | } |
| 3500 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 3501 | } // namespace |
| 3502 | } // namespace net |