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