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.
4
5 #include "base/pickle.h"
6
7 #include <limits.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <memory>
12 #include <string>
13
14 #include "base/macros.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace base {
20
21 namespace {
22
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 = arraysize(testdata) - 1;
41
42 // checks that the results can be read correctly from the Pickle
VerifyResult(const Pickle & pickle)43 void VerifyResult(const Pickle& pickle) {
44 PickleIterator iter(pickle);
45
46 bool outbool;
47 EXPECT_TRUE(iter.ReadBool(&outbool));
48 EXPECT_FALSE(outbool);
49 EXPECT_TRUE(iter.ReadBool(&outbool));
50 EXPECT_TRUE(outbool);
51
52 int outint;
53 EXPECT_TRUE(iter.ReadInt(&outint));
54 EXPECT_EQ(testint, outint);
55
56 long outlong;
57 EXPECT_TRUE(iter.ReadLong(&outlong));
58 EXPECT_EQ(testlong, outlong);
59
60 uint16_t outuint16;
61 EXPECT_TRUE(iter.ReadUInt16(&outuint16));
62 EXPECT_EQ(testuint16, outuint16);
63
64 uint32_t outuint32;
65 EXPECT_TRUE(iter.ReadUInt32(&outuint32));
66 EXPECT_EQ(testuint32, outuint32);
67
68 int64_t outint64;
69 EXPECT_TRUE(iter.ReadInt64(&outint64));
70 EXPECT_EQ(testint64, outint64);
71
72 uint64_t outuint64;
73 EXPECT_TRUE(iter.ReadUInt64(&outuint64));
74 EXPECT_EQ(testuint64, outuint64);
75
76 float outfloat;
77 EXPECT_TRUE(iter.ReadFloat(&outfloat));
78 EXPECT_EQ(testfloat, outfloat);
79
80 double outdouble;
81 EXPECT_TRUE(iter.ReadDouble(&outdouble));
82 EXPECT_EQ(testdouble, outdouble);
83
84 std::string outstring;
85 EXPECT_TRUE(iter.ReadString(&outstring));
86 EXPECT_EQ(teststring, outstring);
87
88 string16 outstring16;
89 EXPECT_TRUE(iter.ReadString16(&outstring16));
90 EXPECT_EQ(teststring16, outstring16);
91
92 StringPiece outstringpiece;
93 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece));
94 EXPECT_EQ(testrawstring, outstringpiece);
95
96 StringPiece16 outstringpiece16;
97 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16));
98 EXPECT_EQ(testrawstring16, outstringpiece16);
99
100 const char* outdata;
101 int outdatalen;
102 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
103 EXPECT_EQ(testdatalen, outdatalen);
104 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0);
105
106 // reads past the end should fail
107 EXPECT_FALSE(iter.ReadInt(&outint));
108 }
109
110 } // namespace
111
TEST(PickleTest,EncodeDecode)112 TEST(PickleTest, EncodeDecode) {
113 Pickle pickle;
114
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);
131
132 // test copy constructor
133 Pickle pickle2(pickle);
134 VerifyResult(pickle2);
135
136 // test operator=
137 Pickle pickle3;
138 pickle3 = pickle;
139 VerifyResult(pickle3);
140 }
141
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.
TEST(PickleTest,LongFrom64Bit)145 TEST(PickleTest, LongFrom64Bit) {
146 Pickle pickle;
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);
150
151 PickleIterator iter(pickle);
152 long outlong;
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)), "");
158 #endif
159 } else {
160 EXPECT_TRUE(iter.ReadLong(&outlong));
161 EXPECT_EQ(testint64, outlong);
162 }
163 }
164
165 // Tests that we can handle really small buffers.
TEST(PickleTest,SmallBuffer)166 TEST(PickleTest, SmallBuffer) {
167 std::unique_ptr<char[]> buffer(new char[1]);
168
169 // We should not touch the buffer.
170 Pickle pickle(buffer.get(), 1);
171
172 PickleIterator iter(pickle);
173 int data;
174 EXPECT_FALSE(iter.ReadInt(&data));
175 }
176
177 // Tests that we can handle improper headers.
TEST(PickleTest,BigSize)178 TEST(PickleTest, BigSize) {
179 int buffer[] = { 0x56035200, 25, 40, 50 };
180
181 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
182
183 PickleIterator iter(pickle);
184 int data;
185 EXPECT_FALSE(iter.ReadInt(&data));
186 }
187
TEST(PickleTest,UnalignedSize)188 TEST(PickleTest, UnalignedSize) {
189 int buffer[] = { 10, 25, 40, 50 };
190
191 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
192
193 PickleIterator iter(pickle);
194 int data;
195 EXPECT_FALSE(iter.ReadInt(&data));
196 }
197
TEST(PickleTest,ZeroLenStr)198 TEST(PickleTest, ZeroLenStr) {
199 Pickle pickle;
200 pickle.WriteString(std::string());
201
202 PickleIterator iter(pickle);
203 std::string outstr;
204 EXPECT_TRUE(iter.ReadString(&outstr));
205 EXPECT_EQ("", outstr);
206 }
207
TEST(PickleTest,ZeroLenStr16)208 TEST(PickleTest, ZeroLenStr16) {
209 Pickle pickle;
210 pickle.WriteString16(string16());
211
212 PickleIterator iter(pickle);
213 std::string outstr;
214 EXPECT_TRUE(iter.ReadString(&outstr));
215 EXPECT_EQ("", outstr);
216 }
217
TEST(PickleTest,BadLenStr)218 TEST(PickleTest, BadLenStr) {
219 Pickle pickle;
220 pickle.WriteInt(-2);
221
222 PickleIterator iter(pickle);
223 std::string outstr;
224 EXPECT_FALSE(iter.ReadString(&outstr));
225 }
226
TEST(PickleTest,BadLenStr16)227 TEST(PickleTest, BadLenStr16) {
228 Pickle pickle;
229 pickle.WriteInt(-1);
230
231 PickleIterator iter(pickle);
232 string16 outstr;
233 EXPECT_FALSE(iter.ReadString16(&outstr));
234 }
235
TEST(PickleTest,PeekNext)236 TEST(PickleTest, PeekNext) {
237 struct CustomHeader : base::Pickle::Header {
238 int cookies[10];
239 };
240
241 Pickle pickle(sizeof(CustomHeader));
242
243 pickle.WriteString("Goooooooooooogle");
244
245 const char* pickle_data = static_cast<const char*>(pickle.data());
246
247 size_t pickle_size;
248
249 // Data range doesn't contain header
250 EXPECT_FALSE(Pickle::PeekNext(
251 sizeof(CustomHeader),
252 pickle_data,
253 pickle_data + sizeof(CustomHeader) - 1,
254 &pickle_size));
255
256 // Data range contains header
257 EXPECT_TRUE(Pickle::PeekNext(
258 sizeof(CustomHeader),
259 pickle_data,
260 pickle_data + sizeof(CustomHeader),
261 &pickle_size));
262 EXPECT_EQ(pickle_size, pickle.size());
263
264 // Data range contains header and some other data
265 EXPECT_TRUE(Pickle::PeekNext(
266 sizeof(CustomHeader),
267 pickle_data,
268 pickle_data + sizeof(CustomHeader) + 1,
269 &pickle_size));
270 EXPECT_EQ(pickle_size, pickle.size());
271
272 // Data range contains full pickle
273 EXPECT_TRUE(Pickle::PeekNext(
274 sizeof(CustomHeader),
275 pickle_data,
276 pickle_data + pickle.size(),
277 &pickle_size));
278 EXPECT_EQ(pickle_size, pickle.size());
279 }
280
TEST(PickleTest,PeekNextOverflow)281 TEST(PickleTest, PeekNextOverflow) {
282 struct CustomHeader : base::Pickle::Header {
283 int cookies[10];
284 };
285
286 CustomHeader header;
287
288 // Check if we can wrap around at all
289 if (sizeof(size_t) > sizeof(header.payload_size))
290 return;
291
292 const char* pickle_data = reinterpret_cast<const char*>(&header);
293
294 size_t pickle_size;
295
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),
301 pickle_data,
302 pickle_data + sizeof(CustomHeader),
303 &pickle_size));
304 EXPECT_EQ(pickle_size, std::numeric_limits<size_t>::max());
305
306 // Ridiculous pickle sizes are fine (callers are supposed to
307 // verify them)
308 header.payload_size =
309 std::numeric_limits<uint32_t>::max() / 2 - sizeof(CustomHeader);
310 EXPECT_TRUE(Pickle::PeekNext(
311 sizeof(CustomHeader),
312 pickle_data,
313 pickle_data + sizeof(CustomHeader),
314 &pickle_size));
315 EXPECT_EQ(pickle_size, std::numeric_limits<uint32_t>::max() / 2);
316 }
317
TEST(PickleTest,FindNext)318 TEST(PickleTest, FindNext) {
319 Pickle pickle;
320 pickle.WriteInt(1);
321 pickle.WriteString("Domo");
322
323 const char* start = reinterpret_cast<const char*>(pickle.data());
324 const char* end = start + pickle.size();
325
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));
329 }
330
TEST(PickleTest,FindNextWithIncompleteHeader)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);
335
336 const char* start = buffer.get();
337 const char* end = start + header_size - 1;
338
339 EXPECT_EQ(nullptr, Pickle::FindNext(header_size, start, end));
340 }
341
342 #if defined(COMPILER_MSVC)
343 #pragma warning(push)
344 #pragma warning(disable: 4146)
345 #endif
TEST(PickleTest,FindNextOverflow)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))
357 return;
358
359 header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2);
360 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
361
362 header->payload_size = -header_size2;
363 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
364
365 header->payload_size = 0;
366 end = start + header_size;
367 EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end));
368 }
369 #if defined(COMPILER_MSVC)
370 #pragma warning(pop)
371 #endif
372
TEST(PickleTest,GetReadPointerAndAdvance)373 TEST(PickleTest, GetReadPointerAndAdvance) {
374 Pickle pickle;
375
376 PickleIterator iter(pickle);
377 EXPECT_FALSE(iter.GetReadPointerAndAdvance(1));
378
379 pickle.WriteInt(1);
380 pickle.WriteInt(2);
381 int bytes = sizeof(int) * 2;
382
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));
390 }
391
TEST(PickleTest,Resize)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++)
397 data_ptr[i] = 'G';
398
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);
402 Pickle pickle;
403 pickle.WriteData(
404 data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t)));
405 size_t cur_payload = payload_size_after_header;
406
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);
410
411 // fill out a full page (noting data header)
412 pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32_t)));
413 cur_payload += unit;
414 EXPECT_EQ(unit * 2, pickle.capacity_after_header());
415 EXPECT_EQ(cur_payload, pickle.payload_size());
416
417 // one more byte should double the capacity
418 pickle.WriteData(data_ptr, 1);
419 cur_payload += 8;
420 EXPECT_EQ(unit * 4, pickle.capacity_after_header());
421 EXPECT_EQ(cur_payload, pickle.payload_size());
422 }
423
424 namespace {
425
426 struct CustomHeader : Pickle::Header {
427 int blah;
428 };
429
430 } // namespace
431
TEST(PickleTest,HeaderPadding)432 TEST(PickleTest, HeaderPadding) {
433 const uint32_t kMagic = 0x12345678;
434
435 Pickle pickle(sizeof(CustomHeader));
436 pickle.WriteInt(kMagic);
437
438 // this should not overwrite the 'int' payload
439 pickle.headerT<CustomHeader>()->blah = 10;
440
441 PickleIterator iter(pickle);
442 int result;
443 ASSERT_TRUE(iter.ReadInt(&result));
444
445 EXPECT_EQ(static_cast<uint32_t>(result), kMagic);
446 }
447
TEST(PickleTest,EqualsOperator)448 TEST(PickleTest, EqualsOperator) {
449 Pickle source;
450 source.WriteInt(1);
451
452 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()),
453 source.size());
454 Pickle copy;
455 copy = copy_refs_source_buffer;
456 ASSERT_EQ(source.size(), copy.size());
457 }
458
TEST(PickleTest,EvilLengths)459 TEST(PickleTest, EvilLengths) {
460 Pickle source;
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);
466 string16 str16;
467 EXPECT_FALSE(iter.ReadString16(&str16));
468
469 // And check we didn't break ReadString16.
470 str16 = (wchar_t) 'A';
471 Pickle str16_pickle;
472 str16_pickle.WriteString16(str16);
473 iter = PickleIterator(str16_pickle);
474 EXPECT_TRUE(iter.ReadString16(&str16));
475 EXPECT_EQ(1U, str16.length());
476
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.
479 Pickle bad_len;
480 bad_len.WriteInt(1 << 31);
481 iter = PickleIterator(bad_len);
482 EXPECT_FALSE(iter.ReadString16(&str16));
483 }
484
485 // Check we can write zero bytes of data and 'data' can be NULL.
TEST(PickleTest,ZeroLength)486 TEST(PickleTest, ZeroLength) {
487 Pickle pickle;
488 pickle.WriteData(nullptr, 0);
489
490 PickleIterator iter(pickle);
491 const char* outdata;
492 int outdatalen;
493 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
494 EXPECT_EQ(0, outdatalen);
495 // We can't assert that outdata is NULL.
496 }
497
498 // Check that ReadBytes works properly with an iterator initialized to NULL.
TEST(PickleTest,ReadBytes)499 TEST(PickleTest, ReadBytes) {
500 Pickle pickle;
501 int data = 0x7abcd;
502 pickle.WriteBytes(&data, sizeof(data));
503
504 PickleIterator iter(pickle);
505 const char* outdata_char = nullptr;
506 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data)));
507
508 int outdata;
509 memcpy(&outdata, outdata_char, sizeof(outdata));
510 EXPECT_EQ(data, outdata);
511 }
512
513 // Checks that when a pickle is deep-copied, the result is not larger than
514 // needed.
TEST(PickleTest,DeepCopyResize)515 TEST(PickleTest, DeepCopyResize) {
516 Pickle pickle;
517 while (pickle.capacity_after_header() != pickle.payload_size())
518 pickle.WriteBool(true);
519
520 // Make a deep copy.
521 Pickle pickle2(pickle);
522
523 // Check that there isn't any extraneous capacity.
524 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header());
525 }
526
527 namespace {
528
529 // Publicly exposes the ClaimBytes interface for testing.
530 class TestingPickle : public Pickle {
531 public:
532 TestingPickle() = default;
533
ClaimBytes(size_t num_bytes)534 void* ClaimBytes(size_t num_bytes) { return Pickle::ClaimBytes(num_bytes); }
535 };
536
537 } // namespace
538
539 // Checks that claimed bytes are zero-initialized.
TEST(PickleTest,ClaimBytesInitialization)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]);
546 }
547 }
548
549 // Checks that ClaimBytes properly advances the write offset.
TEST(PickleTest,ClaimBytes)550 TEST(PickleTest, ClaimBytes) {
551 std::string data("Hello, world!");
552
553 TestingPickle pickle;
554 pickle.WriteUInt32(data.size());
555 void* bytes = pickle.ClaimBytes(data.size());
556 pickle.WriteInt(42);
557 memcpy(bytes, data.data(), data.size());
558
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);
563
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));
567
568 int out_value;
569 EXPECT_TRUE(iter.ReadInt(&out_value));
570 EXPECT_EQ(42, out_value);
571 }
572
573 } // namespace base
574