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 "webrtc/base/arraysize.h"
12 #include "webrtc/base/bytebuffer.h"
13 #include "webrtc/base/byteorder.h"
14 #include "webrtc/base/common.h"
15 #include "webrtc/base/gunit.h"
16
17 namespace rtc {
18
TEST(ByteBufferTest,TestByteOrder)19 TEST(ByteBufferTest, TestByteOrder) {
20 uint16_t n16 = 1;
21 uint32_t n32 = 1;
22 uint64_t n64 = 1;
23
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
TEST(ByteBufferTest,TestBufferLength)56 TEST(ByteBufferTest, TestBufferLength) {
57 ByteBuffer buffer;
58 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());
80
81 EXPECT_TRUE(buffer.Consume(0));
82 EXPECT_EQ(size, buffer.Length());
83
84 EXPECT_TRUE(buffer.Consume(4));
85 size -= 4;
86 EXPECT_EQ(size, buffer.Length());
87 }
88
TEST(ByteBufferTest,TestGetSetReadPosition)89 TEST(ByteBufferTest, TestGetSetReadPosition) {
90 ByteBuffer buffer("ABCDEF", 6);
91 EXPECT_EQ(6U, buffer.Length());
92 ByteBuffer::ReadPosition pos(buffer.GetReadPosition());
93 EXPECT_TRUE(buffer.SetReadPosition(pos));
94 EXPECT_EQ(6U, buffer.Length());
95 std::string read;
96 EXPECT_TRUE(buffer.ReadString(&read, 3));
97 EXPECT_EQ("ABC", read);
98 EXPECT_EQ(3U, buffer.Length());
99 EXPECT_TRUE(buffer.SetReadPosition(pos));
100 EXPECT_EQ(6U, buffer.Length());
101 read.clear();
102 EXPECT_TRUE(buffer.ReadString(&read, 3));
103 EXPECT_EQ("ABC", read);
104 EXPECT_EQ(3U, buffer.Length());
105 // For a resize by writing Capacity() number of bytes.
106 size_t capacity = buffer.Capacity();
107 buffer.ReserveWriteBuffer(buffer.Capacity());
108 EXPECT_EQ(capacity + 3U, buffer.Length());
109 EXPECT_FALSE(buffer.SetReadPosition(pos));
110 read.clear();
111 EXPECT_TRUE(buffer.ReadString(&read, 3));
112 EXPECT_EQ("DEF", read);
113 }
114
TEST(ByteBufferTest,TestReadWriteBuffer)115 TEST(ByteBufferTest, TestReadWriteBuffer) {
116 ByteBuffer::ByteOrder orders[2] = { ByteBuffer::ORDER_HOST,
117 ByteBuffer::ORDER_NETWORK };
118 for (size_t i = 0; i < arraysize(orders); i++) {
119 ByteBuffer buffer(orders[i]);
120 EXPECT_EQ(orders[i], buffer.Order());
121 uint8_t ru8;
122 EXPECT_FALSE(buffer.ReadUInt8(&ru8));
123
124 // Write and read uint8_t.
125 uint8_t wu8 = 1;
126 buffer.WriteUInt8(wu8);
127 EXPECT_TRUE(buffer.ReadUInt8(&ru8));
128 EXPECT_EQ(wu8, ru8);
129 EXPECT_EQ(0U, buffer.Length());
130
131 // Write and read uint16_t.
132 uint16_t wu16 = (1 << 8) + 1;
133 buffer.WriteUInt16(wu16);
134 uint16_t ru16;
135 EXPECT_TRUE(buffer.ReadUInt16(&ru16));
136 EXPECT_EQ(wu16, ru16);
137 EXPECT_EQ(0U, buffer.Length());
138
139 // Write and read uint24.
140 uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
141 buffer.WriteUInt24(wu24);
142 uint32_t ru24;
143 EXPECT_TRUE(buffer.ReadUInt24(&ru24));
144 EXPECT_EQ(wu24, ru24);
145 EXPECT_EQ(0U, buffer.Length());
146
147 // Write and read uint32_t.
148 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
149 buffer.WriteUInt32(wu32);
150 uint32_t ru32;
151 EXPECT_TRUE(buffer.ReadUInt32(&ru32));
152 EXPECT_EQ(wu32, ru32);
153 EXPECT_EQ(0U, buffer.Length());
154
155 // Write and read uint64_t.
156 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
157 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
158 buffer.WriteUInt64(wu64);
159 uint64_t ru64;
160 EXPECT_TRUE(buffer.ReadUInt64(&ru64));
161 EXPECT_EQ(wu64, ru64);
162 EXPECT_EQ(0U, buffer.Length());
163
164 // Write and read string.
165 std::string write_string("hello");
166 buffer.WriteString(write_string);
167 std::string read_string;
168 EXPECT_TRUE(buffer.ReadString(&read_string, write_string.size()));
169 EXPECT_EQ(write_string, read_string);
170 EXPECT_EQ(0U, buffer.Length());
171
172 // Write and read bytes
173 char write_bytes[] = "foo";
174 buffer.WriteBytes(write_bytes, 3);
175 char read_bytes[3];
176 EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
177 for (int i = 0; i < 3; ++i) {
178 EXPECT_EQ(write_bytes[i], read_bytes[i]);
179 }
180 EXPECT_EQ(0U, buffer.Length());
181
182 // Write and read reserved buffer space
183 char* write_dst = buffer.ReserveWriteBuffer(3);
184 memcpy(write_dst, write_bytes, 3);
185 memset(read_bytes, 0, 3);
186 EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
187 for (int i = 0; i < 3; ++i) {
188 EXPECT_EQ(write_bytes[i], read_bytes[i]);
189 }
190 EXPECT_EQ(0U, buffer.Length());
191
192 // Write and read in order.
193 buffer.WriteUInt8(wu8);
194 buffer.WriteUInt16(wu16);
195 buffer.WriteUInt24(wu24);
196 buffer.WriteUInt32(wu32);
197 buffer.WriteUInt64(wu64);
198 EXPECT_TRUE(buffer.ReadUInt8(&ru8));
199 EXPECT_EQ(wu8, ru8);
200 EXPECT_TRUE(buffer.ReadUInt16(&ru16));
201 EXPECT_EQ(wu16, ru16);
202 EXPECT_TRUE(buffer.ReadUInt24(&ru24));
203 EXPECT_EQ(wu24, ru24);
204 EXPECT_TRUE(buffer.ReadUInt32(&ru32));
205 EXPECT_EQ(wu32, ru32);
206 EXPECT_TRUE(buffer.ReadUInt64(&ru64));
207 EXPECT_EQ(wu64, ru64);
208 EXPECT_EQ(0U, buffer.Length());
209 }
210 }
211
212 } // namespace rtc
213