1/*
2 * libjingle
3 * Copyright 2004--2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/bytebuffer.h"
29#include "talk/base/byteorder.h"
30#include "talk/base/common.h"
31#include "talk/base/gunit.h"
32
33namespace talk_base {
34
35TEST(ByteBufferTest, TestByteOrder) {
36  uint16 n16 = 1;
37  uint32 n32 = 1;
38  uint64 n64 = 1;
39
40  EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
41  EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
42  EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
43
44  if (IsHostBigEndian()) {
45    // The host is the network (big) endian.
46    EXPECT_EQ(n16, HostToNetwork16(n16));
47    EXPECT_EQ(n32, HostToNetwork32(n32));
48    EXPECT_EQ(n64, HostToNetwork64(n64));
49
50    // GetBE converts big endian to little endian here.
51    EXPECT_EQ(n16 >> 8, GetBE16(&n16));
52    EXPECT_EQ(n32 >> 24, GetBE32(&n32));
53    EXPECT_EQ(n64 >> 56, GetBE64(&n64));
54  } else {
55    // The host is little endian.
56    EXPECT_NE(n16, HostToNetwork16(n16));
57    EXPECT_NE(n32, HostToNetwork32(n32));
58    EXPECT_NE(n64, HostToNetwork64(n64));
59
60    // GetBE converts little endian to big endian here.
61    EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
62    EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
63    EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
64
65    // GetBE converts little endian to big endian here.
66    EXPECT_EQ(n16 << 8, GetBE16(&n16));
67    EXPECT_EQ(n32 << 24, GetBE32(&n32));
68    EXPECT_EQ(n64 << 56, GetBE64(&n64));
69  }
70}
71
72TEST(ByteBufferTest, TestBufferLength) {
73  ByteBuffer buffer;
74  size_t size = 0;
75  EXPECT_EQ(size, buffer.Length());
76
77  buffer.WriteUInt8(1);
78  ++size;
79  EXPECT_EQ(size, buffer.Length());
80
81  buffer.WriteUInt16(1);
82  size += 2;
83  EXPECT_EQ(size, buffer.Length());
84
85  buffer.WriteUInt24(1);
86  size += 3;
87  EXPECT_EQ(size, buffer.Length());
88
89  buffer.WriteUInt32(1);
90  size += 4;
91  EXPECT_EQ(size, buffer.Length());
92
93  buffer.WriteUInt64(1);
94  size += 8;
95  EXPECT_EQ(size, buffer.Length());
96
97  EXPECT_TRUE(buffer.Consume(0));
98  EXPECT_EQ(size, buffer.Length());
99
100  EXPECT_TRUE(buffer.Consume(4));
101  size -= 4;
102  EXPECT_EQ(size, buffer.Length());
103}
104
105TEST(ByteBufferTest, TestGetSetReadPosition) {
106  ByteBuffer buffer("ABCDEF", 6);
107  EXPECT_EQ(6U, buffer.Length());
108  ByteBuffer::ReadPosition pos(buffer.GetReadPosition());
109  EXPECT_TRUE(buffer.SetReadPosition(pos));
110  EXPECT_EQ(6U, buffer.Length());
111  std::string read;
112  EXPECT_TRUE(buffer.ReadString(&read, 3));
113  EXPECT_EQ("ABC", read);
114  EXPECT_EQ(3U, buffer.Length());
115  EXPECT_TRUE(buffer.SetReadPosition(pos));
116  EXPECT_EQ(6U, buffer.Length());
117  read.clear();
118  EXPECT_TRUE(buffer.ReadString(&read, 3));
119  EXPECT_EQ("ABC", read);
120  EXPECT_EQ(3U, buffer.Length());
121  // For a resize by writing Capacity() number of bytes.
122  size_t capacity = buffer.Capacity();
123  buffer.ReserveWriteBuffer(buffer.Capacity());
124  EXPECT_EQ(capacity + 3U, buffer.Length());
125  EXPECT_FALSE(buffer.SetReadPosition(pos));
126  read.clear();
127  EXPECT_TRUE(buffer.ReadString(&read, 3));
128  EXPECT_EQ("DEF", read);
129}
130
131TEST(ByteBufferTest, TestReadWriteBuffer) {
132  ByteBuffer::ByteOrder orders[2] = { ByteBuffer::ORDER_HOST,
133                                      ByteBuffer::ORDER_NETWORK };
134  for (size_t i = 0; i < ARRAY_SIZE(orders); i++) {
135    ByteBuffer buffer(orders[i]);
136    EXPECT_EQ(orders[i], buffer.Order());
137    uint8 ru8;
138    EXPECT_FALSE(buffer.ReadUInt8(&ru8));
139
140    // Write and read uint8.
141    uint8 wu8 = 1;
142    buffer.WriteUInt8(wu8);
143    EXPECT_TRUE(buffer.ReadUInt8(&ru8));
144    EXPECT_EQ(wu8, ru8);
145    EXPECT_EQ(0U, buffer.Length());
146
147    // Write and read uint16.
148    uint16 wu16 = (1 << 8) + 1;
149    buffer.WriteUInt16(wu16);
150    uint16 ru16;
151    EXPECT_TRUE(buffer.ReadUInt16(&ru16));
152    EXPECT_EQ(wu16, ru16);
153    EXPECT_EQ(0U, buffer.Length());
154
155    // Write and read uint24.
156    uint32 wu24 = (3 << 16) + (2 << 8) + 1;
157    buffer.WriteUInt24(wu24);
158    uint32 ru24;
159    EXPECT_TRUE(buffer.ReadUInt24(&ru24));
160    EXPECT_EQ(wu24, ru24);
161    EXPECT_EQ(0U, buffer.Length());
162
163    // Write and read uint32.
164    uint32 wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
165    buffer.WriteUInt32(wu32);
166    uint32 ru32;
167    EXPECT_TRUE(buffer.ReadUInt32(&ru32));
168    EXPECT_EQ(wu32, ru32);
169    EXPECT_EQ(0U, buffer.Length());
170
171    // Write and read uint64.
172    uint32 another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
173    uint64 wu64 = (static_cast<uint64>(another32) << 32) + wu32;
174    buffer.WriteUInt64(wu64);
175    uint64 ru64;
176    EXPECT_TRUE(buffer.ReadUInt64(&ru64));
177    EXPECT_EQ(wu64, ru64);
178    EXPECT_EQ(0U, buffer.Length());
179
180    // Write and read string.
181    std::string write_string("hello");
182    buffer.WriteString(write_string);
183    std::string read_string;
184    EXPECT_TRUE(buffer.ReadString(&read_string, write_string.size()));
185    EXPECT_EQ(write_string, read_string);
186    EXPECT_EQ(0U, buffer.Length());
187
188    // Write and read bytes
189    char write_bytes[] = "foo";
190    buffer.WriteBytes(write_bytes, 3);
191    char read_bytes[3];
192    EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
193    for (int i = 0; i < 3; ++i) {
194      EXPECT_EQ(write_bytes[i], read_bytes[i]);
195    }
196    EXPECT_EQ(0U, buffer.Length());
197
198    // Write and read reserved buffer space
199    char* write_dst = buffer.ReserveWriteBuffer(3);
200    memcpy(write_dst, write_bytes, 3);
201    memset(read_bytes, 0, 3);
202    EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
203    for (int i = 0; i < 3; ++i) {
204      EXPECT_EQ(write_bytes[i], read_bytes[i]);
205    }
206    EXPECT_EQ(0U, buffer.Length());
207
208    // Write and read in order.
209    buffer.WriteUInt8(wu8);
210    buffer.WriteUInt16(wu16);
211    buffer.WriteUInt24(wu24);
212    buffer.WriteUInt32(wu32);
213    buffer.WriteUInt64(wu64);
214    EXPECT_TRUE(buffer.ReadUInt8(&ru8));
215    EXPECT_EQ(wu8, ru8);
216    EXPECT_TRUE(buffer.ReadUInt16(&ru16));
217    EXPECT_EQ(wu16, ru16);
218    EXPECT_TRUE(buffer.ReadUInt24(&ru24));
219    EXPECT_EQ(wu24, ru24);
220    EXPECT_TRUE(buffer.ReadUInt32(&ru32));
221    EXPECT_EQ(wu32, ru32);
222    EXPECT_TRUE(buffer.ReadUInt64(&ru64));
223    EXPECT_EQ(wu64, ru64);
224    EXPECT_EQ(0U, buffer.Length());
225  }
226}
227
228}  // namespace talk_base
229