OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket_stream/socket_stream.h" | 5 #include "net/socket_stream/socket_stream.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 base::Callback<void(SocketStreamEvent*)> on_close_; | 174 base::Callback<void(SocketStreamEvent*)> on_close_; |
175 base::Callback<void(SocketStreamEvent*)> on_auth_required_; | 175 base::Callback<void(SocketStreamEvent*)> on_auth_required_; |
176 base::Callback<void(SocketStreamEvent*)> on_error_; | 176 base::Callback<void(SocketStreamEvent*)> on_error_; |
177 const CompletionCallback callback_; | 177 const CompletionCallback callback_; |
178 CompletionCallback connection_callback_; | 178 CompletionCallback connection_callback_; |
179 AuthCredentials credentials_; | 179 AuthCredentials credentials_; |
180 | 180 |
181 DISALLOW_COPY_AND_ASSIGN(SocketStreamEventRecorder); | 181 DISALLOW_COPY_AND_ASSIGN(SocketStreamEventRecorder); |
182 }; | 182 }; |
183 | 183 |
| 184 // This is used for the test OnErrorDetachDelegate. |
| 185 class SelfDeletingDelegate : public SocketStream::Delegate { |
| 186 public: |
| 187 // |callback| must cause the test message loop to exit when called. |
| 188 explicit SelfDeletingDelegate(const CompletionCallback& callback) |
| 189 : socket_stream_(), callback_(callback) {} |
| 190 |
| 191 virtual ~SelfDeletingDelegate() {} |
| 192 |
| 193 // Call DetachDelegate(), delete |this|, then run the callback. |
| 194 virtual void OnError(const SocketStream* socket, int error) OVERRIDE { |
| 195 // callback_ will be deleted when we delete |this|, so copy it to call it |
| 196 // afterwards. |
| 197 CompletionCallback callback = callback_; |
| 198 socket_stream_->DetachDelegate(); |
| 199 delete this; |
| 200 callback.Run(OK); |
| 201 } |
| 202 |
| 203 // This can't be passed in the constructor because this object needs to be |
| 204 // created before SocketStream. |
| 205 void set_socket_stream(const scoped_refptr<SocketStream>& socket_stream) { |
| 206 socket_stream_ = socket_stream; |
| 207 EXPECT_EQ(socket_stream_->delegate(), this); |
| 208 } |
| 209 |
| 210 virtual void OnConnected(SocketStream* socket, int max_pending_send_allowed) |
| 211 OVERRIDE { |
| 212 ADD_FAILURE() << "OnConnected() should not be called"; |
| 213 } |
| 214 virtual void OnSentData(SocketStream* socket, int amount_sent) OVERRIDE { |
| 215 ADD_FAILURE() << "OnSentData() should not be called"; |
| 216 } |
| 217 virtual void OnReceivedData(SocketStream* socket, const char* data, int len) |
| 218 OVERRIDE { |
| 219 ADD_FAILURE() << "OnReceivedData() should not be called"; |
| 220 } |
| 221 virtual void OnClose(SocketStream* socket) OVERRIDE { |
| 222 ADD_FAILURE() << "OnClose() should not be called"; |
| 223 } |
| 224 |
| 225 private: |
| 226 scoped_refptr<SocketStream> socket_stream_; |
| 227 const CompletionCallback callback_; |
| 228 |
| 229 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDelegate); |
| 230 }; |
| 231 |
184 class TestURLRequestContextWithProxy : public TestURLRequestContext { | 232 class TestURLRequestContextWithProxy : public TestURLRequestContext { |
185 public: | 233 public: |
186 explicit TestURLRequestContextWithProxy(const std::string& proxy) | 234 explicit TestURLRequestContextWithProxy(const std::string& proxy) |
187 : TestURLRequestContext(true) { | 235 : TestURLRequestContext(true) { |
188 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); | 236 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
189 Init(); | 237 Init(); |
190 } | 238 } |
191 virtual ~TestURLRequestContextWithProxy() {} | 239 virtual ~TestURLRequestContextWithProxy() {} |
192 }; | 240 }; |
193 | 241 |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 test_callback.WaitForResult(); | 887 test_callback.WaitForResult(); |
840 | 888 |
841 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); | 889 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); |
842 ASSERT_EQ(2U, events.size()); | 890 ASSERT_EQ(2U, events.size()); |
843 | 891 |
844 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); | 892 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); |
845 EXPECT_EQ(ERR_ACCESS_DENIED, events[0].error_code); | 893 EXPECT_EQ(ERR_ACCESS_DENIED, events[0].error_code); |
846 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); | 894 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); |
847 } | 895 } |
848 | 896 |
| 897 // Check that a connect failure, followed by the delegate calling DetachDelegate |
| 898 // and deleting itself in the OnError callback, is handled correctly. |
| 899 TEST_F(SocketStreamTest, OnErrorDetachDelegate) { |
| 900 MockClientSocketFactory mock_socket_factory; |
| 901 TestCompletionCallback test_callback; |
| 902 |
| 903 // SelfDeletingDelegate is self-owning; we just need a pointer to it to |
| 904 // connect it and the SocketStream. |
| 905 SelfDeletingDelegate* delegate = |
| 906 new SelfDeletingDelegate(test_callback.callback()); |
| 907 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 908 StaticSocketDataProvider data; |
| 909 data.set_connect_data(mock_connect); |
| 910 mock_socket_factory.AddSocketDataProvider(&data); |
| 911 |
| 912 TestURLRequestContext context; |
| 913 scoped_refptr<SocketStream> socket_stream( |
| 914 new SocketStream(GURL("ws://localhost:9998/echo"), delegate)); |
| 915 socket_stream->set_context(&context); |
| 916 socket_stream->SetClientSocketFactory(&mock_socket_factory); |
| 917 delegate->set_socket_stream(socket_stream); |
| 918 // The delegate pointer will become invalid during the test. Set it to NULL to |
| 919 // avoid holding a dangling pointer. |
| 920 delegate = NULL; |
| 921 |
| 922 socket_stream->Connect(); |
| 923 |
| 924 EXPECT_EQ(OK, test_callback.WaitForResult()); |
| 925 } |
| 926 |
849 } // namespace net | 927 } // namespace net |
OLD | NEW |