1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_LISTEN_SOCKET_UNITTEST_H_ 6 #define NET_BASE_LISTEN_SOCKET_UNITTEST_H_ 7 8 #include "build/build_config.h" 9 10 #if defined(OS_WIN) 11 #include <winsock2.h> 12 #elif defined(OS_POSIX) 13 #include <arpa/inet.h> 14 #include <errno.h> 15 #include <sys/socket.h> 16 #endif 17 18 #include "base/basictypes.h" 19 #include "base/memory/ref_counted.h" 20 #include "base/memory/scoped_ptr.h" 21 #include "base/message_loop/message_loop.h" 22 #include "base/strings/string_util.h" 23 #include "base/synchronization/condition_variable.h" 24 #include "base/synchronization/lock.h" 25 #include "base/threading/thread.h" 26 #include "net/base/net_util.h" 27 #include "net/base/winsock_init.h" 28 #include "net/socket/tcp_listen_socket.h" 29 #include "testing/gtest/include/gtest/gtest.h" 30 31 namespace net { 32 33 enum ActionType { 34 ACTION_NONE = 0, 35 ACTION_LISTEN = 1, 36 ACTION_ACCEPT = 2, 37 ACTION_READ = 3, 38 ACTION_SEND = 4, 39 ACTION_CLOSE = 5, 40 ACTION_SHUTDOWN = 6 41 }; 42 43 class TCPListenSocketTestAction { 44 public: 45 TCPListenSocketTestAction() : action_(ACTION_NONE) {} 46 explicit TCPListenSocketTestAction(ActionType action) : action_(action) {} 47 TCPListenSocketTestAction(ActionType action, std::string data) 48 : action_(action), 49 data_(data) {} 50 51 const std::string data() const { return data_; } 52 ActionType type() const { return action_; } 53 54 private: 55 ActionType action_; 56 std::string data_; 57 }; 58 59 60 // This had to be split out into a separate class because I couldn't 61 // make the testing::Test class refcounted. 62 class TCPListenSocketTester : 63 public StreamListenSocket::Delegate, 64 public base::RefCountedThreadSafe<TCPListenSocketTester> { 65 66 public: 67 TCPListenSocketTester(); 68 69 void SetUp(); 70 void TearDown(); 71 72 void ReportAction(const TCPListenSocketTestAction& action); 73 void NextAction(); 74 75 // read all pending data from the test socket 76 int ClearTestSocket(); 77 // Release the connection and server sockets 78 void Shutdown(); 79 void Listen(); 80 void SendFromTester(); 81 // verify the send/read from client to server 82 void TestClientSend(); 83 // verify send/read of a longer string 84 void TestClientSendLong(); 85 // verify a send/read from server to client 86 void TestServerSend(); 87 // verify multiple sends and reads from server to client. 88 void TestServerSendMultiple(); 89 90 virtual bool Send(SocketDescriptor sock, const std::string& str); 91 92 // StreamListenSocket::Delegate: 93 virtual void DidAccept(StreamListenSocket* server, 94 StreamListenSocket* connection) OVERRIDE; 95 virtual void DidRead(StreamListenSocket* connection, const char* data, 96 int len) OVERRIDE; 97 virtual void DidClose(StreamListenSocket* sock) OVERRIDE; 98 99 scoped_ptr<base::Thread> thread_; 100 base::MessageLoopForIO* loop_; 101 scoped_refptr<TCPListenSocket> server_; 102 StreamListenSocket* connection_; 103 TCPListenSocketTestAction last_action_; 104 105 SocketDescriptor test_socket_; 106 static const int kTestPort; 107 108 base::Lock lock_; // protects |queue_| and wraps |cv_| 109 base::ConditionVariable cv_; 110 std::deque<TCPListenSocketTestAction> queue_; 111 112 protected: 113 friend class base::RefCountedThreadSafe<TCPListenSocketTester>; 114 115 virtual ~TCPListenSocketTester(); 116 117 virtual scoped_refptr<TCPListenSocket> DoListen(); 118 }; 119 120 } // namespace net 121 122 #endif // NET_BASE_LISTEN_SOCKET_UNITTEST_H_ 123