OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
| 11 #include <memory> |
11 #include <signal.h> | 12 #include <signal.h> |
12 #include <stdarg.h> | 13 #include <stdarg.h> |
13 | 14 |
14 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
15 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
16 #include "webrtc/base/physicalsocketserver.h" | 17 #include "webrtc/base/physicalsocketserver.h" |
17 #include "webrtc/base/scoped_ptr.h" | |
18 #include "webrtc/base/socket_unittest.h" | 18 #include "webrtc/base/socket_unittest.h" |
19 #include "webrtc/base/testutils.h" | 19 #include "webrtc/base/testutils.h" |
20 #include "webrtc/base/thread.h" | 20 #include "webrtc/base/thread.h" |
21 | 21 |
22 namespace rtc { | 22 namespace rtc { |
23 | 23 |
24 class PhysicalSocketTest; | 24 class PhysicalSocketTest; |
25 | 25 |
26 class FakeSocketDispatcher : public SocketDispatcher { | 26 class FakeSocketDispatcher : public SocketDispatcher { |
27 public: | 27 public: |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 PhysicalSocketTest() | 93 PhysicalSocketTest() |
94 : server_(new FakePhysicalSocketServer(this)), | 94 : server_(new FakePhysicalSocketServer(this)), |
95 scope_(server_.get()), | 95 scope_(server_.get()), |
96 fail_accept_(false), | 96 fail_accept_(false), |
97 max_send_size_(-1) { | 97 max_send_size_(-1) { |
98 } | 98 } |
99 | 99 |
100 void ConnectInternalAcceptError(const IPAddress& loopback); | 100 void ConnectInternalAcceptError(const IPAddress& loopback); |
101 void WritableAfterPartialWrite(const IPAddress& loopback); | 101 void WritableAfterPartialWrite(const IPAddress& loopback); |
102 | 102 |
103 rtc::scoped_ptr<FakePhysicalSocketServer> server_; | 103 std::unique_ptr<FakePhysicalSocketServer> server_; |
104 SocketServerScope scope_; | 104 SocketServerScope scope_; |
105 bool fail_accept_; | 105 bool fail_accept_; |
106 int max_send_size_; | 106 int max_send_size_; |
107 }; | 107 }; |
108 | 108 |
109 SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket, | 109 SOCKET FakeSocketDispatcher::DoAccept(SOCKET socket, |
110 sockaddr* addr, | 110 sockaddr* addr, |
111 socklen_t* addrlen) { | 111 socklen_t* addrlen) { |
112 FakePhysicalSocketServer* ss = | 112 FakePhysicalSocketServer* ss = |
113 static_cast<FakePhysicalSocketServer*>(socketserver()); | 113 static_cast<FakePhysicalSocketServer*>(socketserver()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 165 |
166 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { | 166 TEST_F(PhysicalSocketTest, TestConnectFailIPv4) { |
167 SocketTest::TestConnectFailIPv4(); | 167 SocketTest::TestConnectFailIPv4(); |
168 } | 168 } |
169 | 169 |
170 void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) { | 170 void PhysicalSocketTest::ConnectInternalAcceptError(const IPAddress& loopback) { |
171 testing::StreamSink sink; | 171 testing::StreamSink sink; |
172 SocketAddress accept_addr; | 172 SocketAddress accept_addr; |
173 | 173 |
174 // Create two clients. | 174 // Create two clients. |
175 scoped_ptr<AsyncSocket> client1(server_->CreateAsyncSocket(loopback.family(), | 175 std::unique_ptr<AsyncSocket> client1( |
176 SOCK_STREAM)); | 176 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
177 sink.Monitor(client1.get()); | 177 sink.Monitor(client1.get()); |
178 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState()); | 178 EXPECT_EQ(AsyncSocket::CS_CLOSED, client1->GetState()); |
179 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr()); | 179 EXPECT_PRED1(IsUnspecOrEmptyIP, client1->GetLocalAddress().ipaddr()); |
180 | 180 |
181 scoped_ptr<AsyncSocket> client2(server_->CreateAsyncSocket(loopback.family(), | 181 std::unique_ptr<AsyncSocket> client2( |
182 SOCK_STREAM)); | 182 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
183 sink.Monitor(client2.get()); | 183 sink.Monitor(client2.get()); |
184 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState()); | 184 EXPECT_EQ(AsyncSocket::CS_CLOSED, client2->GetState()); |
185 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr()); | 185 EXPECT_PRED1(IsUnspecOrEmptyIP, client2->GetLocalAddress().ipaddr()); |
186 | 186 |
187 // Create server and listen. | 187 // Create server and listen. |
188 scoped_ptr<AsyncSocket> server( | 188 std::unique_ptr<AsyncSocket> server( |
189 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); | 189 server_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); |
190 sink.Monitor(server.get()); | 190 sink.Monitor(server.get()); |
191 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); | 191 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); |
192 EXPECT_EQ(0, server->Listen(5)); | 192 EXPECT_EQ(0, server->Listen(5)); |
193 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); | 193 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); |
194 | 194 |
195 // Ensure no pending server connections, since we haven't done anything yet. | 195 // Ensure no pending server connections, since we haven't done anything yet. |
196 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); | 196 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); |
197 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); | 197 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
198 EXPECT_TRUE(accept_addr.IsNil()); | 198 EXPECT_TRUE(accept_addr.IsNil()); |
199 | 199 |
200 // Attempt first connect to listening socket. | 200 // Attempt first connect to listening socket. |
201 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress())); | 201 EXPECT_EQ(0, client1->Connect(server->GetLocalAddress())); |
202 EXPECT_FALSE(client1->GetLocalAddress().IsNil()); | 202 EXPECT_FALSE(client1->GetLocalAddress().IsNil()); |
203 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress()); | 203 EXPECT_NE(server->GetLocalAddress(), client1->GetLocalAddress()); |
204 | 204 |
205 // Client is connecting, outcome not yet determined. | 205 // Client is connecting, outcome not yet determined. |
206 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState()); | 206 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client1->GetState()); |
207 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN)); | 207 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_OPEN)); |
208 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE)); | 208 EXPECT_FALSE(sink.Check(client1.get(), testing::SSE_CLOSE)); |
209 | 209 |
210 // Server has pending connection, try to accept it (will fail). | 210 // Server has pending connection, try to accept it (will fail). |
211 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); | 211 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); |
212 // Simulate "::accept" returning an error. | 212 // Simulate "::accept" returning an error. |
213 SetFailAccept(true); | 213 SetFailAccept(true); |
214 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); | 214 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); |
215 EXPECT_FALSE(accepted); | 215 EXPECT_FALSE(accepted); |
216 ASSERT_TRUE(accept_addr.IsNil()); | 216 ASSERT_TRUE(accept_addr.IsNil()); |
217 | 217 |
218 // Ensure no more pending server connections. | 218 // Ensure no more pending server connections. |
219 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); | 219 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); |
220 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); | 220 EXPECT_TRUE(nullptr == server->Accept(&accept_addr)); |
221 EXPECT_TRUE(accept_addr.IsNil()); | 221 EXPECT_TRUE(accept_addr.IsNil()); |
222 | 222 |
223 // Attempt second connect to listening socket. | 223 // Attempt second connect to listening socket. |
224 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress())); | 224 EXPECT_EQ(0, client2->Connect(server->GetLocalAddress())); |
225 EXPECT_FALSE(client2->GetLocalAddress().IsNil()); | 225 EXPECT_FALSE(client2->GetLocalAddress().IsNil()); |
226 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress()); | 226 EXPECT_NE(server->GetLocalAddress(), client2->GetLocalAddress()); |
227 | 227 |
228 // Client is connecting, outcome not yet determined. | 228 // Client is connecting, outcome not yet determined. |
229 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState()); | 229 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client2->GetState()); |
230 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN)); | 230 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_OPEN)); |
231 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE)); | 231 EXPECT_FALSE(sink.Check(client2.get(), testing::SSE_CLOSE)); |
232 | 232 |
233 // Server has pending connection, try to accept it (will succeed). | 233 // Server has pending connection, try to accept it (will succeed). |
234 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); | 234 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); |
235 SetFailAccept(false); | 235 SetFailAccept(false); |
236 scoped_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr)); | 236 std::unique_ptr<AsyncSocket> accepted2(server->Accept(&accept_addr)); |
237 ASSERT_TRUE(accepted2); | 237 ASSERT_TRUE(accepted2); |
238 EXPECT_FALSE(accept_addr.IsNil()); | 238 EXPECT_FALSE(accept_addr.IsNil()); |
239 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr); | 239 EXPECT_EQ(accepted2->GetRemoteAddress(), accept_addr); |
240 } | 240 } |
241 | 241 |
242 TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) { | 242 TEST_F(PhysicalSocketTest, TestConnectAcceptErrorIPv4) { |
243 ConnectInternalAcceptError(kIPv4Loopback); | 243 ConnectInternalAcceptError(kIPv4Loopback); |
244 } | 244 } |
245 | 245 |
246 // Crashes on Linux. See webrtc:4923. | 246 // Crashes on Linux. See webrtc:4923. |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 if (!ret) { | 508 if (!ret) { |
509 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0] | 509 LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0] |
510 << ", expected none"; | 510 << ", expected none"; |
511 } | 511 } |
512 return ret; | 512 return ret; |
513 } | 513 } |
514 | 514 |
515 static std::vector<int> signals_received_; | 515 static std::vector<int> signals_received_; |
516 static Thread *signaled_thread_; | 516 static Thread *signaled_thread_; |
517 | 517 |
518 scoped_ptr<PhysicalSocketServer> ss_; | 518 std::unique_ptr<PhysicalSocketServer> ss_; |
519 }; | 519 }; |
520 | 520 |
521 std::vector<int> PosixSignalDeliveryTest::signals_received_; | 521 std::vector<int> PosixSignalDeliveryTest::signals_received_; |
522 Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL; | 522 Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL; |
523 | 523 |
524 // Test receiving a synchronous signal while not in Wait() and then entering | 524 // Test receiving a synchronous signal while not in Wait() and then entering |
525 // Wait() afterwards. | 525 // Wait() afterwards. |
526 TEST_F(PosixSignalDeliveryTest, RaiseThenWait) { | 526 TEST_F(PosixSignalDeliveryTest, RaiseThenWait) { |
527 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal)); | 527 ASSERT_TRUE(ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal)); |
528 raise(SIGTERM); | 528 raise(SIGTERM); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) { | 576 TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) { |
577 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); | 577 ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal); |
578 // Mask out SIGTERM so that it can't be delivered to this thread. | 578 // Mask out SIGTERM so that it can't be delivered to this thread. |
579 sigset_t mask; | 579 sigset_t mask; |
580 sigemptyset(&mask); | 580 sigemptyset(&mask); |
581 sigaddset(&mask, SIGTERM); | 581 sigaddset(&mask, SIGTERM); |
582 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL)); | 582 EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL)); |
583 // Start a new thread that raises it. It will have to be delivered to that | 583 // Start a new thread that raises it. It will have to be delivered to that |
584 // thread. Our implementation should safely handle it and dispatch | 584 // thread. Our implementation should safely handle it and dispatch |
585 // RecordSignal() on this thread. | 585 // RecordSignal() on this thread. |
586 scoped_ptr<Thread> thread(new Thread()); | 586 std::unique_ptr<Thread> thread(new Thread()); |
587 scoped_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable()); | 587 std::unique_ptr<RaiseSigTermRunnable> runnable(new RaiseSigTermRunnable()); |
588 thread->Start(runnable.get()); | 588 thread->Start(runnable.get()); |
589 EXPECT_TRUE(ss_->Wait(1500, true)); | 589 EXPECT_TRUE(ss_->Wait(1500, true)); |
590 EXPECT_TRUE(ExpectSignal(SIGTERM)); | 590 EXPECT_TRUE(ExpectSignal(SIGTERM)); |
591 EXPECT_EQ(Thread::Current(), signaled_thread_); | 591 EXPECT_EQ(Thread::Current(), signaled_thread_); |
592 EXPECT_TRUE(ExpectNone()); | 592 EXPECT_TRUE(ExpectNone()); |
593 } | 593 } |
594 | 594 |
595 #endif | 595 #endif |
596 | 596 |
597 } // namespace rtc | 597 } // namespace rtc |
OLD | NEW |