1 /*
2  *  Copyright 2009 The WebRTC Project Authors. All rights reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/scoped_ptr.h"
13 #include "webrtc/base/socket_unittest.h"
14 #include "webrtc/base/thread.h"
15 #include "webrtc/base/macsocketserver.h"
16 
17 namespace rtc {
18 
19 class WakeThread : public Thread {
20  public:
WakeThread(SocketServer * ss)21   WakeThread(SocketServer* ss) : ss_(ss) {
22   }
~WakeThread()23   virtual ~WakeThread() {
24     Stop();
25   }
Run()26   void Run() {
27     ss_->WakeUp();
28   }
29  private:
30   SocketServer* ss_;
31 };
32 
33 #ifndef CARBON_DEPRECATED
34 
35 // Test that MacCFSocketServer::Wait works as expected.
TEST(MacCFSocketServerTest,TestWait)36 TEST(MacCFSocketServerTest, TestWait) {
37   MacCFSocketServer server;
38   uint32_t start = Time();
39   server.Wait(1000, true);
40   EXPECT_GE(TimeSince(start), 1000);
41 }
42 
43 // Test that MacCFSocketServer::Wakeup works as expected.
TEST(MacCFSocketServerTest,TestWakeup)44 TEST(MacCFSocketServerTest, TestWakeup) {
45   MacCFSocketServer server;
46   WakeThread thread(&server);
47   uint32_t start = Time();
48   thread.Start();
49   server.Wait(10000, true);
50   EXPECT_LT(TimeSince(start), 10000);
51 }
52 
53 // Test that MacCarbonSocketServer::Wait works as expected.
TEST(MacCarbonSocketServerTest,TestWait)54 TEST(MacCarbonSocketServerTest, TestWait) {
55   MacCarbonSocketServer server;
56   uint32_t start = Time();
57   server.Wait(1000, true);
58   EXPECT_GE(TimeSince(start), 1000);
59 }
60 
61 // Test that MacCarbonSocketServer::Wakeup works as expected.
TEST(MacCarbonSocketServerTest,TestWakeup)62 TEST(MacCarbonSocketServerTest, TestWakeup) {
63   MacCarbonSocketServer server;
64   WakeThread thread(&server);
65   uint32_t start = Time();
66   thread.Start();
67   server.Wait(10000, true);
68   EXPECT_LT(TimeSince(start), 10000);
69 }
70 
71 // Test that MacCarbonAppSocketServer::Wait works as expected.
TEST(MacCarbonAppSocketServerTest,TestWait)72 TEST(MacCarbonAppSocketServerTest, TestWait) {
73   MacCarbonAppSocketServer server;
74   uint32_t start = Time();
75   server.Wait(1000, true);
76   EXPECT_GE(TimeSince(start), 1000);
77 }
78 
79 // Test that MacCarbonAppSocketServer::Wakeup works as expected.
TEST(MacCarbonAppSocketServerTest,TestWakeup)80 TEST(MacCarbonAppSocketServerTest, TestWakeup) {
81   MacCarbonAppSocketServer server;
82   WakeThread thread(&server);
83   uint32_t start = Time();
84   thread.Start();
85   server.Wait(10000, true);
86   EXPECT_LT(TimeSince(start), 10000);
87 }
88 
89 #endif
90 
91 // Test that MacAsyncSocket passes all the generic Socket tests.
92 class MacAsyncSocketTest : public SocketTest {
93  protected:
MacAsyncSocketTest()94   MacAsyncSocketTest()
95       : server_(CreateSocketServer()),
96         scope_(server_.get()) {}
97   // Override for other implementations of MacBaseSocketServer.
CreateSocketServer()98   virtual MacBaseSocketServer* CreateSocketServer() {
99     return new MacCFSocketServer();
100   };
101   rtc::scoped_ptr<MacBaseSocketServer> server_;
102   SocketServerScope scope_;
103 };
104 
TEST_F(MacAsyncSocketTest,TestConnectIPv4)105 TEST_F(MacAsyncSocketTest, TestConnectIPv4) {
106   SocketTest::TestConnectIPv4();
107 }
108 
TEST_F(MacAsyncSocketTest,TestConnectIPv6)109 TEST_F(MacAsyncSocketTest, TestConnectIPv6) {
110   SocketTest::TestConnectIPv6();
111 }
112 
TEST_F(MacAsyncSocketTest,TestConnectWithDnsLookupIPv4)113 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv4) {
114   SocketTest::TestConnectWithDnsLookupIPv4();
115 }
116 
TEST_F(MacAsyncSocketTest,TestConnectWithDnsLookupIPv6)117 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv6) {
118   SocketTest::TestConnectWithDnsLookupIPv6();
119 }
120 
121 // BUG=https://code.google.com/p/webrtc/issues/detail?id=2272
TEST_F(MacAsyncSocketTest,DISABLED_TestConnectFailIPv4)122 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectFailIPv4) {
123   SocketTest::TestConnectFailIPv4();
124 }
125 
126 // Flaky. See webrtc:4738.
TEST_F(MacAsyncSocketTest,DISABLED_TestConnectFailIPv6)127 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectFailIPv6) {
128   SocketTest::TestConnectFailIPv6();
129 }
130 
131 // Reenable once we have mac async dns
TEST_F(MacAsyncSocketTest,DISABLED_TestConnectWithDnsLookupFailIPv4)132 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv4) {
133   SocketTest::TestConnectWithDnsLookupFailIPv4();
134 }
135 
TEST_F(MacAsyncSocketTest,DISABLED_TestConnectWithDnsLookupFailIPv6)136 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv6) {
137   SocketTest::TestConnectWithDnsLookupFailIPv6();
138 }
139 
TEST_F(MacAsyncSocketTest,TestConnectWithClosedSocketIPv4)140 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv4) {
141   SocketTest::TestConnectWithClosedSocketIPv4();
142 }
143 
TEST_F(MacAsyncSocketTest,TestConnectWithClosedSocketIPv6)144 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv6) {
145   SocketTest::TestConnectWithClosedSocketIPv6();
146 }
147 
148 // Flaky at the moment (10% failure rate).  Seems the client doesn't get
149 // signalled in a timely manner...
TEST_F(MacAsyncSocketTest,DISABLED_TestServerCloseDuringConnectIPv4)150 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv4) {
151   SocketTest::TestServerCloseDuringConnectIPv4();
152 }
153 
TEST_F(MacAsyncSocketTest,DISABLED_TestServerCloseDuringConnectIPv6)154 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv6) {
155   SocketTest::TestServerCloseDuringConnectIPv6();
156 }
157 // Flaky at the moment (0.5% failure rate).  Seems the client doesn't get
158 // signalled in a timely manner...
TEST_F(MacAsyncSocketTest,TestClientCloseDuringConnectIPv4)159 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv4) {
160   SocketTest::TestClientCloseDuringConnectIPv4();
161 }
162 
TEST_F(MacAsyncSocketTest,TestClientCloseDuringConnectIPv6)163 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv6) {
164   SocketTest::TestClientCloseDuringConnectIPv6();
165 }
166 
TEST_F(MacAsyncSocketTest,TestServerCloseIPv4)167 TEST_F(MacAsyncSocketTest, TestServerCloseIPv4) {
168   SocketTest::TestServerCloseIPv4();
169 }
170 
TEST_F(MacAsyncSocketTest,TestServerCloseIPv6)171 TEST_F(MacAsyncSocketTest, TestServerCloseIPv6) {
172   SocketTest::TestServerCloseIPv6();
173 }
174 
TEST_F(MacAsyncSocketTest,TestCloseInClosedCallbackIPv4)175 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv4) {
176   SocketTest::TestCloseInClosedCallbackIPv4();
177 }
178 
TEST_F(MacAsyncSocketTest,TestCloseInClosedCallbackIPv6)179 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv6) {
180   SocketTest::TestCloseInClosedCallbackIPv6();
181 }
182 
TEST_F(MacAsyncSocketTest,TestSocketServerWaitIPv4)183 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv4) {
184   SocketTest::TestSocketServerWaitIPv4();
185 }
186 
TEST_F(MacAsyncSocketTest,TestSocketServerWaitIPv6)187 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv6) {
188   SocketTest::TestSocketServerWaitIPv6();
189 }
190 
TEST_F(MacAsyncSocketTest,TestTcpIPv4)191 TEST_F(MacAsyncSocketTest, TestTcpIPv4) {
192   SocketTest::TestTcpIPv4();
193 }
194 
TEST_F(MacAsyncSocketTest,TestTcpIPv6)195 TEST_F(MacAsyncSocketTest, TestTcpIPv6) {
196   SocketTest::TestTcpIPv6();
197 }
198 
TEST_F(MacAsyncSocketTest,TestSingleFlowControlCallbackIPv4)199 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv4) {
200   SocketTest::TestSingleFlowControlCallbackIPv4();
201 }
202 
TEST_F(MacAsyncSocketTest,TestSingleFlowControlCallbackIPv6)203 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv6) {
204   SocketTest::TestSingleFlowControlCallbackIPv6();
205 }
206 
TEST_F(MacAsyncSocketTest,DISABLED_TestUdpIPv4)207 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv4) {
208   SocketTest::TestUdpIPv4();
209 }
210 
TEST_F(MacAsyncSocketTest,DISABLED_TestUdpIPv6)211 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv6) {
212   SocketTest::TestUdpIPv6();
213 }
214 
TEST_F(MacAsyncSocketTest,DISABLED_TestGetSetOptionsIPv4)215 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv4) {
216   SocketTest::TestGetSetOptionsIPv4();
217 }
218 
TEST_F(MacAsyncSocketTest,DISABLED_TestGetSetOptionsIPv6)219 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv6) {
220   SocketTest::TestGetSetOptionsIPv6();
221 }
222 
223 #ifndef CARBON_DEPRECATED
224 class MacCarbonAppAsyncSocketTest : public MacAsyncSocketTest {
CreateSocketServer()225   virtual MacBaseSocketServer* CreateSocketServer() {
226     return new MacCarbonAppSocketServer();
227   };
228 };
229 
TEST_F(MacCarbonAppAsyncSocketTest,TestSocketServerWaitIPv4)230 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv4) {
231   SocketTest::TestSocketServerWaitIPv4();
232 }
233 
TEST_F(MacCarbonAppAsyncSocketTest,TestSocketServerWaitIPv6)234 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv6) {
235   SocketTest::TestSocketServerWaitIPv6();
236 }
237 #endif
238 }  // namespace rtc
239