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

Side by Side Diff: content/browser/renderer_host/p2p/socket_host_test_utils.h

Issue 13926013: Fix P2PSocketHostTcp to handle async write correctly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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
« no previous file with comments | « content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
10 #include "base/sys_byteorder.h" 12 #include "base/sys_byteorder.h"
13 #include "base/thread_task_runner_handle.h"
11 #include "content/common/p2p_messages.h" 14 #include "content/common/p2p_messages.h"
15 #include "ipc/ipc_message_utils.h"
12 #include "ipc/ipc_sender.h" 16 #include "ipc/ipc_sender.h"
13 #include "ipc/ipc_message_utils.h"
14 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
15 #include "net/base/completion_callback.h" 18 #include "net/base/completion_callback.h"
16 #include "net/base/io_buffer.h" 19 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
18 #include "net/socket/stream_socket.h" 21 #include "net/socket/stream_socket.h"
19 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
21 24
22 namespace { 25 namespace {
23 26
(...skipping 18 matching lines...) Expand all
42 }; 45 };
43 46
44 MockIPCSender::MockIPCSender() { } 47 MockIPCSender::MockIPCSender() { }
45 MockIPCSender::~MockIPCSender() { } 48 MockIPCSender::~MockIPCSender() { }
46 49
47 class FakeSocket : public net::StreamSocket { 50 class FakeSocket : public net::StreamSocket {
48 public: 51 public:
49 FakeSocket(std::string* written_data); 52 FakeSocket(std::string* written_data);
50 virtual ~FakeSocket(); 53 virtual ~FakeSocket();
51 54
55 void set_async_write(bool async_write) { async_write_ = async_write; }
52 void AppendInputData(const char* data, int data_size); 56 void AppendInputData(const char* data, int data_size);
53 int input_pos() const { return input_pos_; } 57 int input_pos() const { return input_pos_; }
54 bool read_pending() const { return read_pending_; } 58 bool read_pending() const { return read_pending_; }
55 void SetPeerAddress(const net::IPEndPoint& peer_address); 59 void SetPeerAddress(const net::IPEndPoint& peer_address);
56 void SetLocalAddress(const net::IPEndPoint& local_address); 60 void SetLocalAddress(const net::IPEndPoint& local_address);
57 61
58 // net::Socket implementation. 62 // net::Socket implementation.
59 virtual int Read(net::IOBuffer* buf, int buf_len, 63 virtual int Read(net::IOBuffer* buf, int buf_len,
60 const net::CompletionCallback& callback) OVERRIDE; 64 const net::CompletionCallback& callback) OVERRIDE;
61 virtual int Write(net::IOBuffer* buf, int buf_len, 65 virtual int Write(net::IOBuffer* buf, int buf_len,
62 const net::CompletionCallback& callback) OVERRIDE; 66 const net::CompletionCallback& callback) OVERRIDE;
63 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; 67 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
64 virtual bool SetSendBufferSize(int32 size) OVERRIDE; 68 virtual bool SetSendBufferSize(int32 size) OVERRIDE;
65 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; 69 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
66 virtual void Disconnect() OVERRIDE; 70 virtual void Disconnect() OVERRIDE;
67 virtual bool IsConnected() const OVERRIDE; 71 virtual bool IsConnected() const OVERRIDE;
68 virtual bool IsConnectedAndIdle() const OVERRIDE; 72 virtual bool IsConnectedAndIdle() const OVERRIDE;
69 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE; 73 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE;
70 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; 74 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE;
71 virtual const net::BoundNetLog& NetLog() const OVERRIDE; 75 virtual const net::BoundNetLog& NetLog() const OVERRIDE;
72 virtual void SetSubresourceSpeculation() OVERRIDE; 76 virtual void SetSubresourceSpeculation() OVERRIDE;
73 virtual void SetOmniboxSpeculation() OVERRIDE; 77 virtual void SetOmniboxSpeculation() OVERRIDE;
74 virtual bool WasEverUsed() const OVERRIDE; 78 virtual bool WasEverUsed() const OVERRIDE;
75 virtual bool UsingTCPFastOpen() const OVERRIDE; 79 virtual bool UsingTCPFastOpen() const OVERRIDE;
76 virtual bool WasNpnNegotiated() const OVERRIDE; 80 virtual bool WasNpnNegotiated() const OVERRIDE;
77 virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE; 81 virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE;
78 virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; 82 virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE;
79 83
80 private: 84 private:
85 void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len,
86 const net::CompletionCallback& callback);
87
81 bool read_pending_; 88 bool read_pending_;
82 scoped_refptr<net::IOBuffer> read_buffer_; 89 scoped_refptr<net::IOBuffer> read_buffer_;
83 int read_buffer_size_; 90 int read_buffer_size_;
84 net::CompletionCallback read_callback_; 91 net::CompletionCallback read_callback_;
85 92
86 std::string* written_data_;
87 std::string input_data_; 93 std::string input_data_;
88 int input_pos_; 94 int input_pos_;
89 95
96 std::string* written_data_;
97 bool async_write_;
98 bool write_pending_;
99
90 net::IPEndPoint peer_address_; 100 net::IPEndPoint peer_address_;
91 net::IPEndPoint local_address_; 101 net::IPEndPoint local_address_;
92 102
93 net::BoundNetLog net_log_; 103 net::BoundNetLog net_log_;
94 }; 104 };
95 105
96 FakeSocket::FakeSocket(std::string* written_data) 106 FakeSocket::FakeSocket(std::string* written_data)
97 : read_pending_(false), 107 : read_pending_(false),
108 input_pos_(0),
98 written_data_(written_data), 109 written_data_(written_data),
99 input_pos_(0) { 110 async_write_(false),
111 write_pending_(false) {
100 } 112 }
101 113
102 FakeSocket::~FakeSocket() { } 114 FakeSocket::~FakeSocket() { }
103 115
104 void FakeSocket::AppendInputData(const char* data, int data_size) { 116 void FakeSocket::AppendInputData(const char* data, int data_size) {
105 input_data_.insert(input_data_.end(), data, data + data_size); 117 input_data_.insert(input_data_.end(), data, data + data_size);
106 // Complete pending read if any. 118 // Complete pending read if any.
107 if (read_pending_) { 119 if (read_pending_) {
108 read_pending_ = false; 120 read_pending_ = false;
109 int result = std::min(read_buffer_size_, 121 int result = std::min(read_buffer_size_,
(...skipping 30 matching lines...) Expand all
140 read_buffer_ = buf; 152 read_buffer_ = buf;
141 read_buffer_size_ = buf_len; 153 read_buffer_size_ = buf_len;
142 read_callback_ = callback; 154 read_callback_ = callback;
143 return net::ERR_IO_PENDING; 155 return net::ERR_IO_PENDING;
144 } 156 }
145 } 157 }
146 158
147 int FakeSocket::Write(net::IOBuffer* buf, int buf_len, 159 int FakeSocket::Write(net::IOBuffer* buf, int buf_len,
148 const net::CompletionCallback& callback) { 160 const net::CompletionCallback& callback) {
149 DCHECK(buf); 161 DCHECK(buf);
162 DCHECK(!write_pending_);
163
164 if (async_write_) {
165
166 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::Bind(
167 &FakeSocket::DoAsyncWrite, base::Unretained(this),
168 scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
169 write_pending_ = true;
170 return net::ERR_IO_PENDING;
171 }
172
150 if (written_data_) { 173 if (written_data_) {
151 written_data_->insert(written_data_->end(), 174 written_data_->insert(written_data_->end(),
152 buf->data(), buf->data() + buf_len); 175 buf->data(), buf->data() + buf_len);
153 } 176 }
154 return buf_len; 177 return buf_len;
155 } 178 }
156 179
180 void FakeSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len,
181 const net::CompletionCallback& callback) {
182 write_pending_ = false;
183
184 if (written_data_) {
185 written_data_->insert(written_data_->end(),
186 buf->data(), buf->data() + buf_len);
187 }
188 callback.Run(buf_len);
189 }
157 190
158 bool FakeSocket::SetReceiveBufferSize(int32 size) { 191 bool FakeSocket::SetReceiveBufferSize(int32 size) {
159 NOTIMPLEMENTED(); 192 NOTIMPLEMENTED();
160 return false; 193 return false;
161 } 194 }
162 bool FakeSocket::SetSendBufferSize(int32 size) { 195 bool FakeSocket::SetSendBufferSize(int32 size) {
163 NOTIMPLEMENTED(); 196 NOTIMPLEMENTED();
164 return false; 197 return false;
165 } 198 }
166 199
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 return false; 311 return false;
279 P2PMsg_OnIncomingTcpConnection::Param params; 312 P2PMsg_OnIncomingTcpConnection::Param params;
280 P2PMsg_OnIncomingTcpConnection::Read( 313 P2PMsg_OnIncomingTcpConnection::Read(
281 arg, &params); 314 arg, &params);
282 return params.b == address; 315 return params.b == address;
283 } 316 }
284 317
285 } // namespace 318 } // namespace
286 319
287 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_ 320 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TEST_UTILS_H_
OLDNEW
« no previous file with comments | « content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698