1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 // TODO(ellyjones): Remove once http://crbug.com/523296 is fixed.
7 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR
8 #include "base/ios/ios_util.h"
10 #include "base/sync_socket.h"
11 #include "base/threading/simple_thread.h"
12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 const int kReceiveTimeoutInMilliseconds
= 750;
19 class HangingReceiveThread
: public base::DelegateSimpleThread::Delegate
{
21 explicit HangingReceiveThread(base::SyncSocket
* socket
)
23 thread_(this, "HangingReceiveThread") {
27 ~HangingReceiveThread() override
{}
31 ASSERT_EQ(socket_
->Peek(), 0u);
33 // Use receive with timeout so we don't hang the test harness indefinitely.
34 ASSERT_EQ(0u, socket_
->ReceiveWithTimeout(
35 &data
, sizeof(data
), base::TimeDelta::FromMilliseconds(
36 kReceiveTimeoutInMilliseconds
)));
44 base::SyncSocket
* socket_
;
45 base::DelegateSimpleThread thread_
;
47 DISALLOW_COPY_AND_ASSIGN(HangingReceiveThread
);
50 // Tests sending data between two SyncSockets. Uses ASSERT() and thus will exit
51 // early upon failure. Callers should use ASSERT_NO_FATAL_FAILURE() if testing
52 // continues after return.
53 void SendReceivePeek(base::SyncSocket
* socket_a
, base::SyncSocket
* socket_b
) {
55 const int kSending
= 123;
56 COMPILE_ASSERT(sizeof(kSending
) == sizeof(received
), Invalid_Data_Size
);
58 ASSERT_EQ(0u, socket_a
->Peek());
59 ASSERT_EQ(0u, socket_b
->Peek());
61 // Verify |socket_a| can send to |socket_a| and |socket_a| can Receive from
63 ASSERT_EQ(sizeof(kSending
), socket_a
->Send(&kSending
, sizeof(kSending
)));
64 ASSERT_EQ(sizeof(kSending
), socket_b
->Peek());
65 ASSERT_EQ(sizeof(kSending
), socket_b
->Receive(&received
, sizeof(kSending
)));
66 ASSERT_EQ(kSending
, received
);
68 ASSERT_EQ(0u, socket_a
->Peek());
69 ASSERT_EQ(0u, socket_b
->Peek());
71 // Now verify the reverse.
73 ASSERT_EQ(sizeof(kSending
), socket_b
->Send(&kSending
, sizeof(kSending
)));
74 ASSERT_EQ(sizeof(kSending
), socket_a
->Peek());
75 ASSERT_EQ(sizeof(kSending
), socket_a
->Receive(&received
, sizeof(kSending
)));
76 ASSERT_EQ(kSending
, received
);
78 ASSERT_EQ(0u, socket_a
->Peek());
79 ASSERT_EQ(0u, socket_b
->Peek());
81 ASSERT_TRUE(socket_a
->Close());
82 ASSERT_TRUE(socket_b
->Close());
85 template <class SocketType
>
86 void NormalSendReceivePeek() {
87 SocketType socket_a
, socket_b
;
88 ASSERT_TRUE(SocketType::CreatePair(&socket_a
, &socket_b
));
89 SendReceivePeek(&socket_a
, &socket_b
);
92 template <class SocketType
>
93 void ClonedSendReceivePeek() {
94 SocketType socket_a
, socket_b
;
95 ASSERT_TRUE(SocketType::CreatePair(&socket_a
, &socket_b
));
97 // Create new SyncSockets from the paired handles.
98 SocketType
socket_c(socket_a
.handle()), socket_d(socket_b
.handle());
99 SendReceivePeek(&socket_c
, &socket_d
);
104 TEST(SyncSocket
, NormalSendReceivePeek
) {
105 NormalSendReceivePeek
<base::SyncSocket
>();
108 TEST(SyncSocket
, ClonedSendReceivePeek
) {
109 ClonedSendReceivePeek
<base::SyncSocket
>();
112 TEST(CancelableSyncSocket
, NormalSendReceivePeek
) {
113 NormalSendReceivePeek
<base::CancelableSyncSocket
>();
116 TEST(CancelableSyncSocket
, ClonedSendReceivePeek
) {
117 ClonedSendReceivePeek
<base::CancelableSyncSocket
>();
120 TEST(CancelableSyncSocket
, CancelReceiveShutdown
) {
121 // TODO(ellyjones): This test fails on iOS 7 devices. http://crbug.com/523296
122 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR
123 if (!base::ios::IsRunningOnIOS8OrLater())
126 base::CancelableSyncSocket socket_a
, socket_b
;
127 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&socket_a
, &socket_b
));
129 base::TimeTicks start
= base::TimeTicks::Now();
130 HangingReceiveThread
thread(&socket_b
);
131 ASSERT_TRUE(socket_b
.Shutdown());
134 // Ensure the receive didn't just timeout.
135 ASSERT_LT((base::TimeTicks::Now() - start
).InMilliseconds(),
136 kReceiveTimeoutInMilliseconds
);
138 ASSERT_TRUE(socket_a
.Close());
139 ASSERT_TRUE(socket_b
.Close());