16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
26e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
36e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// found in the LICENSE file.
46e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/pairing/message_buffer.h"
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "net/base/io_buffer.h"
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace pairing_chromeos {
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)typedef testing::Test MessageBufferTest;
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(MessageBufferTest, BasicReadWrite) {
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MessageBuffer message_buffer;
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(3));
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer->data()[0] = 3;
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer->data()[1] = 1;
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer->data()[2] = 4;
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer, 3);
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 3);
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  char data = 0;
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 3);
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 2);
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 1);
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 1);
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 4);
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 0);
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(MessageBufferTest, SplitBuffer) {
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MessageBuffer message_buffer;
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer0(new net::IOBuffer(1));
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer0->data()[0] = 3;
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer1(new net::IOBuffer(2));
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer1->data()[0] = 1;
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer1->data()[1] = 4;
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer0, 1);
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer1, 2);
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 3);
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  char data[3];
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(data, 3);
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 0);
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data[0], 3);
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data[1], 1);
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data[2], 4);
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)TEST_F(MessageBufferTest, EmptyBuffer) {
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  MessageBuffer message_buffer;
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer0(new net::IOBuffer(1));
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer0->data()[0] = 3;
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer1(new net::IOBuffer(0));
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<net::IOBuffer> io_buffer2(new net::IOBuffer(2));
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer2->data()[0] = 1;
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  io_buffer2->data()[1] = 4;
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer0, 1);
686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer1, 0);
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.AddIOBuffer(io_buffer2, 2);
706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 3);
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  char data = 0;
736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 3);
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 2);
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 1);
786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 1);
796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  message_buffer.ReadBytes(&data, 1);
806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(data, 4);
816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(message_buffer.AvailableBytes(), 0);
826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}  // namespace pairing_chromeos
85