blob: eafe67095823e9db64318c423119f8f8adea076b [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/byte_buffer.h"
Yves Gerey3e707812018-11-28 15:47:4912
13#include <string.h>
14
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/arraysize.h"
Steve Anton10542f22019-01-11 17:11:0016#include "rtc_base/byte_order.h"
Yves Gerey3e707812018-11-28 15:47:4917#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2618
19namespace rtc {
20
21TEST(ByteBufferTest, TestByteOrder) {
Peter Boström0c4e06b2015-10-07 10:23:2122 uint16_t n16 = 1;
23 uint32_t n32 = 1;
24 uint64_t n64 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:2625
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 13:43:3759 ByteBufferWriter buffer;
henrike@webrtc.orgf0488722014-05-13 18:00:2660 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:2682}
83
84TEST(ByteBufferTest, TestReadWriteBuffer) {
Yves Gerey665174f2018-06-19 13:03:0585 ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
86 ByteBufferWriter::ORDER_NETWORK};
tfarina5237aaf2015-11-11 07:44:3087 for (size_t i = 0; i < arraysize(orders); i++) {
jbauchf1f87202016-03-30 13:43:3788 ByteBufferWriter buffer(orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:2689 EXPECT_EQ(orders[i], buffer.Order());
jbauchf1f87202016-03-30 13:43:3790 ByteBufferReader read_buf(nullptr, 0, orders[i]);
91 EXPECT_EQ(orders[i], read_buf.Order());
Peter Boström0c4e06b2015-10-07 10:23:2192 uint8_t ru8;
jbauchf1f87202016-03-30 13:43:3793 EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:2694
Peter Boström0c4e06b2015-10-07 10:23:2195 // Write and read uint8_t.
96 uint8_t wu8 = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:2697 buffer.WriteUInt8(wu8);
jbauchf1f87202016-03-30 13:43:3798 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
99 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26100 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 13:43:37101 EXPECT_EQ(0U, read_buf1.Length());
102 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26103
Peter Boström0c4e06b2015-10-07 10:23:21104 // Write and read uint16_t.
105 uint16_t wu16 = (1 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26106 buffer.WriteUInt16(wu16);
jbauchf1f87202016-03-30 13:43:37107 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 10:23:21108 uint16_t ru16;
jbauchf1f87202016-03-30 13:43:37109 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26110 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 13:43:37111 EXPECT_EQ(0U, read_buf2.Length());
112 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26113
114 // Write and read uint24.
Peter Boström0c4e06b2015-10-07 10:23:21115 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26116 buffer.WriteUInt24(wu24);
jbauchf1f87202016-03-30 13:43:37117 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 10:23:21118 uint32_t ru24;
jbauchf1f87202016-03-30 13:43:37119 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26120 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 13:43:37121 EXPECT_EQ(0U, read_buf3.Length());
122 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26123
Peter Boström0c4e06b2015-10-07 10:23:21124 // Write and read uint32_t.
125 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26126 buffer.WriteUInt32(wu32);
jbauchf1f87202016-03-30 13:43:37127 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 10:23:21128 uint32_t ru32;
jbauchf1f87202016-03-30 13:43:37129 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26130 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 13:43:37131 EXPECT_EQ(0U, read_buf3.Length());
132 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26133
Peter Boström0c4e06b2015-10-07 10:23:21134 // 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:26137 buffer.WriteUInt64(wu64);
jbauchf1f87202016-03-30 13:43:37138 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
Peter Boström0c4e06b2015-10-07 10:23:21139 uint64_t ru64;
jbauchf1f87202016-03-30 13:43:37140 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26141 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 13:43:37142 EXPECT_EQ(0U, read_buf5.Length());
143 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26144
145 // Write and read string.
146 std::string write_string("hello");
147 buffer.WriteString(write_string);
jbauchf1f87202016-03-30 13:43:37148 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26149 std::string read_string;
jbauchf1f87202016-03-30 13:43:37150 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
henrike@webrtc.orgf0488722014-05-13 18:00:26151 EXPECT_EQ(write_string, read_string);
jbauchf1f87202016-03-30 13:43:37152 EXPECT_EQ(0U, read_buf6.Length());
153 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26154
155 // Write and read bytes
156 char write_bytes[] = "foo";
157 buffer.WriteBytes(write_bytes, 3);
jbauchf1f87202016-03-30 13:43:37158 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26159 char read_bytes[3];
jbauchf1f87202016-03-30 13:43:37160 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26161 for (int i = 0; i < 3; ++i) {
162 EXPECT_EQ(write_bytes[i], read_bytes[i]);
163 }
jbauchf1f87202016-03-30 13:43:37164 EXPECT_EQ(0U, read_buf7.Length());
165 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26166
167 // Write and read reserved buffer space
168 char* write_dst = buffer.ReserveWriteBuffer(3);
169 memcpy(write_dst, write_bytes, 3);
jbauchf1f87202016-03-30 13:43:37170 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26171 memset(read_bytes, 0, 3);
jbauchf1f87202016-03-30 13:43:37172 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
henrike@webrtc.orgf0488722014-05-13 18:00:26173 for (int i = 0; i < 3; ++i) {
174 EXPECT_EQ(write_bytes[i], read_bytes[i]);
175 }
jbauchf1f87202016-03-30 13:43:37176 EXPECT_EQ(0U, read_buf8.Length());
177 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26178
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 13:43:37185 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
186 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
henrike@webrtc.orgf0488722014-05-13 18:00:26187 EXPECT_EQ(wu8, ru8);
jbauchf1f87202016-03-30 13:43:37188 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
henrike@webrtc.orgf0488722014-05-13 18:00:26189 EXPECT_EQ(wu16, ru16);
jbauchf1f87202016-03-30 13:43:37190 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
henrike@webrtc.orgf0488722014-05-13 18:00:26191 EXPECT_EQ(wu24, ru24);
jbauchf1f87202016-03-30 13:43:37192 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
henrike@webrtc.orgf0488722014-05-13 18:00:26193 EXPECT_EQ(wu32, ru32);
jbauchf1f87202016-03-30 13:43:37194 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
henrike@webrtc.orgf0488722014-05-13 18:00:26195 EXPECT_EQ(wu64, ru64);
jbauchf1f87202016-03-30 13:43:37196 EXPECT_EQ(0U, read_buf9.Length());
197 buffer.Clear();
henrike@webrtc.orgf0488722014-05-13 18:00:26198 }
199}
200
mikescarlett9a20fa62016-04-11 23:11:38201TEST(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:26261} // namespace rtc