blob: 8fb02fffaee92403996da58568ab876e6a1afb66 [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"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <string.h>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/arraysize.h"
16#include "rtc_base/byteorder.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19namespace rtc {
20
21TEST(ByteBufferTest, TestByteOrder) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020022 uint16_t n16 = 1;
23 uint32_t n32 = 1;
24 uint64_t n64 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
26 EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
27 EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
28 EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
29
30 if (IsHostBigEndian()) {
31 // The host is the network (big) endian.
32 EXPECT_EQ(n16, HostToNetwork16(n16));
33 EXPECT_EQ(n32, HostToNetwork32(n32));
34 EXPECT_EQ(n64, HostToNetwork64(n64));
35
36 // GetBE converts big endian to little endian here.
37 EXPECT_EQ(n16 >> 8, GetBE16(&n16));
38 EXPECT_EQ(n32 >> 24, GetBE32(&n32));
39 EXPECT_EQ(n64 >> 56, GetBE64(&n64));
40 } else {
41 // The host is little endian.
42 EXPECT_NE(n16, HostToNetwork16(n16));
43 EXPECT_NE(n32, HostToNetwork32(n32));
44 EXPECT_NE(n64, HostToNetwork64(n64));
45
46 // GetBE converts little endian to big endian here.
47 EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
48 EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
49 EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
50
51 // GetBE converts little endian to big endian here.
52 EXPECT_EQ(n16 << 8, GetBE16(&n16));
53 EXPECT_EQ(n32 << 24, GetBE32(&n32));
54 EXPECT_EQ(n64 << 56, GetBE64(&n64));
55 }
56}
57
58TEST(ByteBufferTest, TestBufferLength) {
jbauchf1f87202016-03-30 06:43:37 -070059 ByteBufferWriter buffer;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 size_t size = 0;
61 EXPECT_EQ(size, buffer.Length());
62
63 buffer.WriteUInt8(1);
64 ++size;
65 EXPECT_EQ(size, buffer.Length());
66
67 buffer.WriteUInt16(1);
68 size += 2;
69 EXPECT_EQ(size, buffer.Length());
70
71 buffer.WriteUInt24(1);
72 size += 3;
73 EXPECT_EQ(size, buffer.Length());
74
75 buffer.WriteUInt32(1);
76 size += 4;
77 EXPECT_EQ(size, buffer.Length());
78
79 buffer.WriteUInt64(1);
80 size += 8;
81 EXPECT_EQ(size, buffer.Length());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082}
83
84TEST(ByteBufferTest, TestReadWriteBuffer) {
Yves Gerey665174f2018-06-19 15:03:05 +020085 ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
86 ByteBufferWriter::ORDER_NETWORK};
tfarina5237aaf2015-11-10 23:44:30 -080087 for (size_t i = 0; i < arraysize(orders); i++) {
jbauchf1f87202016-03-30 06:43:37 -070088 ByteBufferWriter buffer(orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 EXPECT_EQ(orders[i], buffer.Order());
jbauchf1f87202016-03-30 06:43:37 -070090 ByteBufferReader read_buf(nullptr, 0, orders[i]);
91 EXPECT_EQ(orders[i], read_buf.Order());
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 uint8_t ru8;
jbauchf1f87202016-03-30 06:43:37 -070093 EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 // Write and read uint8_t.
96 uint8_t wu8 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 buffer.WriteUInt8(wu8);
jbauchf1f87202016-03-30 06:43:37 -070098 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
99 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -0700101 EXPECT_EQ(0U, read_buf1.Length());
102 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 // Write and read uint16_t.
105 uint16_t wu16 = (1 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 buffer.WriteUInt16(wu16);
jbauchf1f87202016-03-30 06:43:37 -0700107 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 uint16_t ru16;
jbauchf1f87202016-03-30 06:43:37 -0700109 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700111 EXPECT_EQ(0U, read_buf2.Length());
112 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113
114 // Write and read uint24.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 buffer.WriteUInt24(wu24);
jbauchf1f87202016-03-30 06:43:37 -0700117 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200118 uint32_t ru24;
jbauchf1f87202016-03-30 06:43:37 -0700119 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700121 EXPECT_EQ(0U, read_buf3.Length());
122 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
Peter Boström0c4e06b2015-10-07 12:23:21 +0200124 // Write and read uint32_t.
125 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 buffer.WriteUInt32(wu32);
jbauchf1f87202016-03-30 06:43:37 -0700127 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 uint32_t ru32;
jbauchf1f87202016-03-30 06:43:37 -0700129 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700131 EXPECT_EQ(0U, read_buf3.Length());
132 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134 // Write and read uint64_t.
135 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
136 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700138 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200139 uint64_t ru64;
jbauchf1f87202016-03-30 06:43:37 -0700140 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700142 EXPECT_EQ(0U, read_buf5.Length());
143 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144
145 // Write and read string.
146 std::string write_string("hello");
147 buffer.WriteString(write_string);
jbauchf1f87202016-03-30 06:43:37 -0700148 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000149 std::string read_string;
jbauchf1f87202016-03-30 06:43:37 -0700150 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 EXPECT_EQ(write_string, read_string);
jbauchf1f87202016-03-30 06:43:37 -0700152 EXPECT_EQ(0U, read_buf6.Length());
153 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154
155 // Write and read bytes
156 char write_bytes[] = "foo";
157 buffer.WriteBytes(write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700158 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159 char read_bytes[3];
jbauchf1f87202016-03-30 06:43:37 -0700160 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 for (int i = 0; i < 3; ++i) {
162 EXPECT_EQ(write_bytes[i], read_bytes[i]);
163 }
jbauchf1f87202016-03-30 06:43:37 -0700164 EXPECT_EQ(0U, read_buf7.Length());
165 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166
167 // Write and read reserved buffer space
168 char* write_dst = buffer.ReserveWriteBuffer(3);
169 memcpy(write_dst, write_bytes, 3);
jbauchf1f87202016-03-30 06:43:37 -0700170 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 memset(read_bytes, 0, 3);
jbauchf1f87202016-03-30 06:43:37 -0700172 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173 for (int i = 0; i < 3; ++i) {
174 EXPECT_EQ(write_bytes[i], read_bytes[i]);
175 }
jbauchf1f87202016-03-30 06:43:37 -0700176 EXPECT_EQ(0U, read_buf8.Length());
177 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178
179 // Write and read in order.
180 buffer.WriteUInt8(wu8);
181 buffer.WriteUInt16(wu16);
182 buffer.WriteUInt24(wu24);
183 buffer.WriteUInt32(wu32);
184 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 06:43:37 -0700185 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
186 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 06:43:37 -0700188 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 06:43:37 -0700190 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 06:43:37 -0700192 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 06:43:37 -0700194 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 06:43:37 -0700196 EXPECT_EQ(0U, read_buf9.Length());
197 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 }
199}
200
mikescarlett9a20fa62016-04-11 16:11:38 -0700201TEST(ByteBufferTest, TestReadWriteUVarint) {
202 ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
203 ByteBufferWriter::ORDER_NETWORK};
204 for (ByteBufferWriter::ByteOrder& order : orders) {
205 ByteBufferWriter write_buffer(order);
206 size_t size = 0;
207 EXPECT_EQ(size, write_buffer.Length());
208
209 write_buffer.WriteUVarint(1u);
210 ++size;
211 EXPECT_EQ(size, write_buffer.Length());
212
213 write_buffer.WriteUVarint(2u);
214 ++size;
215 EXPECT_EQ(size, write_buffer.Length());
216
217 write_buffer.WriteUVarint(27u);
218 ++size;
219 EXPECT_EQ(size, write_buffer.Length());
220
221 write_buffer.WriteUVarint(149u);
222 size += 2;
223 EXPECT_EQ(size, write_buffer.Length());
224
225 write_buffer.WriteUVarint(68719476736u);
226 size += 6;
227 EXPECT_EQ(size, write_buffer.Length());
228
229 ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length(),
230 order);
231 EXPECT_EQ(size, read_buffer.Length());
232 uint64_t val1, val2, val3, val4, val5;
233
234 ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
235 EXPECT_EQ(1u, val1);
236 --size;
237 EXPECT_EQ(size, read_buffer.Length());
238
239 ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
240 EXPECT_EQ(2u, val2);
241 --size;
242 EXPECT_EQ(size, read_buffer.Length());
243
244 ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
245 EXPECT_EQ(27u, val3);
246 --size;
247 EXPECT_EQ(size, read_buffer.Length());
248
249 ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
250 EXPECT_EQ(149u, val4);
251 size -= 2;
252 EXPECT_EQ(size, read_buffer.Length());
253
254 ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
255 EXPECT_EQ(68719476736u, val5);
256 size -= 6;
257 EXPECT_EQ(size, read_buffer.Length());
258 }
259}
260
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261} // namespace rtc