1/*
2 * libjingle
3 * Copyright 2009, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/gunit.h"
29#include "talk/base/scoped_ptr.h"
30#include "talk/base/socket_unittest.h"
31#include "talk/base/thread.h"
32#include "talk/base/macsocketserver.h"
33
34namespace talk_base {
35
36class WakeThread : public Thread {
37 public:
38  WakeThread(SocketServer* ss) : ss_(ss) {
39  }
40  virtual ~WakeThread() {
41    Stop();
42  }
43  void Run() {
44    ss_->WakeUp();
45  }
46 private:
47  SocketServer* ss_;
48};
49
50#ifndef CARBON_DEPRECATED
51
52// Test that MacCFSocketServer::Wait works as expected.
53TEST(MacCFSocketServerTest, TestWait) {
54  MacCFSocketServer server;
55  uint32 start = Time();
56  server.Wait(1000, true);
57  EXPECT_GE(TimeSince(start), 1000);
58}
59
60// Test that MacCFSocketServer::Wakeup works as expected.
61TEST(MacCFSocketServerTest, TestWakeup) {
62  MacCFSocketServer server;
63  WakeThread thread(&server);
64  uint32 start = Time();
65  thread.Start();
66  server.Wait(10000, true);
67  EXPECT_LT(TimeSince(start), 10000);
68}
69
70// Test that MacCarbonSocketServer::Wait works as expected.
71TEST(MacCarbonSocketServerTest, TestWait) {
72  MacCarbonSocketServer server;
73  uint32 start = Time();
74  server.Wait(1000, true);
75  EXPECT_GE(TimeSince(start), 1000);
76}
77
78// Test that MacCarbonSocketServer::Wakeup works as expected.
79TEST(MacCarbonSocketServerTest, TestWakeup) {
80  MacCarbonSocketServer server;
81  WakeThread thread(&server);
82  uint32 start = Time();
83  thread.Start();
84  server.Wait(10000, true);
85  EXPECT_LT(TimeSince(start), 10000);
86}
87
88// Test that MacCarbonAppSocketServer::Wait works as expected.
89TEST(MacCarbonAppSocketServerTest, TestWait) {
90  MacCarbonAppSocketServer server;
91  uint32 start = Time();
92  server.Wait(1000, true);
93  EXPECT_GE(TimeSince(start), 1000);
94}
95
96// Test that MacCarbonAppSocketServer::Wakeup works as expected.
97TEST(MacCarbonAppSocketServerTest, TestWakeup) {
98  MacCarbonAppSocketServer server;
99  WakeThread thread(&server);
100  uint32 start = Time();
101  thread.Start();
102  server.Wait(10000, true);
103  EXPECT_LT(TimeSince(start), 10000);
104}
105
106#endif
107
108// Test that MacAsyncSocket passes all the generic Socket tests.
109class MacAsyncSocketTest : public SocketTest {
110 protected:
111  MacAsyncSocketTest()
112      : server_(CreateSocketServer()),
113        scope_(server_.get()) {}
114  // Override for other implementations of MacBaseSocketServer.
115  virtual MacBaseSocketServer* CreateSocketServer() {
116    return new MacCFSocketServer();
117  };
118  talk_base::scoped_ptr<MacBaseSocketServer> server_;
119  SocketServerScope scope_;
120};
121
122TEST_F(MacAsyncSocketTest, TestConnectIPv4) {
123  SocketTest::TestConnectIPv4();
124}
125
126TEST_F(MacAsyncSocketTest, TestConnectIPv6) {
127  SocketTest::TestConnectIPv6();
128}
129
130TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv4) {
131  SocketTest::TestConnectWithDnsLookupIPv4();
132}
133
134TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv6) {
135  SocketTest::TestConnectWithDnsLookupIPv6();
136}
137
138// BUG=https://code.google.com/p/webrtc/issues/detail?id=2272
139TEST_F(MacAsyncSocketTest, DISABLED_TestConnectFailIPv4) {
140  SocketTest::TestConnectFailIPv4();
141}
142
143TEST_F(MacAsyncSocketTest, TestConnectFailIPv6) {
144  SocketTest::TestConnectFailIPv6();
145}
146
147// Reenable once we have mac async dns
148TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv4) {
149  SocketTest::TestConnectWithDnsLookupFailIPv4();
150}
151
152TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv6) {
153  SocketTest::TestConnectWithDnsLookupFailIPv6();
154}
155
156TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv4) {
157  SocketTest::TestConnectWithClosedSocketIPv4();
158}
159
160TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv6) {
161  SocketTest::TestConnectWithClosedSocketIPv6();
162}
163
164// Flaky at the moment (10% failure rate).  Seems the client doesn't get
165// signalled in a timely manner...
166TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv4) {
167  SocketTest::TestServerCloseDuringConnectIPv4();
168}
169
170TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv6) {
171  SocketTest::TestServerCloseDuringConnectIPv6();
172}
173// Flaky at the moment (0.5% failure rate).  Seems the client doesn't get
174// signalled in a timely manner...
175TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv4) {
176  SocketTest::TestClientCloseDuringConnectIPv4();
177}
178
179TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv6) {
180  SocketTest::TestClientCloseDuringConnectIPv6();
181}
182
183TEST_F(MacAsyncSocketTest, TestServerCloseIPv4) {
184  SocketTest::TestServerCloseIPv4();
185}
186
187TEST_F(MacAsyncSocketTest, TestServerCloseIPv6) {
188  SocketTest::TestServerCloseIPv6();
189}
190
191TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv4) {
192  SocketTest::TestCloseInClosedCallbackIPv4();
193}
194
195TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv6) {
196  SocketTest::TestCloseInClosedCallbackIPv6();
197}
198
199TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv4) {
200  SocketTest::TestSocketServerWaitIPv4();
201}
202
203TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv6) {
204  SocketTest::TestSocketServerWaitIPv6();
205}
206
207TEST_F(MacAsyncSocketTest, TestTcpIPv4) {
208  SocketTest::TestTcpIPv4();
209}
210
211TEST_F(MacAsyncSocketTest, TestTcpIPv6) {
212  SocketTest::TestTcpIPv6();
213}
214
215TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv4) {
216  SocketTest::TestSingleFlowControlCallbackIPv4();
217}
218
219TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv6) {
220  SocketTest::TestSingleFlowControlCallbackIPv6();
221}
222
223TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv4) {
224  SocketTest::TestUdpIPv4();
225}
226
227TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv6) {
228  SocketTest::TestUdpIPv6();
229}
230
231TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv4) {
232  SocketTest::TestGetSetOptionsIPv4();
233}
234
235TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv6) {
236  SocketTest::TestGetSetOptionsIPv6();
237}
238
239#ifndef CARBON_DEPRECATED
240class MacCarbonAppAsyncSocketTest : public MacAsyncSocketTest {
241  virtual MacBaseSocketServer* CreateSocketServer() {
242    return new MacCarbonAppSocketServer();
243  };
244};
245
246TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv4) {
247  SocketTest::TestSocketServerWaitIPv4();
248}
249
250TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv6) {
251  SocketTest::TestSocketServerWaitIPv6();
252}
253#endif
254}  // namespace talk_base
255