1 /*
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
11 #include "rtc_base/byte_buffer.h"
12
13 #include <string.h>
14
15 #include "rtc_base/arraysize.h"
16 #include "rtc_base/byte_order.h"
17 #include "test/gtest.h"
18
19 namespace rtc {
20
TEST(ByteBufferTest,TestByteOrder)21 TEST(ByteBufferTest, TestByteOrder) {
22 uint16_t n16 = 1;
23 uint32_t n32 = 1;
24 uint64_t n64 = 1;
25
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
TEST(ByteBufferTest,TestBufferLength)58 TEST(ByteBufferTest, TestBufferLength) {
59 ByteBufferWriter buffer;
60 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());
82 }
83
TEST(ByteBufferTest,TestReadWriteBuffer)84 TEST(ByteBufferTest, TestReadWriteBuffer) {
85 ByteBufferWriter buffer;
86 ByteBufferReader read_buf(nullptr, 0);
87 uint8_t ru8;
88 EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
89
90 // Write and read uint8_t.
91 uint8_t wu8 = 1;
92 buffer.WriteUInt8(wu8);
93 ByteBufferReader read_buf1(buffer.Data(), buffer.Length());
94 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
95 EXPECT_EQ(wu8, ru8);
96 EXPECT_EQ(0U, read_buf1.Length());
97 buffer.Clear();
98
99 // Write and read uint16_t.
100 uint16_t wu16 = (1 << 8) + 1;
101 buffer.WriteUInt16(wu16);
102 ByteBufferReader read_buf2(buffer.Data(), buffer.Length());
103 uint16_t ru16;
104 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
105 EXPECT_EQ(wu16, ru16);
106 EXPECT_EQ(0U, read_buf2.Length());
107 buffer.Clear();
108
109 // Write and read uint24.
110 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
111 buffer.WriteUInt24(wu24);
112 ByteBufferReader read_buf3(buffer.Data(), buffer.Length());
113 uint32_t ru24;
114 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
115 EXPECT_EQ(wu24, ru24);
116 EXPECT_EQ(0U, read_buf3.Length());
117 buffer.Clear();
118
119 // Write and read uint32_t.
120 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
121 buffer.WriteUInt32(wu32);
122 ByteBufferReader read_buf4(buffer.Data(), buffer.Length());
123 uint32_t ru32;
124 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
125 EXPECT_EQ(wu32, ru32);
126 EXPECT_EQ(0U, read_buf3.Length());
127 buffer.Clear();
128
129 // Write and read uint64_t.
130 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
131 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
132 buffer.WriteUInt64(wu64);
133 ByteBufferReader read_buf5(buffer.Data(), buffer.Length());
134 uint64_t ru64;
135 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
136 EXPECT_EQ(wu64, ru64);
137 EXPECT_EQ(0U, read_buf5.Length());
138 buffer.Clear();
139
140 // Write and read string.
141 std::string write_string("hello");
142 buffer.WriteString(write_string);
143 ByteBufferReader read_buf6(buffer.Data(), buffer.Length());
144 std::string read_string;
145 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
146 EXPECT_EQ(write_string, read_string);
147 EXPECT_EQ(0U, read_buf6.Length());
148 buffer.Clear();
149
150 // Write and read bytes
151 char write_bytes[] = "foo";
152 buffer.WriteBytes(write_bytes, 3);
153 ByteBufferReader read_buf7(buffer.Data(), buffer.Length());
154 char read_bytes[3];
155 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
156 for (int i = 0; i < 3; ++i) {
157 EXPECT_EQ(write_bytes[i], read_bytes[i]);
158 }
159 EXPECT_EQ(0U, read_buf7.Length());
160 buffer.Clear();
161
162 // Write and read reserved buffer space
163 char* write_dst = buffer.ReserveWriteBuffer(3);
164 memcpy(write_dst, write_bytes, 3);
165 ByteBufferReader read_buf8(buffer.Data(), buffer.Length());
166 memset(read_bytes, 0, 3);
167 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
168 for (int i = 0; i < 3; ++i) {
169 EXPECT_EQ(write_bytes[i], read_bytes[i]);
170 }
171 EXPECT_EQ(0U, read_buf8.Length());
172 buffer.Clear();
173
174 // Write and read in order.
175 buffer.WriteUInt8(wu8);
176 buffer.WriteUInt16(wu16);
177 buffer.WriteUInt24(wu24);
178 buffer.WriteUInt32(wu32);
179 buffer.WriteUInt64(wu64);
180 ByteBufferReader read_buf9(buffer.Data(), buffer.Length());
181 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
182 EXPECT_EQ(wu8, ru8);
183 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
184 EXPECT_EQ(wu16, ru16);
185 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
186 EXPECT_EQ(wu24, ru24);
187 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
188 EXPECT_EQ(wu32, ru32);
189 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
190 EXPECT_EQ(wu64, ru64);
191 EXPECT_EQ(0U, read_buf9.Length());
192 buffer.Clear();
193 }
194
TEST(ByteBufferTest,TestReadWriteUVarint)195 TEST(ByteBufferTest, TestReadWriteUVarint) {
196 ByteBufferWriter write_buffer;
197 size_t size = 0;
198 EXPECT_EQ(size, write_buffer.Length());
199
200 write_buffer.WriteUVarint(1u);
201 ++size;
202 EXPECT_EQ(size, write_buffer.Length());
203
204 write_buffer.WriteUVarint(2u);
205 ++size;
206 EXPECT_EQ(size, write_buffer.Length());
207
208 write_buffer.WriteUVarint(27u);
209 ++size;
210 EXPECT_EQ(size, write_buffer.Length());
211
212 write_buffer.WriteUVarint(149u);
213 size += 2;
214 EXPECT_EQ(size, write_buffer.Length());
215
216 write_buffer.WriteUVarint(68719476736u);
217 size += 6;
218 EXPECT_EQ(size, write_buffer.Length());
219
220 ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length());
221 EXPECT_EQ(size, read_buffer.Length());
222 uint64_t val1, val2, val3, val4, val5;
223
224 ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
225 EXPECT_EQ(1u, val1);
226 --size;
227 EXPECT_EQ(size, read_buffer.Length());
228
229 ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
230 EXPECT_EQ(2u, val2);
231 --size;
232 EXPECT_EQ(size, read_buffer.Length());
233
234 ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
235 EXPECT_EQ(27u, val3);
236 --size;
237 EXPECT_EQ(size, read_buffer.Length());
238
239 ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
240 EXPECT_EQ(149u, val4);
241 size -= 2;
242 EXPECT_EQ(size, read_buffer.Length());
243
244 ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
245 EXPECT_EQ(68719476736u, val5);
246 size -= 6;
247 EXPECT_EQ(size, read_buffer.Length());
248 }
249
250 } // namespace rtc
251