OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_LISTEN_SOCKET_UNITTEST_H_ | 5 #ifndef NET_BASE_LISTEN_SOCKET_UNITTEST_H_ |
6 #define NET_BASE_LISTEN_SOCKET_UNITTEST_H_ | 6 #define NET_BASE_LISTEN_SOCKET_UNITTEST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 std::string data_; | 61 std::string data_; |
62 }; | 62 }; |
63 | 63 |
64 | 64 |
65 // This had to be split out into a separate class because I couldn't | 65 // This had to be split out into a separate class because I couldn't |
66 // make a the testing::Test class refcounted. | 66 // make a the testing::Test class refcounted. |
67 class ListenSocketTester : | 67 class ListenSocketTester : |
68 public ListenSocket::ListenSocketDelegate, | 68 public ListenSocket::ListenSocketDelegate, |
69 public base::RefCountedThreadSafe<ListenSocketTester> { | 69 public base::RefCountedThreadSafe<ListenSocketTester> { |
70 | 70 |
71 protected: | 71 public: |
72 friend class base::RefCountedThreadSafe<ListenSocketTester>; | 72 ListenSocketTester(); |
73 | 73 |
74 virtual ~ListenSocketTester() {} | 74 void SetUp(); |
75 | 75 void TearDown(); |
76 virtual ListenSocket* DoListen(); | |
77 | |
78 public: | |
79 ListenSocketTester() | |
80 : thread_(NULL), | |
81 loop_(NULL), | |
82 server_(NULL), | |
83 connection_(NULL), | |
84 cv_(&lock_) { | |
85 } | |
86 | |
87 virtual void SetUp(); | |
88 virtual void TearDown(); | |
89 | 76 |
90 void ReportAction(const ListenSocketTestAction& action); | 77 void ReportAction(const ListenSocketTestAction& action); |
91 void NextAction(); | 78 void NextAction(); |
92 | 79 |
93 // read all pending data from the test socket | 80 // read all pending data from the test socket |
94 int ClearTestSocket(); | 81 int ClearTestSocket(); |
95 // Release the connection and server sockets | 82 // Release the connection and server sockets |
96 void Shutdown(); | 83 void Shutdown(); |
97 void Listen(); | 84 void Listen(); |
98 void SendFromTester(); | 85 void SendFromTester(); |
99 virtual void DidAccept(ListenSocket *server, ListenSocket *connection); | |
100 virtual void DidRead(ListenSocket *connection, const char* data, int len); | |
101 virtual void DidClose(ListenSocket *sock); | |
102 virtual bool Send(SOCKET sock, const std::string& str); | |
103 // verify the send/read from client to server | 86 // verify the send/read from client to server |
104 void TestClientSend(); | 87 void TestClientSend(); |
105 // verify send/read of a longer string | 88 // verify send/read of a longer string |
106 void TestClientSendLong(); | 89 void TestClientSendLong(); |
107 // verify a send/read from server to client | 90 // verify a send/read from server to client |
108 void TestServerSend(); | 91 void TestServerSend(); |
109 | 92 |
| 93 virtual bool Send(SOCKET sock, const std::string& str); |
| 94 |
| 95 // ListenSocket::ListenSocketDelegate: |
| 96 virtual void DidAccept(ListenSocket *server, ListenSocket *connection); |
| 97 virtual void DidRead(ListenSocket *connection, const char* data, int len); |
| 98 virtual void DidClose(ListenSocket *sock); |
| 99 |
110 scoped_ptr<base::Thread> thread_; | 100 scoped_ptr<base::Thread> thread_; |
111 MessageLoopForIO* loop_; | 101 MessageLoopForIO* loop_; |
112 ListenSocket* server_; | 102 ListenSocket* server_; |
113 ListenSocket* connection_; | 103 ListenSocket* connection_; |
114 ListenSocketTestAction last_action_; | 104 ListenSocketTestAction last_action_; |
115 | 105 |
116 SOCKET test_socket_; | 106 SOCKET test_socket_; |
117 static const int kTestPort; | 107 static const int kTestPort; |
118 | 108 |
119 base::Lock lock_; // protects |queue_| and wraps |cv_| | 109 base::Lock lock_; // protects |queue_| and wraps |cv_| |
120 base::ConditionVariable cv_; | 110 base::ConditionVariable cv_; |
121 std::deque<ListenSocketTestAction> queue_; | 111 std::deque<ListenSocketTestAction> queue_; |
| 112 |
| 113 protected: |
| 114 friend class base::RefCountedThreadSafe<ListenSocketTester>; |
| 115 |
| 116 virtual ~ListenSocketTester(); |
| 117 |
| 118 virtual ListenSocket* DoListen(); |
122 }; | 119 }; |
123 | 120 |
124 #endif // NET_BASE_LISTEN_SOCKET_UNITTEST_H_ | 121 #endif // NET_BASE_LISTEN_SOCKET_UNITTEST_H_ |
OLD | NEW |