Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "remoting/protocol/connection_tester.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "net/base/io_buffer.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/socket/stream_socket.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace remoting { | |
| 15 namespace protocol { | |
| 16 | |
| 17 StreamConnectionTester::StreamConnectionTester(net::StreamSocket* client_socket, | |
| 18 net::StreamSocket* host_socket, | |
| 19 int message_size, | |
| 20 int message_count) | |
| 21 : message_loop_(MessageLoop::current()), | |
| 22 host_socket_(host_socket), | |
| 23 client_socket_(client_socket), | |
| 24 message_size_(message_size), | |
| 25 message_count_(message_count), | |
| 26 test_data_size_(message_size * message_count), | |
| 27 done_(false), | |
| 28 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 29 write_cb_(this, &StreamConnectionTester::OnWritten)), | |
| 30 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 31 read_cb_(this, &StreamConnectionTester::OnRead)), | |
| 32 write_errors_(0), | |
| 33 read_errors_(0) { | |
| 34 } | |
| 35 | |
| 36 StreamConnectionTester::~StreamConnectionTester() { | |
| 37 } | |
| 38 | |
| 39 void StreamConnectionTester::Start() { | |
| 40 InitBuffers(); | |
| 41 DoRead(); | |
| 42 DoWrite(); | |
| 43 } | |
| 44 | |
| 45 void StreamConnectionTester::CheckResults() { | |
| 46 EXPECT_EQ(0, write_errors_); | |
| 47 EXPECT_EQ(0, read_errors_); | |
| 48 | |
| 49 ASSERT_EQ(test_data_size_, input_buffer_->offset()); | |
| 50 | |
| 51 output_buffer_->SetOffset(0); | |
| 52 ASSERT_EQ(test_data_size_, output_buffer_->size()); | |
| 53 | |
| 54 EXPECT_EQ(0, memcmp(output_buffer_->data(), | |
| 55 input_buffer_->StartOfBuffer(), test_data_size_)); | |
| 56 } | |
| 57 | |
| 58 void StreamConnectionTester::Done() { | |
| 59 done_ = true; | |
| 60 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 61 } | |
| 62 | |
| 63 void StreamConnectionTester::InitBuffers() { | |
| 64 output_buffer_ = new net::DrainableIOBuffer( | |
| 65 new net::IOBuffer(test_data_size_), test_data_size_); | |
| 66 | |
| 67 input_buffer_ = new net::GrowableIOBuffer(); | |
| 68 } | |
| 69 | |
| 70 void StreamConnectionTester::DoWrite() { | |
| 71 int result = 1; | |
| 72 while (result > 0) { | |
| 73 if (output_buffer_->BytesRemaining() == 0) | |
| 74 break; | |
| 75 | |
| 76 int bytes_to_write = std::min(output_buffer_->BytesRemaining(), | |
| 77 message_size_); | |
| 78 result = client_socket_->Write(output_buffer_, bytes_to_write, | |
| 79 &write_cb_); | |
| 80 HandleWriteResult(result); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void StreamConnectionTester::OnWritten(int result) { | |
| 85 HandleWriteResult(result); | |
| 86 DoWrite(); | |
| 87 } | |
| 88 | |
| 89 void StreamConnectionTester::HandleWriteResult(int result) { | |
| 90 if (result <= 0 && result != net::ERR_IO_PENDING) { | |
| 91 LOG(ERROR) << "Received error " << result << " when trying to write"; | |
| 92 write_errors_++; | |
| 93 Done(); | |
| 94 } else if (result > 0) { | |
| 95 output_buffer_->DidConsume(result); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void StreamConnectionTester::DoRead() { | |
| 100 int result = 1; | |
| 101 while (result > 0) { | |
| 102 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); | |
| 103 result = host_socket_->Read(input_buffer_, message_size_, &read_cb_); | |
| 104 HandleReadResult(result); | |
| 105 }; | |
| 106 } | |
| 107 | |
| 108 void StreamConnectionTester::OnRead(int result) { | |
| 109 HandleReadResult(result); | |
| 110 if (!done_) | |
| 111 DoRead(); // Don't try to read again when we are done reading. | |
| 112 } | |
| 113 | |
| 114 void StreamConnectionTester::HandleReadResult(int result) { | |
| 115 if (result <= 0 && result != net::ERR_IO_PENDING) { | |
| 116 if (!done_) { | |
|
Wez
2011/12/09 23:42:33
It looks like this will cause any errors reported
Sergey Ulanov
2011/12/12 22:52:00
There is no end-of-stream for P2P sockets: stream
Wez
2011/12/13 00:12:38
You're right re EOF, but my point is that this cod
Sergey Ulanov
2011/12/13 02:31:55
I see. I removed this if because we don't need it.
| |
| 117 LOG(ERROR) << "Received error " << result << " when trying to read"; | |
| 118 read_errors_++; | |
| 119 Done(); | |
| 120 } | |
| 121 } else if (result > 0) { | |
| 122 // Allocate memory for the next read. | |
| 123 input_buffer_->set_offset(input_buffer_->offset() + result); | |
| 124 if (input_buffer_->offset() == test_data_size_) | |
| 125 Done(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 DatagramConnectionTester::DatagramConnectionTester(net::Socket* client_socket, | |
| 130 net::Socket* host_socket, | |
| 131 int message_size, | |
| 132 int message_count, | |
| 133 int delay_ms) | |
| 134 : message_loop_(MessageLoop::current()), | |
| 135 host_socket_(host_socket), | |
| 136 client_socket_(client_socket), | |
| 137 message_size_(message_size), | |
| 138 message_count_(message_count), | |
| 139 delay_ms_(delay_ms), | |
| 140 done_(false), | |
| 141 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 142 write_cb_(this, &DatagramConnectionTester::OnWritten)), | |
| 143 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 144 read_cb_(this, &DatagramConnectionTester::OnRead)), | |
| 145 write_errors_(0), | |
| 146 read_errors_(0), | |
| 147 packets_sent_(0), | |
| 148 packets_received_(0), | |
| 149 broken_packets_(0) { | |
| 150 sent_packets_.resize(message_count_); | |
| 151 } | |
| 152 | |
| 153 DatagramConnectionTester::~DatagramConnectionTester() { | |
| 154 } | |
| 155 | |
| 156 void DatagramConnectionTester::Start() { | |
| 157 DoRead(); | |
| 158 DoWrite(); | |
| 159 } | |
| 160 | |
| 161 void DatagramConnectionTester::CheckResults() { | |
| 162 EXPECT_EQ(0, write_errors_); | |
| 163 EXPECT_EQ(0, read_errors_); | |
| 164 | |
| 165 EXPECT_EQ(0, broken_packets_); | |
| 166 | |
| 167 // Verify that we've received at least one packet. | |
| 168 EXPECT_GT(packets_received_, 0); | |
|
Wez
2011/12/09 23:42:33
You're testing for >0 on the basis that ==0 means
Sergey Ulanov
2011/12/12 22:52:00
We send 100 packets with 10ms delay between packet
Wez
2011/12/13 00:12:38
Fair enough. Sending 100 packets with 10ms delay
Sergey Ulanov
2011/12/13 02:31:55
I think it's useful to send more than one message
| |
| 169 LOG(INFO) << "Received " << packets_received_ << " packets out of " | |
| 170 << message_count_; | |
| 171 } | |
| 172 | |
| 173 void DatagramConnectionTester::Done() { | |
| 174 done_ = true; | |
| 175 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 176 } | |
| 177 | |
| 178 void DatagramConnectionTester::DoWrite() { | |
| 179 if (packets_sent_ >= message_count_) { | |
| 180 Done(); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_)); | |
| 185 memset(packet->data(), 123, message_size_); | |
| 186 sent_packets_[packets_sent_] = packet; | |
| 187 // Put index of this packet in the beginning of the packet body. | |
| 188 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); | |
| 189 | |
| 190 int result = client_socket_->Write(packet, message_size_, &write_cb_); | |
| 191 HandleWriteResult(result); | |
| 192 } | |
| 193 | |
| 194 void DatagramConnectionTester::OnWritten(int result) { | |
| 195 HandleWriteResult(result); | |
| 196 } | |
| 197 | |
| 198 void DatagramConnectionTester::HandleWriteResult(int result) { | |
| 199 if (result <= 0 && result != net::ERR_IO_PENDING) { | |
| 200 LOG(ERROR) << "Received error " << result << " when trying to write"; | |
| 201 write_errors_++; | |
| 202 Done(); | |
| 203 } else if (result > 0) { | |
| 204 EXPECT_EQ(message_size_, result); | |
| 205 packets_sent_++; | |
| 206 message_loop_->PostDelayedTask(FROM_HERE, base::Bind( | |
| 207 &DatagramConnectionTester::DoWrite, base::Unretained(this)), delay_ms_); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void DatagramConnectionTester::DoRead() { | |
| 212 int result = 1; | |
| 213 while (result > 0) { | |
| 214 int kReadSize = message_size_ * 2; | |
| 215 read_buffer_ = new net::IOBuffer(kReadSize); | |
| 216 | |
| 217 result = host_socket_->Read(read_buffer_, kReadSize, &read_cb_); | |
| 218 HandleReadResult(result); | |
| 219 }; | |
| 220 } | |
| 221 | |
| 222 void DatagramConnectionTester::OnRead(int result) { | |
| 223 HandleReadResult(result); | |
| 224 DoRead(); | |
| 225 } | |
| 226 | |
| 227 void DatagramConnectionTester::HandleReadResult(int result) { | |
| 228 if (result <= 0 && result != net::ERR_IO_PENDING) { | |
| 229 // Error will be received after the socket is closed. | |
| 230 if (!done_) { | |
| 231 LOG(ERROR) << "Received error " << result << " when trying to read"; | |
| 232 read_errors_++; | |
| 233 Done(); | |
| 234 } | |
| 235 } else if (result > 0) { | |
| 236 packets_received_++; | |
| 237 if (message_size_ != result) { | |
| 238 // Invalid packet size; | |
| 239 broken_packets_++; | |
| 240 } else { | |
| 241 // Validate packet body. | |
| 242 int packet_id; | |
| 243 memcpy(&packet_id, read_buffer_->data(), sizeof(packet_id)); | |
| 244 if (packet_id < 0 || packet_id >= message_count_) { | |
| 245 broken_packets_++; | |
| 246 } else { | |
| 247 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(), | |
| 248 message_size_) != 0) | |
| 249 broken_packets_++; | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 } // namespace protocol | |
| 256 } // namespace remoting | |
| OLD | NEW |