Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(699)

Side by Side Diff: net/socket/unix_domain_socket_posix_unittest.cc

Issue 20142003: Remove ref-counting from StreamListenSocket (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <errno.h> 5 #include <errno.h>
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <poll.h> 7 #include <poll.h>
8 #include <sys/socket.h> 8 #include <sys/socket.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/time.h> 10 #include <sys/time.h>
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 base::ConditionVariable condition_; 96 base::ConditionVariable condition_;
97 }; 97 };
98 98
99 class TestListenSocketDelegate : public StreamListenSocket::Delegate { 99 class TestListenSocketDelegate : public StreamListenSocket::Delegate {
100 public: 100 public:
101 explicit TestListenSocketDelegate( 101 explicit TestListenSocketDelegate(
102 const scoped_refptr<EventManager>& event_manager) 102 const scoped_refptr<EventManager>& event_manager)
103 : event_manager_(event_manager) {} 103 : event_manager_(event_manager) {}
104 104
105 virtual void DidAccept(StreamListenSocket* server, 105 virtual void DidAccept(StreamListenSocket* server,
106 StreamListenSocket* connection) OVERRIDE { 106 scoped_ptr<StreamListenSocket> connection) OVERRIDE {
107 LOG(ERROR) << __PRETTY_FUNCTION__; 107 LOG(ERROR) << __PRETTY_FUNCTION__;
108 connection_ = connection; 108 connection_ = connection.Pass();
109 Notify(EVENT_ACCEPT); 109 Notify(EVENT_ACCEPT);
110 } 110 }
111 111
112 virtual void DidRead(StreamListenSocket* connection, 112 virtual void DidRead(StreamListenSocket* connection,
113 const char* data, 113 const char* data,
114 int len) OVERRIDE { 114 int len) OVERRIDE {
115 { 115 {
116 base::AutoLock lock(mutex_); 116 base::AutoLock lock(mutex_);
117 DCHECK(len); 117 DCHECK(len);
118 data_.assign(data, len - 1); 118 data_.assign(data, len - 1);
(...skipping 13 matching lines...) Expand all
132 base::AutoLock lock(mutex_); 132 base::AutoLock lock(mutex_);
133 return data_; 133 return data_;
134 } 134 }
135 135
136 private: 136 private:
137 void Notify(EventType event) { 137 void Notify(EventType event) {
138 event_manager_->Notify(event); 138 event_manager_->Notify(event);
139 } 139 }
140 140
141 const scoped_refptr<EventManager> event_manager_; 141 const scoped_refptr<EventManager> event_manager_;
142 scoped_refptr<StreamListenSocket> connection_; 142 scoped_ptr<StreamListenSocket> connection_;
143 base::Lock mutex_; 143 base::Lock mutex_;
144 string data_; 144 string data_;
145 }; 145 };
146 146
147 bool UserCanConnectCallback( 147 bool UserCanConnectCallback(
148 bool allow_user, const scoped_refptr<EventManager>& event_manager, 148 bool allow_user, const scoped_refptr<EventManager>& event_manager,
149 uid_t, gid_t) { 149 uid_t, gid_t) {
150 event_manager->Notify( 150 event_manager->Notify(
151 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED); 151 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED);
152 return allow_user; 152 return allow_user;
(...skipping 13 matching lines...) Expand all
166 allow_user_(allow_user) {} 166 allow_user_(allow_user) {}
167 167
168 virtual void SetUp() OVERRIDE { 168 virtual void SetUp() OVERRIDE {
169 event_manager_ = new EventManager(); 169 event_manager_ = new EventManager();
170 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_)); 170 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_));
171 DeleteSocketFile(); 171 DeleteSocketFile();
172 } 172 }
173 173
174 virtual void TearDown() OVERRIDE { 174 virtual void TearDown() OVERRIDE {
175 DeleteSocketFile(); 175 DeleteSocketFile();
176 socket_ = NULL; 176 socket_.reset();
177 socket_delegate_.reset(); 177 socket_delegate_.reset();
178 event_manager_ = NULL; 178 event_manager_ = NULL;
179 } 179 }
180 180
181 UnixDomainSocket::AuthCallback MakeAuthCallback() { 181 UnixDomainSocket::AuthCallback MakeAuthCallback() {
182 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_); 182 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_);
183 } 183 }
184 184
185 void DeleteSocketFile() { 185 void DeleteSocketFile() {
186 ASSERT_FALSE(file_path_.empty()); 186 ASSERT_FALSE(file_path_.empty());
(...skipping 28 matching lines...) Expand all
215 FROM_HERE, 215 FROM_HERE,
216 base::Bind(&UnixDomainSocketTestHelper::CreateAndListen, 216 base::Bind(&UnixDomainSocketTestHelper::CreateAndListen,
217 base::Unretained(this))); 217 base::Unretained(this)));
218 return thread.Pass(); 218 return thread.Pass();
219 } 219 }
220 220
221 const base::FilePath file_path_; 221 const base::FilePath file_path_;
222 const bool allow_user_; 222 const bool allow_user_;
223 scoped_refptr<EventManager> event_manager_; 223 scoped_refptr<EventManager> event_manager_;
224 scoped_ptr<TestListenSocketDelegate> socket_delegate_; 224 scoped_ptr<TestListenSocketDelegate> socket_delegate_;
225 scoped_refptr<UnixDomainSocket> socket_; 225 scoped_ptr<UnixDomainSocket> socket_;
226 }; 226 };
227 227
228 class UnixDomainSocketTest : public UnixDomainSocketTestHelper { 228 class UnixDomainSocketTest : public UnixDomainSocketTestHelper {
229 protected: 229 protected:
230 UnixDomainSocketTest() 230 UnixDomainSocketTest()
231 : UnixDomainSocketTestHelper(MakeSocketPath(), true /* allow user */) {} 231 : UnixDomainSocketTestHelper(MakeSocketPath(), true /* allow user */) {}
232 }; 232 };
233 233
234 class UnixDomainSocketTestWithInvalidPath : public UnixDomainSocketTestHelper { 234 class UnixDomainSocketTestWithInvalidPath : public UnixDomainSocketTestHelper {
235 protected: 235 protected:
(...skipping 22 matching lines...) Expand all
258 // Test with an invalid path to make sure that the socket is not backed by a 258 // Test with an invalid path to make sure that the socket is not backed by a
259 // file. 259 // file.
260 TEST_F(UnixDomainSocketTestWithInvalidPath, 260 TEST_F(UnixDomainSocketTestWithInvalidPath,
261 CreateAndListenWithAbstractNamespace) { 261 CreateAndListenWithAbstractNamespace) {
262 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 262 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace(
263 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 263 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
264 EXPECT_FALSE(socket_.get() == NULL); 264 EXPECT_FALSE(socket_.get() == NULL);
265 } 265 }
266 266
267 TEST_F(UnixDomainSocketTest, TestFallbackName) { 267 TEST_F(UnixDomainSocketTest, TestFallbackName) {
268 scoped_refptr<UnixDomainSocket> existing_socket = 268 scoped_ptr<UnixDomainSocket> existing_socket =
269 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 269 UnixDomainSocket::CreateAndListenWithAbstractNamespace(
270 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 270 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
271 EXPECT_FALSE(existing_socket.get() == NULL); 271 EXPECT_FALSE(existing_socket.get() == NULL);
272 // First, try to bind socket with the same name with no fallback name. 272 // First, try to bind socket with the same name with no fallback name.
273 socket_ = 273 socket_ =
274 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 274 UnixDomainSocket::CreateAndListenWithAbstractNamespace(
275 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 275 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
276 EXPECT_TRUE(socket_.get() == NULL); 276 EXPECT_TRUE(socket_.get() == NULL);
277 // Now with a fallback name. 277 // Now with a fallback name.
278 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 278 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace(
279 file_path_.value(), 279 file_path_.value(),
280 MakeSocketPath(kFallbackSocketName), 280 MakeSocketPath(kFallbackSocketName),
281 socket_delegate_.get(), 281 socket_delegate_.get(),
282 MakeAuthCallback()); 282 MakeAuthCallback());
283 EXPECT_FALSE(socket_.get() == NULL); 283 EXPECT_FALSE(socket_.get() == NULL);
284 existing_socket = NULL;
285 } 284 }
286 #endif 285 #endif
287 286
288 TEST_F(UnixDomainSocketTest, TestWithClient) { 287 TEST_F(UnixDomainSocketTest, TestWithClient) {
289 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); 288 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread();
290 EventType event = event_manager_->WaitForEvent(); 289 EventType event = event_manager_->WaitForEvent();
291 ASSERT_EQ(EVENT_LISTEN, event); 290 ASSERT_EQ(EVENT_LISTEN, event);
292 291
293 // Create the client socket. 292 // Create the client socket.
294 const SocketDescriptor sock = CreateClientSocket(); 293 const SocketDescriptor sock = CreateClientSocket();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 329
331 // Send() must fail. 330 // Send() must fail.
332 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); 331 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0));
333 ASSERT_EQ(-1, ret); 332 ASSERT_EQ(-1, ret);
334 ASSERT_EQ(EPIPE, errno); 333 ASSERT_EQ(EPIPE, errno);
335 ASSERT_FALSE(event_manager_->HasPendingEvent()); 334 ASSERT_FALSE(event_manager_->HasPendingEvent());
336 } 335 }
337 336
338 } // namespace 337 } // namespace
339 } // namespace net 338 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/unix_domain_socket_posix.cc ('k') | net/test/embedded_test_server/embedded_test_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698