1 // Copyright (c) 2006-2008 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 <sys/socket.h> 14 #include <errno.h> 15 #include <semaphore.h> 16 #include <arpa/inet.h> 17 #endif 18 19 #include "base/thread.h" 20 #include "base/basictypes.h" 21 #include "base/message_loop.h" 22 #include "base/string_util.h" 23 #include "base/thread.h" 24 #include "net/base/net_util.h" 25 #include "net/base/listen_socket.h" 26 #include "net/base/winsock_init.h" 27 #include "testing/gtest/include/gtest/gtest.h" 28 29 #if defined(OS_POSIX) 30 // Used same name as in Windows to avoid #ifdef where refrenced 31 #define SOCKET int 32 const int INVALID_SOCKET = -1; 33 const int SOCKET_ERROR = -1; 34 #endif 35 36 enum ActionType { 37 ACTION_NONE = 0, 38 ACTION_LISTEN = 1, 39 ACTION_ACCEPT = 2, 40 ACTION_READ = 3, 41 ACTION_SEND = 4, 42 ACTION_CLOSE = 5, 43 ACTION_SHUTDOWN = 6 44 }; 45 46 class ListenSocketTestAction { 47 public: 48 ListenSocketTestAction() : action_(ACTION_NONE) {} 49 explicit ListenSocketTestAction(ActionType action) : action_(action) {} 50 ListenSocketTestAction(ActionType action, std::string data) 51 : action_(action), 52 data_(data) {} 53 54 const std::string data() const { return data_; } 55 const ActionType type() const { return action_; } 56 57 private: 58 ActionType action_; 59 std::string data_; 60 }; 61 62 63 // This had to be split out into a separate class because I couldn't 64 // make a the testing::Test class refcounted. 65 class ListenSocketTester : 66 public ListenSocket::ListenSocketDelegate, 67 public base::RefCountedThreadSafe<ListenSocketTester> { 68 69 protected: 70 friend class base::RefCountedThreadSafe<ListenSocketTester>; 71 72 virtual ~ListenSocketTester() {} 73 74 virtual ListenSocket* DoListen(); 75 76 public: 77 ListenSocketTester() 78 : thread_(NULL), 79 loop_(NULL), 80 server_(NULL), 81 connection_(NULL){ 82 } 83 84 virtual void SetUp(); 85 virtual void TearDown(); 86 87 void ReportAction(const ListenSocketTestAction& action); 88 bool NextAction(int timeout); 89 90 // read all pending data from the test socket 91 int ClearTestSocket(); 92 // Release the connection and server sockets 93 void Shutdown(); 94 void Listen(); 95 void SendFromTester(); 96 virtual void DidAccept(ListenSocket *server, ListenSocket *connection); 97 virtual void DidRead(ListenSocket *connection, const std::string& data); 98 virtual void DidClose(ListenSocket *sock); 99 virtual bool Send(SOCKET sock, const std::string& str); 100 // verify the send/read from client to server 101 void TestClientSend(); 102 // verify send/read of a longer string 103 void TestClientSendLong(); 104 // verify a send/read from server to client 105 void TestServerSend(); 106 107 #if defined(OS_WIN) 108 CRITICAL_SECTION lock_; 109 HANDLE semaphore_; 110 #elif defined(OS_POSIX) 111 pthread_mutex_t lock_; 112 sem_t* semaphore_; 113 #endif 114 115 scoped_ptr<base::Thread> thread_; 116 MessageLoopForIO* loop_; 117 ListenSocket* server_; 118 ListenSocket* connection_; 119 ListenSocketTestAction last_action_; 120 std::deque<ListenSocketTestAction> queue_; 121 SOCKET test_socket_; 122 static const int kTestPort; 123 }; 124 125 #endif // NET_BASE_LISTEN_SOCKET_UNITTEST_H_ 126