jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
deanm@google.com | 75df310 | 2008-08-07 22:40:16 +0900 | [diff] [blame] | 4 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 5 | #include "base/pickle.h" |
| 6 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 11 | #include <memory> |
deanm@google.com | 75df310 | 2008-08-07 22:40:16 +0900 | [diff] [blame] | 12 | #include <string> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 14 | #include "base/macros.h" |
avi@chromium.org | 67d593d | 2013-06-11 04:06:57 +0900 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 19 | namespace base { |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 20 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | namespace { |
| 22 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | const bool testbool1 = false; |
| 24 | const bool testbool2 = true; |
Peter Kasting | 56e720f | 2018-01-17 14:29:09 +0900 | [diff] [blame] | 25 | const int testint = 2'093'847'192; |
| 26 | const long testlong = 1'093'847'192; |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 27 | const uint16_t testuint16 = 32123; |
| 28 | const uint32_t testuint32 = 1593847192; |
Peter Kasting | 56e720f | 2018-01-17 14:29:09 +0900 | [diff] [blame] | 29 | const int64_t testint64 = -0x7E8CA925'3104BDFCLL; |
| 30 | const uint64_t testuint64 = 0xCE8CA925'3104BDF7ULL; |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 31 | const float testfloat = 3.1415926935f; |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 32 | const double testdouble = 2.71828182845904523; |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 33 | const std::string teststring("Hello world"); // note non-aligned string length |
| 34 | const std::wstring testwstring(L"Hello, world"); |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 35 | const string16 teststring16(ASCIIToUTF16("Hello, world")); |
brucedawson | 0fff389 | 2015-03-10 10:46:50 +0900 | [diff] [blame] | 36 | const char testrawstring[] = "Hello new world"; // Test raw string writing |
| 37 | // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 38 | const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 39 | const char testdata[] = "AAA\0BBB\0"; |
| 40 | const int testdatalen = arraysize(testdata) - 1; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | |
brucedawson | 0fff389 | 2015-03-10 10:46:50 +0900 | [diff] [blame] | 42 | // checks that the results can be read correctly from the Pickle |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | void VerifyResult(const Pickle& pickle) { |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 44 | PickleIterator iter(pickle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 45 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | bool outbool; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 47 | EXPECT_TRUE(iter.ReadBool(&outbool)); |
kamil.jiwa@gmail.com | 844e67a | 2013-01-03 21:30:52 +0900 | [diff] [blame] | 48 | EXPECT_FALSE(outbool); |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 49 | EXPECT_TRUE(iter.ReadBool(&outbool)); |
kamil.jiwa@gmail.com | 844e67a | 2013-01-03 21:30:52 +0900 | [diff] [blame] | 50 | EXPECT_TRUE(outbool); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 51 | |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 52 | int outint; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 53 | EXPECT_TRUE(iter.ReadInt(&outint)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 54 | EXPECT_EQ(testint, outint); |
| 55 | |
| 56 | long outlong; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 57 | EXPECT_TRUE(iter.ReadLong(&outlong)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 58 | EXPECT_EQ(testlong, outlong); |
| 59 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 60 | uint16_t outuint16; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 61 | EXPECT_TRUE(iter.ReadUInt16(&outuint16)); |
bryner@chromium.org | c85d0fd | 2011-02-23 04:47:19 +0900 | [diff] [blame] | 62 | EXPECT_EQ(testuint16, outuint16); |
| 63 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 64 | uint32_t outuint32; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 65 | EXPECT_TRUE(iter.ReadUInt32(&outuint32)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 66 | EXPECT_EQ(testuint32, outuint32); |
| 67 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 68 | int64_t outint64; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 69 | EXPECT_TRUE(iter.ReadInt64(&outint64)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 70 | EXPECT_EQ(testint64, outint64); |
| 71 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 72 | uint64_t outuint64; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 73 | EXPECT_TRUE(iter.ReadUInt64(&outuint64)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 74 | EXPECT_EQ(testuint64, outuint64); |
| 75 | |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 76 | float outfloat; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 77 | EXPECT_TRUE(iter.ReadFloat(&outfloat)); |
rbyers@chromium.org | a1f0b98 | 2012-11-29 00:40:58 +0900 | [diff] [blame] | 78 | EXPECT_EQ(testfloat, outfloat); |
| 79 | |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 80 | double outdouble; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 81 | EXPECT_TRUE(iter.ReadDouble(&outdouble)); |
mostynb@opera.com | f0b7853 | 2014-07-15 07:50:32 +0900 | [diff] [blame] | 82 | EXPECT_EQ(testdouble, outdouble); |
| 83 | |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 84 | std::string outstring; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 85 | EXPECT_TRUE(iter.ReadString(&outstring)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 86 | EXPECT_EQ(teststring, outstring); |
| 87 | |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 88 | string16 outstring16; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 89 | EXPECT_TRUE(iter.ReadString16(&outstring16)); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 90 | EXPECT_EQ(teststring16, outstring16); |
| 91 | |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 92 | StringPiece outstringpiece; |
brucedawson | 0fff389 | 2015-03-10 10:46:50 +0900 | [diff] [blame] | 93 | EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece)); |
| 94 | EXPECT_EQ(testrawstring, outstringpiece); |
| 95 | |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 96 | StringPiece16 outstringpiece16; |
brucedawson | 0fff389 | 2015-03-10 10:46:50 +0900 | [diff] [blame] | 97 | EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16)); |
| 98 | EXPECT_EQ(testrawstring16, outstringpiece16); |
| 99 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 100 | const char* outdata; |
| 101 | int outdatalen; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 102 | EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 103 | EXPECT_EQ(testdatalen, outdatalen); |
| 104 | EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 105 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 106 | // reads past the end should fail |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 107 | EXPECT_FALSE(iter.ReadInt(&outint)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace |
| 111 | |
| 112 | TEST(PickleTest, EncodeDecode) { |
| 113 | Pickle pickle; |
| 114 | |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 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); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 | |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 142 | // Tests that reading/writing a long works correctly when the source process |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 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. |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 145 | TEST(PickleTest, LongFrom64Bit) { |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 146 | Pickle pickle; |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 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. |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 149 | pickle.WriteInt64(testint64); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 150 | |
| 151 | PickleIterator iter(pickle); |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 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 |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 159 | } else { |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 160 | EXPECT_TRUE(iter.ReadLong(&outlong)); |
| 161 | EXPECT_EQ(testint64, outlong); |
pkasting | 7ed1f88 | 2014-10-02 12:01:04 +0900 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 165 | // Tests that we can handle really small buffers. |
| 166 | TEST(PickleTest, SmallBuffer) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 167 | std::unique_ptr<char[]> buffer(new char[1]); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 168 | |
| 169 | // We should not touch the buffer. |
| 170 | Pickle pickle(buffer.get(), 1); |
| 171 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 172 | PickleIterator iter(pickle); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 173 | int data; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 174 | EXPECT_FALSE(iter.ReadInt(&data)); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Tests that we can handle improper headers. |
| 178 | TEST(PickleTest, BigSize) { |
| 179 | int buffer[] = { 0x56035200, 25, 40, 50 }; |
| 180 | |
| 181 | Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 182 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 183 | PickleIterator iter(pickle); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 184 | int data; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 185 | EXPECT_FALSE(iter.ReadInt(&data)); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST(PickleTest, UnalignedSize) { |
| 189 | int buffer[] = { 10, 25, 40, 50 }; |
| 190 | |
| 191 | Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 192 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 193 | PickleIterator iter(pickle); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 194 | int data; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 195 | EXPECT_FALSE(iter.ReadInt(&data)); |
rvargas@google.com | 8d9b2b9 | 2010-11-16 04:31:23 +0900 | [diff] [blame] | 196 | } |
| 197 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 198 | TEST(PickleTest, ZeroLenStr) { |
| 199 | Pickle pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 200 | pickle.WriteString(std::string()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 201 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 202 | PickleIterator iter(pickle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 203 | std::string outstr; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 204 | EXPECT_TRUE(iter.ReadString(&outstr)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 205 | EXPECT_EQ("", outstr); |
| 206 | } |
| 207 | |
thestig | 4a30855 | 2015-03-12 05:41:55 +0900 | [diff] [blame] | 208 | TEST(PickleTest, ZeroLenStr16) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 209 | Pickle pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 210 | pickle.WriteString16(string16()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 211 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 212 | PickleIterator iter(pickle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 213 | std::string outstr; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 214 | EXPECT_TRUE(iter.ReadString(&outstr)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 215 | EXPECT_EQ("", outstr); |
| 216 | } |
| 217 | |
| 218 | TEST(PickleTest, BadLenStr) { |
| 219 | Pickle pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 220 | pickle.WriteInt(-2); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 221 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 222 | PickleIterator iter(pickle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 223 | std::string outstr; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 224 | EXPECT_FALSE(iter.ReadString(&outstr)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 225 | } |
| 226 | |
thestig | 4a30855 | 2015-03-12 05:41:55 +0900 | [diff] [blame] | 227 | TEST(PickleTest, BadLenStr16) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 228 | Pickle pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 229 | pickle.WriteInt(-1); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 230 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 231 | PickleIterator iter(pickle); |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 232 | string16 outstr; |
thestig | 4a30855 | 2015-03-12 05:41:55 +0900 | [diff] [blame] | 233 | EXPECT_FALSE(iter.ReadString16(&outstr)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 234 | } |
| 235 | |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 236 | TEST(PickleTest, PeekNext) { |
| 237 | struct CustomHeader : base::Pickle::Header { |
| 238 | int cookies[10]; |
| 239 | }; |
| 240 | |
| 241 | Pickle pickle(sizeof(CustomHeader)); |
| 242 | |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 243 | pickle.WriteString("Goooooooooooogle"); |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 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 | |
| 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 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 318 | TEST(PickleTest, FindNext) { |
| 319 | Pickle pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 320 | pickle.WriteInt(1); |
| 321 | pickle.WriteString("Domo"); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 322 | |
| 323 | const char* start = reinterpret_cast<const char*>(pickle.data()); |
| 324 | const char* end = start + pickle.size(); |
| 325 | |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 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)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 329 | } |
| 330 | |
glider@chromium.org | 8b725fa | 2011-01-26 22:02:27 +0900 | [diff] [blame] | 331 | TEST(PickleTest, FindNextWithIncompleteHeader) { |
| 332 | size_t header_size = sizeof(Pickle::Header); |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 333 | std::unique_ptr<char[]> buffer(new char[header_size - 1]); |
glider@chromium.org | 8b725fa | 2011-01-26 22:02:27 +0900 | [diff] [blame] | 334 | memset(buffer.get(), 0x1, header_size - 1); |
| 335 | |
| 336 | const char* start = buffer.get(); |
| 337 | const char* end = start + header_size - 1; |
| 338 | |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 339 | EXPECT_EQ(nullptr, Pickle::FindNext(header_size, start, end)); |
glider@chromium.org | 8b725fa | 2011-01-26 22:02:27 +0900 | [diff] [blame] | 340 | } |
| 341 | |
halyavin@google.com | 1206480 | 2013-11-01 18:43:45 +0900 | [diff] [blame] | 342 | #if defined(COMPILER_MSVC) |
| 343 | #pragma warning(push) |
| 344 | #pragma warning(disable: 4146) |
| 345 | #endif |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 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; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 350 | std::unique_ptr<char[]> buffer(new char[header_size2 + payload_received]); |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 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); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 360 | EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end)); |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 361 | |
| 362 | header->payload_size = -header_size2; |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 363 | EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end)); |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 364 | |
| 365 | header->payload_size = 0; |
| 366 | end = start + header_size; |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 367 | EXPECT_EQ(nullptr, Pickle::FindNext(header_size2, start, end)); |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 368 | } |
halyavin@google.com | 1206480 | 2013-11-01 18:43:45 +0900 | [diff] [blame] | 369 | #if defined(COMPILER_MSVC) |
| 370 | #pragma warning(pop) |
| 371 | #endif |
halyavin@google.com | 893e5bb | 2013-11-01 18:06:26 +0900 | [diff] [blame] | 372 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 373 | TEST(PickleTest, GetReadPointerAndAdvance) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 374 | Pickle pickle; |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 375 | |
| 376 | PickleIterator iter(pickle); |
| 377 | EXPECT_FALSE(iter.GetReadPointerAndAdvance(1)); |
| 378 | |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 379 | pickle.WriteInt(1); |
| 380 | pickle.WriteInt(2); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 381 | int bytes = sizeof(int) * 2; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 382 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 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)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | TEST(PickleTest, Resize) { |
darin@google.com | cf7eff4 | 2008-08-15 10:05:11 +0900 | [diff] [blame] | 393 | size_t unit = Pickle::kPayloadUnit; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 394 | std::unique_ptr<char[]> data(new char[unit]); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 395 | char* data_ptr = data.get(); |
darin@google.com | cf7eff4 | 2008-08-15 10:05:11 +0900 | [diff] [blame] | 396 | for (size_t i = 0; i < unit; i++) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 401 | const size_t payload_size_after_header = unit - sizeof(uint32_t); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 402 | Pickle pickle; |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 403 | pickle.WriteData( |
| 404 | data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t))); |
darin@google.com | cf7eff4 | 2008-08-15 10:05:11 +0900 | [diff] [blame] | 405 | size_t cur_payload = payload_size_after_header; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 406 | |
mpcomplete@google.com | 54d2f94 | 2009-03-17 05:21:57 +0900 | [diff] [blame] | 407 | // note: we assume 'unit' is a power of 2 |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 408 | EXPECT_EQ(unit, pickle.capacity_after_header()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 409 | EXPECT_EQ(pickle.payload_size(), payload_size_after_header); |
| 410 | |
| 411 | // fill out a full page (noting data header) |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 412 | pickle.WriteData(data_ptr, static_cast<int>(unit - sizeof(uint32_t))); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 413 | cur_payload += unit; |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 414 | EXPECT_EQ(unit * 2, pickle.capacity_after_header()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 415 | EXPECT_EQ(cur_payload, pickle.payload_size()); |
| 416 | |
mpcomplete@google.com | 54d2f94 | 2009-03-17 05:21:57 +0900 | [diff] [blame] | 417 | // one more byte should double the capacity |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 418 | pickle.WriteData(data_ptr, 1); |
halyavin@google.com | 0398c72 | 2014-06-03 08:23:49 +0900 | [diff] [blame] | 419 | cur_payload += 8; |
piman@chromium.org | 5d3eee2 | 2013-10-31 13:03:02 +0900 | [diff] [blame] | 420 | EXPECT_EQ(unit * 4, pickle.capacity_after_header()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 421 | EXPECT_EQ(cur_payload, pickle.payload_size()); |
| 422 | } |
| 423 | |
deanm@google.com | 75df310 | 2008-08-07 22:40:16 +0900 | [diff] [blame] | 424 | namespace { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 425 | |
deanm@google.com | 75df310 | 2008-08-07 22:40:16 +0900 | [diff] [blame] | 426 | struct CustomHeader : Pickle::Header { |
| 427 | int blah; |
| 428 | }; |
| 429 | |
| 430 | } // namespace |
| 431 | |
| 432 | TEST(PickleTest, HeaderPadding) { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 433 | const uint32_t kMagic = 0x12345678; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 441 | PickleIterator iter(pickle); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 442 | int result; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 443 | ASSERT_TRUE(iter.ReadInt(&result)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 444 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 445 | EXPECT_EQ(static_cast<uint32_t>(result), kMagic); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 446 | } |
| 447 | |
| 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()); |
deanm@google.com | 75df310 | 2008-08-07 22:40:16 +0900 | [diff] [blame] | 457 | } |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 458 | |
| 459 | TEST(PickleTest, EvilLengths) { |
| 460 | Pickle source; |
cevans@chromium.org | d8cb112 | 2009-06-26 02:23:49 +0900 | [diff] [blame] | 461 | std::string str(100000, 'A'); |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 462 | source.WriteData(str.c_str(), 100000); |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 463 | // ReadString16 used to have its read buffer length calculation wrong leading |
| 464 | // to out-of-bounds reading. |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 465 | PickleIterator iter(source); |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 466 | string16 str16; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 467 | EXPECT_FALSE(iter.ReadString16(&str16)); |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 468 | |
| 469 | // And check we didn't break ReadString16. |
| 470 | str16 = (wchar_t) 'A'; |
| 471 | Pickle str16_pickle; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 472 | str16_pickle.WriteString16(str16); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 473 | iter = PickleIterator(str16_pickle); |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 474 | EXPECT_TRUE(iter.ReadString16(&str16)); |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 475 | EXPECT_EQ(1U, str16.length()); |
| 476 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 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; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 480 | bad_len.WriteInt(1 << 31); |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 481 | iter = PickleIterator(bad_len); |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 482 | EXPECT_FALSE(iter.ReadString16(&str16)); |
cevans@chromium.org | a834cdd | 2009-06-26 01:54:02 +0900 | [diff] [blame] | 483 | } |
| 484 | |
wtc@chromium.org | c2f15c5 | 2009-07-29 06:00:03 +0900 | [diff] [blame] | 485 | // Check we can write zero bytes of data and 'data' can be NULL. |
| 486 | TEST(PickleTest, ZeroLength) { |
| 487 | Pickle pickle; |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 488 | pickle.WriteData(nullptr, 0); |
wtc@chromium.org | c2f15c5 | 2009-07-29 06:00:03 +0900 | [diff] [blame] | 489 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 490 | PickleIterator iter(pickle); |
wtc@chromium.org | c2f15c5 | 2009-07-29 06:00:03 +0900 | [diff] [blame] | 491 | const char* outdata; |
| 492 | int outdatalen; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 493 | EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
wtc@chromium.org | c2f15c5 | 2009-07-29 06:00:03 +0900 | [diff] [blame] | 494 | EXPECT_EQ(0, outdatalen); |
| 495 | // We can't assert that outdata is NULL. |
| 496 | } |
| 497 | |
mpcomplete@chromium.org | 0409ecb | 2010-03-31 08:52:24 +0900 | [diff] [blame] | 498 | // Check that ReadBytes works properly with an iterator initialized to NULL. |
| 499 | TEST(PickleTest, ReadBytes) { |
| 500 | Pickle pickle; |
| 501 | int data = 0x7abcd; |
Daniel Cheng | 51215ca | 2017-09-22 14:05:07 +0900 | [diff] [blame] | 502 | pickle.WriteBytes(&data, sizeof(data)); |
mpcomplete@chromium.org | 0409ecb | 2010-03-31 08:52:24 +0900 | [diff] [blame] | 503 | |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 504 | PickleIterator iter(pickle); |
Ivan Kotenkov | e88f346 | 2017-11-08 21:37:33 +0900 | [diff] [blame] | 505 | const char* outdata_char = nullptr; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 506 | EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); |
mpcomplete@chromium.org | 0409ecb | 2010-03-31 08:52:24 +0900 | [diff] [blame] | 507 | |
| 508 | int outdata; |
| 509 | memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 510 | EXPECT_EQ(data, outdata); |
| 511 | } |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 512 | |
erikchen | b2ca776 | 2015-09-09 08:36:29 +0900 | [diff] [blame] | 513 | // Checks that when a pickle is deep-copied, the result is not larger than |
| 514 | // needed. |
| 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 | |
rockot | 76180b0 | 2015-12-17 15:19:49 +0900 | [diff] [blame] | 527 | namespace { |
| 528 | |
| 529 | // Publicly exposes the ClaimBytes interface for testing. |
| 530 | class TestingPickle : public Pickle { |
| 531 | public: |
Chris Watkins | d155d9f | 2017-11-29 16:16:38 +0900 | [diff] [blame] | 532 | TestingPickle() = default; |
rockot | 76180b0 | 2015-12-17 15:19:49 +0900 | [diff] [blame] | 533 | |
| 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. |
| 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. |
| 550 | TEST(PickleTest, ClaimBytes) { |
| 551 | std::string data("Hello, world!"); |
| 552 | |
| 553 | TestingPickle pickle; |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 554 | pickle.WriteUInt32(data.size()); |
rockot | 76180b0 | 2015-12-17 15:19:49 +0900 | [diff] [blame] | 555 | void* bytes = pickle.ClaimBytes(data.size()); |
| 556 | pickle.WriteInt(42); |
| 557 | memcpy(bytes, data.data(), data.size()); |
| 558 | |
| 559 | PickleIterator iter(pickle); |
jam | 923e546 | 2016-02-11 05:13:39 +0900 | [diff] [blame] | 560 | uint32_t out_data_length; |
| 561 | EXPECT_TRUE(iter.ReadUInt32(&out_data_length)); |
rockot | 76180b0 | 2015-12-17 15:19:49 +0900 | [diff] [blame] | 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 | |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 573 | } // namespace base |