Update V8 to version 4.7.42.
[chromium-blink-merge.git] / base / sync_socket_unittest.cc
blobff9b8bc8c2f0c7a932d034da2080335e19a7481c
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"
9 #endif
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"
15 namespace {
17 const int kReceiveTimeoutInMilliseconds = 750;
19 class HangingReceiveThread : public base::DelegateSimpleThread::Delegate {
20 public:
21 explicit HangingReceiveThread(base::SyncSocket* socket)
22 : socket_(socket),
23 thread_(this, "HangingReceiveThread") {
24 thread_.Start();
27 ~HangingReceiveThread() override {}
29 void Run() override {
30 int data = 0;
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)));
39 void Stop() {
40 thread_.Join();
43 private:
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) {
54 int received = 0;
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
62 // |socket_a|.
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.
72 received = 0;
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);
102 } // namespace
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())
124 return;
125 #endif
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());
132 thread.Stop();
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());