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 34 namespace talk_base { 35 36 class WakeThread : public Thread { 37 public: 38 WakeThread(SocketServer* ss) : ss_(ss) { 39 } 40 void Run() { 41 ss_->WakeUp(); 42 } 43 private: 44 SocketServer* ss_; 45 }; 46 47 #ifndef CARBON_DEPRECATED 48 49 // Test that MacCFSocketServer::Wait works as expected. 50 TEST(MacCFSocketServerTest, TestWait) { 51 MacCFSocketServer server; 52 uint32 start = Time(); 53 server.Wait(1000, true); 54 EXPECT_GE(TimeSince(start), 1000); 55 } 56 57 // Test that MacCFSocketServer::Wakeup works as expected. 58 TEST(MacCFSocketServerTest, TestWakeup) { 59 MacCFSocketServer server; 60 WakeThread thread(&server); 61 uint32 start = Time(); 62 thread.Start(); 63 server.Wait(10000, true); 64 EXPECT_LT(TimeSince(start), 10000); 65 } 66 67 // Test that MacCarbonSocketServer::Wait works as expected. 68 TEST(MacCarbonSocketServerTest, TestWait) { 69 MacCarbonSocketServer server; 70 uint32 start = Time(); 71 server.Wait(1000, true); 72 EXPECT_GE(TimeSince(start), 1000); 73 } 74 75 // Test that MacCarbonSocketServer::Wakeup works as expected. 76 TEST(MacCarbonSocketServerTest, TestWakeup) { 77 MacCarbonSocketServer server; 78 WakeThread thread(&server); 79 uint32 start = Time(); 80 thread.Start(); 81 server.Wait(10000, true); 82 EXPECT_LT(TimeSince(start), 10000); 83 } 84 85 // Test that MacCarbonAppSocketServer::Wait works as expected. 86 TEST(MacCarbonAppSocketServerTest, TestWait) { 87 MacCarbonAppSocketServer server; 88 uint32 start = Time(); 89 server.Wait(1000, true); 90 EXPECT_GE(TimeSince(start), 1000); 91 } 92 93 // Test that MacCarbonAppSocketServer::Wakeup works as expected. 94 TEST(MacCarbonAppSocketServerTest, TestWakeup) { 95 MacCarbonAppSocketServer server; 96 WakeThread thread(&server); 97 uint32 start = Time(); 98 thread.Start(); 99 server.Wait(10000, true); 100 EXPECT_LT(TimeSince(start), 10000); 101 } 102 103 #endif 104 105 // Test that MacAsyncSocket passes all the generic Socket tests. 106 class MacAsyncSocketTest : public SocketTest { 107 protected: 108 MacAsyncSocketTest() 109 : server_(CreateSocketServer()), 110 scope_(server_.get()) {} 111 // Override for other implementations of MacBaseSocketServer. 112 virtual MacBaseSocketServer* CreateSocketServer() { 113 return new MacCFSocketServer(); 114 }; 115 talk_base::scoped_ptr<MacBaseSocketServer> server_; 116 SocketServerScope scope_; 117 }; 118 119 TEST_F(MacAsyncSocketTest, TestConnectIPv4) { 120 SocketTest::TestConnectIPv4(); 121 } 122 123 TEST_F(MacAsyncSocketTest, TestConnectIPv6) { 124 SocketTest::TestConnectIPv6(); 125 } 126 127 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv4) { 128 SocketTest::TestConnectWithDnsLookupIPv4(); 129 } 130 131 TEST_F(MacAsyncSocketTest, TestConnectWithDnsLookupIPv6) { 132 SocketTest::TestConnectWithDnsLookupIPv6(); 133 } 134 135 TEST_F(MacAsyncSocketTest, TestConnectFailIPv4) { 136 SocketTest::TestConnectFailIPv4(); 137 } 138 139 TEST_F(MacAsyncSocketTest, TestConnectFailIPv6) { 140 SocketTest::TestConnectFailIPv6(); 141 } 142 143 // Reenable once we have mac async dns 144 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv4) { 145 SocketTest::TestConnectWithDnsLookupFailIPv4(); 146 } 147 148 TEST_F(MacAsyncSocketTest, DISABLED_TestConnectWithDnsLookupFailIPv6) { 149 SocketTest::TestConnectWithDnsLookupFailIPv6(); 150 } 151 152 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv4) { 153 SocketTest::TestConnectWithClosedSocketIPv4(); 154 } 155 156 TEST_F(MacAsyncSocketTest, TestConnectWithClosedSocketIPv6) { 157 SocketTest::TestConnectWithClosedSocketIPv6(); 158 } 159 160 // Flaky at the moment (10% failure rate). Seems the client doesn't get 161 // signalled in a timely manner... 162 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv4) { 163 SocketTest::TestServerCloseDuringConnectIPv4(); 164 } 165 166 TEST_F(MacAsyncSocketTest, DISABLED_TestServerCloseDuringConnectIPv6) { 167 SocketTest::TestServerCloseDuringConnectIPv6(); 168 } 169 // Flaky at the moment (0.5% failure rate). Seems the client doesn't get 170 // signalled in a timely manner... 171 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv4) { 172 SocketTest::TestClientCloseDuringConnectIPv4(); 173 } 174 175 TEST_F(MacAsyncSocketTest, TestClientCloseDuringConnectIPv6) { 176 SocketTest::TestClientCloseDuringConnectIPv6(); 177 } 178 179 TEST_F(MacAsyncSocketTest, TestServerCloseIPv4) { 180 SocketTest::TestServerCloseIPv4(); 181 } 182 183 TEST_F(MacAsyncSocketTest, TestServerCloseIPv6) { 184 SocketTest::TestServerCloseIPv6(); 185 } 186 187 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv4) { 188 SocketTest::TestCloseInClosedCallbackIPv4(); 189 } 190 191 TEST_F(MacAsyncSocketTest, TestCloseInClosedCallbackIPv6) { 192 SocketTest::TestCloseInClosedCallbackIPv6(); 193 } 194 195 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv4) { 196 SocketTest::TestSocketServerWaitIPv4(); 197 } 198 199 TEST_F(MacAsyncSocketTest, TestSocketServerWaitIPv6) { 200 SocketTest::TestSocketServerWaitIPv6(); 201 } 202 203 TEST_F(MacAsyncSocketTest, TestTcpIPv4) { 204 SocketTest::TestTcpIPv4(); 205 } 206 207 TEST_F(MacAsyncSocketTest, TestTcpIPv6) { 208 SocketTest::TestTcpIPv6(); 209 } 210 211 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv4) { 212 SocketTest::TestSingleFlowControlCallbackIPv4(); 213 } 214 215 TEST_F(MacAsyncSocketTest, TestSingleFlowControlCallbackIPv6) { 216 SocketTest::TestSingleFlowControlCallbackIPv6(); 217 } 218 219 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv4) { 220 SocketTest::TestUdpIPv4(); 221 } 222 223 TEST_F(MacAsyncSocketTest, DISABLED_TestUdpIPv6) { 224 SocketTest::TestUdpIPv6(); 225 } 226 227 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv4) { 228 SocketTest::TestGetSetOptionsIPv4(); 229 } 230 231 TEST_F(MacAsyncSocketTest, DISABLED_TestGetSetOptionsIPv6) { 232 SocketTest::TestGetSetOptionsIPv6(); 233 } 234 235 #ifndef CARBON_DEPRECATED 236 class MacCarbonAppAsyncSocketTest : public MacAsyncSocketTest { 237 virtual MacBaseSocketServer* CreateSocketServer() { 238 return new MacCarbonAppSocketServer(); 239 }; 240 }; 241 242 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv4) { 243 SocketTest::TestSocketServerWaitIPv4(); 244 } 245 246 TEST_F(MacCarbonAppAsyncSocketTest, TestSocketServerWaitIPv6) { 247 SocketTest::TestSocketServerWaitIPv6(); 248 } 249 #endif 250 } // namespace talk_base 251