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.
5 #include "base/pickle.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 const bool testbool1 = false;
24 const bool testbool2 = true;
25 const int testint = 2'093'847'192;
26 const long testlong = 1'093'847'192;
27 const uint16_t testuint16 = 32123;
28 const uint32_t testuint32 = 1593847192;
29 const int64_t testint64 = -0x7E8CA925'3104BDFCLL;
30 const uint64_t testuint64 = 0xCE8CA925'3104BDF7ULL;
31 const float testfloat = 3.1415926935f;
32 const double testdouble = 2.71828182845904523;
33 const std::string teststring("Hello world"); // note non-aligned string length
34 const std::wstring testwstring(L"Hello, world");
35 const string16 teststring16(ASCIIToUTF16("Hello, world"));
36 const char testrawstring[] = "Hello new world"; // Test raw string writing
37 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars.
38 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0};
39 const char testdata[] = "AAA\0BBB\0";
40 const int testdatalen = base::size(testdata) - 1;
42 // checks that the results can be read correctly from the Pickle
43 void VerifyResult(const Pickle& pickle) {
44 PickleIterator iter(pickle);
47 EXPECT_TRUE(iter.ReadBool(&outbool));
48 EXPECT_FALSE(outbool);
49 EXPECT_TRUE(iter.ReadBool(&outbool));
53 EXPECT_TRUE(iter.ReadInt(&outint));
54 EXPECT_EQ(testint, outint);
57 EXPECT_TRUE(iter.ReadLong(&outlong));
58 EXPECT_EQ(testlong, outlong);
61 EXPECT_TRUE(iter.ReadUInt16(&outuint16));
62 EXPECT_EQ(testuint16, outuint16);
65 EXPECT_TRUE(iter.ReadUInt32(&outuint32));
66 EXPECT_EQ(testuint32, outuint32);
69 EXPECT_TRUE(iter.ReadInt64(&outint64));
70 EXPECT_EQ(testint64, outint64);
73 EXPECT_TRUE(iter.ReadUInt64(&outuint64));
74 EXPECT_EQ(testuint64, outuint64);
77 EXPECT_TRUE(iter.ReadFloat(&outfloat));
78 EXPECT_EQ(testfloat, outfloat);
81 EXPECT_TRUE(iter.ReadDouble(&outdouble));
82 EXPECT_EQ(testdouble, outdouble);
84 std::string outstring;
85 EXPECT_TRUE(iter.ReadString(&outstring));
86 EXPECT_EQ(teststring, outstring);
89 EXPECT_TRUE(iter.ReadString16(&outstring16));
90 EXPECT_EQ(teststring16, outstring16);
92 StringPiece outstringpiece;
93 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece));
94 EXPECT_EQ(testrawstring, outstringpiece);
96 StringPiece16 outstringpiece16;
97 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16));
98 EXPECT_EQ(testrawstring16, outstringpiece16);
102 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
103 EXPECT_EQ(testdatalen, outdatalen);
104 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0);
106 // reads past the end should fail
107 EXPECT_FALSE(iter.ReadInt(&outint));
112 TEST(PickleTest, EncodeDecode) {
115 pickle.WriteBool(testbool1);
116 pickle.WriteBool(testbool2);
117 pickle.WriteInt(testint);
118 pickle.WriteLong(testlong);
119 pickle.WriteUInt16(testuint16);
120 pickle.WriteUInt32(testuint32);
121 pickle.WriteInt64(testint64);
122 pickle.WriteUInt64(testuint64);
123 pickle.WriteFloat(testfloat);
124 pickle.WriteDouble(testdouble);
125 pickle.WriteString(teststring);
126 pickle.WriteString16(teststring16);
127 pickle.WriteString(testrawstring);
128 pickle.WriteString16(testrawstring16);
129 pickle.WriteData(testdata, testdatalen);
130 VerifyResult(pickle);
132 // test copy constructor
133 Pickle pickle2(pickle);
134 VerifyResult(pickle2);
139 VerifyResult(pickle3);
142 // Tests that reading/writing a long works correctly when the source process
143 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both
144 // arms of the conditional in this test.
145 TEST(PickleTest, LongFrom64Bit) {
147 // Under the hood long is always written as a 64-bit value, so simulate a
148 // 64-bit long even on 32-bit architectures by explicitly writing an int64_t.
149 pickle.WriteInt64(testint64);
151 PickleIterator iter(pickle);
153 if (sizeof(long) < sizeof(int64_t)) {
154 // ReadLong() should return false when the original written value can't be
155 // represented as a long.
156 #if GTEST_HAS_DEATH_TEST
157 EXPECT_DEATH(ignore_result(iter.ReadLong(&outlong)), "");
160 EXPECT_TRUE(iter.ReadLong(&outlong));
161 EXPECT_EQ(testint64, outlong);
165 // Tests that we can handle really small buffers.
166 TEST(PickleTest, SmallBuffer) {
167 std::unique_ptr<char[]> buffer(new char[1]);
169 // We should not touch the buffer.
170 Pickle pickle(buffer.get(), 1);
172 PickleIterator iter(pickle);
174 EXPECT_FALSE(iter.ReadInt(&data));
177 // Tests that we can handle improper headers.
178 TEST(PickleTest, BigSize) {
179 int buffer[] = { 0x56035200, 25, 40, 50 };
181 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
183 PickleIterator iter(pickle);
185 EXPECT_FALSE(iter.ReadInt(&data));
188 TEST(PickleTest, UnalignedSize) {
189 int buffer[] = { 10, 25, 40, 50 };
191 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
193 PickleIterator iter(pickle);
195 EXPECT_FALSE(iter.ReadInt(&data));
198 TEST(PickleTest, ZeroLenStr) {
200 pickle.WriteString(std::string());
202 PickleIterator iter(pickle);
204 EXPECT_TRUE(iter.ReadString(&outstr));
205 EXPECT_EQ("", outstr);
208 TEST(PickleTest, ZeroLenStr16) {
210 pickle.WriteString16(string16());
212 PickleIterator iter(pickle);
214 EXPECT_TRUE(iter.ReadString(&outstr));
215 EXPECT_EQ("", outstr);
218 TEST(PickleTest, BadLenStr) {
222 PickleIterator iter(pickle);
224 EXPECT_FALSE(iter.ReadString(&outstr));
227 TEST(PickleTest, BadLenStr16) {
231 PickleIterator iter(pickle);
233 EXPECT_FALSE(iter.ReadString16(&outstr));
236 TEST(PickleTest, PeekNext) {
237 struct CustomHeader : base::Pickle::Header {
241 Pickle pickle(sizeof(CustomHeader));
243 pickle.WriteString("Goooooooooooogle");
245 const char* pickle_data = static_cast<const char*>(pickle.data());
249 // Data range doesn't contain header
250 EXPECT_FALSE(Pickle::PeekNext(
251 sizeof(CustomHeader),
253 pickle_data + sizeof(CustomHeader) - 1,
256 // Data range contains header
257 EXPECT_TRUE(Pickle::PeekNext(
258 sizeof(CustomHeader),
260 pickle_data + sizeof(CustomHeader),
262 EXPECT_EQ(pickle_size, pickle.size());
264 // Data range contains header and some other data
265 EXPECT_TRUE(Pickle::PeekNext(
266 sizeof(CustomHeader),
268 pickle_data + sizeof(CustomHeader) + 1,
270 EXPECT_EQ(pickle_size, pickle.size());
272 // Data range contains full pickle
273 EXPECT_TRUE(Pickle::PeekNext(
274 sizeof(CustomHeader),
276 pickle_data + pickle.size(),
278 EXPECT_EQ(pickle_size, pickle.size());
281 TEST(PickleTest, PeekNextOverflow) {
282 struct CustomHeader : base::Pickle::Header {
288 // Check if we can wrap around at all
289 if (sizeof(size_t) > sizeof(header.payload_size))
292 const char* pickle_data = reinterpret_cast<const char*>(&header);
296 // Wrapping around is detected and reported as maximum size_t value
297 header.payload_size = static_cast<uint32_t>(
298 1 - static_cast<int32_t>(sizeof(CustomHeader)));
299 EXPECT_TRUE(Pickle::PeekNext(
300 sizeof(CustomHeader),
302 pickle_data + sizeof(CustomHeader),
304 EXPECT_EQ(pickle_size, std::numeric_limits<size_t>::max());
306 // Ridiculous pickle sizes are fine (callers are supposed to
308 header.payload_size =
309 std::numeric_limits<uint32_t>::max() / 2 - sizeof(CustomHeader);
310 EXPECT_TRUE(Pickle::PeekNext(
311 sizeof(CustomHeader),
313 pickle_data + sizeof(CustomHeader),
315 EXPECT_EQ(pickle_size, std::numeric_limits<uint32_t>::max() / 2);
318 TEST(PickleTest, FindNext) {
321 pickle.WriteString("Domo");
323 const char* start = reinterpret_cast<const char*>(pickle.data());
324 const char* end = start + pickle.size();
326 EXPECT_EQ(end, Pickle::FindNext(pickle.header_size_, start, end));
327 EXPECT_EQ(nullptr, Pickle::FindNext(pickle.header_size_, start, end - 1));
328 EXPECT_EQ(end, Pickle::FindNext(pickle.header_size_, start, end + 1));
331 TEST(PickleTest, FindNextWithIncompleteHeader) {
332 size_t header_size = sizeof(Pickle::Header);
333 std::unique_ptr<char[]> buffer(new char[header_size - 1]);
334 memset(buffer.get(), 0x1, header_size - 1);
336 const char* start = buffer.get();
337 const char* end = start + header_size - 1;
339 EXPECT_EQ(nullptr, Pickle::FindNext(header_size, start, end));
342 #if defined(COMPILER_MSVC)
343 #pragma warning(push)
344 #pragma warning(disable: 4146)
346 TEST(PickleTest, FindNextOverflow) {
347 size_t header_size = sizeof(Pickle::Header);
348 size_t header_size2 = 2 * header_size;
349 size_t payload_received = 100;
350 std::unique_ptr<char[]> buffer(new char[header_size2 + payload_received]);
351 const char* start = buffer.get();
352 Pickle::Header* header = reinterpret_cast<Pickle::Header*>(buffer.get());
353 const char* end = start + header_size2 + payload_received;
354 // It is impossible to construct an overflow test otherwise.
355 if (sizeof(size_t) > sizeof(header->payload_size) ||
356 sizeof(uintptr_t) > sizeof(header->payload_size))
359 header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2);
360 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
362 header->payload_size = -header_size2;
363 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
365 header->payload_size = 0;
366 end = start + header_size;
367 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
369 #if defined(COMPILER_MSVC)
373 TEST(PickleTest, GetReadPointerAndAdvance) {
376 PickleIterator iter(pickle);
377 EXPECT_FALSE(iter.GetReadPointerAndAdvance(1));
381 int bytes = sizeof(int) * 2;
383 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(0));
384 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(1));
385 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(-1));
386 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes));
387 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes + 1));
388 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MAX));
389 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MIN));
392 TEST(PickleTest, Resize) {
393 size_t unit = Pickle::kPayloadUnit;
394 std::unique_ptr<char[]> data(new char[unit]);
395 char* data_ptr = data.get();
396 for (size_t i = 0; i < unit; i++)
399 // construct a message that will be exactly the size of one payload unit,
400 // note that any data will have a 4-byte header indicating the size
401 const size_t payload_size_after_header = unit - sizeof(uint32_t);
404 data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t)));
405 size_t cur_payload = payload_size_after_header;
407 // note: we assume 'unit' is a power of 2
408 EXPECT_EQ(unit, pickle.capacity_after_header());
409 EXPECT_EQ(pickle.payload_size(), payload_size_after_header);
411 // fill out a full page (noting data header)
412 pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32_t)));
414 EXPECT_EQ(unit * 2, pickle.capacity_after_header());
415 EXPECT_EQ(cur_payload, pickle.payload_size());
417 // one more byte should double the capacity
418 pickle.WriteData(data_ptr, 1);
420 EXPECT_EQ(unit * 4, pickle.capacity_after_header());
421 EXPECT_EQ(cur_payload, pickle.payload_size());
426 struct CustomHeader : Pickle::Header {
432 TEST(PickleTest, HeaderPadding) {
433 const uint32_t kMagic = 0x12345678;
435 Pickle pickle(sizeof(CustomHeader));
436 pickle.WriteInt(kMagic);
438 // this should not overwrite the 'int' payload
439 pickle.headerT<CustomHeader>()->blah = 10;
441 PickleIterator iter(pickle);
443 ASSERT_TRUE(iter.ReadInt(&result));
445 EXPECT_EQ(static_cast<uint32_t>(result), kMagic);
448 TEST(PickleTest, EqualsOperator) {
452 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()),
455 copy = copy_refs_source_buffer;
456 ASSERT_EQ(source.size(), copy.size());
459 TEST(PickleTest, EvilLengths) {
461 std::string str(100000, 'A');
462 source.WriteData(str.c_str(), 100000);
463 // ReadString16 used to have its read buffer length calculation wrong leading
464 // to out-of-bounds reading.
465 PickleIterator iter(source);
467 EXPECT_FALSE(iter.ReadString16(&str16));
469 // And check we didn't break ReadString16.
470 str16 = (wchar_t) 'A';
472 str16_pickle.WriteString16(str16);
473 iter = PickleIterator(str16_pickle);
474 EXPECT_TRUE(iter.ReadString16(&str16));
475 EXPECT_EQ(1U, str16.length());
477 // Check we don't fail in a length check with invalid String16 size.
478 // (1<<31) * sizeof(char16) == 0, so this is particularly evil.
480 bad_len.WriteInt(1 << 31);
481 iter = PickleIterator(bad_len);
482 EXPECT_FALSE(iter.ReadString16(&str16));
485 // Check we can write zero bytes of data and 'data' can be NULL.
486 TEST(PickleTest, ZeroLength) {
488 pickle.WriteData(nullptr, 0);
490 PickleIterator iter(pickle);
493 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
494 EXPECT_EQ(0, outdatalen);
495 // We can't assert that outdata is NULL.
498 // Check that ReadBytes works properly with an iterator initialized to NULL.
499 TEST(PickleTest, ReadBytes) {
502 pickle.WriteBytes(&data, sizeof(data));
504 PickleIterator iter(pickle);
505 const char* outdata_char = nullptr;
506 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data)));
509 memcpy(&outdata, outdata_char, sizeof(outdata));
510 EXPECT_EQ(data, outdata);
513 // Checks that when a pickle is deep-copied, the result is not larger than
515 TEST(PickleTest, DeepCopyResize) {
517 while (pickle.capacity_after_header() != pickle.payload_size())
518 pickle.WriteBool(true);
521 Pickle pickle2(pickle);
523 // Check that there isn't any extraneous capacity.
524 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header());
529 // Publicly exposes the ClaimBytes interface for testing.
530 class TestingPickle : public Pickle {
532 TestingPickle() = default;
534 void* ClaimBytes(size_t num_bytes) { return Pickle::ClaimBytes(num_bytes); }
539 // Checks that claimed bytes are zero-initialized.
540 TEST(PickleTest, ClaimBytesInitialization) {
541 static const int kChunkSize = 64;
542 TestingPickle pickle;
543 const char* bytes = static_cast<const char*>(pickle.ClaimBytes(kChunkSize));
544 for (size_t i = 0; i < kChunkSize; ++i) {
545 EXPECT_EQ(0, bytes[i]);
549 // Checks that ClaimBytes properly advances the write offset.
550 TEST(PickleTest, ClaimBytes) {
551 std::string data("Hello, world!");
553 TestingPickle pickle;
554 pickle.WriteUInt32(data.size());
555 void* bytes = pickle.ClaimBytes(data.size());
557 memcpy(bytes, data.data(), data.size());
559 PickleIterator iter(pickle);
560 uint32_t out_data_length;
561 EXPECT_TRUE(iter.ReadUInt32(&out_data_length));
562 EXPECT_EQ(data.size(), out_data_length);
564 const char* out_data = nullptr;
565 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length));
566 EXPECT_EQ(data, std::string(out_data, out_data_length));
569 EXPECT_TRUE(iter.ReadInt(&out_value));
570 EXPECT_EQ(42, out_value);
573 TEST(PickleTest, ReachedEnd) {
579 PickleIterator iter(pickle);
582 EXPECT_FALSE(iter.ReachedEnd());
583 EXPECT_TRUE(iter.ReadInt(&out));
586 EXPECT_FALSE(iter.ReachedEnd());
587 EXPECT_TRUE(iter.ReadInt(&out));
590 EXPECT_FALSE(iter.ReachedEnd());
591 EXPECT_TRUE(iter.ReadInt(&out));
594 EXPECT_TRUE(iter.ReachedEnd());
595 EXPECT_FALSE(iter.ReadInt(&out));
596 EXPECT_TRUE(iter.ReachedEnd());