OLD | NEW |
1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 const int testint = 2093847192; | 15 const int testint = 2093847192; |
16 const std::string teststr("Hello world"); // note non-aligned string length | 16 const std::string teststr("Hello world"); // note non-aligned string length |
17 const std::wstring testwstr(L"Hello, world"); | 17 const std::wstring testwstr(L"Hello, world"); |
18 const char testdata[] = "AAA\0BBB\0"; | 18 const char testdata[] = "AAA\0BBB\0"; |
19 const int testdatalen = arraysize(testdata) - 1; | 19 const int testdatalen = arraysize(testdata) - 1; |
20 const bool testbool1 = false; | 20 const bool testbool1 = false; |
21 const bool testbool2 = true; | 21 const bool testbool2 = true; |
22 const uint16 testuint16 = 32123; | 22 const uint16 testuint16 = 32123; |
23 | 23 |
24 // checks that the result | 24 // checks that the result |
25 void VerifyResult(const Pickle& pickle) { | 25 void VerifyResult(const Pickle& pickle) { |
26 void* iter = NULL; | 26 PickleIterator iter(pickle); |
27 | 27 |
28 int outint; | 28 int outint; |
29 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | 29 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
30 EXPECT_EQ(testint, outint); | 30 EXPECT_EQ(testint, outint); |
31 | 31 |
32 std::string outstr; | 32 std::string outstr; |
33 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 33 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
34 EXPECT_EQ(teststr, outstr); | 34 EXPECT_EQ(teststr, outstr); |
35 | 35 |
36 std::wstring outwstr; | 36 std::wstring outwstr; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 VerifyResult(pickle3); | 93 VerifyResult(pickle3); |
94 } | 94 } |
95 | 95 |
96 // Tests that we can handle really small buffers. | 96 // Tests that we can handle really small buffers. |
97 TEST(PickleTest, SmallBuffer) { | 97 TEST(PickleTest, SmallBuffer) { |
98 scoped_array<char> buffer(new char[1]); | 98 scoped_array<char> buffer(new char[1]); |
99 | 99 |
100 // We should not touch the buffer. | 100 // We should not touch the buffer. |
101 Pickle pickle(buffer.get(), 1); | 101 Pickle pickle(buffer.get(), 1); |
102 | 102 |
103 void* iter = NULL; | 103 PickleIterator iter(pickle); |
104 int data; | 104 int data; |
105 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); | 105 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
106 } | 106 } |
107 | 107 |
108 // Tests that we can handle improper headers. | 108 // Tests that we can handle improper headers. |
109 TEST(PickleTest, BigSize) { | 109 TEST(PickleTest, BigSize) { |
110 int buffer[] = { 0x56035200, 25, 40, 50 }; | 110 int buffer[] = { 0x56035200, 25, 40, 50 }; |
111 | 111 |
112 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); | 112 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
113 | 113 |
114 void* iter = NULL; | 114 PickleIterator iter(pickle); |
115 int data; | 115 int data; |
116 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); | 116 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
117 } | 117 } |
118 | 118 |
119 TEST(PickleTest, UnalignedSize) { | 119 TEST(PickleTest, UnalignedSize) { |
120 int buffer[] = { 10, 25, 40, 50 }; | 120 int buffer[] = { 10, 25, 40, 50 }; |
121 | 121 |
122 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); | 122 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
123 | 123 |
124 void* iter = NULL; | 124 PickleIterator iter(pickle); |
125 int data; | 125 int data; |
126 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); | 126 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
127 } | 127 } |
128 | 128 |
129 TEST(PickleTest, ZeroLenStr) { | 129 TEST(PickleTest, ZeroLenStr) { |
130 Pickle pickle; | 130 Pickle pickle; |
131 EXPECT_TRUE(pickle.WriteString("")); | 131 EXPECT_TRUE(pickle.WriteString("")); |
132 | 132 |
133 void* iter = NULL; | 133 PickleIterator iter(pickle); |
134 std::string outstr; | 134 std::string outstr; |
135 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 135 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
136 EXPECT_EQ("", outstr); | 136 EXPECT_EQ("", outstr); |
137 } | 137 } |
138 | 138 |
139 TEST(PickleTest, ZeroLenWStr) { | 139 TEST(PickleTest, ZeroLenWStr) { |
140 Pickle pickle; | 140 Pickle pickle; |
141 EXPECT_TRUE(pickle.WriteWString(L"")); | 141 EXPECT_TRUE(pickle.WriteWString(L"")); |
142 | 142 |
143 void* iter = NULL; | 143 PickleIterator iter(pickle); |
144 std::string outstr; | 144 std::string outstr; |
145 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 145 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
146 EXPECT_EQ("", outstr); | 146 EXPECT_EQ("", outstr); |
147 } | 147 } |
148 | 148 |
149 TEST(PickleTest, BadLenStr) { | 149 TEST(PickleTest, BadLenStr) { |
150 Pickle pickle; | 150 Pickle pickle; |
151 EXPECT_TRUE(pickle.WriteInt(-2)); | 151 EXPECT_TRUE(pickle.WriteInt(-2)); |
152 | 152 |
153 void* iter = NULL; | 153 PickleIterator iter(pickle); |
154 std::string outstr; | 154 std::string outstr; |
155 EXPECT_FALSE(pickle.ReadString(&iter, &outstr)); | 155 EXPECT_FALSE(pickle.ReadString(&iter, &outstr)); |
156 } | 156 } |
157 | 157 |
158 TEST(PickleTest, BadLenWStr) { | 158 TEST(PickleTest, BadLenWStr) { |
159 Pickle pickle; | 159 Pickle pickle; |
160 EXPECT_TRUE(pickle.WriteInt(-1)); | 160 EXPECT_TRUE(pickle.WriteInt(-1)); |
161 | 161 |
162 void* iter = NULL; | 162 PickleIterator iter(pickle); |
163 std::wstring woutstr; | 163 std::wstring woutstr; |
164 EXPECT_FALSE(pickle.ReadWString(&iter, &woutstr)); | 164 EXPECT_FALSE(pickle.ReadWString(&iter, &woutstr)); |
165 } | 165 } |
166 | 166 |
167 TEST(PickleTest, FindNext) { | 167 TEST(PickleTest, FindNext) { |
168 Pickle pickle; | 168 Pickle pickle; |
169 EXPECT_TRUE(pickle.WriteInt(1)); | 169 EXPECT_TRUE(pickle.WriteInt(1)); |
170 EXPECT_TRUE(pickle.WriteString("Domo")); | 170 EXPECT_TRUE(pickle.WriteString("Domo")); |
171 | 171 |
172 const char* start = reinterpret_cast<const char*>(pickle.data()); | 172 const char* start = reinterpret_cast<const char*>(pickle.data()); |
173 const char* end = start + pickle.size(); | 173 const char* end = start + pickle.size(); |
174 | 174 |
175 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); | 175 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); |
176 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); | 176 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); |
177 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1)); | 177 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1)); |
178 } | 178 } |
179 | 179 |
180 TEST(PickleTest, FindNextWithIncompleteHeader) { | 180 TEST(PickleTest, FindNextWithIncompleteHeader) { |
181 size_t header_size = sizeof(Pickle::Header); | 181 size_t header_size = sizeof(Pickle::Header); |
182 scoped_array<char> buffer(new char[header_size - 1]); | 182 scoped_array<char> buffer(new char[header_size - 1]); |
183 memset(buffer.get(), 0x1, header_size - 1); | 183 memset(buffer.get(), 0x1, header_size - 1); |
184 | 184 |
185 const char* start = buffer.get(); | 185 const char* start = buffer.get(); |
186 const char* end = start + header_size - 1; | 186 const char* end = start + header_size - 1; |
187 | 187 |
188 EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end)); | 188 EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end)); |
189 } | 189 } |
190 | 190 |
191 TEST(PickleTest, IteratorHasRoom) { | 191 TEST(PickleTest, GetReadPointerAndAdvance) { |
192 Pickle pickle; | 192 Pickle pickle; |
| 193 |
| 194 PickleIterator iter(pickle); |
| 195 EXPECT_FALSE(iter.GetReadPointerAndAdvance(1)); |
| 196 |
193 EXPECT_TRUE(pickle.WriteInt(1)); | 197 EXPECT_TRUE(pickle.WriteInt(1)); |
194 EXPECT_TRUE(pickle.WriteInt(2)); | 198 EXPECT_TRUE(pickle.WriteInt(2)); |
| 199 int bytes = sizeof(int) * 2; |
195 | 200 |
196 const void* iter = 0; | 201 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(0)); |
197 EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, 1)); | 202 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(1)); |
198 iter = pickle.payload(); | 203 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(-1)); |
199 EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, 0)); | 204 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes)); |
200 EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, 1)); | 205 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes + 1)); |
201 EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, -1)); | 206 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MAX)); |
202 EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, sizeof(int) * 2)); | 207 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MIN)); |
203 EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, (sizeof(int) * 2) + 1)); | |
204 } | 208 } |
205 | 209 |
206 TEST(PickleTest, Resize) { | 210 TEST(PickleTest, Resize) { |
207 size_t unit = Pickle::kPayloadUnit; | 211 size_t unit = Pickle::kPayloadUnit; |
208 scoped_array<char> data(new char[unit]); | 212 scoped_array<char> data(new char[unit]); |
209 char* data_ptr = data.get(); | 213 char* data_ptr = data.get(); |
210 for (size_t i = 0; i < unit; i++) | 214 for (size_t i = 0; i < unit; i++) |
211 data_ptr[i] = 'G'; | 215 data_ptr[i] = 'G'; |
212 | 216 |
213 // construct a message that will be exactly the size of one payload unit, | 217 // construct a message that will be exactly the size of one payload unit, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 249 |
246 TEST(PickleTest, HeaderPadding) { | 250 TEST(PickleTest, HeaderPadding) { |
247 const uint32 kMagic = 0x12345678; | 251 const uint32 kMagic = 0x12345678; |
248 | 252 |
249 Pickle pickle(sizeof(CustomHeader)); | 253 Pickle pickle(sizeof(CustomHeader)); |
250 pickle.WriteInt(kMagic); | 254 pickle.WriteInt(kMagic); |
251 | 255 |
252 // this should not overwrite the 'int' payload | 256 // this should not overwrite the 'int' payload |
253 pickle.headerT<CustomHeader>()->blah = 10; | 257 pickle.headerT<CustomHeader>()->blah = 10; |
254 | 258 |
255 void* iter = NULL; | 259 PickleIterator iter(pickle); |
256 int result; | 260 int result; |
257 ASSERT_TRUE(pickle.ReadInt(&iter, &result)); | 261 ASSERT_TRUE(pickle.ReadInt(&iter, &result)); |
258 | 262 |
259 EXPECT_EQ(static_cast<uint32>(result), kMagic); | 263 EXPECT_EQ(static_cast<uint32>(result), kMagic); |
260 } | 264 } |
261 | 265 |
262 TEST(PickleTest, EqualsOperator) { | 266 TEST(PickleTest, EqualsOperator) { |
263 Pickle source; | 267 Pickle source; |
264 source.WriteInt(1); | 268 source.WriteInt(1); |
265 | 269 |
266 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), | 270 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), |
267 source.size()); | 271 source.size()); |
268 Pickle copy; | 272 Pickle copy; |
269 copy = copy_refs_source_buffer; | 273 copy = copy_refs_source_buffer; |
270 ASSERT_EQ(source.size(), copy.size()); | 274 ASSERT_EQ(source.size(), copy.size()); |
271 } | 275 } |
272 | 276 |
273 TEST(PickleTest, EvilLengths) { | 277 TEST(PickleTest, EvilLengths) { |
274 Pickle source; | 278 Pickle source; |
275 std::string str(100000, 'A'); | 279 std::string str(100000, 'A'); |
276 source.WriteData(str.c_str(), 100000); | 280 EXPECT_TRUE(source.WriteData(str.c_str(), 100000)); |
277 // ReadString16 used to have its read buffer length calculation wrong leading | 281 // ReadString16 used to have its read buffer length calculation wrong leading |
278 // to out-of-bounds reading. | 282 // to out-of-bounds reading. |
279 void* iter = NULL; | 283 PickleIterator iter(source); |
280 string16 str16; | 284 string16 str16; |
281 EXPECT_FALSE(source.ReadString16(&iter, &str16)); | 285 EXPECT_FALSE(source.ReadString16(&iter, &str16)); |
282 | 286 |
283 // And check we didn't break ReadString16. | 287 // And check we didn't break ReadString16. |
284 str16 = (wchar_t) 'A'; | 288 str16 = (wchar_t) 'A'; |
285 Pickle str16_pickle; | 289 Pickle str16_pickle; |
286 str16_pickle.WriteString16(str16); | 290 EXPECT_TRUE(str16_pickle.WriteString16(str16)); |
287 iter = NULL; | 291 iter = PickleIterator(str16_pickle); |
288 EXPECT_TRUE(str16_pickle.ReadString16(&iter, &str16)); | 292 EXPECT_TRUE(str16_pickle.ReadString16(&iter, &str16)); |
289 EXPECT_EQ(1U, str16.length()); | 293 EXPECT_EQ(1U, str16.length()); |
290 | 294 |
| 295 // Check we don't fail in a length check with invalid String16 size. |
| 296 // (1<<31) * sizeof(char16) == 0, so this is particularly evil. |
| 297 Pickle bad_len; |
| 298 EXPECT_TRUE(bad_len.WriteInt(1 << 31)); |
| 299 iter = PickleIterator(bad_len); |
| 300 EXPECT_FALSE(bad_len.ReadString16(&iter, &str16)); |
| 301 |
291 // Check we don't fail in a length check with large WStrings. | 302 // Check we don't fail in a length check with large WStrings. |
292 Pickle big_len; | 303 Pickle big_len; |
293 big_len.WriteInt(1 << 30); | 304 EXPECT_TRUE(big_len.WriteInt(1 << 30)); |
294 iter = NULL; | 305 iter = PickleIterator(big_len); |
295 std::wstring wstr; | 306 std::wstring wstr; |
296 EXPECT_FALSE(big_len.ReadWString(&iter, &wstr)); | 307 EXPECT_FALSE(big_len.ReadWString(&iter, &wstr)); |
297 } | 308 } |
298 | 309 |
299 // Check we can write zero bytes of data and 'data' can be NULL. | 310 // Check we can write zero bytes of data and 'data' can be NULL. |
300 TEST(PickleTest, ZeroLength) { | 311 TEST(PickleTest, ZeroLength) { |
301 Pickle pickle; | 312 Pickle pickle; |
302 EXPECT_TRUE(pickle.WriteData(NULL, 0)); | 313 EXPECT_TRUE(pickle.WriteData(NULL, 0)); |
303 | 314 |
304 void* iter = NULL; | 315 PickleIterator iter(pickle); |
305 const char* outdata; | 316 const char* outdata; |
306 int outdatalen; | 317 int outdatalen; |
307 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 318 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
308 EXPECT_EQ(0, outdatalen); | 319 EXPECT_EQ(0, outdatalen); |
309 // We can't assert that outdata is NULL. | 320 // We can't assert that outdata is NULL. |
310 } | 321 } |
311 | 322 |
312 // Check that ReadBytes works properly with an iterator initialized to NULL. | 323 // Check that ReadBytes works properly with an iterator initialized to NULL. |
313 TEST(PickleTest, ReadBytes) { | 324 TEST(PickleTest, ReadBytes) { |
314 Pickle pickle; | 325 Pickle pickle; |
315 int data = 0x7abcd; | 326 int data = 0x7abcd; |
316 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 327 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
317 | 328 |
318 void* iter = NULL; | 329 PickleIterator iter(pickle); |
319 const char* outdata_char; | 330 const char* outdata_char = NULL; |
320 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 331 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
321 | 332 |
322 int outdata; | 333 int outdata; |
323 memcpy(&outdata, outdata_char, sizeof(outdata)); | 334 memcpy(&outdata, outdata_char, sizeof(outdata)); |
324 EXPECT_EQ(data, outdata); | 335 EXPECT_EQ(data, outdata); |
325 } | 336 } |
OLD | NEW |