[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | d26cd527 | 2008-08-07 13:40:16 | [diff] [blame] | 4 | |
| 5 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "base/pickle.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 10 | #include "base/strings/string16.h" |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 14 | // Remove when this file is in the base namespace. |
| 15 | using base::string16; |
| 16 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | namespace { |
| 18 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | const bool testbool1 = false; |
| 20 | const bool testbool2 = true; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 21 | const int testint = 2093847192; |
| 22 | const long testlong = 1093847192; |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 23 | const uint16 testuint16 = 32123; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 24 | const uint32 testuint32 = 1593847192; |
| 25 | const int64 testint64 = -0x7E8CA9253104BDFCLL; |
| 26 | const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; |
| 27 | const size_t testsizet = 0xFEDC7654; |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 28 | const float testfloat = 3.1415926935f; |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 29 | const double testdouble = 2.71828182845904523; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 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")); |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 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}; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 36 | const char testdata[] = "AAA\0BBB\0"; |
| 37 | const int testdatalen = arraysize(testdata) - 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 39 | // checks that the results can be read correctly from the Pickle |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 40 | void VerifyResult(const Pickle& pickle) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 41 | PickleIterator iter(pickle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | bool outbool; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 44 | EXPECT_TRUE(iter.ReadBool(&outbool)); |
[email protected] | 1885369 | 2013-01-03 12:30:52 | [diff] [blame] | 45 | EXPECT_FALSE(outbool); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 46 | EXPECT_TRUE(iter.ReadBool(&outbool)); |
[email protected] | 1885369 | 2013-01-03 12:30:52 | [diff] [blame] | 47 | EXPECT_TRUE(outbool); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 49 | int outint; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 50 | EXPECT_TRUE(iter.ReadInt(&outint)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 51 | EXPECT_EQ(testint, outint); |
| 52 | |
| 53 | long outlong; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 54 | EXPECT_TRUE(iter.ReadLong(&outlong)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 55 | EXPECT_EQ(testlong, outlong); |
| 56 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 57 | uint16 outuint16; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 58 | EXPECT_TRUE(iter.ReadUInt16(&outuint16)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 59 | EXPECT_EQ(testuint16, outuint16); |
| 60 | |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 61 | uint32 outuint32; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 62 | EXPECT_TRUE(iter.ReadUInt32(&outuint32)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 63 | EXPECT_EQ(testuint32, outuint32); |
| 64 | |
| 65 | int64 outint64; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 66 | EXPECT_TRUE(iter.ReadInt64(&outint64)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 67 | EXPECT_EQ(testint64, outint64); |
| 68 | |
| 69 | uint64 outuint64; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 70 | EXPECT_TRUE(iter.ReadUInt64(&outuint64)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 71 | EXPECT_EQ(testuint64, outuint64); |
| 72 | |
| 73 | size_t outsizet; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 74 | EXPECT_TRUE(iter.ReadSizeT(&outsizet)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 75 | EXPECT_EQ(testsizet, outsizet); |
| 76 | |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 77 | float outfloat; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 78 | EXPECT_TRUE(iter.ReadFloat(&outfloat)); |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 79 | EXPECT_EQ(testfloat, outfloat); |
| 80 | |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 81 | double outdouble; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 82 | EXPECT_TRUE(iter.ReadDouble(&outdouble)); |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 83 | EXPECT_EQ(testdouble, outdouble); |
| 84 | |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 85 | std::string outstring; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 86 | EXPECT_TRUE(iter.ReadString(&outstring)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 87 | EXPECT_EQ(teststring, outstring); |
| 88 | |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 89 | base::string16 outstring16; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 90 | EXPECT_TRUE(iter.ReadString16(&outstring16)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 91 | EXPECT_EQ(teststring16, outstring16); |
| 92 | |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 93 | base::StringPiece outstringpiece; |
| 94 | EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece)); |
| 95 | EXPECT_EQ(testrawstring, outstringpiece); |
| 96 | |
| 97 | base::StringPiece16 outstringpiece16; |
| 98 | EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16)); |
| 99 | EXPECT_EQ(testrawstring16, outstringpiece16); |
| 100 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | const char* outdata; |
| 102 | int outdatalen; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 103 | EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | EXPECT_EQ(testdatalen, outdatalen); |
| 105 | EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 106 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | // reads past the end should fail |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 108 | EXPECT_FALSE(iter.ReadInt(&outint)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } // namespace |
| 112 | |
| 113 | TEST(PickleTest, EncodeDecode) { |
| 114 | Pickle pickle; |
| 115 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | EXPECT_TRUE(pickle.WriteBool(testbool1)); |
| 117 | EXPECT_TRUE(pickle.WriteBool(testbool2)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 118 | EXPECT_TRUE(pickle.WriteInt(testint)); |
| 119 | EXPECT_TRUE( |
| 120 | pickle.WriteLongUsingDangerousNonPortableLessPersistableForm(testlong)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 121 | EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 122 | EXPECT_TRUE(pickle.WriteUInt32(testuint32)); |
| 123 | EXPECT_TRUE(pickle.WriteInt64(testint64)); |
| 124 | EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
| 125 | EXPECT_TRUE(pickle.WriteSizeT(testsizet)); |
[email protected] | b1f61b03 | 2012-11-28 15:40:58 | [diff] [blame] | 126 | EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
[email protected] | 915cc7d | 2014-07-14 22:50:32 | [diff] [blame] | 127 | EXPECT_TRUE(pickle.WriteDouble(testdouble)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 128 | EXPECT_TRUE(pickle.WriteString(teststring)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 129 | EXPECT_TRUE(pickle.WriteString16(teststring16)); |
brucedawson | eaa3896 | 2015-03-10 01:46:50 | [diff] [blame] | 130 | EXPECT_TRUE(pickle.WriteString(testrawstring)); |
| 131 | EXPECT_TRUE(pickle.WriteString16(testrawstring16)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 132 | EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | VerifyResult(pickle); |
| 134 | |
| 135 | // test copy constructor |
| 136 | Pickle pickle2(pickle); |
| 137 | VerifyResult(pickle2); |
| 138 | |
| 139 | // test operator= |
| 140 | Pickle pickle3; |
| 141 | pickle3 = pickle; |
| 142 | VerifyResult(pickle3); |
| 143 | } |
| 144 | |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 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)); |
| 153 | |
| 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. |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 159 | EXPECT_FALSE(iter.ReadSizeT(&outsizet)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 160 | } else { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 161 | EXPECT_TRUE(iter.ReadSizeT(&outsizet)); |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 162 | EXPECT_EQ(testuint64, outsizet); |
| 163 | } |
| 164 | } |
| 165 | |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 166 | // Tests that we can handle really small buffers. |
| 167 | TEST(PickleTest, SmallBuffer) { |
[email protected] | 604eb05 | 2013-01-18 14:21:58 | [diff] [blame] | 168 | scoped_ptr<char[]> buffer(new char[1]); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 169 | |
| 170 | // We should not touch the buffer. |
| 171 | Pickle pickle(buffer.get(), 1); |
| 172 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 173 | PickleIterator iter(pickle); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 174 | int data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 175 | EXPECT_FALSE(iter.ReadInt(&data)); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Tests that we can handle improper headers. |
| 179 | TEST(PickleTest, BigSize) { |
| 180 | int buffer[] = { 0x56035200, 25, 40, 50 }; |
| 181 | |
| 182 | Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 183 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 184 | PickleIterator iter(pickle); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 185 | int data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 186 | EXPECT_FALSE(iter.ReadInt(&data)); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST(PickleTest, UnalignedSize) { |
| 190 | int buffer[] = { 10, 25, 40, 50 }; |
| 191 | |
| 192 | Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 193 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 194 | PickleIterator iter(pickle); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 195 | int data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 196 | EXPECT_FALSE(iter.ReadInt(&data)); |
[email protected] | d87f8e6f | 2010-11-15 19:31:23 | [diff] [blame] | 197 | } |
| 198 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | TEST(PickleTest, ZeroLenStr) { |
| 200 | Pickle pickle; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 201 | EXPECT_TRUE(pickle.WriteString(std::string())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 203 | PickleIterator iter(pickle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | std::string outstr; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 205 | EXPECT_TRUE(iter.ReadString(&outstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 206 | EXPECT_EQ("", outstr); |
| 207 | } |
| 208 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame^] | 209 | TEST(PickleTest, ZeroLenStr16) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | Pickle pickle; |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame^] | 211 | EXPECT_TRUE(pickle.WriteString16(base::string16())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 212 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 213 | PickleIterator iter(pickle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 214 | std::string outstr; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 215 | EXPECT_TRUE(iter.ReadString(&outstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | EXPECT_EQ("", outstr); |
| 217 | } |
| 218 | |
| 219 | TEST(PickleTest, BadLenStr) { |
| 220 | Pickle pickle; |
| 221 | EXPECT_TRUE(pickle.WriteInt(-2)); |
| 222 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 223 | PickleIterator iter(pickle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 224 | std::string outstr; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 225 | EXPECT_FALSE(iter.ReadString(&outstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | } |
| 227 | |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame^] | 228 | TEST(PickleTest, BadLenStr16) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 229 | Pickle pickle; |
| 230 | EXPECT_TRUE(pickle.WriteInt(-1)); |
| 231 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 232 | PickleIterator iter(pickle); |
thestig | f84f17f | 2015-03-11 20:41:55 | [diff] [blame^] | 233 | base::string16 outstr; |
| 234 | EXPECT_FALSE(iter.ReadString16(&outstr)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | TEST(PickleTest, FindNext) { |
| 238 | Pickle pickle; |
| 239 | EXPECT_TRUE(pickle.WriteInt(1)); |
| 240 | EXPECT_TRUE(pickle.WriteString("Domo")); |
| 241 | |
| 242 | const char* start = reinterpret_cast<const char*>(pickle.data()); |
| 243 | const char* end = start + pickle.size(); |
| 244 | |
| 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)); |
| 248 | } |
| 249 | |
[email protected] | 137d237 | 2011-01-26 13:02:27 | [diff] [blame] | 250 | TEST(PickleTest, FindNextWithIncompleteHeader) { |
| 251 | size_t header_size = sizeof(Pickle::Header); |
[email protected] | 604eb05 | 2013-01-18 14:21:58 | [diff] [blame] | 252 | scoped_ptr<char[]> buffer(new char[header_size - 1]); |
[email protected] | 137d237 | 2011-01-26 13:02:27 | [diff] [blame] | 253 | memset(buffer.get(), 0x1, header_size - 1); |
| 254 | |
| 255 | const char* start = buffer.get(); |
| 256 | const char* end = start + header_size - 1; |
| 257 | |
| 258 | EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end)); |
| 259 | } |
| 260 | |
[email protected] | 9dbfa984 | 2013-11-01 09:43:45 | [diff] [blame] | 261 | #if defined(COMPILER_MSVC) |
| 262 | #pragma warning(push) |
| 263 | #pragma warning(disable: 4146) |
| 264 | #endif |
[email protected] | 33a38dd | 2013-11-01 09:06:26 | [diff] [blame] | 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; |
| 277 | |
| 278 | header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2); |
| 279 | EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end)); |
| 280 | |
| 281 | header->payload_size = -header_size2; |
| 282 | EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end)); |
| 283 | |
| 284 | header->payload_size = 0; |
| 285 | end = start + header_size; |
| 286 | EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end)); |
| 287 | } |
[email protected] | 9dbfa984 | 2013-11-01 09:43:45 | [diff] [blame] | 288 | #if defined(COMPILER_MSVC) |
| 289 | #pragma warning(pop) |
| 290 | #endif |
[email protected] | 33a38dd | 2013-11-01 09:06:26 | [diff] [blame] | 291 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 292 | TEST(PickleTest, GetReadPointerAndAdvance) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 293 | Pickle pickle; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 294 | |
| 295 | PickleIterator iter(pickle); |
| 296 | EXPECT_FALSE(iter.GetReadPointerAndAdvance(1)); |
| 297 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 298 | EXPECT_TRUE(pickle.WriteInt(1)); |
| 299 | EXPECT_TRUE(pickle.WriteInt(2)); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 300 | int bytes = sizeof(int) * 2; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 301 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 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)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(PickleTest, Resize) { |
[email protected] | 44a1cbfa | 2008-08-15 01:05:11 | [diff] [blame] | 312 | size_t unit = Pickle::kPayloadUnit; |
[email protected] | 604eb05 | 2013-01-18 14:21:58 | [diff] [blame] | 313 | scoped_ptr<char[]> data(new char[unit]); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 314 | char* data_ptr = data.get(); |
[email protected] | 44a1cbfa | 2008-08-15 01:05:11 | [diff] [blame] | 315 | for (size_t i = 0; i < unit; i++) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 316 | data_ptr[i] = 'G'; |
| 317 | |
| 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 |
[email protected] | 44a1cbfa | 2008-08-15 01:05:11 | [diff] [blame] | 320 | const size_t payload_size_after_header = unit - sizeof(uint32); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 321 | Pickle pickle; |
[email protected] | 1ac7948 | 2008-08-15 01:40:41 | [diff] [blame] | 322 | pickle.WriteData(data_ptr, |
| 323 | static_cast<int>(payload_size_after_header - sizeof(uint32))); |
[email protected] | 44a1cbfa | 2008-08-15 01:05:11 | [diff] [blame] | 324 | size_t cur_payload = payload_size_after_header; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | |
[email protected] | 2de4626 | 2009-03-16 20:21:57 | [diff] [blame] | 326 | // note: we assume 'unit' is a power of 2 |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 327 | EXPECT_EQ(unit, pickle.capacity_after_header()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | EXPECT_EQ(pickle.payload_size(), payload_size_after_header); |
| 329 | |
| 330 | // fill out a full page (noting data header) |
[email protected] | 1ac7948 | 2008-08-15 01:40:41 | [diff] [blame] | 331 | pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 332 | cur_payload += unit; |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 333 | EXPECT_EQ(unit * 2, pickle.capacity_after_header()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 334 | EXPECT_EQ(cur_payload, pickle.payload_size()); |
| 335 | |
[email protected] | 2de4626 | 2009-03-16 20:21:57 | [diff] [blame] | 336 | // one more byte should double the capacity |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 337 | pickle.WriteData(data_ptr, 1); |
[email protected] | a15016f | 2014-06-02 23:23:49 | [diff] [blame] | 338 | cur_payload += 8; |
[email protected] | d1b319fc | 2013-10-31 04:03:02 | [diff] [blame] | 339 | EXPECT_EQ(unit * 4, pickle.capacity_after_header()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 340 | EXPECT_EQ(cur_payload, pickle.payload_size()); |
| 341 | } |
| 342 | |
[email protected] | d26cd527 | 2008-08-07 13:40:16 | [diff] [blame] | 343 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | |
[email protected] | d26cd527 | 2008-08-07 13:40:16 | [diff] [blame] | 345 | struct CustomHeader : Pickle::Header { |
| 346 | int blah; |
| 347 | }; |
| 348 | |
| 349 | } // namespace |
| 350 | |
| 351 | TEST(PickleTest, HeaderPadding) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 352 | const uint32 kMagic = 0x12345678; |
| 353 | |
| 354 | Pickle pickle(sizeof(CustomHeader)); |
| 355 | pickle.WriteInt(kMagic); |
| 356 | |
| 357 | // this should not overwrite the 'int' payload |
| 358 | pickle.headerT<CustomHeader>()->blah = 10; |
| 359 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 360 | PickleIterator iter(pickle); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 361 | int result; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 362 | ASSERT_TRUE(iter.ReadInt(&result)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 363 | |
[email protected] | 44a1cbfa | 2008-08-15 01:05:11 | [diff] [blame] | 364 | EXPECT_EQ(static_cast<uint32>(result), kMagic); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | TEST(PickleTest, EqualsOperator) { |
| 368 | Pickle source; |
| 369 | source.WriteInt(1); |
| 370 | |
| 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()); |
[email protected] | d26cd527 | 2008-08-07 13:40:16 | [diff] [blame] | 376 | } |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 377 | |
| 378 | TEST(PickleTest, EvilLengths) { |
| 379 | Pickle source; |
[email protected] | b235357d | 2009-06-25 17:23:49 | [diff] [blame] | 380 | std::string str(100000, 'A'); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 381 | EXPECT_TRUE(source.WriteData(str.c_str(), 100000)); |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 382 | // ReadString16 used to have its read buffer length calculation wrong leading |
| 383 | // to out-of-bounds reading. |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 384 | PickleIterator iter(source); |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 385 | string16 str16; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 386 | EXPECT_FALSE(iter.ReadString16(&str16)); |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 387 | |
| 388 | // And check we didn't break ReadString16. |
| 389 | str16 = (wchar_t) 'A'; |
| 390 | Pickle str16_pickle; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 391 | EXPECT_TRUE(str16_pickle.WriteString16(str16)); |
| 392 | iter = PickleIterator(str16_pickle); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 393 | EXPECT_TRUE(iter.ReadString16(&str16)); |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 394 | EXPECT_EQ(1U, str16.length()); |
| 395 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 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); |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 401 | EXPECT_FALSE(iter.ReadString16(&str16)); |
[email protected] | 8766556 | 2009-06-25 16:54:02 | [diff] [blame] | 402 | } |
| 403 | |
[email protected] | e64ff5e | 2009-07-28 21:00:03 | [diff] [blame] | 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)); |
| 408 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 409 | PickleIterator iter(pickle); |
[email protected] | e64ff5e | 2009-07-28 21:00:03 | [diff] [blame] | 410 | const char* outdata; |
| 411 | int outdatalen; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 412 | EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
[email protected] | e64ff5e | 2009-07-28 21:00:03 | [diff] [blame] | 413 | EXPECT_EQ(0, outdatalen); |
| 414 | // We can't assert that outdata is NULL. |
| 415 | } |
| 416 | |
[email protected] | 26d2f47 | 2010-03-30 23:52:24 | [diff] [blame] | 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))); |
| 422 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 423 | PickleIterator iter(pickle); |
| 424 | const char* outdata_char = NULL; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 425 | EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); |
[email protected] | 26d2f47 | 2010-03-30 23:52:24 | [diff] [blame] | 426 | |
| 427 | int outdata; |
| 428 | memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 429 | EXPECT_EQ(data, outdata); |
| 430 | } |