Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / connection_tester.cc
blob5a70f871c9fde1119d8d29cbbbf42889a03195bc
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 #include "remoting/protocol/connection_tester.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
11 #include "remoting/protocol/p2p_datagram_socket.h"
12 #include "remoting/protocol/p2p_stream_socket.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace remoting {
16 namespace protocol {
18 StreamConnectionTester::StreamConnectionTester(P2PStreamSocket* client_socket,
19 P2PStreamSocket* host_socket,
20 int message_size,
21 int message_count)
22 : message_loop_(base::MessageLoop::current()),
23 host_socket_(host_socket),
24 client_socket_(client_socket),
25 message_size_(message_size),
26 test_data_size_(message_size * message_count),
27 done_(false),
28 write_errors_(0),
29 read_errors_(0) {
32 StreamConnectionTester::~StreamConnectionTester() {
35 void StreamConnectionTester::Start() {
36 InitBuffers();
37 DoRead();
38 DoWrite();
41 void StreamConnectionTester::CheckResults() {
42 EXPECT_EQ(0, write_errors_);
43 EXPECT_EQ(0, read_errors_);
45 ASSERT_EQ(test_data_size_, input_buffer_->offset());
47 output_buffer_->SetOffset(0);
48 ASSERT_EQ(test_data_size_, output_buffer_->size());
50 EXPECT_EQ(0, memcmp(output_buffer_->data(),
51 input_buffer_->StartOfBuffer(), test_data_size_));
54 void StreamConnectionTester::Done() {
55 done_ = true;
56 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
59 void StreamConnectionTester::InitBuffers() {
60 output_buffer_ = new net::DrainableIOBuffer(
61 new net::IOBuffer(test_data_size_), test_data_size_);
62 for (int i = 0; i < test_data_size_; ++i) {
63 output_buffer_->data()[i] = static_cast<char>(i);
66 input_buffer_ = new net::GrowableIOBuffer();
69 void StreamConnectionTester::DoWrite() {
70 int result = 1;
71 while (result > 0) {
72 if (output_buffer_->BytesRemaining() == 0)
73 break;
75 int bytes_to_write = std::min(output_buffer_->BytesRemaining(),
76 message_size_);
77 result = client_socket_->Write(
78 output_buffer_.get(),
79 bytes_to_write,
80 base::Bind(&StreamConnectionTester::OnWritten, base::Unretained(this)));
81 HandleWriteResult(result);
85 void StreamConnectionTester::OnWritten(int result) {
86 HandleWriteResult(result);
87 DoWrite();
90 void StreamConnectionTester::HandleWriteResult(int result) {
91 if (result <= 0 && result != net::ERR_IO_PENDING) {
92 LOG(ERROR) << "Received error " << result << " when trying to write";
93 write_errors_++;
94 Done();
95 } else if (result > 0) {
96 output_buffer_->DidConsume(result);
100 void StreamConnectionTester::DoRead() {
101 int result = 1;
102 while (result > 0) {
103 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_);
104 result = host_socket_->Read(
105 input_buffer_.get(),
106 message_size_,
107 base::Bind(&StreamConnectionTester::OnRead, base::Unretained(this)));
108 HandleReadResult(result);
112 void StreamConnectionTester::OnRead(int result) {
113 HandleReadResult(result);
114 if (!done_)
115 DoRead(); // Don't try to read again when we are done reading.
118 void StreamConnectionTester::HandleReadResult(int result) {
119 if (result <= 0 && result != net::ERR_IO_PENDING) {
120 LOG(ERROR) << "Received error " << result << " when trying to read";
121 read_errors_++;
122 Done();
123 } else if (result > 0) {
124 // Allocate memory for the next read.
125 input_buffer_->set_offset(input_buffer_->offset() + result);
126 if (input_buffer_->offset() == test_data_size_)
127 Done();
131 DatagramConnectionTester::DatagramConnectionTester(
132 P2PDatagramSocket* client_socket,
133 P2PDatagramSocket* host_socket,
134 int message_size,
135 int message_count,
136 int delay_ms)
137 : message_loop_(base::MessageLoop::current()),
138 host_socket_(host_socket),
139 client_socket_(client_socket),
140 message_size_(message_size),
141 message_count_(message_count),
142 delay_ms_(delay_ms),
143 done_(false),
144 write_errors_(0),
145 read_errors_(0),
146 packets_sent_(0),
147 packets_received_(0),
148 bad_packets_received_(0) {
149 sent_packets_.resize(message_count_);
152 DatagramConnectionTester::~DatagramConnectionTester() {
155 void DatagramConnectionTester::Start() {
156 DoRead();
157 DoWrite();
160 void DatagramConnectionTester::CheckResults() {
161 EXPECT_EQ(0, write_errors_);
162 EXPECT_EQ(0, read_errors_);
164 EXPECT_EQ(0, bad_packets_received_);
166 // Verify that we've received at least one packet.
167 EXPECT_GT(packets_received_, 0);
168 VLOG(0) << "Received " << packets_received_ << " packets out of "
169 << message_count_;
172 void DatagramConnectionTester::Done() {
173 done_ = true;
174 message_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
177 void DatagramConnectionTester::DoWrite() {
178 if (packets_sent_ >= message_count_) {
179 Done();
180 return;
183 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_));
184 for (int i = 0; i < message_size_; ++i) {
185 packet->data()[i] = static_cast<char>(i);
187 sent_packets_[packets_sent_] = packet;
188 // Put index of this packet in the beginning of the packet body.
189 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_));
191 int result = client_socket_->Send(
192 packet.get(), message_size_,
193 base::Bind(&DatagramConnectionTester::OnWritten, base::Unretained(this)));
194 HandleWriteResult(result);
197 void DatagramConnectionTester::OnWritten(int result) {
198 HandleWriteResult(result);
201 void DatagramConnectionTester::HandleWriteResult(int result) {
202 if (result <= 0 && result != net::ERR_IO_PENDING) {
203 LOG(ERROR) << "Received error " << result << " when trying to write";
204 write_errors_++;
205 Done();
206 } else if (result > 0) {
207 EXPECT_EQ(message_size_, result);
208 packets_sent_++;
209 message_loop_->PostDelayedTask(
210 FROM_HERE,
211 base::Bind(&DatagramConnectionTester::DoWrite, base::Unretained(this)),
212 base::TimeDelta::FromMilliseconds(delay_ms_));
216 void DatagramConnectionTester::DoRead() {
217 int result = 1;
218 while (result > 0) {
219 int kReadSize = message_size_ * 2;
220 read_buffer_ = new net::IOBuffer(kReadSize);
222 result = host_socket_->Recv(
223 read_buffer_.get(), kReadSize,
224 base::Bind(&DatagramConnectionTester::OnRead, base::Unretained(this)));
225 HandleReadResult(result);
229 void DatagramConnectionTester::OnRead(int result) {
230 HandleReadResult(result);
231 DoRead();
234 void DatagramConnectionTester::HandleReadResult(int result) {
235 if (result <= 0 && result != net::ERR_IO_PENDING) {
236 // Error will be received after the socket is closed.
237 LOG(ERROR) << "Received error " << result << " when trying to read";
238 read_errors_++;
239 Done();
240 } else if (result > 0) {
241 packets_received_++;
242 if (message_size_ != result) {
243 // Invalid packet size;
244 bad_packets_received_++;
245 } else {
246 // Validate packet body.
247 int packet_id;
248 memcpy(&packet_id, read_buffer_->data(), sizeof(packet_id));
249 if (packet_id < 0 || packet_id >= message_count_) {
250 bad_packets_received_++;
251 } else {
252 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(),
253 message_size_) != 0)
254 bad_packets_received_++;
260 } // namespace protocol
261 } // namespace remoting