OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <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/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 226 |
227 TEST(PickleTest, BadLenStr16) { | 227 TEST(PickleTest, BadLenStr16) { |
228 Pickle pickle; | 228 Pickle pickle; |
229 EXPECT_TRUE(pickle.WriteInt(-1)); | 229 EXPECT_TRUE(pickle.WriteInt(-1)); |
230 | 230 |
231 PickleIterator iter(pickle); | 231 PickleIterator iter(pickle); |
232 string16 outstr; | 232 string16 outstr; |
233 EXPECT_FALSE(iter.ReadString16(&outstr)); | 233 EXPECT_FALSE(iter.ReadString16(&outstr)); |
234 } | 234 } |
235 | 235 |
| 236 TEST(PickleTest, PeekNext) { |
| 237 struct CustomHeader : base::Pickle::Header { |
| 238 int cookies[10]; |
| 239 }; |
| 240 |
| 241 Pickle pickle(sizeof(CustomHeader)); |
| 242 |
| 243 EXPECT_TRUE(pickle.WriteString("Goooooooooooogle")); |
| 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 |
236 TEST(PickleTest, FindNext) { | 318 TEST(PickleTest, FindNext) { |
237 Pickle pickle; | 319 Pickle pickle; |
238 EXPECT_TRUE(pickle.WriteInt(1)); | 320 EXPECT_TRUE(pickle.WriteInt(1)); |
239 EXPECT_TRUE(pickle.WriteString("Domo")); | 321 EXPECT_TRUE(pickle.WriteString("Domo")); |
240 | 322 |
241 const char* start = reinterpret_cast<const char*>(pickle.data()); | 323 const char* start = reinterpret_cast<const char*>(pickle.data()); |
242 const char* end = start + pickle.size(); | 324 const char* end = start + pickle.size(); |
243 | 325 |
244 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); | 326 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); |
245 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); | 327 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 pickle.WriteBool(true); | 518 pickle.WriteBool(true); |
437 | 519 |
438 // Make a deep copy. | 520 // Make a deep copy. |
439 Pickle pickle2(pickle); | 521 Pickle pickle2(pickle); |
440 | 522 |
441 // Check that there isn't any extraneous capacity. | 523 // Check that there isn't any extraneous capacity. |
442 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header()); | 524 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header()); |
443 } | 525 } |
444 | 526 |
445 } // namespace base | 527 } // namespace base |
OLD | NEW |