You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, 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. #include <signal.h>
  28. #include <stdarg.h>
  29. #include "talk/base/gunit.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/base/physicalsocketserver.h"
  32. #include "talk/base/scoped_ptr.h"
  33. #include "talk/base/socket_unittest.h"
  34. #include "talk/base/thread.h"
  35. namespace talk_base {
  36. class PhysicalSocketTest : public SocketTest {
  37. };
  38. TEST_F(PhysicalSocketTest, TestConnectIPv4) {
  39. SocketTest::TestConnectIPv4();
  40. }
  41. TEST_F(PhysicalSocketTest, TestConnectIPv6) {
  42. SocketTest::TestConnectIPv6();
  43. }
  44. TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv4) {
  45. SocketTest::TestConnectWithDnsLookupIPv4();
  46. }
  47. TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupIPv6) {
  48. SocketTest::TestConnectWithDnsLookupIPv6();
  49. }
  50. TEST_F(PhysicalSocketTest, TestConnectFailIPv4) {
  51. SocketTest::TestConnectFailIPv4();
  52. }
  53. TEST_F(PhysicalSocketTest, TestConnectFailIPv6) {
  54. SocketTest::TestConnectFailIPv6();
  55. }
  56. TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv4) {
  57. SocketTest::TestConnectWithDnsLookupFailIPv4();
  58. }
  59. TEST_F(PhysicalSocketTest, TestConnectWithDnsLookupFailIPv6) {
  60. SocketTest::TestConnectWithDnsLookupFailIPv6();
  61. }
  62. #ifdef OSX
  63. // This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
  64. TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv4) {
  65. #else
  66. TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv4) {
  67. #endif
  68. SocketTest::TestConnectWithClosedSocketIPv4();
  69. }
  70. #ifdef OSX
  71. // This test crashes the OS X kernel on 10.6 (at bsd/netinet/tcp_subr.c:2118).
  72. TEST_F(PhysicalSocketTest, DISABLED_TestConnectWithClosedSocketIPv6) {
  73. #else
  74. TEST_F(PhysicalSocketTest, TestConnectWithClosedSocketIPv6) {
  75. #endif
  76. SocketTest::TestConnectWithClosedSocketIPv6();
  77. }
  78. TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv4) {
  79. SocketTest::TestConnectWhileNotClosedIPv4();
  80. }
  81. TEST_F(PhysicalSocketTest, TestConnectWhileNotClosedIPv6) {
  82. SocketTest::TestConnectWhileNotClosedIPv6();
  83. }
  84. TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv4) {
  85. SocketTest::TestServerCloseDuringConnectIPv4();
  86. }
  87. TEST_F(PhysicalSocketTest, TestServerCloseDuringConnectIPv6) {
  88. SocketTest::TestServerCloseDuringConnectIPv6();
  89. }
  90. TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv4) {
  91. SocketTest::TestClientCloseDuringConnectIPv4();
  92. }
  93. TEST_F(PhysicalSocketTest, TestClientCloseDuringConnectIPv6) {
  94. SocketTest::TestClientCloseDuringConnectIPv6();
  95. }
  96. TEST_F(PhysicalSocketTest, TestServerCloseIPv4) {
  97. SocketTest::TestServerCloseIPv4();
  98. }
  99. TEST_F(PhysicalSocketTest, TestServerCloseIPv6) {
  100. SocketTest::TestServerCloseIPv6();
  101. }
  102. TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv4) {
  103. SocketTest::TestCloseInClosedCallbackIPv4();
  104. }
  105. TEST_F(PhysicalSocketTest, TestCloseInClosedCallbackIPv6) {
  106. SocketTest::TestCloseInClosedCallbackIPv6();
  107. }
  108. TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv4) {
  109. SocketTest::TestSocketServerWaitIPv4();
  110. }
  111. TEST_F(PhysicalSocketTest, TestSocketServerWaitIPv6) {
  112. SocketTest::TestSocketServerWaitIPv6();
  113. }
  114. TEST_F(PhysicalSocketTest, TestTcpIPv4) {
  115. SocketTest::TestTcpIPv4();
  116. }
  117. TEST_F(PhysicalSocketTest, TestTcpIPv6) {
  118. SocketTest::TestTcpIPv6();
  119. }
  120. TEST_F(PhysicalSocketTest, TestUdpIPv4) {
  121. SocketTest::TestUdpIPv4();
  122. }
  123. TEST_F(PhysicalSocketTest, TestUdpIPv6) {
  124. SocketTest::TestUdpIPv6();
  125. }
  126. TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv4) {
  127. SocketTest::TestGetSetOptionsIPv4();
  128. }
  129. TEST_F(PhysicalSocketTest, TestGetSetOptionsIPv6) {
  130. SocketTest::TestGetSetOptionsIPv6();
  131. }
  132. #ifdef POSIX
  133. class PosixSignalDeliveryTest : public testing::Test {
  134. public:
  135. static void RecordSignal(int signum) {
  136. signals_received_.push_back(signum);
  137. signaled_thread_ = Thread::Current();
  138. }
  139. protected:
  140. void SetUp() {
  141. ss_.reset(new PhysicalSocketServer());
  142. }
  143. void TearDown() {
  144. ss_.reset(NULL);
  145. signals_received_.clear();
  146. signaled_thread_ = NULL;
  147. }
  148. bool ExpectSignal(int signum) {
  149. if (signals_received_.empty()) {
  150. LOG(LS_ERROR) << "ExpectSignal(): No signal received";
  151. return false;
  152. }
  153. if (signals_received_[0] != signum) {
  154. LOG(LS_ERROR) << "ExpectSignal(): Received signal " <<
  155. signals_received_[0] << ", expected " << signum;
  156. return false;
  157. }
  158. signals_received_.erase(signals_received_.begin());
  159. return true;
  160. }
  161. bool ExpectNone() {
  162. bool ret = signals_received_.empty();
  163. if (!ret) {
  164. LOG(LS_ERROR) << "ExpectNone(): Received signal " << signals_received_[0]
  165. << ", expected none";
  166. }
  167. return ret;
  168. }
  169. static std::vector<int> signals_received_;
  170. static Thread *signaled_thread_;
  171. scoped_ptr<PhysicalSocketServer> ss_;
  172. };
  173. std::vector<int> PosixSignalDeliveryTest::signals_received_;
  174. Thread *PosixSignalDeliveryTest::signaled_thread_ = NULL;
  175. // Test receiving a synchronous signal while not in Wait() and then entering
  176. // Wait() afterwards.
  177. TEST_F(PosixSignalDeliveryTest, RaiseThenWait) {
  178. ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
  179. raise(SIGTERM);
  180. EXPECT_TRUE(ss_->Wait(0, true));
  181. EXPECT_TRUE(ExpectSignal(SIGTERM));
  182. EXPECT_TRUE(ExpectNone());
  183. }
  184. // Test that we can handle getting tons of repeated signals and that we see all
  185. // the different ones.
  186. TEST_F(PosixSignalDeliveryTest, InsanelyManySignals) {
  187. ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
  188. ss_->SetPosixSignalHandler(SIGINT, &RecordSignal);
  189. for (int i = 0; i < 10000; ++i) {
  190. raise(SIGTERM);
  191. }
  192. raise(SIGINT);
  193. EXPECT_TRUE(ss_->Wait(0, true));
  194. // Order will be lowest signal numbers first.
  195. EXPECT_TRUE(ExpectSignal(SIGINT));
  196. EXPECT_TRUE(ExpectSignal(SIGTERM));
  197. EXPECT_TRUE(ExpectNone());
  198. }
  199. // Test that a signal during a Wait() call is detected.
  200. TEST_F(PosixSignalDeliveryTest, SignalDuringWait) {
  201. ss_->SetPosixSignalHandler(SIGALRM, &RecordSignal);
  202. alarm(1);
  203. EXPECT_TRUE(ss_->Wait(1500, true));
  204. EXPECT_TRUE(ExpectSignal(SIGALRM));
  205. EXPECT_TRUE(ExpectNone());
  206. }
  207. class RaiseSigTermRunnable : public Runnable {
  208. void Run(Thread *thread) {
  209. thread->socketserver()->Wait(1000, false);
  210. // Allow SIGTERM. This will be the only thread with it not masked so it will
  211. // be delivered to us.
  212. sigset_t mask;
  213. sigemptyset(&mask);
  214. pthread_sigmask(SIG_SETMASK, &mask, NULL);
  215. // Raise it.
  216. raise(SIGTERM);
  217. }
  218. };
  219. // Test that it works no matter what thread the kernel chooses to give the
  220. // signal to (since it's not guaranteed to be the one that Wait() runs on).
  221. TEST_F(PosixSignalDeliveryTest, SignalOnDifferentThread) {
  222. ss_->SetPosixSignalHandler(SIGTERM, &RecordSignal);
  223. // Mask out SIGTERM so that it can't be delivered to this thread.
  224. sigset_t mask;
  225. sigemptyset(&mask);
  226. sigaddset(&mask, SIGTERM);
  227. EXPECT_EQ(0, pthread_sigmask(SIG_SETMASK, &mask, NULL));
  228. // Start a new thread that raises it. It will have to be delivered to that
  229. // thread. Our implementation should safely handle it and dispatch
  230. // RecordSignal() on this thread.
  231. scoped_ptr<Thread> thread(new Thread());
  232. thread->Start(new RaiseSigTermRunnable());
  233. EXPECT_TRUE(ss_->Wait(1500, true));
  234. EXPECT_TRUE(ExpectSignal(SIGTERM));
  235. EXPECT_EQ(Thread::Current(), signaled_thread_);
  236. EXPECT_TRUE(ExpectNone());
  237. }
  238. #endif
  239. } // namespace talk_base