Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: net/base/listen_socket_unittest.cc

Issue 6264013: Clean up net unit testing code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Re-virtualize TestDelegate::OnResponseCompleted Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "net/base/listen_socket_unittest.h" 5 #include "net/base/listen_socket_unittest.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <sys/types.h> 8 #include <sys/types.h>
9 9
10 #include "base/eintr_wrapper.h" 10 #include "base/eintr_wrapper.h"
11 #include "net/base/net_util.h" 11 #include "net/base/net_util.h"
12 #include "testing/platform_test.h" 12 #include "testing/platform_test.h"
13 13
14 const int ListenSocketTester::kTestPort = 9999; 14 const int ListenSocketTester::kTestPort = 9999;
15 15
16 static const int kReadBufSize = 1024; 16 static const int kReadBufSize = 1024;
17 static const char kHelloWorld[] = "HELLO, WORLD"; 17 static const char kHelloWorld[] = "HELLO, WORLD";
18 static const int kMaxQueueSize = 20; 18 static const int kMaxQueueSize = 20;
19 static const char kLoopback[] = "127.0.0.1"; 19 static const char kLoopback[] = "127.0.0.1";
20 static const int kDefaultTimeoutMs = 5000; 20 static const int kDefaultTimeoutMs = 5000;
21 21
22 ListenSocket* ListenSocketTester::DoListen() { 22 ListenSocketTester::ListenSocketTester()
23 return ListenSocket::Listen(kLoopback, kTestPort, this); 23 : thread_(NULL),
24 loop_(NULL),
25 server_(NULL),
26 connection_(NULL),
27 cv_(&lock_) {
24 } 28 }
25 29
26 void ListenSocketTester::SetUp() { 30 void ListenSocketTester::SetUp() {
27 base::Thread::Options options; 31 base::Thread::Options options;
28 options.message_loop_type = MessageLoop::TYPE_IO; 32 options.message_loop_type = MessageLoop::TYPE_IO;
29 thread_.reset(new base::Thread("socketio_test")); 33 thread_.reset(new base::Thread("socketio_test"));
30 thread_->StartWithOptions(options); 34 thread_->StartWithOptions(options);
31 loop_ = reinterpret_cast<MessageLoopForIO*>(thread_->message_loop()); 35 loop_ = reinterpret_cast<MessageLoopForIO*>(thread_->message_loop());
32 36
33 loop_->PostTask(FROM_HERE, NewRunnableMethod( 37 loop_->PostTask(FROM_HERE, NewRunnableMethod(
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 server_->AddRef(); 118 server_->AddRef();
115 ReportAction(ListenSocketTestAction(ACTION_LISTEN)); 119 ReportAction(ListenSocketTestAction(ACTION_LISTEN));
116 } 120 }
117 } 121 }
118 122
119 void ListenSocketTester::SendFromTester() { 123 void ListenSocketTester::SendFromTester() {
120 connection_->Send(kHelloWorld); 124 connection_->Send(kHelloWorld);
121 ReportAction(ListenSocketTestAction(ACTION_SEND)); 125 ReportAction(ListenSocketTestAction(ACTION_SEND));
122 } 126 }
123 127
124 void ListenSocketTester::DidAccept(ListenSocket *server,
125 ListenSocket *connection) {
126 connection_ = connection;
127 connection_->AddRef();
128 ReportAction(ListenSocketTestAction(ACTION_ACCEPT));
129 }
130
131 void ListenSocketTester::DidRead(ListenSocket *connection,
132 const char* data,
133 int len) {
134 std::string str(data, len);
135 ReportAction(ListenSocketTestAction(ACTION_READ, str));
136 }
137
138 void ListenSocketTester::DidClose(ListenSocket *sock) {
139 ReportAction(ListenSocketTestAction(ACTION_CLOSE));
140 }
141
142 bool ListenSocketTester::Send(SOCKET sock, const std::string& str) {
143 int len = static_cast<int>(str.length());
144 int send_len = HANDLE_EINTR(send(sock, str.data(), len, 0));
145 if (send_len == SOCKET_ERROR) {
146 LOG(ERROR) << "send failed: " << errno;
147 return false;
148 } else if (send_len != len) {
149 return false;
150 }
151 return true;
152 }
153
154 void ListenSocketTester::TestClientSend() { 128 void ListenSocketTester::TestClientSend() {
155 ASSERT_TRUE(Send(test_socket_, kHelloWorld)); 129 ASSERT_TRUE(Send(test_socket_, kHelloWorld));
156 NextAction(); 130 NextAction();
157 ASSERT_EQ(ACTION_READ, last_action_.type()); 131 ASSERT_EQ(ACTION_READ, last_action_.type());
158 ASSERT_EQ(last_action_.data(), kHelloWorld); 132 ASSERT_EQ(last_action_.data(), kHelloWorld);
159 } 133 }
160 134
161 void ListenSocketTester::TestClientSendLong() { 135 void ListenSocketTester::TestClientSendLong() {
162 size_t hello_len = strlen(kHelloWorld); 136 size_t hello_len = strlen(kHelloWorld);
163 std::string long_string; 137 std::string long_string;
(...skipping 29 matching lines...) Expand all
193 int r = HANDLE_EINTR(recv(test_socket_, buf, buf_len, 0)); 167 int r = HANDLE_EINTR(recv(test_socket_, buf, buf_len, 0));
194 ASSERT_GE(r, 0); 168 ASSERT_GE(r, 0);
195 recv_len += static_cast<unsigned>(r); 169 recv_len += static_cast<unsigned>(r);
196 if (!r) 170 if (!r)
197 break; 171 break;
198 } 172 }
199 buf[recv_len] = 0; 173 buf[recv_len] = 0;
200 ASSERT_STREQ(buf, kHelloWorld); 174 ASSERT_STREQ(buf, kHelloWorld);
201 } 175 }
202 176
177 bool ListenSocketTester::Send(SOCKET sock, const std::string& str) {
178 int len = static_cast<int>(str.length());
179 int send_len = HANDLE_EINTR(send(sock, str.data(), len, 0));
180 if (send_len == SOCKET_ERROR) {
181 LOG(ERROR) << "send failed: " << errno;
182 return false;
183 } else if (send_len != len) {
184 return false;
185 }
186 return true;
187 }
188
189 void ListenSocketTester::DidAccept(ListenSocket *server,
190 ListenSocket *connection) {
191 connection_ = connection;
192 connection_->AddRef();
193 ReportAction(ListenSocketTestAction(ACTION_ACCEPT));
194 }
195
196 void ListenSocketTester::DidRead(ListenSocket *connection,
197 const char* data,
198 int len) {
199 std::string str(data, len);
200 ReportAction(ListenSocketTestAction(ACTION_READ, str));
201 }
202
203 void ListenSocketTester::DidClose(ListenSocket *sock) {
204 ReportAction(ListenSocketTestAction(ACTION_CLOSE));
205 }
206
207 ListenSocketTester::~ListenSocketTester() {}
208
209 ListenSocket* ListenSocketTester::DoListen() {
210 return ListenSocket::Listen(kLoopback, kTestPort, this);
211 }
212
203 213
204 class ListenSocketTest: public PlatformTest { 214 class ListenSocketTest: public PlatformTest {
205 public: 215 public:
206 ListenSocketTest() { 216 ListenSocketTest() {
207 tester_ = NULL; 217 tester_ = NULL;
208 } 218 }
209 219
210 virtual void SetUp() { 220 virtual void SetUp() {
211 PlatformTest::SetUp(); 221 PlatformTest::SetUp();
212 tester_ = new ListenSocketTester(); 222 tester_ = new ListenSocketTester();
(...skipping 13 matching lines...) Expand all
226 tester_->TestClientSend(); 236 tester_->TestClientSend();
227 } 237 }
228 238
229 TEST_F(ListenSocketTest, ClientSendLong) { 239 TEST_F(ListenSocketTest, ClientSendLong) {
230 tester_->TestClientSendLong(); 240 tester_->TestClientSendLong();
231 } 241 }
232 242
233 TEST_F(ListenSocketTest, ServerSend) { 243 TEST_F(ListenSocketTest, ServerSend) {
234 tester_->TestServerSend(); 244 tester_->TestServerSend();
235 } 245 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698