OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | |
8 #include "base/bind.h" | 7 #include "base/bind.h" |
9 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
12 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
13 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
15 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
16 #include "net/dns/mock_host_resolver.h" | 15 #include "net/dns/mock_host_resolver.h" |
17 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // delete the connected_sock_, which will close it. | 50 // delete the connected_sock_, which will close it. |
52 connected_sock_.reset(); | 51 connected_sock_.reset(); |
53 } | 52 } |
54 | 53 |
55 void AcceptCallback(int res) { | 54 void AcceptCallback(int res) { |
56 ASSERT_EQ(OK, res); | 55 ASSERT_EQ(OK, res); |
57 connect_loop_.Quit(); | 56 connect_loop_.Quit(); |
58 } | 57 } |
59 | 58 |
60 int DrainClientSocket(IOBuffer* buf, | 59 int DrainClientSocket(IOBuffer* buf, |
61 uint32 buf_len, | 60 uint32_t buf_len, |
62 uint32 bytes_to_read, | 61 uint32_t bytes_to_read, |
63 TestCompletionCallback* callback); | 62 TestCompletionCallback* callback); |
64 | 63 |
65 // Establishes a connection to the server. | 64 // Establishes a connection to the server. |
66 void EstablishConnection(TestCompletionCallback* callback); | 65 void EstablishConnection(TestCompletionCallback* callback); |
67 | 66 |
68 // Sends a request from the client to the server socket. Makes the server read | 67 // Sends a request from the client to the server socket. Makes the server read |
69 // the request and send a response. | 68 // the request and send a response. |
70 void SendRequestAndResponse(); | 69 void SendRequestAndResponse(); |
71 | 70 |
72 // Makes |connected_sock_| to read |expected_bytes_read| bytes. Returns the | 71 // Makes |connected_sock_| to read |expected_bytes_read| bytes. Returns the |
73 // the data read as a string. | 72 // the data read as a string. |
74 std::string ReadServerData(int expected_bytes_read); | 73 std::string ReadServerData(int expected_bytes_read); |
75 | 74 |
76 // Sends server response. | 75 // Sends server response. |
77 void SendServerResponse(); | 76 void SendServerResponse(); |
78 | 77 |
79 void set_close_server_socket_on_next_send(bool close) { | 78 void set_close_server_socket_on_next_send(bool close) { |
80 close_server_socket_on_next_send_ = close; | 79 close_server_socket_on_next_send_ = close; |
81 } | 80 } |
82 | 81 |
83 protected: | 82 protected: |
84 base::RunLoop connect_loop_; | 83 base::RunLoop connect_loop_; |
85 uint16 listen_port_; | 84 uint16_t listen_port_; |
86 TestNetLog net_log_; | 85 TestNetLog net_log_; |
87 ClientSocketFactory* const socket_factory_; | 86 ClientSocketFactory* const socket_factory_; |
88 scoped_ptr<StreamSocket> sock_; | 87 scoped_ptr<StreamSocket> sock_; |
89 scoped_ptr<StreamSocket> connected_sock_; | 88 scoped_ptr<StreamSocket> connected_sock_; |
90 | 89 |
91 private: | 90 private: |
92 scoped_ptr<TCPServerSocket> listen_sock_; | 91 scoped_ptr<TCPServerSocket> listen_sock_; |
93 bool close_server_socket_on_next_send_; | 92 bool close_server_socket_on_next_send_; |
94 }; | 93 }; |
95 | 94 |
(...skipping 22 matching lines...) Expand all Loading... |
118 NULL, BoundNetLog()); | 117 NULL, BoundNetLog()); |
119 CHECK_EQ(ERR_IO_PENDING, rv); | 118 CHECK_EQ(ERR_IO_PENDING, rv); |
120 rv = callback.WaitForResult(); | 119 rv = callback.WaitForResult(); |
121 CHECK_EQ(rv, OK); | 120 CHECK_EQ(rv, OK); |
122 sock_ = socket_factory_->CreateTransportClientSocket(addr, &net_log_, | 121 sock_ = socket_factory_->CreateTransportClientSocket(addr, &net_log_, |
123 NetLog::Source()); | 122 NetLog::Source()); |
124 } | 123 } |
125 | 124 |
126 int TransportClientSocketTest::DrainClientSocket( | 125 int TransportClientSocketTest::DrainClientSocket( |
127 IOBuffer* buf, | 126 IOBuffer* buf, |
128 uint32 buf_len, | 127 uint32_t buf_len, |
129 uint32 bytes_to_read, | 128 uint32_t bytes_to_read, |
130 TestCompletionCallback* callback) { | 129 TestCompletionCallback* callback) { |
131 int rv = OK; | 130 int rv = OK; |
132 uint32 bytes_read = 0; | 131 uint32_t bytes_read = 0; |
133 | 132 |
134 while (bytes_read < bytes_to_read) { | 133 while (bytes_read < bytes_to_read) { |
135 rv = sock_->Read(buf, buf_len, callback->callback()); | 134 rv = sock_->Read(buf, buf_len, callback->callback()); |
136 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 135 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
137 rv = callback->GetResult(rv); | 136 rv = callback->GetResult(rv); |
138 EXPECT_GT(rv, 0); | 137 EXPECT_GT(rv, 0); |
139 bytes_read += rv; | 138 bytes_read += rv; |
140 } | 139 } |
141 | 140 |
142 return static_cast<int>(bytes_read); | 141 return static_cast<int>(bytes_read); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 EXPECT_TRUE( | 252 EXPECT_TRUE( |
254 LogContainsEndEvent(net_log_entries, -1, NetLog::TYPE_TCP_CONNECT)); | 253 LogContainsEndEvent(net_log_entries, -1, NetLog::TYPE_TCP_CONNECT)); |
255 | 254 |
256 sock_->Disconnect(); | 255 sock_->Disconnect(); |
257 EXPECT_FALSE(sock_->IsConnected()); | 256 EXPECT_FALSE(sock_->IsConnected()); |
258 } | 257 } |
259 | 258 |
260 TEST_P(TransportClientSocketTest, IsConnected) { | 259 TEST_P(TransportClientSocketTest, IsConnected) { |
261 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 260 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
262 TestCompletionCallback callback; | 261 TestCompletionCallback callback; |
263 uint32 bytes_read; | 262 uint32_t bytes_read; |
264 | 263 |
265 EXPECT_FALSE(sock_->IsConnected()); | 264 EXPECT_FALSE(sock_->IsConnected()); |
266 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 265 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
267 | 266 |
268 EstablishConnection(&callback); | 267 EstablishConnection(&callback); |
269 | 268 |
270 EXPECT_TRUE(sock_->IsConnected()); | 269 EXPECT_TRUE(sock_->IsConnected()); |
271 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | 270 EXPECT_TRUE(sock_->IsConnectedAndIdle()); |
272 | 271 |
273 // Send the request and wait for the server to respond. | 272 // Send the request and wait for the server to respond. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 317 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
319 } | 318 } |
320 | 319 |
321 TEST_P(TransportClientSocketTest, Read) { | 320 TEST_P(TransportClientSocketTest, Read) { |
322 TestCompletionCallback callback; | 321 TestCompletionCallback callback; |
323 EstablishConnection(&callback); | 322 EstablishConnection(&callback); |
324 | 323 |
325 SendRequestAndResponse(); | 324 SendRequestAndResponse(); |
326 | 325 |
327 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 326 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
328 uint32 bytes_read = | 327 uint32_t bytes_read = |
329 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); | 328 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); |
330 ASSERT_EQ(bytes_read, strlen(kServerReply)); | 329 ASSERT_EQ(bytes_read, strlen(kServerReply)); |
331 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); | 330 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); |
332 | 331 |
333 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 332 // All data has been read now. Read once more to force an ERR_IO_PENDING, and |
334 // then close the server socket, and note the close. | 333 // then close the server socket, and note the close. |
335 | 334 |
336 int rv = sock_->Read(buf.get(), 4096, callback.callback()); | 335 int rv = sock_->Read(buf.get(), 4096, callback.callback()); |
337 ASSERT_EQ(ERR_IO_PENDING, rv); | 336 ASSERT_EQ(ERR_IO_PENDING, rv); |
338 CloseServerSocket(); | 337 CloseServerSocket(); |
339 EXPECT_EQ(0, callback.WaitForResult()); | 338 EXPECT_EQ(0, callback.WaitForResult()); |
340 } | 339 } |
341 | 340 |
342 TEST_P(TransportClientSocketTest, Read_SmallChunks) { | 341 TEST_P(TransportClientSocketTest, Read_SmallChunks) { |
343 TestCompletionCallback callback; | 342 TestCompletionCallback callback; |
344 EstablishConnection(&callback); | 343 EstablishConnection(&callback); |
345 | 344 |
346 SendRequestAndResponse(); | 345 SendRequestAndResponse(); |
347 | 346 |
348 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 347 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
349 uint32 bytes_read = 0; | 348 uint32_t bytes_read = 0; |
350 while (bytes_read < strlen(kServerReply)) { | 349 while (bytes_read < strlen(kServerReply)) { |
351 int rv = sock_->Read(buf.get(), 1, callback.callback()); | 350 int rv = sock_->Read(buf.get(), 1, callback.callback()); |
352 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 351 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
353 | 352 |
354 rv = callback.GetResult(rv); | 353 rv = callback.GetResult(rv); |
355 | 354 |
356 ASSERT_EQ(1, rv); | 355 ASSERT_EQ(1, rv); |
357 bytes_read += rv; | 356 bytes_read += rv; |
358 } | 357 } |
359 | 358 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 | 463 |
465 // It's possible the read is blocked because it's already read all the data. | 464 // It's possible the read is blocked because it's already read all the data. |
466 // Close the server socket, so there will at least be a 0-byte read. | 465 // Close the server socket, so there will at least be a 0-byte read. |
467 CloseServerSocket(); | 466 CloseServerSocket(); |
468 | 467 |
469 rv = callback.WaitForResult(); | 468 rv = callback.WaitForResult(); |
470 EXPECT_GE(rv, 0); | 469 EXPECT_GE(rv, 0); |
471 } | 470 } |
472 | 471 |
473 } // namespace net | 472 } // namespace net |
OLD | NEW |