Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 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" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 6 | #include "base/memory/raw_ptr.h" |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 7 | |
| 8 | #include "base/bind.h" |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 9 | #include "net/base/io_buffer.h" |
| 10 | #include "net/base/net_errors.h" |
Ramin Halavati | a621c1f | 2018-02-14 20:50:46 | [diff] [blame] | 11 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 12 | #include "remoting/proto/video.pb.h" |
| 13 | #include "remoting/protocol/message_pipe.h" |
| 14 | #include "remoting/protocol/message_serialization.h" |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 15 | #include "remoting/protocol/p2p_datagram_socket.h" |
| 16 | #include "remoting/protocol/p2p_stream_socket.h" |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 19 | namespace remoting::protocol { |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 20 | |
sergeyu | aa22c08 | 2015-07-20 19:41:13 | [diff] [blame] | 21 | StreamConnectionTester::StreamConnectionTester(P2PStreamSocket* client_socket, |
| 22 | P2PStreamSocket* host_socket, |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 23 | int message_size, |
| 24 | int message_count) |
Gabriel Charette | b79557e | 2019-06-20 19:17:00 | [diff] [blame] | 25 | : host_socket_(host_socket), |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 26 | client_socket_(client_socket), |
| 27 | message_size_(message_size), |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 28 | test_data_size_(message_size * message_count), |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 29 | write_errors_(0), |
fdoray | 6d056ff | 2016-07-04 21:56:42 | [diff] [blame] | 30 | read_errors_(0) {} |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 31 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 32 | StreamConnectionTester::~StreamConnectionTester() = default; |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 33 | |
Gabriel Charette | b79557e | 2019-06-20 19:17:00 | [diff] [blame] | 34 | void StreamConnectionTester::Start(base::OnceClosure on_done) { |
| 35 | on_done_ = std::move(on_done); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 36 | InitBuffers(); |
| 37 | DoRead(); |
| 38 | DoWrite(); |
| 39 | } |
| 40 | |
| 41 | void StreamConnectionTester::CheckResults() { |
| 42 | EXPECT_EQ(0, write_errors_); |
| 43 | EXPECT_EQ(0, read_errors_); |
| 44 | |
| 45 | ASSERT_EQ(test_data_size_, input_buffer_->offset()); |
| 46 | |
| 47 | output_buffer_->SetOffset(0); |
| 48 | ASSERT_EQ(test_data_size_, output_buffer_->size()); |
| 49 | |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 50 | EXPECT_EQ(0, memcmp(output_buffer_->data(), input_buffer_->StartOfBuffer(), |
| 51 | test_data_size_)); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void StreamConnectionTester::Done() { |
Gabriel Charette | b79557e | 2019-06-20 19:17:00 | [diff] [blame] | 55 | std::move(on_done_).Run(); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void StreamConnectionTester::InitBuffers() { |
Victor Costan | cd43978 | 2018-08-30 07:27:57 | [diff] [blame] | 59 | output_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>( |
| 60 | base::MakeRefCounted<net::IOBuffer>(test_data_size_), test_data_size_); |
[email protected] | 16934f3 | 2011-12-13 22:04:38 | [diff] [blame] | 61 | for (int i = 0; i < test_data_size_; ++i) { |
| 62 | output_buffer_->data()[i] = static_cast<char>(i); |
| 63 | } |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 64 | |
Victor Costan | 8a7a19a | 2018-09-10 21:57:16 | [diff] [blame] | 65 | input_buffer_ = base::MakeRefCounted<net::GrowableIOBuffer>(); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void StreamConnectionTester::DoWrite() { |
| 69 | int result = 1; |
| 70 | while (result > 0) { |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 71 | if (output_buffer_->BytesRemaining() == 0) { |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 72 | break; |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 73 | } |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 74 | |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 75 | int bytes_to_write = |
| 76 | std::min(output_buffer_->BytesRemaining(), message_size_); |
Jan Wilken Dörrie | a0e772a | 2020-04-01 18:28:19 | [diff] [blame] | 77 | result = |
| 78 | client_socket_->Write(output_buffer_.get(), bytes_to_write, |
| 79 | base::BindOnce(&StreamConnectionTester::OnWritten, |
| 80 | base::Unretained(this)), |
| 81 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 82 | HandleWriteResult(result); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void StreamConnectionTester::OnWritten(int result) { |
| 87 | HandleWriteResult(result); |
| 88 | DoWrite(); |
| 89 | } |
| 90 | |
| 91 | void StreamConnectionTester::HandleWriteResult(int result) { |
| 92 | if (result <= 0 && result != net::ERR_IO_PENDING) { |
| 93 | LOG(ERROR) << "Received error " << result << " when trying to write"; |
| 94 | write_errors_++; |
| 95 | Done(); |
| 96 | } else if (result > 0) { |
| 97 | output_buffer_->DidConsume(result); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void StreamConnectionTester::DoRead() { |
| 102 | int result = 1; |
| 103 | while (result > 0) { |
| 104 | input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); |
Jan Wilken Dörrie | a0e772a | 2020-04-01 18:28:19 | [diff] [blame] | 105 | result = host_socket_->Read(input_buffer_.get(), message_size_, |
| 106 | base::BindOnce(&StreamConnectionTester::OnRead, |
| 107 | base::Unretained(this))); |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 108 | HandleReadResult(result); |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | void StreamConnectionTester::OnRead(int result) { |
| 113 | HandleReadResult(result); |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 114 | if (!on_done_.is_null()) { |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 115 | DoRead(); // Don't try to read again when we are done reading. |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 116 | } |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void StreamConnectionTester::HandleReadResult(int result) { |
| 120 | if (result <= 0 && result != net::ERR_IO_PENDING) { |
| 121 | LOG(ERROR) << "Received error " << result << " when trying to read"; |
| 122 | read_errors_++; |
| 123 | Done(); |
| 124 | } else if (result > 0) { |
| 125 | // Allocate memory for the next read. |
| 126 | input_buffer_->set_offset(input_buffer_->offset() + result); |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 127 | if (input_buffer_->offset() == test_data_size_) { |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 128 | Done(); |
Joe Downing | 353ba2c7 | 2023-01-11 22:37:34 | [diff] [blame] | 129 | } |
[email protected] | 57651a8 | 2011-12-13 03:54:28 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 133 | class MessagePipeConnectionTester::MessageSender |
| 134 | : public MessagePipe::EventHandler { |
| 135 | public: |
| 136 | MessageSender(MessagePipe* pipe, int message_size, int message_count) |
| 137 | : pipe_(pipe), |
| 138 | message_size_(message_size), |
| 139 | message_count_(message_count) {} |
| 140 | |
| 141 | void Start() { pipe_->Start(this); } |
| 142 | |
| 143 | const std::vector<std::unique_ptr<VideoPacket>>& sent_messages() { |
| 144 | return sent_messages_; |
| 145 | } |
| 146 | |
| 147 | // MessagePipe::EventHandler interface. |
| 148 | void OnMessagePipeOpen() override { |
| 149 | for (int i = 0; i < message_count_; ++i) { |
| 150 | std::unique_ptr<VideoPacket> message(new VideoPacket()); |
| 151 | message->mutable_data()->resize(message_size_); |
| 152 | for (int p = 0; p < message_size_; ++p) { |
| 153 | message->mutable_data()[0] = static_cast<char>(i + p); |
| 154 | } |
Evan Stade | 86dadf15 | 2020-03-16 20:06:33 | [diff] [blame] | 155 | pipe_->Send(message.get(), {}); |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 156 | sent_messages_.push_back(std::move(message)); |
| 157 | } |
| 158 | } |
| 159 | void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override { |
| 160 | NOTREACHED(); |
| 161 | } |
| 162 | void OnMessagePipeClosed() override { NOTREACHED(); } |
| 163 | |
| 164 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 165 | raw_ptr<MessagePipe> pipe_; |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 166 | int message_size_; |
| 167 | int message_count_; |
| 168 | |
| 169 | std::vector<std::unique_ptr<VideoPacket>> sent_messages_; |
| 170 | }; |
| 171 | |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 172 | MessagePipeConnectionTester::MessagePipeConnectionTester( |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 173 | MessagePipe* host_pipe, |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 174 | MessagePipe* client_pipe, |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 175 | int message_size, |
| 176 | int message_count) |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 177 | : client_pipe_(client_pipe), |
| 178 | sender_(new MessageSender(host_pipe, message_size, message_count)) {} |
| 179 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 180 | MessagePipeConnectionTester::~MessagePipeConnectionTester() = default; |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 181 | |
| 182 | void MessagePipeConnectionTester::RunAndCheckResults() { |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 183 | sender_->Start(); |
| 184 | client_pipe_->Start(this); |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 185 | |
| 186 | run_loop_.Run(); |
| 187 | |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 188 | ASSERT_EQ(sender_->sent_messages().size(), received_messages_.size()); |
| 189 | for (size_t i = 0; i < sender_->sent_messages().size(); ++i) { |
| 190 | EXPECT_TRUE(sender_->sent_messages()[i]->data() == |
| 191 | received_messages_[i]->data()); |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 195 | void MessagePipeConnectionTester::OnMessagePipeOpen() {} |
| 196 | |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 197 | void MessagePipeConnectionTester::OnMessageReceived( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 198 | std::unique_ptr<CompoundBuffer> message) { |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 199 | received_messages_.push_back(ParseMessage<VideoPacket>(message.get())); |
sergeyu | 5b4667b | 2017-03-17 19:12:48 | [diff] [blame] | 200 | if (received_messages_.size() >= sender_->sent_messages().size()) { |
sergeyu | 786463e | 2016-02-04 18:46:10 | [diff] [blame] | 201 | run_loop_.Quit(); |
| 202 | } |
| 203 | } |
| 204 | |
sergeyu | d059c46 | 2016-07-20 19:34:10 | [diff] [blame] | 205 | void MessagePipeConnectionTester::OnMessagePipeClosed() { |
| 206 | run_loop_.Quit(); |
| 207 | FAIL(); |
| 208 | } |
| 209 | |
Joe Downing | 39d710e | 2022-08-25 20:11:45 | [diff] [blame] | 210 | } // namespace remoting::protocol |