OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <limits.h> |
| 6 #include <stddef.h> |
| 7 #include <stdint.h> |
| 8 |
5 #include <string> | 9 #include <string> |
6 | 10 |
7 #include "base/basictypes.h" | 11 #include "base/macros.h" |
8 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
9 #include "base/pickle.h" | 13 #include "base/pickle.h" |
10 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
11 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 | 17 |
14 namespace base { | 18 namespace base { |
15 | 19 |
16 namespace { | 20 namespace { |
17 | 21 |
18 const bool testbool1 = false; | 22 const bool testbool1 = false; |
19 const bool testbool2 = true; | 23 const bool testbool2 = true; |
20 const int testint = 2093847192; | 24 const int testint = 2093847192; |
21 const long testlong = 1093847192; | 25 const long testlong = 1093847192; |
22 const uint16 testuint16 = 32123; | 26 const uint16_t testuint16 = 32123; |
23 const uint32 testuint32 = 1593847192; | 27 const uint32_t testuint32 = 1593847192; |
24 const int64 testint64 = -0x7E8CA9253104BDFCLL; | 28 const int64_t testint64 = -0x7E8CA9253104BDFCLL; |
25 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; | 29 const uint64_t testuint64 = 0xCE8CA9253104BDF7ULL; |
26 const size_t testsizet = 0xFEDC7654; | 30 const size_t testsizet = 0xFEDC7654; |
27 const float testfloat = 3.1415926935f; | 31 const float testfloat = 3.1415926935f; |
28 const double testdouble = 2.71828182845904523; | 32 const double testdouble = 2.71828182845904523; |
29 const std::string teststring("Hello world"); // note non-aligned string length | 33 const std::string teststring("Hello world"); // note non-aligned string length |
30 const std::wstring testwstring(L"Hello, world"); | 34 const std::wstring testwstring(L"Hello, world"); |
31 const string16 teststring16(ASCIIToUTF16("Hello, world")); | 35 const string16 teststring16(ASCIIToUTF16("Hello, world")); |
32 const char testrawstring[] = "Hello new world"; // Test raw string writing | 36 const char testrawstring[] = "Hello new world"; // Test raw string writing |
33 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. | 37 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. |
34 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; | 38 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; |
35 const char testdata[] = "AAA\0BBB\0"; | 39 const char testdata[] = "AAA\0BBB\0"; |
(...skipping 10 matching lines...) Expand all Loading... |
46 EXPECT_TRUE(outbool); | 50 EXPECT_TRUE(outbool); |
47 | 51 |
48 int outint; | 52 int outint; |
49 EXPECT_TRUE(iter.ReadInt(&outint)); | 53 EXPECT_TRUE(iter.ReadInt(&outint)); |
50 EXPECT_EQ(testint, outint); | 54 EXPECT_EQ(testint, outint); |
51 | 55 |
52 long outlong; | 56 long outlong; |
53 EXPECT_TRUE(iter.ReadLong(&outlong)); | 57 EXPECT_TRUE(iter.ReadLong(&outlong)); |
54 EXPECT_EQ(testlong, outlong); | 58 EXPECT_EQ(testlong, outlong); |
55 | 59 |
56 uint16 outuint16; | 60 uint16_t outuint16; |
57 EXPECT_TRUE(iter.ReadUInt16(&outuint16)); | 61 EXPECT_TRUE(iter.ReadUInt16(&outuint16)); |
58 EXPECT_EQ(testuint16, outuint16); | 62 EXPECT_EQ(testuint16, outuint16); |
59 | 63 |
60 uint32 outuint32; | 64 uint32_t outuint32; |
61 EXPECT_TRUE(iter.ReadUInt32(&outuint32)); | 65 EXPECT_TRUE(iter.ReadUInt32(&outuint32)); |
62 EXPECT_EQ(testuint32, outuint32); | 66 EXPECT_EQ(testuint32, outuint32); |
63 | 67 |
64 int64 outint64; | 68 int64_t outint64; |
65 EXPECT_TRUE(iter.ReadInt64(&outint64)); | 69 EXPECT_TRUE(iter.ReadInt64(&outint64)); |
66 EXPECT_EQ(testint64, outint64); | 70 EXPECT_EQ(testint64, outint64); |
67 | 71 |
68 uint64 outuint64; | 72 uint64_t outuint64; |
69 EXPECT_TRUE(iter.ReadUInt64(&outuint64)); | 73 EXPECT_TRUE(iter.ReadUInt64(&outuint64)); |
70 EXPECT_EQ(testuint64, outuint64); | 74 EXPECT_EQ(testuint64, outuint64); |
71 | 75 |
72 size_t outsizet; | 76 size_t outsizet; |
73 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); | 77 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); |
74 EXPECT_EQ(testsizet, outsizet); | 78 EXPECT_EQ(testsizet, outsizet); |
75 | 79 |
76 float outfloat; | 80 float outfloat; |
77 EXPECT_TRUE(iter.ReadFloat(&outfloat)); | 81 EXPECT_TRUE(iter.ReadFloat(&outfloat)); |
78 EXPECT_EQ(testfloat, outfloat); | 82 EXPECT_EQ(testfloat, outfloat); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 pickle3 = pickle; | 144 pickle3 = pickle; |
141 VerifyResult(pickle3); | 145 VerifyResult(pickle3); |
142 } | 146 } |
143 | 147 |
144 // Tests that reading/writing a size_t works correctly when the source process | 148 // Tests that reading/writing a size_t works correctly when the source process |
145 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both | 149 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both |
146 // arms of the conditional in this test. | 150 // arms of the conditional in this test. |
147 TEST(PickleTest, SizeTFrom64Bit) { | 151 TEST(PickleTest, SizeTFrom64Bit) { |
148 Pickle pickle; | 152 Pickle pickle; |
149 // Under the hood size_t is always written as a 64-bit value, so simulate a | 153 // Under the hood size_t is always written as a 64-bit value, so simulate a |
150 // 64-bit size_t even on 32-bit architectures by explicitly writing a uint64. | 154 // 64-bit size_t even on 32-bit architectures by explicitly writing a |
| 155 // uint64_t. |
151 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); | 156 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
152 | 157 |
153 PickleIterator iter(pickle); | 158 PickleIterator iter(pickle); |
154 size_t outsizet; | 159 size_t outsizet; |
155 if (sizeof(size_t) < sizeof(uint64)) { | 160 if (sizeof(size_t) < sizeof(uint64_t)) { |
156 // ReadSizeT() should return false when the original written value can't be | 161 // ReadSizeT() should return false when the original written value can't be |
157 // represented as a size_t. | 162 // represented as a size_t. |
158 EXPECT_FALSE(iter.ReadSizeT(&outsizet)); | 163 EXPECT_FALSE(iter.ReadSizeT(&outsizet)); |
159 } else { | 164 } else { |
160 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); | 165 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); |
161 EXPECT_EQ(testuint64, outsizet); | 166 EXPECT_EQ(testuint64, outsizet); |
162 } | 167 } |
163 } | 168 } |
164 | 169 |
165 // Tests that we can handle really small buffers. | 170 // Tests that we can handle really small buffers. |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 | 396 |
392 TEST(PickleTest, Resize) { | 397 TEST(PickleTest, Resize) { |
393 size_t unit = Pickle::kPayloadUnit; | 398 size_t unit = Pickle::kPayloadUnit; |
394 scoped_ptr<char[]> data(new char[unit]); | 399 scoped_ptr<char[]> data(new char[unit]); |
395 char* data_ptr = data.get(); | 400 char* data_ptr = data.get(); |
396 for (size_t i = 0; i < unit; i++) | 401 for (size_t i = 0; i < unit; i++) |
397 data_ptr[i] = 'G'; | 402 data_ptr[i] = 'G'; |
398 | 403 |
399 // construct a message that will be exactly the size of one payload unit, | 404 // 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 | 405 // note that any data will have a 4-byte header indicating the size |
401 const size_t payload_size_after_header = unit - sizeof(uint32); | 406 const size_t payload_size_after_header = unit - sizeof(uint32_t); |
402 Pickle pickle; | 407 Pickle pickle; |
403 pickle.WriteData(data_ptr, | 408 pickle.WriteData( |
404 static_cast<int>(payload_size_after_header - sizeof(uint32))); | 409 data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t))); |
405 size_t cur_payload = payload_size_after_header; | 410 size_t cur_payload = payload_size_after_header; |
406 | 411 |
407 // note: we assume 'unit' is a power of 2 | 412 // note: we assume 'unit' is a power of 2 |
408 EXPECT_EQ(unit, pickle.capacity_after_header()); | 413 EXPECT_EQ(unit, pickle.capacity_after_header()); |
409 EXPECT_EQ(pickle.payload_size(), payload_size_after_header); | 414 EXPECT_EQ(pickle.payload_size(), payload_size_after_header); |
410 | 415 |
411 // fill out a full page (noting data header) | 416 // fill out a full page (noting data header) |
412 pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32))); | 417 pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32_t))); |
413 cur_payload += unit; | 418 cur_payload += unit; |
414 EXPECT_EQ(unit * 2, pickle.capacity_after_header()); | 419 EXPECT_EQ(unit * 2, pickle.capacity_after_header()); |
415 EXPECT_EQ(cur_payload, pickle.payload_size()); | 420 EXPECT_EQ(cur_payload, pickle.payload_size()); |
416 | 421 |
417 // one more byte should double the capacity | 422 // one more byte should double the capacity |
418 pickle.WriteData(data_ptr, 1); | 423 pickle.WriteData(data_ptr, 1); |
419 cur_payload += 8; | 424 cur_payload += 8; |
420 EXPECT_EQ(unit * 4, pickle.capacity_after_header()); | 425 EXPECT_EQ(unit * 4, pickle.capacity_after_header()); |
421 EXPECT_EQ(cur_payload, pickle.payload_size()); | 426 EXPECT_EQ(cur_payload, pickle.payload_size()); |
422 } | 427 } |
423 | 428 |
424 namespace { | 429 namespace { |
425 | 430 |
426 struct CustomHeader : Pickle::Header { | 431 struct CustomHeader : Pickle::Header { |
427 int blah; | 432 int blah; |
428 }; | 433 }; |
429 | 434 |
430 } // namespace | 435 } // namespace |
431 | 436 |
432 TEST(PickleTest, HeaderPadding) { | 437 TEST(PickleTest, HeaderPadding) { |
433 const uint32 kMagic = 0x12345678; | 438 const uint32_t kMagic = 0x12345678; |
434 | 439 |
435 Pickle pickle(sizeof(CustomHeader)); | 440 Pickle pickle(sizeof(CustomHeader)); |
436 pickle.WriteInt(kMagic); | 441 pickle.WriteInt(kMagic); |
437 | 442 |
438 // this should not overwrite the 'int' payload | 443 // this should not overwrite the 'int' payload |
439 pickle.headerT<CustomHeader>()->blah = 10; | 444 pickle.headerT<CustomHeader>()->blah = 10; |
440 | 445 |
441 PickleIterator iter(pickle); | 446 PickleIterator iter(pickle); |
442 int result; | 447 int result; |
443 ASSERT_TRUE(iter.ReadInt(&result)); | 448 ASSERT_TRUE(iter.ReadInt(&result)); |
444 | 449 |
445 EXPECT_EQ(static_cast<uint32>(result), kMagic); | 450 EXPECT_EQ(static_cast<uint32_t>(result), kMagic); |
446 } | 451 } |
447 | 452 |
448 TEST(PickleTest, EqualsOperator) { | 453 TEST(PickleTest, EqualsOperator) { |
449 Pickle source; | 454 Pickle source; |
450 source.WriteInt(1); | 455 source.WriteInt(1); |
451 | 456 |
452 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), | 457 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), |
453 source.size()); | 458 source.size()); |
454 Pickle copy; | 459 Pickle copy; |
455 copy = copy_refs_source_buffer; | 460 copy = copy_refs_source_buffer; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 const char* out_data = nullptr; | 569 const char* out_data = nullptr; |
565 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length)); | 570 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length)); |
566 EXPECT_EQ(data, std::string(out_data, out_data_length)); | 571 EXPECT_EQ(data, std::string(out_data, out_data_length)); |
567 | 572 |
568 int out_value; | 573 int out_value; |
569 EXPECT_TRUE(iter.ReadInt(&out_value)); | 574 EXPECT_TRUE(iter.ReadInt(&out_value)); |
570 EXPECT_EQ(42, out_value); | 575 EXPECT_EQ(42, out_value); |
571 } | 576 } |
572 | 577 |
573 } // namespace base | 578 } // namespace base |
OLD | NEW |