blob: 6140e9fbfca6f1e85694712b8cfb64e9f5aa5268 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/bytebuffer.h"
12#include "rtc_base/arraysize.h"
13#include "rtc_base/byteorder.h"
14#include "rtc_base/gunit.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
16namespace rtc {
17
18TEST(ByteBufferTest, TestByteOrder) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020019 uint16_t n16 = 1;
20 uint32_t n32 = 1;
21 uint64_t n64 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
23 EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
24 EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
25 EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
26
27 if (IsHostBigEndian()) {
28 // The host is the network (big) endian.
29 EXPECT_EQ(n16, HostToNetwork16(n16));
30 EXPECT_EQ(n32, HostToNetwork32(n32));
31 EXPECT_EQ(n64, HostToNetwork64(n64));
32
33 // GetBE converts big endian to little endian here.
34 EXPECT_EQ(n16 >> 8, GetBE16(&n16));
35 EXPECT_EQ(n32 >> 24, GetBE32(&n32));
36 EXPECT_EQ(n64 >> 56, GetBE64(&n64));
37 } else {
38 // The host is little endian.
39 EXPECT_NE(n16, HostToNetwork16(n16));
40 EXPECT_NE(n32, HostToNetwork32(n32));
41 EXPECT_NE(n64, HostToNetwork64(n64));
42
43 // GetBE converts little endian to big endian here.
44 EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
45 EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
46 EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
47
48 // GetBE converts little endian to big endian here.
49 EXPECT_EQ(n16 << 8, GetBE16(&n16));
50 EXPECT_EQ(n32 << 24, GetBE32(&n32));
51 EXPECT_EQ(n64 << 56, GetBE64(&n64));
52 }
53}
54
55TEST(ByteBufferTest, TestBufferLength) {
jbauchf1f87202016-03-30 06:43:37 -070056 ByteBufferWriter buffer;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 size_t size = 0;
58 EXPECT_EQ(size, buffer.Length());
59
60 buffer.WriteUInt8(1);
61 ++size;
62 EXPECT_EQ(size, buffer.Length());
63
64 buffer.WriteUInt16(1);
65 size += 2;
66 EXPECT_EQ(size, buffer.Length());
67
68 buffer.WriteUInt24(1);
69 size += 3;
70 EXPECT_EQ(size, buffer.Length());
71
72 buffer.WriteUInt32(1);
73 size += 4;
74 EXPECT_EQ(size, buffer.Length());
75
76 buffer.WriteUInt64(1);
77 size += 8;
78 EXPECT_EQ(size, buffer.Length());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079}
80
81TEST(ByteBufferTest, TestReadWriteBuffer) {
jbauchf1f87202016-03-30 06:43:37 -070082 ByteBufferWriter::ByteOrder orders[2] = { ByteBufferWriter::ORDER_HOST,
83 ByteBufferWriter::ORDER_NETWORK };
tfarina5237aaf2015-11-10 23:44:30 -080084 for (size_t i = 0; i < arraysize(orders); i++) {
jbauchf1f87202016-03-30 06:43:37 -070085 ByteBufferWriter buffer(orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 EXPECT_EQ(orders[i], buffer.Order());
jbauchf1f87202016-03-30 06:43:37 -070087 ByteBufferReader read_buf(nullptr, 0, orders[i]);
88 EXPECT_EQ(orders[i], read_buf.Order());
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 uint8_t ru8;
jbauchf1f87202016-03-30 06:43:37 -070090 EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 // Write and read uint8_t.
93 uint8_t wu8 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 buffer.WriteUInt8(wu8);
jbauchf1f87202016-03-30 06:43:37 -070095 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
96 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -070098 EXPECT_EQ(0U, read_buf1.Length());
99 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 // Write and read uint16_t.
102 uint16_t wu16 = (1 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 buffer.WriteUInt16(wu16);
jbauchf1f87202016-03-30 06:43:37 -0700104 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 uint16_t ru16;
jbauchf1f87202016-03-30 06:43:37 -0700106 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700108 EXPECT_EQ(0U, read_buf2.Length());
109 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110
111 // Write and read uint24.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200112 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 buffer.WriteUInt24(wu24);
jbauchf1f87202016-03-30 06:43:37 -0700114 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115 uint32_t ru24;
jbauchf1f87202016-03-30 06:43:37 -0700116 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700118 EXPECT_EQ(0U, read_buf3.Length());
119 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120
Peter Boström0c4e06b2015-10-07 12:23:21 +0200121 // Write and read uint32_t.
122 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 buffer.WriteUInt32(wu32);
jbauchf1f87202016-03-30 06:43:37 -0700124 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125 uint32_t ru32;
jbauchf1f87202016-03-30 06:43:37 -0700126 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700128 EXPECT_EQ(0U, read_buf3.Length());
129 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Peter Boström0c4e06b2015-10-07 12:23:21 +0200131 // Write and read uint64_t.
132 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
133 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700135 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136 uint64_t ru64;
jbauchf1f87202016-03-30 06:43:37 -0700137 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700139 EXPECT_EQ(0U, read_buf5.Length());
140 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141
142 // Write and read string.
143 std::string write_string("hello");
144 buffer.WriteString(write_string);
jbauchf1f87202016-03-30 06:43:37 -0700145 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 std::string read_string;
jbauchf1f87202016-03-30 06:43:37 -0700147 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 EXPECT_EQ(write_string, read_string);
jbauchf1f87202016-03-30 06:43:37 -0700149 EXPECT_EQ(0U, read_buf6.Length());
150 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151
152 // Write and read bytes
153 char write_bytes[] = "foo";
154 buffer.WriteBytes(write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700155 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000156 char read_bytes[3];
jbauchf1f87202016-03-30 06:43:37 -0700157 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 for (int i = 0; i < 3; ++i) {
159 EXPECT_EQ(write_bytes[i], read_bytes[i]);
160 }
jbauchf1f87202016-03-30 06:43:37 -0700161 EXPECT_EQ(0U, read_buf7.Length());
162 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163
164 // Write and read reserved buffer space
165 char* write_dst = buffer.ReserveWriteBuffer(3);
166 memcpy(write_dst, write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700167 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 memset(read_bytes, 0, 3);
jbauchf1f87202016-03-30 06:43:37 -0700169 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000170 for (int i = 0; i < 3; ++i) {
171 EXPECT_EQ(write_bytes[i], read_bytes[i]);
172 }
jbauchf1f87202016-03-30 06:43:37 -0700173 EXPECT_EQ(0U, read_buf8.Length());
174 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175
176 // Write and read in order.
177 buffer.WriteUInt8(wu8);
178 buffer.WriteUInt16(wu16);
179 buffer.WriteUInt24(wu24);
180 buffer.WriteUInt32(wu32);
181 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700182 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
183 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -0700185 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700187 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700189 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700191 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700193 EXPECT_EQ(0U, read_buf9.Length());
194 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 }
196}
197
mikescarlett9a20fa62016-04-11 16:11:38 -0700198TEST(ByteBufferTest, TestReadWriteUVarint) {
199 ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
200 ByteBufferWriter::ORDER_NETWORK};
201 for (ByteBufferWriter::ByteOrder& order : orders) {
202 ByteBufferWriter write_buffer(order);
203 size_t size = 0;
204 EXPECT_EQ(size, write_buffer.Length());
205
206 write_buffer.WriteUVarint(1u);
207 ++size;
208 EXPECT_EQ(size, write_buffer.Length());
209
210 write_buffer.WriteUVarint(2u);
211 ++size;
212 EXPECT_EQ(size, write_buffer.Length());
213
214 write_buffer.WriteUVarint(27u);
215 ++size;
216 EXPECT_EQ(size, write_buffer.Length());
217
218 write_buffer.WriteUVarint(149u);
219 size += 2;
220 EXPECT_EQ(size, write_buffer.Length());
221
222 write_buffer.WriteUVarint(68719476736u);
223 size += 6;
224 EXPECT_EQ(size, write_buffer.Length());
225
226 ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length(),
227 order);
228 EXPECT_EQ(size, read_buffer.Length());
229 uint64_t val1, val2, val3, val4, val5;
230
231 ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
232 EXPECT_EQ(1u, val1);
233 --size;
234 EXPECT_EQ(size, read_buffer.Length());
235
236 ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
237 EXPECT_EQ(2u, val2);
238 --size;
239 EXPECT_EQ(size, read_buffer.Length());
240
241 ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
242 EXPECT_EQ(27u, val3);
243 --size;
244 EXPECT_EQ(size, read_buffer.Length());
245
246 ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
247 EXPECT_EQ(149u, val4);
248 size -= 2;
249 EXPECT_EQ(size, read_buffer.Length());
250
251 ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
252 EXPECT_EQ(68719476736u, val5);
253 size -= 6;
254 EXPECT_EQ(size, read_buffer.Length());
255 }
256}
257
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000258} // namespace rtc