1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/pickle.h"
10 #include "base/strings/string16.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 // Remove when this file is in the base namespace.
18 const int testint
= 2093847192;
19 const std::string
teststr("Hello world"); // note non-aligned string length
20 const std::wstring
testwstr(L
"Hello, world");
21 const char testdata
[] = "AAA\0BBB\0";
22 const int testdatalen
= arraysize(testdata
) - 1;
23 const bool testbool1
= false;
24 const bool testbool2
= true;
25 const uint16 testuint16
= 32123;
26 const float testfloat
= 3.1415926935f
;
27 const double testdouble
= 2.71828182845904523;
29 // checks that the result
30 void VerifyResult(const Pickle
& pickle
) {
31 PickleIterator
iter(pickle
);
34 EXPECT_TRUE(pickle
.ReadInt(&iter
, &outint
));
35 EXPECT_EQ(testint
, outint
);
38 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
39 EXPECT_EQ(teststr
, outstr
);
42 EXPECT_TRUE(pickle
.ReadWString(&iter
, &outwstr
));
43 EXPECT_EQ(testwstr
, outwstr
);
46 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
47 EXPECT_FALSE(outbool
);
48 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
52 EXPECT_TRUE(pickle
.ReadUInt16(&iter
, &outuint16
));
53 EXPECT_EQ(testuint16
, outuint16
);
56 EXPECT_TRUE(pickle
.ReadFloat(&iter
, &outfloat
));
57 EXPECT_EQ(testfloat
, outfloat
);
60 EXPECT_TRUE(pickle
.ReadDouble(&iter
, &outdouble
));
61 EXPECT_EQ(testdouble
, outdouble
);
65 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
66 EXPECT_EQ(testdatalen
, outdatalen
);
67 EXPECT_EQ(memcmp(testdata
, outdata
, outdatalen
), 0);
69 // reads past the end should fail
70 EXPECT_FALSE(pickle
.ReadInt(&iter
, &outint
));
75 TEST(PickleTest
, EncodeDecode
) {
78 EXPECT_TRUE(pickle
.WriteInt(testint
));
79 EXPECT_TRUE(pickle
.WriteString(teststr
));
80 EXPECT_TRUE(pickle
.WriteWString(testwstr
));
81 EXPECT_TRUE(pickle
.WriteBool(testbool1
));
82 EXPECT_TRUE(pickle
.WriteBool(testbool2
));
83 EXPECT_TRUE(pickle
.WriteUInt16(testuint16
));
84 EXPECT_TRUE(pickle
.WriteFloat(testfloat
));
85 EXPECT_TRUE(pickle
.WriteDouble(testdouble
));
86 EXPECT_TRUE(pickle
.WriteData(testdata
, testdatalen
));
89 // test copy constructor
90 Pickle
pickle2(pickle
);
91 VerifyResult(pickle2
);
96 VerifyResult(pickle3
);
99 // Tests that we can handle really small buffers.
100 TEST(PickleTest
, SmallBuffer
) {
101 scoped_ptr
<char[]> buffer(new char[1]);
103 // We should not touch the buffer.
104 Pickle
pickle(buffer
.get(), 1);
106 PickleIterator
iter(pickle
);
108 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
111 // Tests that we can handle improper headers.
112 TEST(PickleTest
, BigSize
) {
113 int buffer
[] = { 0x56035200, 25, 40, 50 };
115 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
117 PickleIterator
iter(pickle
);
119 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
122 TEST(PickleTest
, UnalignedSize
) {
123 int buffer
[] = { 10, 25, 40, 50 };
125 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
127 PickleIterator
iter(pickle
);
129 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
132 TEST(PickleTest
, ZeroLenStr
) {
134 EXPECT_TRUE(pickle
.WriteString(std::string()));
136 PickleIterator
iter(pickle
);
138 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
139 EXPECT_EQ("", outstr
);
142 TEST(PickleTest
, ZeroLenWStr
) {
144 EXPECT_TRUE(pickle
.WriteWString(std::wstring()));
146 PickleIterator
iter(pickle
);
148 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
149 EXPECT_EQ("", outstr
);
152 TEST(PickleTest
, BadLenStr
) {
154 EXPECT_TRUE(pickle
.WriteInt(-2));
156 PickleIterator
iter(pickle
);
158 EXPECT_FALSE(pickle
.ReadString(&iter
, &outstr
));
161 TEST(PickleTest
, BadLenWStr
) {
163 EXPECT_TRUE(pickle
.WriteInt(-1));
165 PickleIterator
iter(pickle
);
166 std::wstring woutstr
;
167 EXPECT_FALSE(pickle
.ReadWString(&iter
, &woutstr
));
170 TEST(PickleTest
, FindNext
) {
172 EXPECT_TRUE(pickle
.WriteInt(1));
173 EXPECT_TRUE(pickle
.WriteString("Domo"));
175 const char* start
= reinterpret_cast<const char*>(pickle
.data());
176 const char* end
= start
+ pickle
.size();
178 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
));
179 EXPECT_TRUE(NULL
== Pickle::FindNext(pickle
.header_size_
, start
, end
- 1));
180 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
+ 1));
183 TEST(PickleTest
, FindNextWithIncompleteHeader
) {
184 size_t header_size
= sizeof(Pickle::Header
);
185 scoped_ptr
<char[]> buffer(new char[header_size
- 1]);
186 memset(buffer
.get(), 0x1, header_size
- 1);
188 const char* start
= buffer
.get();
189 const char* end
= start
+ header_size
- 1;
191 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size
, start
, end
));
194 #if defined(COMPILER_MSVC)
195 #pragma warning(push)
196 #pragma warning(disable: 4146)
198 TEST(PickleTest
, FindNextOverflow
) {
199 size_t header_size
= sizeof(Pickle::Header
);
200 size_t header_size2
= 2 * header_size
;
201 size_t payload_received
= 100;
202 scoped_ptr
<char[]> buffer(new char[header_size2
+ payload_received
]);
203 const char* start
= buffer
.get();
204 Pickle::Header
* header
= reinterpret_cast<Pickle::Header
*>(buffer
.get());
205 const char* end
= start
+ header_size2
+ payload_received
;
206 // It is impossible to construct an overflow test otherwise.
207 if (sizeof(size_t) > sizeof(header
->payload_size
) ||
208 sizeof(uintptr_t) > sizeof(header
->payload_size
))
211 header
->payload_size
= -(reinterpret_cast<uintptr_t>(start
) + header_size2
);
212 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
214 header
->payload_size
= -header_size2
;
215 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
217 header
->payload_size
= 0;
218 end
= start
+ header_size
;
219 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
221 #if defined(COMPILER_MSVC)
225 TEST(PickleTest
, GetReadPointerAndAdvance
) {
228 PickleIterator
iter(pickle
);
229 EXPECT_FALSE(iter
.GetReadPointerAndAdvance(1));
231 EXPECT_TRUE(pickle
.WriteInt(1));
232 EXPECT_TRUE(pickle
.WriteInt(2));
233 int bytes
= sizeof(int) * 2;
235 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(0));
236 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(1));
237 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(-1));
238 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
));
239 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
+ 1));
240 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MAX
));
241 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MIN
));
244 TEST(PickleTest
, Resize
) {
245 size_t unit
= Pickle::kPayloadUnit
;
246 scoped_ptr
<char[]> data(new char[unit
]);
247 char* data_ptr
= data
.get();
248 for (size_t i
= 0; i
< unit
; i
++)
251 // construct a message that will be exactly the size of one payload unit,
252 // note that any data will have a 4-byte header indicating the size
253 const size_t payload_size_after_header
= unit
- sizeof(uint32
);
255 pickle
.WriteData(data_ptr
,
256 static_cast<int>(payload_size_after_header
- sizeof(uint32
)));
257 size_t cur_payload
= payload_size_after_header
;
259 // note: we assume 'unit' is a power of 2
260 EXPECT_EQ(unit
, pickle
.capacity_after_header());
261 EXPECT_EQ(pickle
.payload_size(), payload_size_after_header
);
263 // fill out a full page (noting data header)
264 pickle
.WriteData(data_ptr
, static_cast<int>(unit
- sizeof(uint32
)));
266 EXPECT_EQ(unit
* 2, pickle
.capacity_after_header());
267 EXPECT_EQ(cur_payload
, pickle
.payload_size());
269 // one more byte should double the capacity
270 pickle
.WriteData(data_ptr
, 1);
272 EXPECT_EQ(unit
* 4, pickle
.capacity_after_header());
273 EXPECT_EQ(cur_payload
, pickle
.payload_size());
278 struct CustomHeader
: Pickle::Header
{
284 TEST(PickleTest
, HeaderPadding
) {
285 const uint32 kMagic
= 0x12345678;
287 Pickle
pickle(sizeof(CustomHeader
));
288 pickle
.WriteInt(kMagic
);
290 // this should not overwrite the 'int' payload
291 pickle
.headerT
<CustomHeader
>()->blah
= 10;
293 PickleIterator
iter(pickle
);
295 ASSERT_TRUE(pickle
.ReadInt(&iter
, &result
));
297 EXPECT_EQ(static_cast<uint32
>(result
), kMagic
);
300 TEST(PickleTest
, EqualsOperator
) {
304 Pickle
copy_refs_source_buffer(static_cast<const char*>(source
.data()),
307 copy
= copy_refs_source_buffer
;
308 ASSERT_EQ(source
.size(), copy
.size());
311 TEST(PickleTest
, EvilLengths
) {
313 std::string
str(100000, 'A');
314 EXPECT_TRUE(source
.WriteData(str
.c_str(), 100000));
315 // ReadString16 used to have its read buffer length calculation wrong leading
316 // to out-of-bounds reading.
317 PickleIterator
iter(source
);
319 EXPECT_FALSE(source
.ReadString16(&iter
, &str16
));
321 // And check we didn't break ReadString16.
322 str16
= (wchar_t) 'A';
324 EXPECT_TRUE(str16_pickle
.WriteString16(str16
));
325 iter
= PickleIterator(str16_pickle
);
326 EXPECT_TRUE(str16_pickle
.ReadString16(&iter
, &str16
));
327 EXPECT_EQ(1U, str16
.length());
329 // Check we don't fail in a length check with invalid String16 size.
330 // (1<<31) * sizeof(char16) == 0, so this is particularly evil.
332 EXPECT_TRUE(bad_len
.WriteInt(1 << 31));
333 iter
= PickleIterator(bad_len
);
334 EXPECT_FALSE(bad_len
.ReadString16(&iter
, &str16
));
336 // Check we don't fail in a length check with large WStrings.
338 EXPECT_TRUE(big_len
.WriteInt(1 << 30));
339 iter
= PickleIterator(big_len
);
341 EXPECT_FALSE(big_len
.ReadWString(&iter
, &wstr
));
344 // Check we can write zero bytes of data and 'data' can be NULL.
345 TEST(PickleTest
, ZeroLength
) {
347 EXPECT_TRUE(pickle
.WriteData(NULL
, 0));
349 PickleIterator
iter(pickle
);
352 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
353 EXPECT_EQ(0, outdatalen
);
354 // We can't assert that outdata is NULL.
357 // Check that ReadBytes works properly with an iterator initialized to NULL.
358 TEST(PickleTest
, ReadBytes
) {
361 EXPECT_TRUE(pickle
.WriteBytes(&data
, sizeof(data
)));
363 PickleIterator
iter(pickle
);
364 const char* outdata_char
= NULL
;
365 EXPECT_TRUE(pickle
.ReadBytes(&iter
, &outdata_char
, sizeof(data
)));
368 memcpy(&outdata
, outdata_char
, sizeof(outdata
));
369 EXPECT_EQ(data
, outdata
);