Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: base/pickle_unittest.cc

Issue 1619363002: Add compile time checks against longs being used in IPC structs on 32 bit Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> 5 #include <limits.h>
6 #include <stddef.h> 6 #include <stddef.h>
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/pickle.h" 13 #include "base/pickle.h"
14 #include "base/strings/string16.h" 14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace base { 18 namespace base {
19 19
20 namespace { 20 namespace {
21 21
22 const bool testbool1 = false; 22 const bool testbool1 = false;
23 const bool testbool2 = true; 23 const bool testbool2 = true;
24 const int testint = 2093847192; 24 const int testint = 2093847192;
25 const long testlong = 1093847192; 25 const long testlong = 1093847192;
26 const uint16_t testuint16 = 32123; 26 const uint16_t testuint16 = 32123;
27 const uint32_t testuint32 = 1593847192; 27 const uint32_t testuint32 = 1593847192;
28 const int64_t testint64 = -0x7E8CA9253104BDFCLL; 28 const int64_t testint64 = -0x7E8CA9253104BDFCLL;
29 const uint64_t testuint64 = 0xCE8CA9253104BDF7ULL; 29 const uint64_t testuint64 = 0xCE8CA9253104BDF7ULL;
30 const size_t testsizet = 0xFEDC7654;
31 const float testfloat = 3.1415926935f; 30 const float testfloat = 3.1415926935f;
32 const double testdouble = 2.71828182845904523; 31 const double testdouble = 2.71828182845904523;
33 const std::string teststring("Hello world"); // note non-aligned string length 32 const std::string teststring("Hello world"); // note non-aligned string length
34 const std::wstring testwstring(L"Hello, world"); 33 const std::wstring testwstring(L"Hello, world");
35 const string16 teststring16(ASCIIToUTF16("Hello, world")); 34 const string16 teststring16(ASCIIToUTF16("Hello, world"));
36 const char testrawstring[] = "Hello new world"; // Test raw string writing 35 const char testrawstring[] = "Hello new world"; // Test raw string writing
37 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. 36 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars.
38 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; 37 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0};
39 const char testdata[] = "AAA\0BBB\0"; 38 const char testdata[] = "AAA\0BBB\0";
40 const int testdatalen = arraysize(testdata) - 1; 39 const int testdatalen = arraysize(testdata) - 1;
(...skipping 25 matching lines...) Expand all
66 EXPECT_EQ(testuint32, outuint32); 65 EXPECT_EQ(testuint32, outuint32);
67 66
68 int64_t outint64; 67 int64_t outint64;
69 EXPECT_TRUE(iter.ReadInt64(&outint64)); 68 EXPECT_TRUE(iter.ReadInt64(&outint64));
70 EXPECT_EQ(testint64, outint64); 69 EXPECT_EQ(testint64, outint64);
71 70
72 uint64_t outuint64; 71 uint64_t outuint64;
73 EXPECT_TRUE(iter.ReadUInt64(&outuint64)); 72 EXPECT_TRUE(iter.ReadUInt64(&outuint64));
74 EXPECT_EQ(testuint64, outuint64); 73 EXPECT_EQ(testuint64, outuint64);
75 74
76 size_t outsizet;
77 EXPECT_TRUE(iter.ReadSizeT(&outsizet));
78 EXPECT_EQ(testsizet, outsizet);
79
80 float outfloat; 75 float outfloat;
81 EXPECT_TRUE(iter.ReadFloat(&outfloat)); 76 EXPECT_TRUE(iter.ReadFloat(&outfloat));
82 EXPECT_EQ(testfloat, outfloat); 77 EXPECT_EQ(testfloat, outfloat);
83 78
84 double outdouble; 79 double outdouble;
85 EXPECT_TRUE(iter.ReadDouble(&outdouble)); 80 EXPECT_TRUE(iter.ReadDouble(&outdouble));
86 EXPECT_EQ(testdouble, outdouble); 81 EXPECT_EQ(testdouble, outdouble);
87 82
88 std::string outstring; 83 std::string outstring;
89 EXPECT_TRUE(iter.ReadString(&outstring)); 84 EXPECT_TRUE(iter.ReadString(&outstring));
(...skipping 22 matching lines...) Expand all
112 } 107 }
113 108
114 } // namespace 109 } // namespace
115 110
116 TEST(PickleTest, EncodeDecode) { 111 TEST(PickleTest, EncodeDecode) {
117 Pickle pickle; 112 Pickle pickle;
118 113
119 EXPECT_TRUE(pickle.WriteBool(testbool1)); 114 EXPECT_TRUE(pickle.WriteBool(testbool1));
120 EXPECT_TRUE(pickle.WriteBool(testbool2)); 115 EXPECT_TRUE(pickle.WriteBool(testbool2));
121 EXPECT_TRUE(pickle.WriteInt(testint)); 116 EXPECT_TRUE(pickle.WriteInt(testint));
122 EXPECT_TRUE( 117 EXPECT_TRUE(pickle.WriteLong(testlong));
123 pickle.WriteLongUsingDangerousNonPortableLessPersistableForm(testlong));
124 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); 118 EXPECT_TRUE(pickle.WriteUInt16(testuint16));
125 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); 119 EXPECT_TRUE(pickle.WriteUInt32(testuint32));
126 EXPECT_TRUE(pickle.WriteInt64(testint64)); 120 EXPECT_TRUE(pickle.WriteInt64(testint64));
127 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); 121 EXPECT_TRUE(pickle.WriteUInt64(testuint64));
128 EXPECT_TRUE(pickle.WriteSizeT(testsizet));
129 EXPECT_TRUE(pickle.WriteFloat(testfloat)); 122 EXPECT_TRUE(pickle.WriteFloat(testfloat));
130 EXPECT_TRUE(pickle.WriteDouble(testdouble)); 123 EXPECT_TRUE(pickle.WriteDouble(testdouble));
131 EXPECT_TRUE(pickle.WriteString(teststring)); 124 EXPECT_TRUE(pickle.WriteString(teststring));
132 EXPECT_TRUE(pickle.WriteString16(teststring16)); 125 EXPECT_TRUE(pickle.WriteString16(teststring16));
133 EXPECT_TRUE(pickle.WriteString(testrawstring)); 126 EXPECT_TRUE(pickle.WriteString(testrawstring));
134 EXPECT_TRUE(pickle.WriteString16(testrawstring16)); 127 EXPECT_TRUE(pickle.WriteString16(testrawstring16));
135 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); 128 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen));
136 VerifyResult(pickle); 129 VerifyResult(pickle);
137 130
138 // test copy constructor 131 // test copy constructor
139 Pickle pickle2(pickle); 132 Pickle pickle2(pickle);
140 VerifyResult(pickle2); 133 VerifyResult(pickle2);
141 134
142 // test operator= 135 // test operator=
143 Pickle pickle3; 136 Pickle pickle3;
144 pickle3 = pickle; 137 pickle3 = pickle;
145 VerifyResult(pickle3); 138 VerifyResult(pickle3);
146 } 139 }
147 140
148 // Tests that reading/writing a size_t works correctly when the source process 141 // Tests that reading/writing a long works correctly when the source process
149 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both 142 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both
150 // arms of the conditional in this test. 143 // arms of the conditional in this test.
151 TEST(PickleTest, SizeTFrom64Bit) { 144 TEST(PickleTest, LongFrom64Bit) {
152 Pickle pickle; 145 Pickle pickle;
153 // Under the hood size_t is always written as a 64-bit value, so simulate a 146 // Under the hood long is always written as a 64-bit value, so simulate a
154 // 64-bit size_t even on 32-bit architectures by explicitly writing a 147 // 64-bit long even on 32-bit architectures by explicitly writing an int64_t.
155 // uint64_t. 148 EXPECT_TRUE(pickle.WriteInt64(testint64));
156 EXPECT_TRUE(pickle.WriteUInt64(testuint64));
157 149
158 PickleIterator iter(pickle); 150 PickleIterator iter(pickle);
159 size_t outsizet; 151 long outlong;
160 if (sizeof(size_t) < sizeof(uint64_t)) { 152 if (sizeof(long) < sizeof(int64_t)) {
161 // ReadSizeT() should return false when the original written value can't be 153 // ReadLong() should return false when the original written value can't be
162 // represented as a size_t. 154 // represented as a long.
163 EXPECT_FALSE(iter.ReadSizeT(&outsizet)); 155 #if GTEST_HAS_DEATH_TEST
156 EXPECT_DEATH(ignore_result(iter.ReadLong(&outlong)), "");
157 #endif
164 } else { 158 } else {
165 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); 159 EXPECT_TRUE(iter.ReadLong(&outlong));
166 EXPECT_EQ(testuint64, outsizet); 160 EXPECT_EQ(testint64, outlong);
167 } 161 }
168 } 162 }
169 163
170 // Tests that we can handle really small buffers. 164 // Tests that we can handle really small buffers.
171 TEST(PickleTest, SmallBuffer) { 165 TEST(PickleTest, SmallBuffer) {
172 scoped_ptr<char[]> buffer(new char[1]); 166 scoped_ptr<char[]> buffer(new char[1]);
173 167
174 // We should not touch the buffer. 168 // We should not touch the buffer.
175 Pickle pickle(buffer.get(), 1); 169 Pickle pickle(buffer.get(), 1);
176 170
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 for (size_t i = 0; i < kChunkSize; ++i) { 543 for (size_t i = 0; i < kChunkSize; ++i) {
550 EXPECT_EQ(0, bytes[i]); 544 EXPECT_EQ(0, bytes[i]);
551 } 545 }
552 } 546 }
553 547
554 // Checks that ClaimBytes properly advances the write offset. 548 // Checks that ClaimBytes properly advances the write offset.
555 TEST(PickleTest, ClaimBytes) { 549 TEST(PickleTest, ClaimBytes) {
556 std::string data("Hello, world!"); 550 std::string data("Hello, world!");
557 551
558 TestingPickle pickle; 552 TestingPickle pickle;
559 pickle.WriteSizeT(data.size()); 553 pickle.WriteUInt32(data.size());
560 void* bytes = pickle.ClaimBytes(data.size()); 554 void* bytes = pickle.ClaimBytes(data.size());
561 pickle.WriteInt(42); 555 pickle.WriteInt(42);
562 memcpy(bytes, data.data(), data.size()); 556 memcpy(bytes, data.data(), data.size());
563 557
564 PickleIterator iter(pickle); 558 PickleIterator iter(pickle);
565 size_t out_data_length; 559 uint32_t out_data_length;
566 EXPECT_TRUE(iter.ReadSizeT(&out_data_length)); 560 EXPECT_TRUE(iter.ReadUInt32(&out_data_length));
567 EXPECT_EQ(data.size(), out_data_length); 561 EXPECT_EQ(data.size(), out_data_length);
568 562
569 const char* out_data = nullptr; 563 const char* out_data = nullptr;
570 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length)); 564 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length));
571 EXPECT_EQ(data, std::string(out_data, out_data_length)); 565 EXPECT_EQ(data, std::string(out_data, out_data_length));
572 566
573 int out_value; 567 int out_value;
574 EXPECT_TRUE(iter.ReadInt(&out_value)); 568 EXPECT_TRUE(iter.ReadInt(&out_value));
575 EXPECT_EQ(42, out_value); 569 EXPECT_EQ(42, out_value);
576 } 570 }
(...skipping 10 matching lines...) Expand all
587 { 581 {
588 TestingPickle pickle; 582 TestingPickle pickle;
589 base::PickleSizer sizer; 583 base::PickleSizer sizer;
590 pickle.WriteInt(42); 584 pickle.WriteInt(42);
591 sizer.AddInt(); 585 sizer.AddInt();
592 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 586 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
593 } 587 }
594 { 588 {
595 TestingPickle pickle; 589 TestingPickle pickle;
596 base::PickleSizer sizer; 590 base::PickleSizer sizer;
597 pickle.WriteLongUsingDangerousNonPortableLessPersistableForm(42); 591 pickle.WriteLong(42);
598 sizer.AddLongUsingDangerousNonPortableLessPersistableForm(); 592 sizer.AddLong();
599 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 593 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
600 } 594 }
601 { 595 {
602 TestingPickle pickle; 596 TestingPickle pickle;
603 base::PickleSizer sizer; 597 base::PickleSizer sizer;
604 pickle.WriteUInt16(42); 598 pickle.WriteUInt16(42);
605 sizer.AddUInt16(); 599 sizer.AddUInt16();
606 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 600 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
607 } 601 }
608 { 602 {
(...skipping 13 matching lines...) Expand all
622 { 616 {
623 TestingPickle pickle; 617 TestingPickle pickle;
624 base::PickleSizer sizer; 618 base::PickleSizer sizer;
625 pickle.WriteUInt64(42); 619 pickle.WriteUInt64(42);
626 sizer.AddUInt64(); 620 sizer.AddUInt64();
627 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 621 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
628 } 622 }
629 { 623 {
630 TestingPickle pickle; 624 TestingPickle pickle;
631 base::PickleSizer sizer; 625 base::PickleSizer sizer;
632 pickle.WriteSizeT(42);
633 sizer.AddSizeT();
634 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
635 }
636 {
637 TestingPickle pickle;
638 base::PickleSizer sizer;
639 pickle.WriteFloat(42.0f); 626 pickle.WriteFloat(42.0f);
640 sizer.AddFloat(); 627 sizer.AddFloat();
641 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 628 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
642 } 629 }
643 { 630 {
644 TestingPickle pickle; 631 TestingPickle pickle;
645 base::PickleSizer sizer; 632 base::PickleSizer sizer;
646 pickle.WriteDouble(42.0); 633 pickle.WriteDouble(42.0);
647 sizer.AddDouble(); 634 sizer.AddDouble();
648 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 635 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
(...skipping 22 matching lines...) Expand all
671 { 658 {
672 TestingPickle pickle; 659 TestingPickle pickle;
673 base::PickleSizer sizer; 660 base::PickleSizer sizer;
674 pickle.WriteBytes(testdata, testdatalen); 661 pickle.WriteBytes(testdata, testdatalen);
675 sizer.AddBytes(testdatalen); 662 sizer.AddBytes(testdatalen);
676 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); 663 EXPECT_EQ(sizer.payload_size(), pickle.payload_size());
677 } 664 }
678 } 665 }
679 666
680 } // namespace base 667 } // namespace base
OLDNEW
« no previous file with comments | « base/pickle.cc ('k') | ipc/ipc_message_utils.h » ('j') | ipc/ipc_message_utils.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698