Home | History | Annotate | Download | only in base
      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:
     21   WakeThread(SocketServer* ss) : ss_(ss) {
     22   }
     23   virtual ~WakeThread() {
     24     Stop();
     25   }
     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.
     36 TEST(MacCFSocketServerTest, TestWait) {
     37   MacCFSocketServer server;
     38   uint32 start = Time();
     39   server.Wait(1000, true);
     40   EXPECT_GE(TimeSince(start), 1000);
     41 }
     42 
     43 // Test that MacCFSocketServer::Wakeup works as expected.
     44 TEST(MacCFSocketServerTest, TestWakeup) {
     45   MacCFSocketServer server;
     46   WakeThread thread(&server);
     47   uint32 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.
     54 TEST(MacCarbonSocketServerTest, TestWait) {
     55   MacCarbonSocketServer server;
     56   uint32 start = Time();
     57   server.Wait(1000, true);
     58   EXPECT_GE(TimeSince(start), 1000);
     59 }
     60 
     61 // Test that MacCarbonSocketServer::Wakeup works as expected.
     62 TEST(MacCarbonSocketServerTest, TestWakeup) {
     63   MacCarbonSocketServer server;
     64   WakeThread thread(&server);
     65   uint32 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.
     72 TEST(MacCarbonAppSocketServerTest, TestWait) {
     73   MacCarbonAppSocketServer server;
     74   uint32 start = Time();
     75   server.Wait(1000, true);
     76   EXPECT_GE(TimeSince(start), 1000);
     77 }
     78 
     79 // Test that MacCarbonAppSocketServer::Wakeup works as expected.
     80 TEST(MacCarbonAppSocketServerTest, TestWakeup) {
     81   MacCarbonAppSocketServer server;
     82   WakeThread thread(&server);
     83   uint32 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:
     94   MacAsyncSocketTest()
     95       : server_(CreateSocketServer()),
     96         scope_(server_.get()) {}
     97   // Override for other implementations of MacBaseSocketServer.
     98   virtual MacBaseSocketServer* CreateSocketServer() {
     99     return new MacCFSocketServer();
    100   };
    101   rtc::scoped_ptr<MacBaseSocketServer> server_;
    102   SocketServerScope scope_;
    103 };
    104 
    105 TEST_F(MacAsyncSocketTest, TestConnectIPv4) {
    106   SocketTest::TestConnectIPv4();
    107 }
    108 
    109 TEST_F(MacAsyncSocketTest, TestConnectIPv6) {
    110   SocketTest::TestConnectIPv6();
    111 }
    112 
    113 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv4) {
    114   SocketTest::TestConnectWithDnsLookupIPv4();
    115 }
    116 
    117 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv6) {
    118   SocketTest::TestConnectWithDnsLookupIPv6();
    119 }
    120 
    121 // BUG=https://code.google.com/p/webrtc/issues/detail?id=2272
    122 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectFailIPv4) {
    123   SocketTest::TestConnectFailIPv4();
    124 }
    125 
    126 TEST_F(MacAsyncSocketTest, TestConnectFailIPv6) {
    127   SocketTest::TestConnectFailIPv6();
    128 }
    129 
    130 // Reenable once we have mac async dns
    131 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv4) {
    132   SocketTest::TestConnectWithDnsLookupFailIPv4();
    133 }
    134 
    135 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv6) {
    136   SocketTest::TestConnectWithDnsLookupFailIPv6();
    137 }
    138 
    139 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv4) {
    140   SocketTest::TestConnectWithClosedSocketIPv4();
    141 }
    142 
    143 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv6) {
    144   SocketTest::TestConnectWithClosedSocketIPv6();
    145 }
    146 
    147 // Flaky at the moment (10% failure rate).  Seems the client doesn't get
    148 // signalled in a timely manner...
    149 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv4) {
    150   SocketTest::TestServerCloseDuringConnectIPv4();
    151 }
    152 
    153 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv6) {
    154   SocketTest::TestServerCloseDuringConnectIPv6();
    155 }
    156 // Flaky at the moment (0.5% failure rate).  Seems the client doesn't get
    157 // signalled in a timely manner...
    158 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv4) {
    159   SocketTest::TestClientCloseDuringConnectIPv4();
    160 }
    161 
    162 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv6) {
    163   SocketTest::TestClientCloseDuringConnectIPv6();
    164 }
    165 
    166 TEST_F(MacAsyncSocketTest, TestServerCloseIPv4) {
    167   SocketTest::TestServerCloseIPv4();
    168 }
    169 
    170 TEST_F(MacAsyncSocketTest, TestServerCloseIPv6) {
    171   SocketTest::TestServerCloseIPv6();
    172 }
    173 
    174 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv4) {
    175   SocketTest::TestCloseInClosedCallbackIPv4();
    176 }
    177 
    178 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv6) {
    179   SocketTest::TestCloseInClosedCallbackIPv6();
    180 }
    181 
    182 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv4) {
    183   SocketTest::TestSocketServerWaitIPv4();
    184 }
    185 
    186 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv6) {
    187   SocketTest::TestSocketServerWaitIPv6();
    188 }
    189 
    190 TEST_F(MacAsyncSocketTest, TestTcpIPv4) {
    191   SocketTest::TestTcpIPv4();
    192 }
    193 
    194 TEST_F(MacAsyncSocketTest, TestTcpIPv6) {
    195   SocketTest::TestTcpIPv6();
    196 }
    197 
    198 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv4) {
    199   SocketTest::TestSingleFlowControlCallbackIPv4();
    200 }
    201 
    202 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv6) {
    203   SocketTest::TestSingleFlowControlCallbackIPv6();
    204 }
    205 
    206 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv4) {
    207   SocketTest::TestUdpIPv4();
    208 }
    209 
    210 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv6) {
    211   SocketTest::TestUdpIPv6();
    212 }
    213 
    214 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv4) {
    215   SocketTest::TestGetSetOptionsIPv4();
    216 }
    217 
    218 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv6) {
    219   SocketTest::TestGetSetOptionsIPv6();
    220 }
    221 
    222 #ifndef CARBON_DEPRECATED
    223 class MacCarbonAppAsyncSocketTest : public MacAsyncSocketTest {
    224   virtual MacBaseSocketServer* CreateSocketServer() {
    225     return new MacCarbonAppSocketServer();
    226   };
    227 };
    228 
    229 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv4) {
    230   SocketTest::TestSocketServerWaitIPv4();
    231 }
    232 
    233 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv6) {
    234   SocketTest::TestSocketServerWaitIPv6();
    235 }
    236 #endif
    237 }  // namespace rtc
    238