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.
5 #include "components/pairing/message_buffer.h"
7 #include "net/base/io_buffer.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace pairing_chromeos
{
12 typedef testing::Test MessageBufferTest
;
14 TEST_F(MessageBufferTest
, BasicReadWrite
) {
15 MessageBuffer message_buffer
;
16 scoped_refptr
<net::IOBuffer
> io_buffer(new net::IOBuffer(3));
17 io_buffer
->data()[0] = 3;
18 io_buffer
->data()[1] = 1;
19 io_buffer
->data()[2] = 4;
21 message_buffer
.AddIOBuffer(io_buffer
, 3);
23 EXPECT_EQ(message_buffer
.AvailableBytes(), 3);
25 message_buffer
.ReadBytes(&data
, 1);
27 EXPECT_EQ(message_buffer
.AvailableBytes(), 2);
28 message_buffer
.ReadBytes(&data
, 1);
30 EXPECT_EQ(message_buffer
.AvailableBytes(), 1);
31 message_buffer
.ReadBytes(&data
, 1);
33 EXPECT_EQ(message_buffer
.AvailableBytes(), 0);
36 TEST_F(MessageBufferTest
, SplitBuffer
) {
37 MessageBuffer message_buffer
;
38 scoped_refptr
<net::IOBuffer
> io_buffer0(new net::IOBuffer(1));
39 io_buffer0
->data()[0] = 3;
41 scoped_refptr
<net::IOBuffer
> io_buffer1(new net::IOBuffer(2));
42 io_buffer1
->data()[0] = 1;
43 io_buffer1
->data()[1] = 4;
45 message_buffer
.AddIOBuffer(io_buffer0
, 1);
46 message_buffer
.AddIOBuffer(io_buffer1
, 2);
48 EXPECT_EQ(message_buffer
.AvailableBytes(), 3);
50 message_buffer
.ReadBytes(data
, 3);
51 EXPECT_EQ(message_buffer
.AvailableBytes(), 0);
52 EXPECT_EQ(data
[0], 3);
53 EXPECT_EQ(data
[1], 1);
54 EXPECT_EQ(data
[2], 4);
57 TEST_F(MessageBufferTest
, EmptyBuffer
) {
58 MessageBuffer message_buffer
;
59 scoped_refptr
<net::IOBuffer
> io_buffer0(new net::IOBuffer(1));
60 io_buffer0
->data()[0] = 3;
62 scoped_refptr
<net::IOBuffer
> io_buffer1(new net::IOBuffer(0));
63 scoped_refptr
<net::IOBuffer
> io_buffer2(new net::IOBuffer(2));
64 io_buffer2
->data()[0] = 1;
65 io_buffer2
->data()[1] = 4;
67 message_buffer
.AddIOBuffer(io_buffer0
, 1);
68 message_buffer
.AddIOBuffer(io_buffer1
, 0);
69 message_buffer
.AddIOBuffer(io_buffer2
, 2);
71 EXPECT_EQ(message_buffer
.AvailableBytes(), 3);
73 message_buffer
.ReadBytes(&data
, 1);
75 EXPECT_EQ(message_buffer
.AvailableBytes(), 2);
76 message_buffer
.ReadBytes(&data
, 1);
78 EXPECT_EQ(message_buffer
.AvailableBytes(), 1);
79 message_buffer
.ReadBytes(&data
, 1);
81 EXPECT_EQ(message_buffer
.AvailableBytes(), 0);
84 } // namespace pairing_chromeos