Implement Site Settings \ Images category.
[chromium-blink-merge.git] / base / pickle_unittest.cc
bloba2c405ce9181b51c59d627e730d417901138dadd
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 <string>
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 "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // Remove when this file is in the base namespace.
15 using base::string16;
17 namespace {
19 const bool testbool1 = false;
20 const bool testbool2 = true;
21 const int testint = 2093847192;
22 const long testlong = 1093847192;
23 const uint16 testuint16 = 32123;
24 const uint32 testuint32 = 1593847192;
25 const int64 testint64 = -0x7E8CA9253104BDFCLL;
26 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL;
27 const size_t testsizet = 0xFEDC7654;
28 const float testfloat = 3.1415926935f;
29 const double testdouble = 2.71828182845904523;
30 const std::string teststring("Hello world"); // note non-aligned string length
31 const std::wstring testwstring(L"Hello, world");
32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world"));
33 const char testrawstring[] = "Hello new world"; // Test raw string writing
34 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars.
35 const base::char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0};
36 const char testdata[] = "AAA\0BBB\0";
37 const int testdatalen = arraysize(testdata) - 1;
39 // checks that the results can be read correctly from the Pickle
40 void VerifyResult(const Pickle& pickle) {
41 PickleIterator iter(pickle);
43 bool outbool;
44 EXPECT_TRUE(iter.ReadBool(&outbool));
45 EXPECT_FALSE(outbool);
46 EXPECT_TRUE(iter.ReadBool(&outbool));
47 EXPECT_TRUE(outbool);
49 int outint;
50 EXPECT_TRUE(iter.ReadInt(&outint));
51 EXPECT_EQ(testint, outint);
53 long outlong;
54 EXPECT_TRUE(iter.ReadLong(&outlong));
55 EXPECT_EQ(testlong, outlong);
57 uint16 outuint16;
58 EXPECT_TRUE(iter.ReadUInt16(&outuint16));
59 EXPECT_EQ(testuint16, outuint16);
61 uint32 outuint32;
62 EXPECT_TRUE(iter.ReadUInt32(&outuint32));
63 EXPECT_EQ(testuint32, outuint32);
65 int64 outint64;
66 EXPECT_TRUE(iter.ReadInt64(&outint64));
67 EXPECT_EQ(testint64, outint64);
69 uint64 outuint64;
70 EXPECT_TRUE(iter.ReadUInt64(&outuint64));
71 EXPECT_EQ(testuint64, outuint64);
73 size_t outsizet;
74 EXPECT_TRUE(iter.ReadSizeT(&outsizet));
75 EXPECT_EQ(testsizet, outsizet);
77 float outfloat;
78 EXPECT_TRUE(iter.ReadFloat(&outfloat));
79 EXPECT_EQ(testfloat, outfloat);
81 double outdouble;
82 EXPECT_TRUE(iter.ReadDouble(&outdouble));
83 EXPECT_EQ(testdouble, outdouble);
85 std::string outstring;
86 EXPECT_TRUE(iter.ReadString(&outstring));
87 EXPECT_EQ(teststring, outstring);
89 base::string16 outstring16;
90 EXPECT_TRUE(iter.ReadString16(&outstring16));
91 EXPECT_EQ(teststring16, outstring16);
93 base::StringPiece outstringpiece;
94 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece));
95 EXPECT_EQ(testrawstring, outstringpiece);
97 base::StringPiece16 outstringpiece16;
98 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16));
99 EXPECT_EQ(testrawstring16, outstringpiece16);
101 const char* outdata;
102 int outdatalen;
103 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
104 EXPECT_EQ(testdatalen, outdatalen);
105 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0);
107 // reads past the end should fail
108 EXPECT_FALSE(iter.ReadInt(&outint));
111 } // namespace
113 TEST(PickleTest, EncodeDecode) {
114 Pickle pickle;
116 EXPECT_TRUE(pickle.WriteBool(testbool1));
117 EXPECT_TRUE(pickle.WriteBool(testbool2));
118 EXPECT_TRUE(pickle.WriteInt(testint));
119 EXPECT_TRUE(
120 pickle.WriteLongUsingDangerousNonPortableLessPersistableForm(testlong));
121 EXPECT_TRUE(pickle.WriteUInt16(testuint16));
122 EXPECT_TRUE(pickle.WriteUInt32(testuint32));
123 EXPECT_TRUE(pickle.WriteInt64(testint64));
124 EXPECT_TRUE(pickle.WriteUInt64(testuint64));
125 EXPECT_TRUE(pickle.WriteSizeT(testsizet));
126 EXPECT_TRUE(pickle.WriteFloat(testfloat));
127 EXPECT_TRUE(pickle.WriteDouble(testdouble));
128 EXPECT_TRUE(pickle.WriteString(teststring));
129 EXPECT_TRUE(pickle.WriteString16(teststring16));
130 EXPECT_TRUE(pickle.WriteString(testrawstring));
131 EXPECT_TRUE(pickle.WriteString16(testrawstring16));
132 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen));
133 VerifyResult(pickle);
135 // test copy constructor
136 Pickle pickle2(pickle);
137 VerifyResult(pickle2);
139 // test operator=
140 Pickle pickle3;
141 pickle3 = pickle;
142 VerifyResult(pickle3);
145 // Tests that reading/writing a size_t works correctly when the source process
146 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both
147 // arms of the conditional in this test.
148 TEST(PickleTest, SizeTFrom64Bit) {
149 Pickle pickle;
150 // Under the hood size_t is always written as a 64-bit value, so simulate a
151 // 64-bit size_t even on 32-bit architectures by explicitly writing a uint64.
152 EXPECT_TRUE(pickle.WriteUInt64(testuint64));
154 PickleIterator iter(pickle);
155 size_t outsizet;
156 if (sizeof(size_t) < sizeof(uint64)) {
157 // ReadSizeT() should return false when the original written value can't be
158 // represented as a size_t.
159 EXPECT_FALSE(iter.ReadSizeT(&outsizet));
160 } else {
161 EXPECT_TRUE(iter.ReadSizeT(&outsizet));
162 EXPECT_EQ(testuint64, outsizet);
166 // Tests that we can handle really small buffers.
167 TEST(PickleTest, SmallBuffer) {
168 scoped_ptr<char[]> buffer(new char[1]);
170 // We should not touch the buffer.
171 Pickle pickle(buffer.get(), 1);
173 PickleIterator iter(pickle);
174 int data;
175 EXPECT_FALSE(iter.ReadInt(&data));
178 // Tests that we can handle improper headers.
179 TEST(PickleTest, BigSize) {
180 int buffer[] = { 0x56035200, 25, 40, 50 };
182 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
184 PickleIterator iter(pickle);
185 int data;
186 EXPECT_FALSE(iter.ReadInt(&data));
189 TEST(PickleTest, UnalignedSize) {
190 int buffer[] = { 10, 25, 40, 50 };
192 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer));
194 PickleIterator iter(pickle);
195 int data;
196 EXPECT_FALSE(iter.ReadInt(&data));
199 TEST(PickleTest, ZeroLenStr) {
200 Pickle pickle;
201 EXPECT_TRUE(pickle.WriteString(std::string()));
203 PickleIterator iter(pickle);
204 std::string outstr;
205 EXPECT_TRUE(iter.ReadString(&outstr));
206 EXPECT_EQ("", outstr);
209 TEST(PickleTest, ZeroLenStr16) {
210 Pickle pickle;
211 EXPECT_TRUE(pickle.WriteString16(base::string16()));
213 PickleIterator iter(pickle);
214 std::string outstr;
215 EXPECT_TRUE(iter.ReadString(&outstr));
216 EXPECT_EQ("", outstr);
219 TEST(PickleTest, BadLenStr) {
220 Pickle pickle;
221 EXPECT_TRUE(pickle.WriteInt(-2));
223 PickleIterator iter(pickle);
224 std::string outstr;
225 EXPECT_FALSE(iter.ReadString(&outstr));
228 TEST(PickleTest, BadLenStr16) {
229 Pickle pickle;
230 EXPECT_TRUE(pickle.WriteInt(-1));
232 PickleIterator iter(pickle);
233 base::string16 outstr;
234 EXPECT_FALSE(iter.ReadString16(&outstr));
237 TEST(PickleTest, FindNext) {
238 Pickle pickle;
239 EXPECT_TRUE(pickle.WriteInt(1));
240 EXPECT_TRUE(pickle.WriteString("Domo"));
242 const char* start = reinterpret_cast<const char*>(pickle.data());
243 const char* end = start + pickle.size();
245 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end));
246 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1));
247 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1));
250 TEST(PickleTest, FindNextWithIncompleteHeader) {
251 size_t header_size = sizeof(Pickle::Header);
252 scoped_ptr<char[]> buffer(new char[header_size - 1]);
253 memset(buffer.get(), 0x1, header_size - 1);
255 const char* start = buffer.get();
256 const char* end = start + header_size - 1;
258 EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end));
261 #if defined(COMPILER_MSVC)
262 #pragma warning(push)
263 #pragma warning(disable: 4146)
264 #endif
265 TEST(PickleTest, FindNextOverflow) {
266 size_t header_size = sizeof(Pickle::Header);
267 size_t header_size2 = 2 * header_size;
268 size_t payload_received = 100;
269 scoped_ptr<char[]> buffer(new char[header_size2 + payload_received]);
270 const char* start = buffer.get();
271 Pickle::Header* header = reinterpret_cast<Pickle::Header*>(buffer.get());
272 const char* end = start + header_size2 + payload_received;
273 // It is impossible to construct an overflow test otherwise.
274 if (sizeof(size_t) > sizeof(header->payload_size) ||
275 sizeof(uintptr_t) > sizeof(header->payload_size))
276 return;
278 header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2);
279 EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end));
281 header->payload_size = -header_size2;
282 EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end));
284 header->payload_size = 0;
285 end = start + header_size;
286 EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end));
288 #if defined(COMPILER_MSVC)
289 #pragma warning(pop)
290 #endif
292 TEST(PickleTest, GetReadPointerAndAdvance) {
293 Pickle pickle;
295 PickleIterator iter(pickle);
296 EXPECT_FALSE(iter.GetReadPointerAndAdvance(1));
298 EXPECT_TRUE(pickle.WriteInt(1));
299 EXPECT_TRUE(pickle.WriteInt(2));
300 int bytes = sizeof(int) * 2;
302 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(0));
303 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(1));
304 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(-1));
305 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes));
306 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes + 1));
307 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MAX));
308 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MIN));
311 TEST(PickleTest, Resize) {
312 size_t unit = Pickle::kPayloadUnit;
313 scoped_ptr<char[]> data(new char[unit]);
314 char* data_ptr = data.get();
315 for (size_t i = 0; i < unit; i++)
316 data_ptr[i] = 'G';
318 // construct a message that will be exactly the size of one payload unit,
319 // note that any data will have a 4-byte header indicating the size
320 const size_t payload_size_after_header = unit - sizeof(uint32);
321 Pickle pickle;
322 pickle.WriteData(data_ptr,
323 static_cast<int>(payload_size_after_header - sizeof(uint32)));
324 size_t cur_payload = payload_size_after_header;
326 // note: we assume 'unit' is a power of 2
327 EXPECT_EQ(unit, pickle.capacity_after_header());
328 EXPECT_EQ(pickle.payload_size(), payload_size_after_header);
330 // fill out a full page (noting data header)
331 pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32)));
332 cur_payload += unit;
333 EXPECT_EQ(unit * 2, pickle.capacity_after_header());
334 EXPECT_EQ(cur_payload, pickle.payload_size());
336 // one more byte should double the capacity
337 pickle.WriteData(data_ptr, 1);
338 cur_payload += 8;
339 EXPECT_EQ(unit * 4, pickle.capacity_after_header());
340 EXPECT_EQ(cur_payload, pickle.payload_size());
343 namespace {
345 struct CustomHeader : Pickle::Header {
346 int blah;
349 } // namespace
351 TEST(PickleTest, HeaderPadding) {
352 const uint32 kMagic = 0x12345678;
354 Pickle pickle(sizeof(CustomHeader));
355 pickle.WriteInt(kMagic);
357 // this should not overwrite the 'int' payload
358 pickle.headerT<CustomHeader>()->blah = 10;
360 PickleIterator iter(pickle);
361 int result;
362 ASSERT_TRUE(iter.ReadInt(&result));
364 EXPECT_EQ(static_cast<uint32>(result), kMagic);
367 TEST(PickleTest, EqualsOperator) {
368 Pickle source;
369 source.WriteInt(1);
371 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()),
372 source.size());
373 Pickle copy;
374 copy = copy_refs_source_buffer;
375 ASSERT_EQ(source.size(), copy.size());
378 TEST(PickleTest, EvilLengths) {
379 Pickle source;
380 std::string str(100000, 'A');
381 EXPECT_TRUE(source.WriteData(str.c_str(), 100000));
382 // ReadString16 used to have its read buffer length calculation wrong leading
383 // to out-of-bounds reading.
384 PickleIterator iter(source);
385 string16 str16;
386 EXPECT_FALSE(iter.ReadString16(&str16));
388 // And check we didn't break ReadString16.
389 str16 = (wchar_t) 'A';
390 Pickle str16_pickle;
391 EXPECT_TRUE(str16_pickle.WriteString16(str16));
392 iter = PickleIterator(str16_pickle);
393 EXPECT_TRUE(iter.ReadString16(&str16));
394 EXPECT_EQ(1U, str16.length());
396 // Check we don't fail in a length check with invalid String16 size.
397 // (1<<31) * sizeof(char16) == 0, so this is particularly evil.
398 Pickle bad_len;
399 EXPECT_TRUE(bad_len.WriteInt(1 << 31));
400 iter = PickleIterator(bad_len);
401 EXPECT_FALSE(iter.ReadString16(&str16));
404 // Check we can write zero bytes of data and 'data' can be NULL.
405 TEST(PickleTest, ZeroLength) {
406 Pickle pickle;
407 EXPECT_TRUE(pickle.WriteData(NULL, 0));
409 PickleIterator iter(pickle);
410 const char* outdata;
411 int outdatalen;
412 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen));
413 EXPECT_EQ(0, outdatalen);
414 // We can't assert that outdata is NULL.
417 // Check that ReadBytes works properly with an iterator initialized to NULL.
418 TEST(PickleTest, ReadBytes) {
419 Pickle pickle;
420 int data = 0x7abcd;
421 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data)));
423 PickleIterator iter(pickle);
424 const char* outdata_char = NULL;
425 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data)));
427 int outdata;
428 memcpy(&outdata, outdata_char, sizeof(outdata));
429 EXPECT_EQ(data, outdata);