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

Side by Side Diff: ipc/ipc_channel_posix_unittest.cc

Issue 14383024: ipc: Use base::MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 // These tests are POSIX only. 5 // These tests are POSIX only.
6 6
7 #include "ipc/ipc_channel_posix.h" 7 #include "ipc/ipc_channel_posix.h"
8 8
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <sys/socket.h> 10 #include <sys/socket.h>
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 virtual void OnChannelListenError() OVERRIDE { 75 virtual void OnChannelListenError() OVERRIDE {
76 status_ = LISTEN_ERROR; 76 status_ = LISTEN_ERROR;
77 if (!quit_only_on_message_) { 77 if (!quit_only_on_message_) {
78 QuitRunLoop(); 78 QuitRunLoop();
79 } 79 }
80 } 80 }
81 81
82 STATUS status() { return status_; } 82 STATUS status() { return status_; }
83 83
84 void QuitRunLoop() { 84 void QuitRunLoop() {
85 MessageLoopForIO::current()->QuitNow(); 85 base::MessageLoopForIO::current()->QuitNow();
86 } 86 }
87 87
88 private: 88 private:
89 // The current status of the listener. 89 // The current status of the listener.
90 STATUS status_; 90 STATUS status_;
91 // If |quit_only_on_message_| then the listener will only break out of 91 // If |quit_only_on_message_| then the listener will only break out of
92 // the run loop when kQuitMessage is received. 92 // the run loop when kQuitMessage is received.
93 bool quit_only_on_message_; 93 bool quit_only_on_message_;
94 }; 94 };
95 95
96 class IPCChannelPosixTest : public base::MultiProcessTest { 96 class IPCChannelPosixTest : public base::MultiProcessTest {
97 public: 97 public:
98 static void SetUpSocket(IPC::ChannelHandle *handle, 98 static void SetUpSocket(IPC::ChannelHandle *handle,
99 IPC::Channel::Mode mode); 99 IPC::Channel::Mode mode);
100 static void SpinRunLoop(base::TimeDelta delay); 100 static void SpinRunLoop(base::TimeDelta delay);
101 static const std::string GetConnectionSocketName(); 101 static const std::string GetConnectionSocketName();
102 static const std::string GetChannelDirName(); 102 static const std::string GetChannelDirName();
103 103
104 protected: 104 protected:
105 virtual void SetUp(); 105 virtual void SetUp();
106 virtual void TearDown(); 106 virtual void TearDown();
107 107
108 private: 108 private:
109 scoped_ptr<MessageLoopForIO> message_loop_; 109 scoped_ptr<base::MessageLoopForIO> message_loop_;
110 }; 110 };
111 111
112 const std::string IPCChannelPosixTest::GetChannelDirName() { 112 const std::string IPCChannelPosixTest::GetChannelDirName() {
113 #if defined(OS_ANDROID) 113 #if defined(OS_ANDROID)
114 base::FilePath tmp_dir; 114 base::FilePath tmp_dir;
115 PathService::Get(base::DIR_CACHE, &tmp_dir); 115 PathService::Get(base::DIR_CACHE, &tmp_dir);
116 return tmp_dir.value(); 116 return tmp_dir.value();
117 #else 117 #else
118 return "/var/tmp"; 118 return "/var/tmp";
119 #endif 119 #endif
120 } 120 }
121 121
122 const std::string IPCChannelPosixTest::GetConnectionSocketName() { 122 const std::string IPCChannelPosixTest::GetConnectionSocketName() {
123 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket"; 123 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
124 } 124 }
125 125
126 void IPCChannelPosixTest::SetUp() { 126 void IPCChannelPosixTest::SetUp() {
127 MultiProcessTest::SetUp(); 127 MultiProcessTest::SetUp();
128 // Construct a fresh IO Message loop for the duration of each test. 128 // Construct a fresh IO Message loop for the duration of each test.
129 message_loop_.reset(new MessageLoopForIO()); 129 message_loop_.reset(new base::MessageLoopForIO());
130 } 130 }
131 131
132 void IPCChannelPosixTest::TearDown() { 132 void IPCChannelPosixTest::TearDown() {
133 message_loop_.reset(NULL); 133 message_loop_.reset(NULL);
134 MultiProcessTest::TearDown(); 134 MultiProcessTest::TearDown();
135 } 135 }
136 136
137 // Create up a socket and bind and listen to it, or connect it 137 // Create up a socket and bind and listen to it, or connect it
138 // depending on the |mode|. 138 // depending on the |mode|.
139 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle, 139 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 server_address_len), 0) << server_address.sun_path 173 server_address_len), 0) << server_address.sun_path
174 << ": " << strerror(errno) 174 << ": " << strerror(errno)
175 << "(" << errno << ")"; 175 << "(" << errno << ")";
176 } else { 176 } else {
177 FAIL() << "Unknown mode " << mode; 177 FAIL() << "Unknown mode " << mode;
178 } 178 }
179 handle->socket.fd = socket_fd; 179 handle->socket.fd = socket_fd;
180 } 180 }
181 181
182 void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) { 182 void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
183 MessageLoopForIO *loop = MessageLoopForIO::current(); 183 base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
184 // Post a quit task so that this loop eventually ends and we don't hang 184 // Post a quit task so that this loop eventually ends and we don't hang
185 // in the case of a bad test. Usually, the run loop will quit sooner than 185 // in the case of a bad test. Usually, the run loop will quit sooner than
186 // that because all tests use a IPCChannelPosixTestListener which quits the 186 // that because all tests use a IPCChannelPosixTestListener which quits the
187 // current run loop on any channel activity. 187 // current run loop on any channel activity.
188 loop->PostDelayedTask( 188 loop->PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(), delay);
189 FROM_HERE,
190 MessageLoop::QuitClosure(),
191 delay);
192 loop->Run(); 189 loop->Run();
193 } 190 }
194 191
195 TEST_F(IPCChannelPosixTest, BasicListen) { 192 TEST_F(IPCChannelPosixTest, BasicListen) {
196 const std::string kChannelName = 193 const std::string kChannelName =
197 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; 194 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
198 195
199 // Test creating a socket that is listening. 196 // Test creating a socket that is listening.
200 IPC::ChannelHandle handle(kChannelName); 197 IPC::ChannelHandle handle(kChannelName);
201 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); 198 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER);
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); 381 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
385 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( 382 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
386 connection_socket_name)); 383 connection_socket_name));
387 channel.Close(); 384 channel.Close();
388 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( 385 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
389 connection_socket_name)); 386 connection_socket_name));
390 } 387 }
391 388
392 // A long running process that connects to us 389 // A long running process that connects to us
393 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { 390 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
394 MessageLoopForIO message_loop; 391 base::MessageLoopForIO message_loop;
395 IPCChannelPosixTestListener listener(true); 392 IPCChannelPosixTestListener listener(true);
396 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); 393 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
397 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); 394 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
398 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); 395 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
399 EXPECT_TRUE(channel.Connect()); 396 EXPECT_TRUE(channel.Connect());
400 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); 397 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
401 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); 398 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
402 return 0; 399 return 0;
403 } 400 }
404 401
405 // Simple external process that shouldn't be able to connect to us. 402 // Simple external process that shouldn't be able to connect to us.
406 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { 403 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
407 MessageLoopForIO message_loop; 404 base::MessageLoopForIO message_loop;
408 IPCChannelPosixTestListener listener(false); 405 IPCChannelPosixTestListener listener(false);
409 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); 406 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
410 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); 407 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
411 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); 408 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
412 409
413 // In this case connect may succeed or fail depending on if the packet 410 // In this case connect may succeed or fail depending on if the packet
414 // actually gets sent at sendmsg. Since we never delay on send, we may not 411 // actually gets sent at sendmsg. Since we never delay on send, we may not
415 // see the error. However even if connect succeeds, eventually we will get an 412 // see the error. However even if connect succeeds, eventually we will get an
416 // error back since the channel will be closed when we attempt to read from 413 // error back since the channel will be closed when we attempt to read from
417 // it. 414 // it.
418 bool connected = channel.Connect(); 415 bool connected = channel.Connect();
419 if (connected) { 416 if (connected) {
420 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); 417 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
421 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); 418 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
422 } else { 419 } else {
423 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); 420 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
424 } 421 }
425 return 0; 422 return 0;
426 } 423 }
427 424
428 } // namespace 425 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698