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.
5 #ifndef NET_BASE_LISTEN_SOCKET_UNITTEST_H_
6 #define NET_BASE_LISTEN_SOCKET_UNITTEST_H_
8 #include "build/build_config.h"
12 #elif defined(OS_POSIX)
13 #include <arpa/inet.h>
15 #include <sys/socket.h>
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"
43 class TCPListenSocketTestAction {
45 TCPListenSocketTestAction() : action_(ACTION_NONE) {}
46 explicit TCPListenSocketTestAction(ActionType action) : action_(action) {}
47 TCPListenSocketTestAction(ActionType action, std::string data)
51 const std::string data() const { return data_; }
52 ActionType type() const { return action_; }
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> {
67 TCPListenSocketTester();
72 void ReportAction(const TCPListenSocketTestAction& action);
75 // read all pending data from the test socket
76 int ClearTestSocket();
77 // Release the connection and server sockets
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();
90 virtual bool Send(SocketDescriptor sock, const std::string& str);
92 // StreamListenSocket::Delegate:
93 void DidAccept(StreamListenSocket* server,
94 scoped_ptr<StreamListenSocket> connection) override;
95 void DidRead(StreamListenSocket* connection,
98 void DidClose(StreamListenSocket* sock) override;
100 scoped_ptr<base::Thread> thread_;
101 base::MessageLoopForIO* loop_;
102 scoped_ptr<TCPListenSocket> server_;
103 scoped_ptr<StreamListenSocket> connection_;
104 TCPListenSocketTestAction last_action_;
106 SocketDescriptor test_socket_;
108 base::Lock lock_; // Protects |queue_| and |server_port_|. Wraps |cv_|.
109 base::ConditionVariable cv_;
110 std::deque<TCPListenSocketTestAction> queue_;
113 friend class base::RefCountedThreadSafe<TCPListenSocketTester>;
115 ~TCPListenSocketTester() override;
117 virtual scoped_ptr<TCPListenSocket> DoListen();
119 // Getters/setters for |server_port_|. They use |lock_| for thread safety.
121 void SetServerPort(int server_port);
123 // Port the server is using. Must have |lock_| to access. Set by Listen() on
124 // the server's thread.
130 #endif // NET_BASE_LISTEN_SOCKET_UNITTEST_H_