1 // Copyright 2014 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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_piece.h"
9 #include "device/serial/data_sender.h"
10 #include "device/serial/data_sink_receiver.h"
11 #include "device/serial/data_stream.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
14 #include "third_party/mojo/src/mojo/public/cpp/environment/async_waiter.h"
18 class DataSinkTest
: public testing::Test
{
22 EVENT_READ_BUFFER_READY
,
23 EVENT_CANCEL_RECEIVED
,
26 EVENT_CANCEL_COMPLETE
,
33 has_send_error_(false),
35 seen_connection_error_(false),
36 expected_event_(EVENT_NONE
) {}
38 void SetUp() override
{
39 message_loop_
.reset(new base::MessageLoop
);
40 mojo::InterfacePtr
<serial::DataSink
> sink_handle
;
41 sink_receiver_
= new DataSinkReceiver(
42 mojo::GetProxy(&sink_handle
),
43 base::Bind(&DataSinkTest::OnDataToRead
, base::Unretained(this)),
44 base::Bind(&DataSinkTest::OnCancel
, base::Unretained(this)),
45 base::Bind(&DataSinkTest::OnError
, base::Unretained(this)));
46 sender_
.reset(new DataSender(sink_handle
.Pass(), kBufferSize
, kFatalError
));
49 void TearDown() override
{
51 message_loop_
.reset();
52 if (sink_receiver_
.get())
53 EXPECT_TRUE(sink_receiver_
->HasOneRef());
56 void WaitForEvent(Event event
) {
57 expected_event_
= event
;
58 base::RunLoop run_loop
;
59 stop_run_loop_
= run_loop
.QuitClosure();
63 void EventReceived(Event event
) {
64 if (event
== expected_event_
&& !stop_run_loop_
.is_null())
69 seen_connection_error_
= true;
70 EventReceived(EVENT_ERROR
);
73 void ExpectDataAndReadAll(const base::StringPiece
& expected_data
) {
74 ExpectData(expected_data
, static_cast<uint32_t>(expected_data
.size()), 0);
77 void ExpectData(const base::StringPiece
& expected_data
,
78 uint32_t bytes_to_read
,
80 DCHECK(bytes_to_read
<= static_cast<uint32_t>(expected_data
.size()));
82 while (data
.size() < static_cast<size_t>(expected_data
.size())) {
84 WaitForEvent(EVENT_READ_BUFFER_READY
);
85 ASSERT_TRUE(read_buffer_
);
86 data
.append(read_buffer_
->GetData(), read_buffer_
->GetSize());
87 if (bytes_to_read
<= read_buffer_
->GetSize()) {
89 read_buffer_
->DoneWithError(bytes_to_read
, error
);
91 read_buffer_
->Done(bytes_to_read
);
95 bytes_to_read
-= read_buffer_
->GetSize();
96 read_buffer_
->Done(read_buffer_
->GetSize());
100 // If we terminate early, we may not see all of the data. In that case,
101 // check that the part we saw matches what we expected.
102 if (static_cast<uint32_t>(data
.size()) <
103 static_cast<uint32_t>(expected_data
.size()) &&
104 data
.size() >= bytes_to_read
) {
105 EXPECT_EQ(expected_data
.substr(0, data
.size()), data
);
108 EXPECT_EQ(expected_data
, data
);
111 void ExpectSendSuccess(uint32_t expected_bytes_sent
) {
113 WaitForEvent(EVENT_SEND_SUCCESS
);
114 EXPECT_EQ(expected_bytes_sent
, bytes_sent_
);
115 EXPECT_FALSE(has_send_error_
);
118 void ExpectSendError(uint32_t expected_bytes_sent
, int32_t expected_error
) {
122 WaitForEvent(EVENT_SEND_ERROR
);
123 EXPECT_EQ(expected_bytes_sent
, bytes_sent_
);
124 EXPECT_TRUE(has_send_error_
);
125 EXPECT_EQ(expected_error
, send_error_
);
128 void ExpectCancel(int32_t expected_error
) {
130 WaitForEvent(EVENT_CANCEL_RECEIVED
);
131 EXPECT_EQ(expected_error
, cancel_error_
);
134 void ExpectCancelResult() { WaitForEvent(EVENT_CANCEL_COMPLETE
); }
136 bool Send(const base::StringPiece
& data
) {
137 return sender_
->Send(
139 base::Bind(&DataSinkTest::OnDataSent
, base::Unretained(this)),
140 base::Bind(&DataSinkTest::OnSendError
, base::Unretained(this)));
143 void OnDataSent(uint32_t bytes_sent
) {
144 bytes_sent_
= bytes_sent
;
145 has_send_error_
= false;
146 EventReceived(EVENT_SEND_SUCCESS
);
149 void OnSendError(uint32_t bytes_sent
, int32_t error
) {
150 bytes_sent_
= bytes_sent
;
152 has_send_error_
= true;
153 EventReceived(EVENT_SEND_ERROR
);
156 void OnDataToRead(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
157 read_buffer_
= buffer
.Pass();
158 read_buffer_contents_
=
159 std::string(read_buffer_
->GetData(), read_buffer_
->GetSize());
160 EventReceived(EVENT_READ_BUFFER_READY
);
163 bool Cancel(int32_t error
) {
164 return sender_
->Cancel(
165 error
, base::Bind(&DataSinkTest::CancelAck
, base::Unretained(this)));
168 void CancelAck() { EventReceived(EVENT_CANCEL_COMPLETE
); }
170 void OnCancel(int32_t error
) {
171 cancel_error_
= error
;
172 EventReceived(EVENT_CANCEL_RECEIVED
);
176 static const int32_t kFatalError
;
177 static const uint32_t kBufferSize
;
178 scoped_ptr
<base::MessageLoop
> message_loop_
;
179 base::Closure stop_run_loop_
;
181 scoped_refptr
<DataSinkReceiver
> sink_receiver_
;
182 scoped_ptr
<DataSender
> sender_
;
184 uint32_t bytes_sent_
;
186 bool has_send_error_
;
187 int32_t cancel_error_
;
188 scoped_ptr
<ReadOnlyBuffer
> read_buffer_
;
189 std::string read_buffer_contents_
;
191 bool seen_connection_error_
;
193 Event expected_event_
;
196 DISALLOW_COPY_AND_ASSIGN(DataSinkTest
);
199 const int32_t DataSinkTest::kFatalError
= -10;
200 const uint32_t DataSinkTest::kBufferSize
= 100;
202 TEST_F(DataSinkTest
, Basic
) {
203 ASSERT_TRUE(Send("a"));
204 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("a"));
205 ExpectSendSuccess(1);
208 TEST_F(DataSinkTest
, LargeSend
) {
209 std::string pattern
= "1234567890";
211 for (uint32_t i
= 0; i
< kBufferSize
; i
++)
212 data
.append(pattern
);
213 ASSERT_TRUE(Send(data
));
214 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll(data
));
215 ExpectSendSuccess(static_cast<uint32_t>(data
.size()));
218 TEST_F(DataSinkTest
, ErrorAndAllData
) {
219 ASSERT_TRUE(Send("a"));
220 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 1, -1));
221 ExpectSendError(1, -1);
224 TEST_F(DataSinkTest
, ErrorAndPartialData
) {
225 ASSERT_TRUE(Send("ab"));
226 ASSERT_NO_FATAL_FAILURE(ExpectData("ab", 1, -1));
227 ExpectSendError(1, -1);
229 ASSERT_TRUE(Send("c"));
230 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
231 ExpectSendSuccess(1);
234 TEST_F(DataSinkTest
, ErrorAndPartialDataAtPacketBoundary
) {
235 ASSERT_TRUE(Send("ab"));
236 ASSERT_NO_FATAL_FAILURE(ExpectData("ab", 2, -1));
237 ExpectSendError(2, -1);
239 ASSERT_TRUE(Send("c"));
240 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
241 ExpectSendSuccess(1);
244 TEST_F(DataSinkTest
, ErrorInSecondPacket
) {
245 ASSERT_TRUE(Send("a"));
246 ASSERT_TRUE(Send("b"));
247 ASSERT_NO_FATAL_FAILURE(ExpectData("ab", 2, -1));
248 ExpectSendSuccess(1);
249 ExpectSendError(1, -1);
251 ASSERT_TRUE(Send("c"));
252 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
253 ExpectSendSuccess(1);
256 TEST_F(DataSinkTest
, ErrorBeforeLargeSend
) {
257 ASSERT_TRUE(Send("a"));
258 std::string pattern
= "123456789";
260 for (int i
= 0; i
< 100; i
++)
261 data
.append("1234567890");
262 ASSERT_TRUE(Send(data
));
263 ASSERT_NO_FATAL_FAILURE(ExpectData("a" + data
, 1, -1));
264 ExpectSendError(1, -1);
265 ExpectSendError(0, -1);
268 TEST_F(DataSinkTest
, ErrorOnly
) {
269 ASSERT_TRUE(Send("a"));
270 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 0, -1));
271 ExpectSendError(0, -1);
273 ASSERT_TRUE(Send("b"));
274 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("b"));
275 ExpectSendSuccess(1);
278 TEST_F(DataSinkTest
, Cancel
) {
279 ASSERT_TRUE(Send("a"));
280 WaitForEvent(EVENT_READ_BUFFER_READY
);
281 ASSERT_TRUE(Cancel(-2));
282 // Check that sends fail until the cancel operation completes.
283 EXPECT_FALSE(Send("b"));
284 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-2));
285 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 0, -1));
286 // Check that the error reported by the sink implementation is reported to the
287 // client - not the cancellation error.
288 ExpectSendError(0, -1);
289 ExpectCancelResult();
291 EXPECT_TRUE(Send("c"));
292 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
293 ExpectSendSuccess(1);
296 TEST_F(DataSinkTest
, CancelSinkSucceeds
) {
297 ASSERT_TRUE(Send("a"));
298 EXPECT_TRUE(Send("b"));
299 WaitForEvent(EVENT_READ_BUFFER_READY
);
300 ASSERT_TRUE(Cancel(-2));
301 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-2));
302 ASSERT_NO_FATAL_FAILURE(ExpectData("ab", 1, 0));
303 ExpectSendError(1, -2);
304 ExpectCancelResult();
306 EXPECT_TRUE(Send("c"));
307 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
308 ExpectSendSuccess(1);
311 TEST_F(DataSinkTest
, CancelTwice
) {
312 ASSERT_TRUE(Send("a"));
313 WaitForEvent(EVENT_READ_BUFFER_READY
);
314 ASSERT_TRUE(Cancel(-2));
315 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-2));
316 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 0, -2));
317 ExpectSendError(0, -2);
318 ExpectCancelResult();
320 EXPECT_TRUE(Send("b"));
321 WaitForEvent(EVENT_READ_BUFFER_READY
);
322 ASSERT_TRUE(Cancel(-3));
323 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-3));
324 ASSERT_NO_FATAL_FAILURE(ExpectData("b", 0, -3));
325 ExpectSendError(0, -3);
326 ExpectCancelResult();
328 EXPECT_TRUE(Send("c"));
329 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
330 ExpectSendSuccess(1);
333 TEST_F(DataSinkTest
, CancelTwiceWithNoSendBetween
) {
334 ASSERT_TRUE(Send("a"));
335 WaitForEvent(EVENT_READ_BUFFER_READY
);
336 ASSERT_TRUE(Cancel(-2));
337 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-2));
338 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 0, -2));
339 ExpectSendError(0, -2);
340 ExpectCancelResult();
341 ASSERT_TRUE(Cancel(-3));
342 ExpectCancelResult();
344 EXPECT_TRUE(Send("b"));
345 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("b"));
346 ExpectSendSuccess(1);
349 TEST_F(DataSinkTest
, CancelDuringError
) {
350 ASSERT_TRUE(Send("a"));
351 WaitForEvent(EVENT_READ_BUFFER_READY
);
352 ASSERT_TRUE(Cancel(-2));
353 ASSERT_NO_FATAL_FAILURE(ExpectData("a", 0, -1));
354 ExpectSendError(0, -1);
355 ExpectCancelResult();
357 EXPECT_TRUE(Send("a"));
358 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("a"));
359 ExpectSendSuccess(1);
362 TEST_F(DataSinkTest
, CancelWithoutSend
) {
363 ASSERT_TRUE(Cancel(-2));
364 ExpectCancelResult();
366 EXPECT_TRUE(Send("a"));
367 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("a"));
368 ExpectSendSuccess(1);
369 EXPECT_EQ(0, cancel_error_
);
372 TEST_F(DataSinkTest
, CancelPartialPacket
) {
373 ASSERT_TRUE(Send("ab"));
374 WaitForEvent(EVENT_READ_BUFFER_READY
);
375 ASSERT_TRUE(Cancel(-2));
376 ASSERT_NO_FATAL_FAILURE(ExpectCancel(-2));
377 ASSERT_NO_FATAL_FAILURE(ExpectData("ab", 1, -2));
378 ExpectSendError(1, -2);
379 ExpectCancelResult();
381 EXPECT_TRUE(Send("c"));
382 ASSERT_NO_FATAL_FAILURE(ExpectDataAndReadAll("c"));
383 ExpectSendSuccess(1);
386 TEST_F(DataSinkTest
, SinkReceiverShutdown
) {
387 ASSERT_TRUE(Send("a"));
388 ASSERT_TRUE(Send(std::string(kBufferSize
* 10, 'b')));
389 ASSERT_TRUE(Cancel(-1));
390 sink_receiver_
->ShutDown();
391 sink_receiver_
= NULL
;
392 ExpectSendError(0, kFatalError
);
393 ExpectSendError(0, kFatalError
);
394 ExpectCancelResult();
395 ASSERT_FALSE(Send("a"));
396 ASSERT_FALSE(Cancel(-1));
399 TEST_F(DataSinkTest
, SenderShutdown
) {
400 ASSERT_TRUE(Send("a"));
401 ASSERT_TRUE(Send(std::string(kBufferSize
* 10, 'b')));
402 ASSERT_TRUE(Cancel(-1));
404 ExpectSendError(0, kFatalError
);
405 ExpectSendError(0, kFatalError
);
406 ExpectCancelResult();
407 if (!seen_connection_error_
)
408 WaitForEvent(EVENT_ERROR
);
409 EXPECT_TRUE(seen_connection_error_
);
412 } // namespace device