OLD | NEW |
| (Empty) |
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/gunit.h" | |
15 | |
16 namespace rtc { | |
17 | |
18 TEST(ByteBufferTest, TestByteOrder) { | |
19 uint16_t n16 = 1; | |
20 uint32_t n32 = 1; | |
21 uint64_t n64 = 1; | |
22 | |
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 | |
55 TEST(ByteBufferTest, TestBufferLength) { | |
56 ByteBufferWriter buffer; | |
57 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()); | |
79 } | |
80 | |
81 TEST(ByteBufferTest, TestReadWriteBuffer) { | |
82 ByteBufferWriter::ByteOrder orders[2] = { ByteBufferWriter::ORDER_HOST, | |
83 ByteBufferWriter::ORDER_NETWORK }; | |
84 for (size_t i = 0; i < arraysize(orders); i++) { | |
85 ByteBufferWriter buffer(orders[i]); | |
86 EXPECT_EQ(orders[i], buffer.Order()); | |
87 ByteBufferReader read_buf(nullptr, 0, orders[i]); | |
88 EXPECT_EQ(orders[i], read_buf.Order()); | |
89 uint8_t ru8; | |
90 EXPECT_FALSE(read_buf.ReadUInt8(&ru8)); | |
91 | |
92 // Write and read uint8_t. | |
93 uint8_t wu8 = 1; | |
94 buffer.WriteUInt8(wu8); | |
95 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]); | |
96 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8)); | |
97 EXPECT_EQ(wu8, ru8); | |
98 EXPECT_EQ(0U, read_buf1.Length()); | |
99 buffer.Clear(); | |
100 | |
101 // Write and read uint16_t. | |
102 uint16_t wu16 = (1 << 8) + 1; | |
103 buffer.WriteUInt16(wu16); | |
104 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]); | |
105 uint16_t ru16; | |
106 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16)); | |
107 EXPECT_EQ(wu16, ru16); | |
108 EXPECT_EQ(0U, read_buf2.Length()); | |
109 buffer.Clear(); | |
110 | |
111 // Write and read uint24. | |
112 uint32_t wu24 = (3 << 16) + (2 << 8) + 1; | |
113 buffer.WriteUInt24(wu24); | |
114 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]); | |
115 uint32_t ru24; | |
116 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24)); | |
117 EXPECT_EQ(wu24, ru24); | |
118 EXPECT_EQ(0U, read_buf3.Length()); | |
119 buffer.Clear(); | |
120 | |
121 // Write and read uint32_t. | |
122 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1; | |
123 buffer.WriteUInt32(wu32); | |
124 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]); | |
125 uint32_t ru32; | |
126 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32)); | |
127 EXPECT_EQ(wu32, ru32); | |
128 EXPECT_EQ(0U, read_buf3.Length()); | |
129 buffer.Clear(); | |
130 | |
131 // 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; | |
134 buffer.WriteUInt64(wu64); | |
135 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]); | |
136 uint64_t ru64; | |
137 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64)); | |
138 EXPECT_EQ(wu64, ru64); | |
139 EXPECT_EQ(0U, read_buf5.Length()); | |
140 buffer.Clear(); | |
141 | |
142 // Write and read string. | |
143 std::string write_string("hello"); | |
144 buffer.WriteString(write_string); | |
145 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]); | |
146 std::string read_string; | |
147 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size())); | |
148 EXPECT_EQ(write_string, read_string); | |
149 EXPECT_EQ(0U, read_buf6.Length()); | |
150 buffer.Clear(); | |
151 | |
152 // Write and read bytes | |
153 char write_bytes[] = "foo"; | |
154 buffer.WriteBytes(write_bytes, 3); | |
155 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]); | |
156 char read_bytes[3]; | |
157 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3)); | |
158 for (int i = 0; i < 3; ++i) { | |
159 EXPECT_EQ(write_bytes[i], read_bytes[i]); | |
160 } | |
161 EXPECT_EQ(0U, read_buf7.Length()); | |
162 buffer.Clear(); | |
163 | |
164 // Write and read reserved buffer space | |
165 char* write_dst = buffer.ReserveWriteBuffer(3); | |
166 memcpy(write_dst, write_bytes, 3); | |
167 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]); | |
168 memset(read_bytes, 0, 3); | |
169 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3)); | |
170 for (int i = 0; i < 3; ++i) { | |
171 EXPECT_EQ(write_bytes[i], read_bytes[i]); | |
172 } | |
173 EXPECT_EQ(0U, read_buf8.Length()); | |
174 buffer.Clear(); | |
175 | |
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); | |
182 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]); | |
183 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8)); | |
184 EXPECT_EQ(wu8, ru8); | |
185 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16)); | |
186 EXPECT_EQ(wu16, ru16); | |
187 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24)); | |
188 EXPECT_EQ(wu24, ru24); | |
189 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32)); | |
190 EXPECT_EQ(wu32, ru32); | |
191 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64)); | |
192 EXPECT_EQ(wu64, ru64); | |
193 EXPECT_EQ(0U, read_buf9.Length()); | |
194 buffer.Clear(); | |
195 } | |
196 } | |
197 | |
198 TEST(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 | |
258 } // namespace rtc | |
OLD | NEW |