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