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/string16.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 const int testint
= 2093847192;
16 const std::string
teststr("Hello world"); // note non-aligned string length
17 const std::wstring
testwstr(L
"Hello, world");
18 const char testdata
[] = "AAA\0BBB\0";
19 const int testdatalen
= arraysize(testdata
) - 1;
20 const bool testbool1
= false;
21 const bool testbool2
= true;
22 const uint16 testuint16
= 32123;
24 // checks that the result
25 void VerifyResult(const Pickle
& pickle
) {
26 PickleIterator
iter(pickle
);
29 EXPECT_TRUE(pickle
.ReadInt(&iter
, &outint
));
30 EXPECT_EQ(testint
, outint
);
33 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
34 EXPECT_EQ(teststr
, outstr
);
37 EXPECT_TRUE(pickle
.ReadWString(&iter
, &outwstr
));
38 EXPECT_EQ(testwstr
, outwstr
);
41 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
42 EXPECT_EQ(testbool1
, outbool
);
43 EXPECT_TRUE(pickle
.ReadBool(&iter
, &outbool
));
44 EXPECT_EQ(testbool2
, outbool
);
47 EXPECT_TRUE(pickle
.ReadUInt16(&iter
, &outuint16
));
48 EXPECT_EQ(testuint16
, outuint16
);
52 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
53 EXPECT_EQ(testdatalen
, outdatalen
);
54 EXPECT_EQ(memcmp(testdata
, outdata
, outdatalen
), 0);
56 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
57 EXPECT_EQ(testdatalen
, outdatalen
);
58 EXPECT_EQ(memcmp(testdata
, outdata
, outdatalen
), 0);
60 // reads past the end should fail
61 EXPECT_FALSE(pickle
.ReadInt(&iter
, &outint
));
66 TEST(PickleTest
, EncodeDecode
) {
69 EXPECT_TRUE(pickle
.WriteInt(testint
));
70 EXPECT_TRUE(pickle
.WriteString(teststr
));
71 EXPECT_TRUE(pickle
.WriteWString(testwstr
));
72 EXPECT_TRUE(pickle
.WriteBool(testbool1
));
73 EXPECT_TRUE(pickle
.WriteBool(testbool2
));
74 EXPECT_TRUE(pickle
.WriteUInt16(testuint16
));
75 EXPECT_TRUE(pickle
.WriteData(testdata
, testdatalen
));
77 // Over allocate BeginWriteData so we can test TrimWriteData.
78 char* dest
= pickle
.BeginWriteData(testdatalen
+ 100);
80 memcpy(dest
, testdata
, testdatalen
);
82 pickle
.TrimWriteData(testdatalen
);
86 // test copy constructor
87 Pickle
pickle2(pickle
);
88 VerifyResult(pickle2
);
93 VerifyResult(pickle3
);
96 // Tests that we can handle really small buffers.
97 TEST(PickleTest
, SmallBuffer
) {
98 scoped_array
<char> buffer(new char[1]);
100 // We should not touch the buffer.
101 Pickle
pickle(buffer
.get(), 1);
103 PickleIterator
iter(pickle
);
105 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
108 // Tests that we can handle improper headers.
109 TEST(PickleTest
, BigSize
) {
110 int buffer
[] = { 0x56035200, 25, 40, 50 };
112 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
114 PickleIterator
iter(pickle
);
116 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
119 TEST(PickleTest
, UnalignedSize
) {
120 int buffer
[] = { 10, 25, 40, 50 };
122 Pickle
pickle(reinterpret_cast<char*>(buffer
), sizeof(buffer
));
124 PickleIterator
iter(pickle
);
126 EXPECT_FALSE(pickle
.ReadInt(&iter
, &data
));
129 TEST(PickleTest
, ZeroLenStr
) {
131 EXPECT_TRUE(pickle
.WriteString(""));
133 PickleIterator
iter(pickle
);
135 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
136 EXPECT_EQ("", outstr
);
139 TEST(PickleTest
, ZeroLenWStr
) {
141 EXPECT_TRUE(pickle
.WriteWString(L
""));
143 PickleIterator
iter(pickle
);
145 EXPECT_TRUE(pickle
.ReadString(&iter
, &outstr
));
146 EXPECT_EQ("", outstr
);
149 TEST(PickleTest
, BadLenStr
) {
151 EXPECT_TRUE(pickle
.WriteInt(-2));
153 PickleIterator
iter(pickle
);
155 EXPECT_FALSE(pickle
.ReadString(&iter
, &outstr
));
158 TEST(PickleTest
, BadLenWStr
) {
160 EXPECT_TRUE(pickle
.WriteInt(-1));
162 PickleIterator
iter(pickle
);
163 std::wstring woutstr
;
164 EXPECT_FALSE(pickle
.ReadWString(&iter
, &woutstr
));
167 TEST(PickleTest
, FindNext
) {
169 EXPECT_TRUE(pickle
.WriteInt(1));
170 EXPECT_TRUE(pickle
.WriteString("Domo"));
172 const char* start
= reinterpret_cast<const char*>(pickle
.data());
173 const char* end
= start
+ pickle
.size();
175 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
));
176 EXPECT_TRUE(NULL
== Pickle::FindNext(pickle
.header_size_
, start
, end
- 1));
177 EXPECT_TRUE(end
== Pickle::FindNext(pickle
.header_size_
, start
, end
+ 1));
180 TEST(PickleTest
, FindNextWithIncompleteHeader
) {
181 size_t header_size
= sizeof(Pickle::Header
);
182 scoped_array
<char> buffer(new char[header_size
- 1]);
183 memset(buffer
.get(), 0x1, header_size
- 1);
185 const char* start
= buffer
.get();
186 const char* end
= start
+ header_size
- 1;
188 EXPECT_TRUE(NULL
== Pickle::FindNext(header_size
, start
, end
));
191 TEST(PickleTest
, GetReadPointerAndAdvance
) {
194 PickleIterator
iter(pickle
);
195 EXPECT_FALSE(iter
.GetReadPointerAndAdvance(1));
197 EXPECT_TRUE(pickle
.WriteInt(1));
198 EXPECT_TRUE(pickle
.WriteInt(2));
199 int bytes
= sizeof(int) * 2;
201 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(0));
202 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(1));
203 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(-1));
204 EXPECT_TRUE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
));
205 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(bytes
+ 1));
206 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MAX
));
207 EXPECT_FALSE(PickleIterator(pickle
).GetReadPointerAndAdvance(INT_MIN
));
210 TEST(PickleTest
, Resize
) {
211 size_t unit
= Pickle::kPayloadUnit
;
212 scoped_array
<char> data(new char[unit
]);
213 char* data_ptr
= data
.get();
214 for (size_t i
= 0; i
< unit
; i
++)
217 // construct a message that will be exactly the size of one payload unit,
218 // note that any data will have a 4-byte header indicating the size
219 const size_t payload_size_after_header
= unit
- sizeof(uint32
);
221 pickle
.WriteData(data_ptr
,
222 static_cast<int>(payload_size_after_header
- sizeof(uint32
)));
223 size_t cur_payload
= payload_size_after_header
;
225 // note: we assume 'unit' is a power of 2
226 EXPECT_EQ(unit
, pickle
.capacity());
227 EXPECT_EQ(pickle
.payload_size(), payload_size_after_header
);
229 // fill out a full page (noting data header)
230 pickle
.WriteData(data_ptr
, static_cast<int>(unit
- sizeof(uint32
)));
232 EXPECT_EQ(unit
* 2, pickle
.capacity());
233 EXPECT_EQ(cur_payload
, pickle
.payload_size());
235 // one more byte should double the capacity
236 pickle
.WriteData(data_ptr
, 1);
238 EXPECT_EQ(unit
* 4, pickle
.capacity());
239 EXPECT_EQ(cur_payload
, pickle
.payload_size());
244 struct CustomHeader
: Pickle::Header
{
250 TEST(PickleTest
, HeaderPadding
) {
251 const uint32 kMagic
= 0x12345678;
253 Pickle
pickle(sizeof(CustomHeader
));
254 pickle
.WriteInt(kMagic
);
256 // this should not overwrite the 'int' payload
257 pickle
.headerT
<CustomHeader
>()->blah
= 10;
259 PickleIterator
iter(pickle
);
261 ASSERT_TRUE(pickle
.ReadInt(&iter
, &result
));
263 EXPECT_EQ(static_cast<uint32
>(result
), kMagic
);
266 TEST(PickleTest
, EqualsOperator
) {
270 Pickle
copy_refs_source_buffer(static_cast<const char*>(source
.data()),
273 copy
= copy_refs_source_buffer
;
274 ASSERT_EQ(source
.size(), copy
.size());
277 TEST(PickleTest
, EvilLengths
) {
279 std::string
str(100000, 'A');
280 EXPECT_TRUE(source
.WriteData(str
.c_str(), 100000));
281 // ReadString16 used to have its read buffer length calculation wrong leading
282 // to out-of-bounds reading.
283 PickleIterator
iter(source
);
285 EXPECT_FALSE(source
.ReadString16(&iter
, &str16
));
287 // And check we didn't break ReadString16.
288 str16
= (wchar_t) 'A';
290 EXPECT_TRUE(str16_pickle
.WriteString16(str16
));
291 iter
= PickleIterator(str16_pickle
);
292 EXPECT_TRUE(str16_pickle
.ReadString16(&iter
, &str16
));
293 EXPECT_EQ(1U, str16
.length());
295 // Check we don't fail in a length check with invalid String16 size.
296 // (1<<31) * sizeof(char16) == 0, so this is particularly evil.
298 EXPECT_TRUE(bad_len
.WriteInt(1 << 31));
299 iter
= PickleIterator(bad_len
);
300 EXPECT_FALSE(bad_len
.ReadString16(&iter
, &str16
));
302 // Check we don't fail in a length check with large WStrings.
304 EXPECT_TRUE(big_len
.WriteInt(1 << 30));
305 iter
= PickleIterator(big_len
);
307 EXPECT_FALSE(big_len
.ReadWString(&iter
, &wstr
));
310 // Check we can write zero bytes of data and 'data' can be NULL.
311 TEST(PickleTest
, ZeroLength
) {
313 EXPECT_TRUE(pickle
.WriteData(NULL
, 0));
315 PickleIterator
iter(pickle
);
318 EXPECT_TRUE(pickle
.ReadData(&iter
, &outdata
, &outdatalen
));
319 EXPECT_EQ(0, outdatalen
);
320 // We can't assert that outdata is NULL.
323 // Check that ReadBytes works properly with an iterator initialized to NULL.
324 TEST(PickleTest
, ReadBytes
) {
327 EXPECT_TRUE(pickle
.WriteBytes(&data
, sizeof(data
)));
329 PickleIterator
iter(pickle
);
330 const char* outdata_char
= NULL
;
331 EXPECT_TRUE(pickle
.ReadBytes(&iter
, &outdata_char
, sizeof(data
)));
334 memcpy(&outdata
, outdata_char
, sizeof(outdata
));
335 EXPECT_EQ(data
, outdata
);