blob: bdbb159b66597316bbbd29b697b21d96a714de4e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
tfarina5237aaf2015-11-10 23:44:30 -080011#include "webrtc/base/arraysize.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include "webrtc/base/bytebuffer.h"
13#include "webrtc/base/byteorder.h"
14#include "webrtc/base/common.h"
15#include "webrtc/base/gunit.h"
16
17namespace rtc {
18
19TEST(ByteBufferTest, TestByteOrder) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020020 uint16_t n16 = 1;
21 uint32_t n32 = 1;
22 uint64_t n64 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24 EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
25 EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
26 EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
27
28 if (IsHostBigEndian()) {
29 // The host is the network (big) endian.
30 EXPECT_EQ(n16, HostToNetwork16(n16));
31 EXPECT_EQ(n32, HostToNetwork32(n32));
32 EXPECT_EQ(n64, HostToNetwork64(n64));
33
34 // GetBE converts big endian to little endian here.
35 EXPECT_EQ(n16 >> 8, GetBE16(&n16));
36 EXPECT_EQ(n32 >> 24, GetBE32(&n32));
37 EXPECT_EQ(n64 >> 56, GetBE64(&n64));
38 } else {
39 // The host is little endian.
40 EXPECT_NE(n16, HostToNetwork16(n16));
41 EXPECT_NE(n32, HostToNetwork32(n32));
42 EXPECT_NE(n64, HostToNetwork64(n64));
43
44 // GetBE converts little endian to big endian here.
45 EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
46 EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
47 EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
48
49 // GetBE converts little endian to big endian here.
50 EXPECT_EQ(n16 << 8, GetBE16(&n16));
51 EXPECT_EQ(n32 << 24, GetBE32(&n32));
52 EXPECT_EQ(n64 << 56, GetBE64(&n64));
53 }
54}
55
56TEST(ByteBufferTest, TestBufferLength) {
jbauchf1f87202016-03-30 06:43:37 -070057 ByteBufferWriter buffer;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 size_t size = 0;
59 EXPECT_EQ(size, buffer.Length());
60
61 buffer.WriteUInt8(1);
62 ++size;
63 EXPECT_EQ(size, buffer.Length());
64
65 buffer.WriteUInt16(1);
66 size += 2;
67 EXPECT_EQ(size, buffer.Length());
68
69 buffer.WriteUInt24(1);
70 size += 3;
71 EXPECT_EQ(size, buffer.Length());
72
73 buffer.WriteUInt32(1);
74 size += 4;
75 EXPECT_EQ(size, buffer.Length());
76
77 buffer.WriteUInt64(1);
78 size += 8;
79 EXPECT_EQ(size, buffer.Length());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080}
81
82TEST(ByteBufferTest, TestReadWriteBuffer) {
jbauchf1f87202016-03-30 06:43:37 -070083 ByteBufferWriter::ByteOrder orders[2] = { ByteBufferWriter::ORDER_HOST,
84 ByteBufferWriter::ORDER_NETWORK };
tfarina5237aaf2015-11-10 23:44:30 -080085 for (size_t i = 0; i < arraysize(orders); i++) {
jbauchf1f87202016-03-30 06:43:37 -070086 ByteBufferWriter buffer(orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 EXPECT_EQ(orders[i], buffer.Order());
jbauchf1f87202016-03-30 06:43:37 -070088 ByteBufferReader read_buf(nullptr, 0, orders[i]);
89 EXPECT_EQ(orders[i], read_buf.Order());
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 uint8_t ru8;
jbauchf1f87202016-03-30 06:43:37 -070091 EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 // Write and read uint8_t.
94 uint8_t wu8 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 buffer.WriteUInt8(wu8);
jbauchf1f87202016-03-30 06:43:37 -070096 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
97 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -070099 EXPECT_EQ(0U, read_buf1.Length());
100 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 // Write and read uint16_t.
103 uint16_t wu16 = (1 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 buffer.WriteUInt16(wu16);
jbauchf1f87202016-03-30 06:43:37 -0700105 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 uint16_t ru16;
jbauchf1f87202016-03-30 06:43:37 -0700107 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700109 EXPECT_EQ(0U, read_buf2.Length());
110 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112 // Write and read uint24.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 buffer.WriteUInt24(wu24);
jbauchf1f87202016-03-30 06:43:37 -0700115 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200116 uint32_t ru24;
jbauchf1f87202016-03-30 06:43:37 -0700117 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700119 EXPECT_EQ(0U, read_buf3.Length());
120 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121
Peter Boström0c4e06b2015-10-07 12:23:21 +0200122 // Write and read uint32_t.
123 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124 buffer.WriteUInt32(wu32);
jbauchf1f87202016-03-30 06:43:37 -0700125 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126 uint32_t ru32;
jbauchf1f87202016-03-30 06:43:37 -0700127 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700129 EXPECT_EQ(0U, read_buf3.Length());
130 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132 // Write and read uint64_t.
133 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
134 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700136 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 uint64_t ru64;
jbauchf1f87202016-03-30 06:43:37 -0700138 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700140 EXPECT_EQ(0U, read_buf5.Length());
141 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142
143 // Write and read string.
144 std::string write_string("hello");
145 buffer.WriteString(write_string);
jbauchf1f87202016-03-30 06:43:37 -0700146 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 std::string read_string;
jbauchf1f87202016-03-30 06:43:37 -0700148 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149 EXPECT_EQ(write_string, read_string);
jbauchf1f87202016-03-30 06:43:37 -0700150 EXPECT_EQ(0U, read_buf6.Length());
151 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152
153 // Write and read bytes
154 char write_bytes[] = "foo";
155 buffer.WriteBytes(write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700156 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 char read_bytes[3];
jbauchf1f87202016-03-30 06:43:37 -0700158 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159 for (int i = 0; i < 3; ++i) {
160 EXPECT_EQ(write_bytes[i], read_bytes[i]);
161 }
jbauchf1f87202016-03-30 06:43:37 -0700162 EXPECT_EQ(0U, read_buf7.Length());
163 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164
165 // Write and read reserved buffer space
166 char* write_dst = buffer.ReserveWriteBuffer(3);
167 memcpy(write_dst, write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700168 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 memset(read_bytes, 0, 3);
jbauchf1f87202016-03-30 06:43:37 -0700170 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 for (int i = 0; i < 3; ++i) {
172 EXPECT_EQ(write_bytes[i], read_bytes[i]);
173 }
jbauchf1f87202016-03-30 06:43:37 -0700174 EXPECT_EQ(0U, read_buf8.Length());
175 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176
177 // Write and read in order.
178 buffer.WriteUInt8(wu8);
179 buffer.WriteUInt16(wu16);
180 buffer.WriteUInt24(wu24);
181 buffer.WriteUInt32(wu32);
182 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700183 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
184 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -0700186 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700188 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700190 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700192 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700194 EXPECT_EQ(0U, read_buf9.Length());
195 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196 }
197}
198
mikescarlett9a20fa62016-04-11 16:11:38 -0700199TEST(ByteBufferTest, TestReadWriteUVarint) {
200 ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
201 ByteBufferWriter::ORDER_NETWORK};
202 for (ByteBufferWriter::ByteOrder& order : orders) {
203 ByteBufferWriter write_buffer(order);
204 size_t size = 0;
205 EXPECT_EQ(size, write_buffer.Length());
206
207 write_buffer.WriteUVarint(1u);
208 ++size;
209 EXPECT_EQ(size, write_buffer.Length());
210
211 write_buffer.WriteUVarint(2u);
212 ++size;
213 EXPECT_EQ(size, write_buffer.Length());
214
215 write_buffer.WriteUVarint(27u);
216 ++size;
217 EXPECT_EQ(size, write_buffer.Length());
218
219 write_buffer.WriteUVarint(149u);
220 size += 2;
221 EXPECT_EQ(size, write_buffer.Length());
222
223 write_buffer.WriteUVarint(68719476736u);
224 size += 6;
225 EXPECT_EQ(size, write_buffer.Length());
226
227 ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length(),
228 order);
229 EXPECT_EQ(size, read_buffer.Length());
230 uint64_t val1, val2, val3, val4, val5;
231
232 ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
233 EXPECT_EQ(1u, val1);
234 --size;
235 EXPECT_EQ(size, read_buffer.Length());
236
237 ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
238 EXPECT_EQ(2u, val2);
239 --size;
240 EXPECT_EQ(size, read_buffer.Length());
241
242 ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
243 EXPECT_EQ(27u, val3);
244 --size;
245 EXPECT_EQ(size, read_buffer.Length());
246
247 ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
248 EXPECT_EQ(149u, val4);
249 size -= 2;
250 EXPECT_EQ(size, read_buffer.Length());
251
252 ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
253 EXPECT_EQ(68719476736u, val5);
254 size -= 6;
255 EXPECT_EQ(size, read_buffer.Length());
256 }
257}
258
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259} // namespace rtc