OLD | NEW |
(Empty) | |
| 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 |
| 7 #include "net/socket/next_proto.h" |
| 8 #include "net/socket/socket_test_util.h" |
| 9 #include "net/socket/stream_socket.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // A SocketStream implementation with no functionality at all. |
| 17 // TODO(ricea): If you need to use this in another file, please move it to |
| 18 // socket_test_util.h. |
| 19 class FakeStreamSocket : public StreamSocket { |
| 20 public: |
| 21 FakeStreamSocket() {} |
| 22 |
| 23 // SocketStream implementation |
| 24 virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
| 25 return ERR_FAILED; |
| 26 } |
| 27 |
| 28 virtual void Disconnect() OVERRIDE { return; } |
| 29 |
| 30 virtual bool IsConnected() const OVERRIDE { return false; } |
| 31 |
| 32 virtual bool IsConnectedAndIdle() const OVERRIDE { return false; } |
| 33 |
| 34 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
| 35 return ERR_FAILED; |
| 36 } |
| 37 |
| 38 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
| 39 return ERR_FAILED; |
| 40 } |
| 41 |
| 42 virtual const BoundNetLog& NetLog() const OVERRIDE { return bound_net_log_; } |
| 43 |
| 44 virtual void SetSubresourceSpeculation() OVERRIDE { return; } |
| 45 virtual void SetOmniboxSpeculation() OVERRIDE { return; } |
| 46 |
| 47 virtual bool WasEverUsed() const OVERRIDE { return false; } |
| 48 |
| 49 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
| 50 |
| 51 virtual bool WasNpnNegotiated() const OVERRIDE { return false; } |
| 52 |
| 53 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
| 54 return kProtoUnknown; |
| 55 } |
| 56 |
| 57 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } |
| 58 |
| 59 // Socket implementation |
| 60 virtual int Read(IOBuffer* buf, |
| 61 int buf_len, |
| 62 const CompletionCallback& callback) OVERRIDE { |
| 63 return ERR_FAILED; |
| 64 } |
| 65 |
| 66 virtual int Write(IOBuffer* buf, |
| 67 int buf_len, |
| 68 const CompletionCallback& callback) OVERRIDE { |
| 69 return ERR_FAILED; |
| 70 } |
| 71 |
| 72 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return ERR_FAILED; } |
| 73 |
| 74 virtual int SetSendBufferSize(int32 size) OVERRIDE { return ERR_FAILED; } |
| 75 |
| 76 private: |
| 77 BoundNetLog bound_net_log_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
| 80 }; |
| 81 |
| 82 class FakeWaiter : public WebSocketEndpointLockManager::Waiter { |
| 83 public: |
| 84 FakeWaiter() : called_(false) {} |
| 85 |
| 86 virtual void GotEndpointLock() OVERRIDE { |
| 87 CHECK(!called_); |
| 88 called_ = true; |
| 89 } |
| 90 |
| 91 bool called() const { return called_; } |
| 92 |
| 93 private: |
| 94 bool called_; |
| 95 }; |
| 96 |
| 97 class WebSocketEndpointLockManagerTest : public ::testing::Test { |
| 98 protected: |
| 99 WebSocketEndpointLockManagerTest() |
| 100 : instance_(WebSocketEndpointLockManager::GetInstance()) {} |
| 101 virtual ~WebSocketEndpointLockManagerTest() { |
| 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()); |
| 117 } |
| 118 } |
| 119 |
| 120 WebSocketEndpointLockManager* const instance_; |
| 121 }; |
| 122 |
| 123 TEST_F(WebSocketEndpointLockManagerTest, GetInstanceWorks) { |
| 124 // All the work is done by the test framework. |
| 125 } |
| 126 |
| 127 TEST_F(WebSocketEndpointLockManagerTest, LockEndpointReturnsOkOnce) { |
| 128 FakeWaiter waiters[2]; |
| 129 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 130 EXPECT_EQ(ERR_IO_PENDING, |
| 131 instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 132 |
| 133 UnlockDummyEndpoint(2); |
| 134 } |
| 135 |
| 136 TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledOnOk) { |
| 137 FakeWaiter waiter; |
| 138 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiter)); |
| 139 EXPECT_FALSE(waiter.called()); |
| 140 |
| 141 UnlockDummyEndpoint(1); |
| 142 } |
| 143 |
| 144 TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledImmediately) { |
| 145 FakeWaiter waiters[2]; |
| 146 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 147 EXPECT_EQ(ERR_IO_PENDING, |
| 148 instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 149 EXPECT_FALSE(waiters[1].called()); |
| 150 |
| 151 UnlockDummyEndpoint(2); |
| 152 } |
| 153 |
| 154 TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockCalledWhenUnlocked) { |
| 155 FakeWaiter waiters[2]; |
| 156 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 157 EXPECT_EQ(ERR_IO_PENDING, |
| 158 instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 159 instance()->UnlockEndpoint(DummyEndpoint()); |
| 160 EXPECT_TRUE(waiters[1].called()); |
| 161 |
| 162 UnlockDummyEndpoint(1); |
| 163 } |
| 164 |
| 165 TEST_F(WebSocketEndpointLockManagerTest, |
| 166 EndpointUnlockedIfWaiterAlreadyDeleted) { |
| 167 FakeWaiter first_lock_holder; |
| 168 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &first_lock_holder)); |
| 169 |
| 170 { |
| 171 FakeWaiter short_lived_waiter; |
| 172 EXPECT_EQ(ERR_IO_PENDING, |
| 173 instance()->LockEndpoint(DummyEndpoint(), &short_lived_waiter)); |
| 174 } |
| 175 |
| 176 instance()->UnlockEndpoint(DummyEndpoint()); |
| 177 |
| 178 FakeWaiter second_lock_holder; |
| 179 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &second_lock_holder)); |
| 180 |
| 181 UnlockDummyEndpoint(1); |
| 182 } |
| 183 |
| 184 TEST_F(WebSocketEndpointLockManagerTest, RememberSocketWorks) { |
| 185 FakeWaiter waiters[2]; |
| 186 FakeStreamSocket dummy_socket; |
| 187 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0])); |
| 188 EXPECT_EQ(ERR_IO_PENDING, |
| 189 instance()->LockEndpoint(DummyEndpoint(), &waiters[1])); |
| 190 |
| 191 instance()->RememberSocket(&dummy_socket, DummyEndpoint()); |
| 192 instance()->UnlockSocket(&dummy_socket); |
| 193 EXPECT_TRUE(waiters[1].called()); |
| 194 |
| 195 UnlockDummyEndpoint(1); |
| 196 } |
| 197 |
| 198 // Calling UnlockSocket() on the same socket a second time should be harmless. |
| 199 TEST_F(WebSocketEndpointLockManagerTest, UnlockSocketTwice) { |
| 200 FakeWaiter waiter; |
| 201 FakeStreamSocket dummy_socket; |
| 202 EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiter)); |
| 203 instance()->RememberSocket(&dummy_socket, DummyEndpoint()); |
| 204 instance()->UnlockSocket(&dummy_socket); |
| 205 instance()->UnlockSocket(&dummy_socket); |
| 206 } |
| 207 |
| 208 } // namespace |
| 209 |
| 210 } // namespace net |
OLD | NEW |