OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/base/listen_socket_unittest.h" | 5 #include "net/base/listen_socket_unittest.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 | 8 |
| 9 #include "base/eintr_wrappers.h" |
9 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
10 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
11 | 12 |
12 const int ListenSocketTester::kTestPort = 9999; | 13 const int ListenSocketTester::kTestPort = 9999; |
13 | 14 |
14 static const int kReadBufSize = 1024; | 15 static const int kReadBufSize = 1024; |
15 static const char* kHelloWorld = "HELLO, WORLD"; | 16 static const char* kHelloWorld = "HELLO, WORLD"; |
16 static const int kMaxQueueSize = 20; | 17 static const int kMaxQueueSize = 20; |
17 static const char* kLoopback = "127.0.0.1"; | 18 static const char* kLoopback = "127.0.0.1"; |
18 static const int kDefaultTimeoutMs = 5000; | 19 static const int kDefaultTimeoutMs = 5000; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 ASSERT_TRUE(NextAction(kDefaultTimeoutMs)); | 51 ASSERT_TRUE(NextAction(kDefaultTimeoutMs)); |
51 ASSERT_FALSE(server_ == NULL); | 52 ASSERT_FALSE(server_ == NULL); |
52 ASSERT_EQ(ACTION_LISTEN, last_action_.type()); | 53 ASSERT_EQ(ACTION_LISTEN, last_action_.type()); |
53 | 54 |
54 // verify the connect/accept and setup test_socket_ | 55 // verify the connect/accept and setup test_socket_ |
55 test_socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 56 test_socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
56 struct sockaddr_in client; | 57 struct sockaddr_in client; |
57 client.sin_family = AF_INET; | 58 client.sin_family = AF_INET; |
58 client.sin_addr.s_addr = inet_addr(kLoopback); | 59 client.sin_addr.s_addr = inet_addr(kLoopback); |
59 client.sin_port = htons(kTestPort); | 60 client.sin_port = htons(kTestPort); |
60 int ret = connect(test_socket_, | 61 int ret = |
61 reinterpret_cast<sockaddr*>(&client), sizeof(client)); | 62 HANDLE_EINTR(connect(test_socket_, reinterpret_cast<sockaddr*>(&client), |
| 63 sizeof(client))); |
62 ASSERT_NE(ret, SOCKET_ERROR); | 64 ASSERT_NE(ret, SOCKET_ERROR); |
63 | 65 |
64 net::SetNonBlocking(test_socket_); | 66 net::SetNonBlocking(test_socket_); |
65 ASSERT_TRUE(NextAction(kDefaultTimeoutMs)); | 67 ASSERT_TRUE(NextAction(kDefaultTimeoutMs)); |
66 ASSERT_EQ(ACTION_ACCEPT, last_action_.type()); | 68 ASSERT_EQ(ACTION_ACCEPT, last_action_.type()); |
67 } | 69 } |
68 | 70 |
69 void ListenSocketTester::TearDown() { | 71 void ListenSocketTester::TearDown() { |
70 // verify close | 72 // verify close |
71 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 pthread_mutex_unlock(&lock_); | 149 pthread_mutex_unlock(&lock_); |
148 return true; | 150 return true; |
149 #endif | 151 #endif |
150 } | 152 } |
151 | 153 |
152 int ListenSocketTester::ClearTestSocket() { | 154 int ListenSocketTester::ClearTestSocket() { |
153 char buf[kReadBufSize]; | 155 char buf[kReadBufSize]; |
154 int len_ret = 0; | 156 int len_ret = 0; |
155 int time_out = 0; | 157 int time_out = 0; |
156 do { | 158 do { |
157 int len = recv(test_socket_, buf, kReadBufSize, 0); | 159 int len = HANDLE_EINTR(recv(test_socket_, buf, kReadBufSize, 0)); |
158 #if defined(OS_WIN) | 160 #if defined(OS_WIN) |
159 if (len == SOCKET_ERROR) { | 161 if (len == SOCKET_ERROR) { |
160 int err = WSAGetLastError(); | 162 int err = WSAGetLastError(); |
161 if (err == WSAEWOULDBLOCK) { | 163 if (err == WSAEWOULDBLOCK) { |
162 #elif defined(OS_POSIX) | 164 #elif defined(OS_POSIX) |
163 if (len == SOCKET_ERROR) { | 165 if (len == SOCKET_ERROR) { |
164 if (errno == EWOULDBLOCK || errno == EAGAIN) { | 166 if (errno == EWOULDBLOCK || errno == EAGAIN) { |
165 #endif | 167 #endif |
166 PlatformThread::Sleep(1); | 168 PlatformThread::Sleep(1); |
167 time_out++; | 169 time_out++; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 const std::string& data) { | 214 const std::string& data) { |
213 ReportAction(ListenSocketTestAction(ACTION_READ, data)); | 215 ReportAction(ListenSocketTestAction(ACTION_READ, data)); |
214 } | 216 } |
215 | 217 |
216 void ListenSocketTester::DidClose(ListenSocket *sock) { | 218 void ListenSocketTester::DidClose(ListenSocket *sock) { |
217 ReportAction(ListenSocketTestAction(ACTION_CLOSE)); | 219 ReportAction(ListenSocketTestAction(ACTION_CLOSE)); |
218 } | 220 } |
219 | 221 |
220 bool ListenSocketTester::Send(SOCKET sock, const std::string& str) { | 222 bool ListenSocketTester::Send(SOCKET sock, const std::string& str) { |
221 int len = static_cast<int>(str.length()); | 223 int len = static_cast<int>(str.length()); |
222 int send_len = send(sock, str.data(), len, 0); | 224 int send_len = HANDLE_EINTR(send(sock, str.data(), len, 0)); |
223 if (send_len == SOCKET_ERROR) { | 225 if (send_len == SOCKET_ERROR) { |
224 LOG(ERROR) << "send failed: " << errno; | 226 LOG(ERROR) << "send failed: " << errno; |
225 return false; | 227 return false; |
226 } else if (send_len != len) { | 228 } else if (send_len != len) { |
227 return false; | 229 return false; |
228 } | 230 } |
229 return true; | 231 return true; |
230 } | 232 } |
231 | 233 |
232 void ListenSocketTester::TestClientSend() { | 234 void ListenSocketTester::TestClientSend() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 ASSERT_EQ(ACTION_SEND, last_action_.type()); | 268 ASSERT_EQ(ACTION_SEND, last_action_.type()); |
267 // TODO(erikkay): Without this sleep, the recv seems to fail a small amount | 269 // TODO(erikkay): Without this sleep, the recv seems to fail a small amount |
268 // of the time. I could fix this by making the socket blocking, but then | 270 // of the time. I could fix this by making the socket blocking, but then |
269 // this test might hang in the case of errors. It would be nice to do | 271 // this test might hang in the case of errors. It would be nice to do |
270 // something that felt more reliable here. | 272 // something that felt more reliable here. |
271 PlatformThread::Sleep(10); // sleep for 10ms | 273 PlatformThread::Sleep(10); // sleep for 10ms |
272 const int buf_len = 200; | 274 const int buf_len = 200; |
273 char buf[buf_len+1]; | 275 char buf[buf_len+1]; |
274 int recv_len; | 276 int recv_len; |
275 do { | 277 do { |
276 recv_len = recv(test_socket_, buf, buf_len, 0); | 278 recv_len = HANDLE_EINTR(recv(test_socket_, buf, buf_len, 0)); |
277 #if defined(OS_POSIX) | 279 #if defined(OS_POSIX) |
278 } while (recv_len == SOCKET_ERROR && errno == EINTR); | 280 } while (recv_len == SOCKET_ERROR && errno == EINTR); |
279 #else | 281 #else |
280 } while (false); | 282 } while (false); |
281 #endif | 283 #endif |
282 ASSERT_NE(recv_len, SOCKET_ERROR); | 284 ASSERT_NE(recv_len, SOCKET_ERROR); |
283 buf[recv_len] = 0; | 285 buf[recv_len] = 0; |
284 ASSERT_STREQ(buf, kHelloWorld); | 286 ASSERT_STREQ(buf, kHelloWorld); |
285 } | 287 } |
286 | 288 |
(...skipping 23 matching lines...) Expand all Loading... |
310 tester_->TestClientSend(); | 312 tester_->TestClientSend(); |
311 } | 313 } |
312 | 314 |
313 TEST_F(ListenSocketTest, ClientSendLong) { | 315 TEST_F(ListenSocketTest, ClientSendLong) { |
314 tester_->TestClientSendLong(); | 316 tester_->TestClientSendLong(); |
315 } | 317 } |
316 | 318 |
317 TEST_F(ListenSocketTest, ServerSend) { | 319 TEST_F(ListenSocketTest, ServerSend) { |
318 tester_->TestServerSend(); | 320 tester_->TestServerSend(); |
319 } | 321 } |
OLD | NEW |