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
;
28 // checks that the result
29 void VerifyResult(const Pickle
& pickle
) {
30 PickleIterator
iter(pickle
);
33 EXPECT_TRUE(pickle
.ReadInt(&iter
, &outint
));
34 EXPECT_EQ(testint
, outint
);
37 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
38 EXPECT_EQ(teststr
, outstr
);
41 EXPECT_TRUE(pickle
.ReadWString(&iter
, &outwstr
));
42 EXPECT_EQ(testwstr
, outwstr
);
45 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
46 EXPECT_FALSE(outbool
);
47 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
51 EXPECT_TRUE(pickle
.ReadUInt16(&iter
, &outuint16
));
52 EXPECT_EQ(testuint16
, outuint16
);
55 EXPECT_TRUE(pickle
.ReadFloat(&iter
, &outfloat
));
56 EXPECT_EQ(testfloat
, outfloat
);
60 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
61 EXPECT_EQ(testdatalen
, outdatalen
);
62 EXPECT_EQ(memcmp(testdata
, outdata
, outdatalen
), 0);
64 // reads past the end should fail
65 EXPECT_FALSE(pickle
.ReadInt(&iter
, &outint
));
70 TEST(PickleTest
, EncodeDecode
) {
73 EXPECT_TRUE(pickle
.WriteInt(testint
));
74 EXPECT_TRUE(pickle
.WriteString(teststr
));
75 EXPECT_TRUE(pickle
.WriteWString(testwstr
));
76 EXPECT_TRUE(pickle
.WriteBool(testbool1
));
77 EXPECT_TRUE(pickle
.WriteBool(testbool2
));
78 EXPECT_TRUE(pickle
.WriteUInt16(testuint16
));
79 EXPECT_TRUE(pickle
.WriteFloat(testfloat
));
80 EXPECT_TRUE(pickle
.WriteData(testdata
, testdatalen
));
83 // test copy constructor
84 Pickle
pickle2(pickle
);
85 VerifyResult(pickle2
);
90 VerifyResult(pickle3
);
93 // Tests that we can handle really small buffers.
94 TEST(PickleTest
, SmallBuffer
) {
95 scoped_ptr
<char[]> buffer(new char[1]);
97 // We should not touch the buffer.
98 Pickle
pickle(buffer
.get(), 1);
100 PickleIterator
iter(pickle
);
102 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
105 // Tests that we can handle improper headers.
106 TEST(PickleTest
, BigSize
) {
107 int buffer
[] = { 0x56035200, 25, 40, 50 };
109 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
111 PickleIterator
iter(pickle
);
113 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
116 TEST(PickleTest
, UnalignedSize
) {
117 int buffer
[] = { 10, 25, 40, 50 };
119 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
121 PickleIterator
iter(pickle
);
123 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
126 TEST(PickleTest
, ZeroLenStr
) {
128 EXPECT_TRUE(pickle
.WriteString(std::string()));
130 PickleIterator
iter(pickle
);
132 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
133 EXPECT_EQ("", outstr
);
136 TEST(PickleTest
, ZeroLenWStr
) {
138 EXPECT_TRUE(pickle
.WriteWString(std::wstring()));
140 PickleIterator
iter(pickle
);
142 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
143 EXPECT_EQ("", outstr
);
146 TEST(PickleTest
, BadLenStr
) {
148 EXPECT_TRUE(pickle
.WriteInt(-2));
150 PickleIterator
iter(pickle
);
152 EXPECT_FALSE(pickle
.ReadString(&iter
, &outstr
));
155 TEST(PickleTest
, BadLenWStr
) {
157 EXPECT_TRUE(pickle
.WriteInt(-1));
159 PickleIterator
iter(pickle
);
160 std::wstring woutstr
;
161 EXPECT_FALSE(pickle
.ReadWString(&iter
, &woutstr
));
164 TEST(PickleTest
, FindNext
) {
166 EXPECT_TRUE(pickle
.WriteInt(1));
167 EXPECT_TRUE(pickle
.WriteString("Domo"));
169 const char* start
= reinterpret_cast<const char*>(pickle
.data());
170 const char* end
= start
+ pickle
.size();
172 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
));
173 EXPECT_TRUE(NULL
== Pickle::FindNext(pickle
.header_size_
, start
, end
- 1));
174 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
+ 1));
177 TEST(PickleTest
, FindNextWithIncompleteHeader
) {
178 size_t header_size
= sizeof(Pickle::Header
);
179 scoped_ptr
<char[]> buffer(new char[header_size
- 1]);
180 memset(buffer
.get(), 0x1, header_size
- 1);
182 const char* start
= buffer
.get();
183 const char* end
= start
+ header_size
- 1;
185 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size
, start
, end
));
188 #if defined(COMPILER_MSVC)
189 #pragma warning(push)
190 #pragma warning(disable: 4146)
192 TEST(PickleTest
, FindNextOverflow
) {
193 size_t header_size
= sizeof(Pickle::Header
);
194 size_t header_size2
= 2 * header_size
;
195 size_t payload_received
= 100;
196 scoped_ptr
<char[]> buffer(new char[header_size2
+ payload_received
]);
197 const char* start
= buffer
.get();
198 Pickle::Header
* header
= reinterpret_cast<Pickle::Header
*>(buffer
.get());
199 const char* end
= start
+ header_size2
+ payload_received
;
200 // It is impossible to construct an overflow test otherwise.
201 if (sizeof(size_t) > sizeof(header
->payload_size
) ||
202 sizeof(uintptr_t) > sizeof(header
->payload_size
))
205 header
->payload_size
= -(reinterpret_cast<uintptr_t>(start
) + header_size2
);
206 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
208 header
->payload_size
= -header_size2
;
209 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
211 header
->payload_size
= 0;
212 end
= start
+ header_size
;
213 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size2
, start
, end
));
215 #if defined(COMPILER_MSVC)
219 TEST(PickleTest
, GetReadPointerAndAdvance
) {
222 PickleIterator
iter(pickle
);
223 EXPECT_FALSE(iter
.GetReadPointerAndAdvance(1));
225 EXPECT_TRUE(pickle
.WriteInt(1));
226 EXPECT_TRUE(pickle
.WriteInt(2));
227 int bytes
= sizeof(int) * 2;
229 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(0));
230 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(1));
231 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(-1));
232 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
));
233 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
+ 1));
234 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MAX
));
235 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MIN
));
238 TEST(PickleTest
, Resize
) {
239 size_t unit
= Pickle::kPayloadUnit
;
240 scoped_ptr
<char[]> data(new char[unit
]);
241 char* data_ptr
= data
.get();
242 for (size_t i
= 0; i
< unit
; i
++)
245 // construct a message that will be exactly the size of one payload unit,
246 // note that any data will have a 4-byte header indicating the size
247 const size_t payload_size_after_header
= unit
- sizeof(uint32
);
249 pickle
.WriteData(data_ptr
,
250 static_cast<int>(payload_size_after_header
- sizeof(uint32
)));
251 size_t cur_payload
= payload_size_after_header
;
253 // note: we assume 'unit' is a power of 2
254 EXPECT_EQ(unit
, pickle
.capacity_after_header());
255 EXPECT_EQ(pickle
.payload_size(), payload_size_after_header
);
257 // fill out a full page (noting data header)
258 pickle
.WriteData(data_ptr
, static_cast<int>(unit
- sizeof(uint32
)));
260 EXPECT_EQ(unit
* 2, pickle
.capacity_after_header());
261 EXPECT_EQ(cur_payload
, pickle
.payload_size());
263 // one more byte should double the capacity
264 pickle
.WriteData(data_ptr
, 1);
266 EXPECT_EQ(unit
* 4, pickle
.capacity_after_header());
267 EXPECT_EQ(cur_payload
, pickle
.payload_size());
272 struct CustomHeader
: Pickle::Header
{
278 TEST(PickleTest
, HeaderPadding
) {
279 const uint32 kMagic
= 0x12345678;
281 Pickle
pickle(sizeof(CustomHeader
));
282 pickle
.WriteInt(kMagic
);
284 // this should not overwrite the 'int' payload
285 pickle
.headerT
<CustomHeader
>()->blah
= 10;
287 PickleIterator
iter(pickle
);
289 ASSERT_TRUE(pickle
.ReadInt(&iter
, &result
));
291 EXPECT_EQ(static_cast<uint32
>(result
), kMagic
);
294 TEST(PickleTest
, EqualsOperator
) {
298 Pickle
copy_refs_source_buffer(static_cast<const char*>(source
.data()),
301 copy
= copy_refs_source_buffer
;
302 ASSERT_EQ(source
.size(), copy
.size());
305 TEST(PickleTest
, EvilLengths
) {
307 std::string
str(100000, 'A');
308 EXPECT_TRUE(source
.WriteData(str
.c_str(), 100000));
309 // ReadString16 used to have its read buffer length calculation wrong leading
310 // to out-of-bounds reading.
311 PickleIterator
iter(source
);
313 EXPECT_FALSE(source
.ReadString16(&iter
, &str16
));
315 // And check we didn't break ReadString16.
316 str16
= (wchar_t) 'A';
318 EXPECT_TRUE(str16_pickle
.WriteString16(str16
));
319 iter
= PickleIterator(str16_pickle
);
320 EXPECT_TRUE(str16_pickle
.ReadString16(&iter
, &str16
));
321 EXPECT_EQ(1U, str16
.length());
323 // Check we don't fail in a length check with invalid String16 size.
324 // (1<<31) * sizeof(char16) == 0, so this is particularly evil.
326 EXPECT_TRUE(bad_len
.WriteInt(1 << 31));
327 iter
= PickleIterator(bad_len
);
328 EXPECT_FALSE(bad_len
.ReadString16(&iter
, &str16
));
330 // Check we don't fail in a length check with large WStrings.
332 EXPECT_TRUE(big_len
.WriteInt(1 << 30));
333 iter
= PickleIterator(big_len
);
335 EXPECT_FALSE(big_len
.ReadWString(&iter
, &wstr
));
338 // Check we can write zero bytes of data and 'data' can be NULL.
339 TEST(PickleTest
, ZeroLength
) {
341 EXPECT_TRUE(pickle
.WriteData(NULL
, 0));
343 PickleIterator
iter(pickle
);
346 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
347 EXPECT_EQ(0, outdatalen
);
348 // We can't assert that outdata is NULL.
351 // Check that ReadBytes works properly with an iterator initialized to NULL.
352 TEST(PickleTest
, ReadBytes
) {
355 EXPECT_TRUE(pickle
.WriteBytes(&data
, sizeof(data
)));
357 PickleIterator
iter(pickle
);
358 const char* outdata_char
= NULL
;
359 EXPECT_TRUE(pickle
.ReadBytes(&iter
, &outdata_char
, sizeof(data
)));
362 memcpy(&outdata
, outdata_char
, sizeof(outdata
));
363 EXPECT_EQ(data
, outdata
);