[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 1 | // Copyright 2014 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/socket/websocket_endpoint_lock_manager.h" |
| 6 | |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 7 | #include "base/message_loop/message_loop.h" |
| 8 | #include "base/run_loop.h" |
| 9 | #include "base/time/time.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 10 | #include "net/base/net_errors.h" |
| 11 | #include "net/socket/next_proto.h" |
| 12 | #include "net/socket/socket_test_util.h" |
| 13 | #include "net/socket/stream_socket.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // A StreamSocket implementation with no functionality at all. |
| 21 | // TODO(ricea): If you need to use this in another file, please move it to |
| 22 | // socket_test_util.h. |
| 23 | class FakeStreamSocket : public StreamSocket { |
| 24 | public: |
| 25 | FakeStreamSocket() {} |
| 26 | |
| 27 | // StreamSocket implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 28 | int Connect(const CompletionCallback& callback) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 29 | return ERR_FAILED; |
| 30 | } |
| 31 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 32 | void Disconnect() override { return; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 33 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 34 | bool IsConnected() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 35 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 36 | bool IsConnectedAndIdle() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 37 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 38 | int GetPeerAddress(IPEndPoint* address) const override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 39 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 40 | int GetLocalAddress(IPEndPoint* address) const override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 41 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 42 | const BoundNetLog& NetLog() const override { return bound_net_log_; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 43 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 44 | void SetSubresourceSpeculation() override { return; } |
| 45 | void SetOmniboxSpeculation() override { return; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 46 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 47 | bool WasEverUsed() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 48 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 49 | bool UsingTCPFastOpen() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 50 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 51 | bool WasNpnNegotiated() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 52 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 53 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 54 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 55 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 56 | |
| 57 | // Socket implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 58 | int Read(IOBuffer* buf, |
| 59 | int buf_len, |
| 60 | const CompletionCallback& callback) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 61 | return ERR_FAILED; |
| 62 | } |
| 63 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 64 | int Write(IOBuffer* buf, |
| 65 | int buf_len, |
| 66 | const CompletionCallback& callback) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 67 | return ERR_FAILED; |
| 68 | } |
| 69 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 70 | int SetReceiveBufferSize(int32 size) override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 71 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 72 | int SetSendBufferSize(int32 size) override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | BoundNetLog bound_net_log_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
| 78 | }; |
| 79 | |
| 80 | class FakeWaiter : public WebSocketEndpointLockManager::Waiter { |
| 81 | public: |
| 82 | FakeWaiter() : called_(false) {} |
| 83 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 84 | void GotEndpointLock() override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 85 | CHECK(!called_); |
| 86 | called_ = true; |
| 87 | } |
| 88 | |
| 89 | bool called() const { return called_; } |
| 90 | |
| 91 | private: |
| 92 | bool called_; |
| 93 | }; |
| 94 | |
| 95 | class WebSocketEndpointLockManagerTest : public ::testing::Test { |
| 96 | protected: |
| 97 | WebSocketEndpointLockManagerTest() |
| 98 | : instance_(WebSocketEndpointLockManager::GetInstance()) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 99 | ~WebSocketEndpointLockManagerTest() override { |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 100 | // Permit any pending asynchronous unlock operations to complete. |
| 101 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 102 | // If this check fails then subsequent tests may fail. |
| 103 | CHECK(instance_->IsEmpty()); |
| 104 | } |
| 105 | |
| 106 | WebSocketEndpointLockManager* instance() const { return instance_; } |
| 107 | |
| 108 | IPEndPoint DummyEndpoint() { |
| 109 | IPAddressNumber ip_address_number; |
| 110 | CHECK(ParseIPLiteralToNumber("127.0.0.1", &ip_address_number)); |
| 111 | return IPEndPoint(ip_address_number, 80); |
| 112 | } |
| 113 | |
| 114 | void UnlockDummyEndpoint(int times) { |
| 115 | for (int i = 0; i < times; ++i) { |
| 116 | instance()->UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 117 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 121 | static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); } |
| 122 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 123 | WebSocketEndpointLockManager* const instance_; |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 124 | ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_; |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | TEST_F(WebSocketEndpointLockManagerTest, GetInstanceWorks) { |
| 128 | // All the work is done by the test framework. |
| 129 | } |
| 130 | |
| 131 | TEST_F(WebSocketEndpointLockManagerTest, LockEndpointReturnsOkOnce) { |
| 132 | FakeWaiter waiters[2]; |
| 133 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 134 | EXPECT_EQ(ERR_IO_PENDING, |
| 135 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 136 | |
| 137 | UnlockDummyEndpoint(2); |
| 138 | } |
| 139 | |
| 140 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledOnOk) { |
| 141 | FakeWaiter waiter; |
| 142 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiter)); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 143 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 144 | EXPECT_FALSE(waiter.called()); |
| 145 | |
| 146 | UnlockDummyEndpoint(1); |
| 147 | } |
| 148 | |
| 149 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledImmediately) { |
| 150 | FakeWaiter waiters[2]; |
| 151 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 152 | EXPECT_EQ(ERR_IO_PENDING, |
| 153 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 154 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 155 | EXPECT_FALSE(waiters[1].called()); |
| 156 | |
| 157 | UnlockDummyEndpoint(2); |
| 158 | } |
| 159 | |
| 160 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockCalledWhenUnlocked) { |
| 161 | FakeWaiter waiters[2]; |
| 162 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 163 | EXPECT_EQ(ERR_IO_PENDING, |
| 164 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 165 | instance()->UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 166 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 167 | EXPECT_TRUE(waiters[1].called()); |
| 168 | |
| 169 | UnlockDummyEndpoint(1); |
| 170 | } |
| 171 | |
| 172 | TEST_F(WebSocketEndpointLockManagerTest, |
| 173 | EndpointUnlockedIfWaiterAlreadyDeleted) { |
| 174 | FakeWaiter first_lock_holder; |
| 175 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &first_lock_holder)); |
| 176 | |
| 177 | { |
| 178 | FakeWaiter short_lived_waiter; |
| 179 | EXPECT_EQ(ERR_IO_PENDING, |
| 180 | instance()->LockEndpoint(DummyEndpoint(), &short_lived_waiter)); |
| 181 | } |
| 182 | |
| 183 | instance()->UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 184 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 185 | |
| 186 | FakeWaiter second_lock_holder; |
| 187 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &second_lock_holder)); |
| 188 | |
| 189 | UnlockDummyEndpoint(1); |
| 190 | } |
| 191 | |
| 192 | TEST_F(WebSocketEndpointLockManagerTest, RememberSocketWorks) { |
| 193 | FakeWaiter waiters[2]; |
| 194 | FakeStreamSocket dummy_socket; |
| 195 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 196 | EXPECT_EQ(ERR_IO_PENDING, |
| 197 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 198 | |
| 199 | instance()->RememberSocket(&dummy_socket, DummyEndpoint()); |
| 200 | instance()->UnlockSocket(&dummy_socket); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 201 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 202 | EXPECT_TRUE(waiters[1].called()); |
| 203 | |
| 204 | UnlockDummyEndpoint(1); |
| 205 | } |
| 206 | |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 207 | // UnlockEndpoint() should cause any sockets remembered for this endpoint |
| 208 | // to be forgotten. |
| 209 | TEST_F(WebSocketEndpointLockManagerTest, SocketAssociationForgottenOnUnlock) { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 210 | FakeWaiter waiter; |
| 211 | FakeStreamSocket dummy_socket; |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 212 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 213 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiter)); |
| 214 | instance()->RememberSocket(&dummy_socket, DummyEndpoint()); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 215 | instance()->UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 216 | RunUntilIdle(); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 217 | EXPECT_TRUE(instance()->IsEmpty()); |
| 218 | } |
| 219 | |
| 220 | // When ownership of the endpoint is passed to a new waiter, the new waiter can |
| 221 | // call RememberSocket() again. |
| 222 | TEST_F(WebSocketEndpointLockManagerTest, NextWaiterCanCallRememberSocketAgain) { |
| 223 | FakeWaiter waiters[2]; |
| 224 | FakeStreamSocket dummy_sockets[2]; |
| 225 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 226 | EXPECT_EQ(ERR_IO_PENDING, |
| 227 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 228 | |
| 229 | instance()->RememberSocket(&dummy_sockets[0], DummyEndpoint()); |
| 230 | instance()->UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 231 | RunUntilIdle(); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 232 | EXPECT_TRUE(waiters[1].called()); |
| 233 | instance()->RememberSocket(&dummy_sockets[1], DummyEndpoint()); |
| 234 | |
| 235 | UnlockDummyEndpoint(1); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 236 | } |
| 237 | |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 238 | // Calling UnlockSocket() after UnlockEndpoint() does nothing. |
| 239 | TEST_F(WebSocketEndpointLockManagerTest, |
| 240 | UnlockSocketAfterUnlockEndpointDoesNothing) { |
| 241 | FakeWaiter waiters[3]; |
| 242 | FakeStreamSocket dummy_socket; |
| 243 | |
| 244 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 245 | EXPECT_EQ(ERR_IO_PENDING, |
| 246 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 247 | EXPECT_EQ(ERR_IO_PENDING, |
| 248 | instance()->LockEndpoint(DummyEndpoint(), &waiters[2])); |
| 249 | instance()->RememberSocket(&dummy_socket, DummyEndpoint()); |
| 250 | instance()->UnlockEndpoint(DummyEndpoint()); |
| 251 | instance()->UnlockSocket(&dummy_socket); |
| 252 | RunUntilIdle(); |
| 253 | EXPECT_TRUE(waiters[1].called()); |
| 254 | EXPECT_FALSE(waiters[2].called()); |
| 255 | |
| 256 | UnlockDummyEndpoint(2); |
| 257 | } |
| 258 | |
| 259 | // UnlockEndpoint() should always be asynchronous. |
| 260 | TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsAsynchronous) { |
| 261 | FakeWaiter waiters[2]; |
| 262 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 263 | EXPECT_EQ(ERR_IO_PENDING, |
| 264 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 265 | |
| 266 | instance()->UnlockEndpoint(DummyEndpoint()); |
| 267 | EXPECT_FALSE(waiters[1].called()); |
| 268 | RunUntilIdle(); |
| 269 | EXPECT_TRUE(waiters[1].called()); |
| 270 | |
| 271 | UnlockDummyEndpoint(1); |
| 272 | } |
| 273 | |
| 274 | // UnlockEndpoint() should normally have a delay. |
| 275 | TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsDelayed) { |
| 276 | const base::TimeDelta one_millisecond = base::TimeDelta::FromMilliseconds(1); |
| 277 | instance()->SetUnlockDelayForTesting(one_millisecond); |
| 278 | FakeWaiter waiters[2]; |
| 279 | EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 280 | EXPECT_EQ(ERR_IO_PENDING, |
| 281 | instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 282 | |
| 283 | instance()->UnlockEndpoint(DummyEndpoint()); |
| 284 | RunUntilIdle(); |
| 285 | EXPECT_FALSE(waiters[1].called()); |
| 286 | base::RunLoop run_loop; |
| 287 | base::MessageLoop::current()->PostDelayedTask( |
| 288 | FROM_HERE, run_loop.QuitClosure(), one_millisecond); |
| 289 | run_loop.Run(); |
| 290 | EXPECT_TRUE(waiters[1].called()); |
| 291 | instance()->SetUnlockDelayForTesting(base::TimeDelta()); |
| 292 | UnlockDummyEndpoint(1); |
| 293 | } |
| 294 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 295 | } // namespace |
| 296 | |
| 297 | } // namespace net |