| 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 20 matching lines...) Expand all Loading... |
| 31 const std::wstring testwstring(L"Hello, world"); | 31 const std::wstring testwstring(L"Hello, world"); |
| 32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world")); | 32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world")); |
| 33 const char testdata[] = "AAA\0BBB\0"; | 33 const char testdata[] = "AAA\0BBB\0"; |
| 34 const int testdatalen = arraysize(testdata) - 1; | 34 const int testdatalen = arraysize(testdata) - 1; |
| 35 | 35 |
| 36 // checks that the result | 36 // checks that the result |
| 37 void VerifyResult(const Pickle& pickle) { | 37 void VerifyResult(const Pickle& pickle) { |
| 38 PickleIterator iter(pickle); | 38 PickleIterator iter(pickle); |
| 39 | 39 |
| 40 bool outbool; | 40 bool outbool; |
| 41 EXPECT_TRUE(iter.ReadBool(&outbool)); | 41 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 42 EXPECT_FALSE(outbool); | 42 EXPECT_FALSE(outbool); |
| 43 EXPECT_TRUE(iter.ReadBool(&outbool)); | 43 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 44 EXPECT_TRUE(outbool); | 44 EXPECT_TRUE(outbool); |
| 45 | 45 |
| 46 int outint; | 46 int outint; |
| 47 EXPECT_TRUE(iter.ReadInt(&outint)); | 47 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
| 48 EXPECT_EQ(testint, outint); | 48 EXPECT_EQ(testint, outint); |
| 49 | 49 |
| 50 long outlong; | 50 long outlong; |
| 51 EXPECT_TRUE(iter.ReadLong(&outlong)); | 51 EXPECT_TRUE(pickle.ReadLong(&iter, &outlong)); |
| 52 EXPECT_EQ(testlong, outlong); | 52 EXPECT_EQ(testlong, outlong); |
| 53 | 53 |
| 54 uint16 outuint16; | 54 uint16 outuint16; |
| 55 EXPECT_TRUE(iter.ReadUInt16(&outuint16)); | 55 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
| 56 EXPECT_EQ(testuint16, outuint16); | 56 EXPECT_EQ(testuint16, outuint16); |
| 57 | 57 |
| 58 uint32 outuint32; | 58 uint32 outuint32; |
| 59 EXPECT_TRUE(iter.ReadUInt32(&outuint32)); | 59 EXPECT_TRUE(pickle.ReadUInt32(&iter, &outuint32)); |
| 60 EXPECT_EQ(testuint32, outuint32); | 60 EXPECT_EQ(testuint32, outuint32); |
| 61 | 61 |
| 62 int64 outint64; | 62 int64 outint64; |
| 63 EXPECT_TRUE(iter.ReadInt64(&outint64)); | 63 EXPECT_TRUE(pickle.ReadInt64(&iter, &outint64)); |
| 64 EXPECT_EQ(testint64, outint64); | 64 EXPECT_EQ(testint64, outint64); |
| 65 | 65 |
| 66 uint64 outuint64; | 66 uint64 outuint64; |
| 67 EXPECT_TRUE(iter.ReadUInt64(&outuint64)); | 67 EXPECT_TRUE(pickle.ReadUInt64(&iter, &outuint64)); |
| 68 EXPECT_EQ(testuint64, outuint64); | 68 EXPECT_EQ(testuint64, outuint64); |
| 69 | 69 |
| 70 size_t outsizet; | 70 size_t outsizet; |
| 71 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); | 71 EXPECT_TRUE(pickle.ReadSizeT(&iter, &outsizet)); |
| 72 EXPECT_EQ(testsizet, outsizet); | 72 EXPECT_EQ(testsizet, outsizet); |
| 73 | 73 |
| 74 float outfloat; | 74 float outfloat; |
| 75 EXPECT_TRUE(iter.ReadFloat(&outfloat)); | 75 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); |
| 76 EXPECT_EQ(testfloat, outfloat); | 76 EXPECT_EQ(testfloat, outfloat); |
| 77 | 77 |
| 78 double outdouble; | 78 double outdouble; |
| 79 EXPECT_TRUE(iter.ReadDouble(&outdouble)); | 79 EXPECT_TRUE(pickle.ReadDouble(&iter, &outdouble)); |
| 80 EXPECT_EQ(testdouble, outdouble); | 80 EXPECT_EQ(testdouble, outdouble); |
| 81 | 81 |
| 82 std::string outstring; | 82 std::string outstring; |
| 83 EXPECT_TRUE(iter.ReadString(&outstring)); | 83 EXPECT_TRUE(pickle.ReadString(&iter, &outstring)); |
| 84 EXPECT_EQ(teststring, outstring); | 84 EXPECT_EQ(teststring, outstring); |
| 85 | 85 |
| 86 std::wstring outwstring; | 86 std::wstring outwstring; |
| 87 EXPECT_TRUE(iter.ReadWString(&outwstring)); | 87 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstring)); |
| 88 EXPECT_EQ(testwstring, outwstring); | 88 EXPECT_EQ(testwstring, outwstring); |
| 89 | 89 |
| 90 base::string16 outstring16; | 90 base::string16 outstring16; |
| 91 EXPECT_TRUE(iter.ReadString16(&outstring16)); | 91 EXPECT_TRUE(pickle.ReadString16(&iter, &outstring16)); |
| 92 EXPECT_EQ(teststring16, outstring16); | 92 EXPECT_EQ(teststring16, outstring16); |
| 93 | 93 |
| 94 const char* outdata; | 94 const char* outdata; |
| 95 int outdatalen; | 95 int outdatalen; |
| 96 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); | 96 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
| 97 EXPECT_EQ(testdatalen, outdatalen); | 97 EXPECT_EQ(testdatalen, outdatalen); |
| 98 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 98 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 99 | 99 |
| 100 // reads past the end should fail | 100 // reads past the end should fail |
| 101 EXPECT_FALSE(iter.ReadInt(&outint)); | 101 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace | 104 } // namespace |
| 105 | 105 |
| 106 TEST(PickleTest, EncodeDecode) { | 106 TEST(PickleTest, EncodeDecode) { |
| 107 Pickle pickle; | 107 Pickle pickle; |
| 108 | 108 |
| 109 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 109 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
| 110 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 110 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
| 111 EXPECT_TRUE(pickle.WriteInt(testint)); | 111 EXPECT_TRUE(pickle.WriteInt(testint)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 141 Pickle pickle; | 141 Pickle pickle; |
| 142 // Under the hood size_t is always written as a 64-bit value, so simulate a | 142 // Under the hood size_t is always written as a 64-bit value, so simulate a |
| 143 // 64-bit size_t even on 32-bit architectures by explicitly writing a uint64. | 143 // 64-bit size_t even on 32-bit architectures by explicitly writing a uint64. |
| 144 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); | 144 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
| 145 | 145 |
| 146 PickleIterator iter(pickle); | 146 PickleIterator iter(pickle); |
| 147 size_t outsizet; | 147 size_t outsizet; |
| 148 if (sizeof(size_t) < sizeof(uint64)) { | 148 if (sizeof(size_t) < sizeof(uint64)) { |
| 149 // ReadSizeT() should return false when the original written value can't be | 149 // ReadSizeT() should return false when the original written value can't be |
| 150 // represented as a size_t. | 150 // represented as a size_t. |
| 151 EXPECT_FALSE(iter.ReadSizeT(&outsizet)); | 151 EXPECT_FALSE(pickle.ReadSizeT(&iter, &outsizet)); |
| 152 } else { | 152 } else { |
| 153 EXPECT_TRUE(iter.ReadSizeT(&outsizet)); | 153 EXPECT_TRUE(pickle.ReadSizeT(&iter, &outsizet)); |
| 154 EXPECT_EQ(testuint64, outsizet); | 154 EXPECT_EQ(testuint64, outsizet); |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 | 157 |
| 158 // Tests that we can handle really small buffers. | 158 // Tests that we can handle really small buffers. |
| 159 TEST(PickleTest, SmallBuffer) { | 159 TEST(PickleTest, SmallBuffer) { |
| 160 scoped_ptr<char[]> buffer(new char[1]); | 160 scoped_ptr<char[]> buffer(new char[1]); |
| 161 | 161 |
| 162 // We should not touch the buffer. | 162 // We should not touch the buffer. |
| 163 Pickle pickle(buffer.get(), 1); | 163 Pickle pickle(buffer.get(), 1); |
| 164 | 164 |
| 165 PickleIterator iter(pickle); | 165 PickleIterator iter(pickle); |
| 166 int data; | 166 int data; |
| 167 EXPECT_FALSE(iter.ReadInt(&data)); | 167 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Tests that we can handle improper headers. | 170 // Tests that we can handle improper headers. |
| 171 TEST(PickleTest, BigSize) { | 171 TEST(PickleTest, BigSize) { |
| 172 int buffer[] = { 0x56035200, 25, 40, 50 }; | 172 int buffer[] = { 0x56035200, 25, 40, 50 }; |
| 173 | 173 |
| 174 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); | 174 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 175 | 175 |
| 176 PickleIterator iter(pickle); | 176 PickleIterator iter(pickle); |
| 177 int data; | 177 int data; |
| 178 EXPECT_FALSE(iter.ReadInt(&data)); | 178 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
| 179 } | 179 } |
| 180 | 180 |
| 181 TEST(PickleTest, UnalignedSize) { | 181 TEST(PickleTest, UnalignedSize) { |
| 182 int buffer[] = { 10, 25, 40, 50 }; | 182 int buffer[] = { 10, 25, 40, 50 }; |
| 183 | 183 |
| 184 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); | 184 Pickle pickle(reinterpret_cast<char*>(buffer), sizeof(buffer)); |
| 185 | 185 |
| 186 PickleIterator iter(pickle); | 186 PickleIterator iter(pickle); |
| 187 int data; | 187 int data; |
| 188 EXPECT_FALSE(iter.ReadInt(&data)); | 188 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
| 189 } | 189 } |
| 190 | 190 |
| 191 TEST(PickleTest, ZeroLenStr) { | 191 TEST(PickleTest, ZeroLenStr) { |
| 192 Pickle pickle; | 192 Pickle pickle; |
| 193 EXPECT_TRUE(pickle.WriteString(std::string())); | 193 EXPECT_TRUE(pickle.WriteString(std::string())); |
| 194 | 194 |
| 195 PickleIterator iter(pickle); | 195 PickleIterator iter(pickle); |
| 196 std::string outstr; | 196 std::string outstr; |
| 197 EXPECT_TRUE(iter.ReadString(&outstr)); | 197 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
| 198 EXPECT_EQ("", outstr); | 198 EXPECT_EQ("", outstr); |
| 199 } | 199 } |
| 200 | 200 |
| 201 TEST(PickleTest, ZeroLenWStr) { | 201 TEST(PickleTest, ZeroLenWStr) { |
| 202 Pickle pickle; | 202 Pickle pickle; |
| 203 EXPECT_TRUE(pickle.WriteWString(std::wstring())); | 203 EXPECT_TRUE(pickle.WriteWString(std::wstring())); |
| 204 | 204 |
| 205 PickleIterator iter(pickle); | 205 PickleIterator iter(pickle); |
| 206 std::string outstr; | 206 std::string outstr; |
| 207 EXPECT_TRUE(iter.ReadString(&outstr)); | 207 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
| 208 EXPECT_EQ("", outstr); | 208 EXPECT_EQ("", outstr); |
| 209 } | 209 } |
| 210 | 210 |
| 211 TEST(PickleTest, BadLenStr) { | 211 TEST(PickleTest, BadLenStr) { |
| 212 Pickle pickle; | 212 Pickle pickle; |
| 213 EXPECT_TRUE(pickle.WriteInt(-2)); | 213 EXPECT_TRUE(pickle.WriteInt(-2)); |
| 214 | 214 |
| 215 PickleIterator iter(pickle); | 215 PickleIterator iter(pickle); |
| 216 std::string outstr; | 216 std::string outstr; |
| 217 EXPECT_FALSE(iter.ReadString(&outstr)); | 217 EXPECT_FALSE(pickle.ReadString(&iter, &outstr)); |
| 218 } | 218 } |
| 219 | 219 |
| 220 TEST(PickleTest, BadLenWStr) { | 220 TEST(PickleTest, BadLenWStr) { |
| 221 Pickle pickle; | 221 Pickle pickle; |
| 222 EXPECT_TRUE(pickle.WriteInt(-1)); | 222 EXPECT_TRUE(pickle.WriteInt(-1)); |
| 223 | 223 |
| 224 PickleIterator iter(pickle); | 224 PickleIterator iter(pickle); |
| 225 std::wstring woutstr; | 225 std::wstring woutstr; |
| 226 EXPECT_FALSE(iter.ReadWString(&woutstr)); | 226 EXPECT_FALSE(pickle.ReadWString(&iter, &woutstr)); |
| 227 } | 227 } |
| 228 | 228 |
| 229 TEST(PickleTest, FindNext) { | 229 TEST(PickleTest, FindNext) { |
| 230 Pickle pickle; | 230 Pickle pickle; |
| 231 EXPECT_TRUE(pickle.WriteInt(1)); | 231 EXPECT_TRUE(pickle.WriteInt(1)); |
| 232 EXPECT_TRUE(pickle.WriteString("Domo")); | 232 EXPECT_TRUE(pickle.WriteString("Domo")); |
| 233 | 233 |
| 234 const char* start = reinterpret_cast<const char*>(pickle.data()); | 234 const char* start = reinterpret_cast<const char*>(pickle.data()); |
| 235 const char* end = start + pickle.size(); | 235 const char* end = start + pickle.size(); |
| 236 | 236 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 const uint32 kMagic = 0x12345678; | 344 const uint32 kMagic = 0x12345678; |
| 345 | 345 |
| 346 Pickle pickle(sizeof(CustomHeader)); | 346 Pickle pickle(sizeof(CustomHeader)); |
| 347 pickle.WriteInt(kMagic); | 347 pickle.WriteInt(kMagic); |
| 348 | 348 |
| 349 // this should not overwrite the 'int' payload | 349 // this should not overwrite the 'int' payload |
| 350 pickle.headerT<CustomHeader>()->blah = 10; | 350 pickle.headerT<CustomHeader>()->blah = 10; |
| 351 | 351 |
| 352 PickleIterator iter(pickle); | 352 PickleIterator iter(pickle); |
| 353 int result; | 353 int result; |
| 354 ASSERT_TRUE(iter.ReadInt(&result)); | 354 ASSERT_TRUE(pickle.ReadInt(&iter, &result)); |
| 355 | 355 |
| 356 EXPECT_EQ(static_cast<uint32>(result), kMagic); | 356 EXPECT_EQ(static_cast<uint32>(result), kMagic); |
| 357 } | 357 } |
| 358 | 358 |
| 359 TEST(PickleTest, EqualsOperator) { | 359 TEST(PickleTest, EqualsOperator) { |
| 360 Pickle source; | 360 Pickle source; |
| 361 source.WriteInt(1); | 361 source.WriteInt(1); |
| 362 | 362 |
| 363 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), | 363 Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()), |
| 364 source.size()); | 364 source.size()); |
| 365 Pickle copy; | 365 Pickle copy; |
| 366 copy = copy_refs_source_buffer; | 366 copy = copy_refs_source_buffer; |
| 367 ASSERT_EQ(source.size(), copy.size()); | 367 ASSERT_EQ(source.size(), copy.size()); |
| 368 } | 368 } |
| 369 | 369 |
| 370 TEST(PickleTest, EvilLengths) { | 370 TEST(PickleTest, EvilLengths) { |
| 371 Pickle source; | 371 Pickle source; |
| 372 std::string str(100000, 'A'); | 372 std::string str(100000, 'A'); |
| 373 EXPECT_TRUE(source.WriteData(str.c_str(), 100000)); | 373 EXPECT_TRUE(source.WriteData(str.c_str(), 100000)); |
| 374 // ReadString16 used to have its read buffer length calculation wrong leading | 374 // ReadString16 used to have its read buffer length calculation wrong leading |
| 375 // to out-of-bounds reading. | 375 // to out-of-bounds reading. |
| 376 PickleIterator iter(source); | 376 PickleIterator iter(source); |
| 377 string16 str16; | 377 string16 str16; |
| 378 EXPECT_FALSE(iter.ReadString16(&str16)); | 378 EXPECT_FALSE(source.ReadString16(&iter, &str16)); |
| 379 | 379 |
| 380 // And check we didn't break ReadString16. | 380 // And check we didn't break ReadString16. |
| 381 str16 = (wchar_t) 'A'; | 381 str16 = (wchar_t) 'A'; |
| 382 Pickle str16_pickle; | 382 Pickle str16_pickle; |
| 383 EXPECT_TRUE(str16_pickle.WriteString16(str16)); | 383 EXPECT_TRUE(str16_pickle.WriteString16(str16)); |
| 384 iter = PickleIterator(str16_pickle); | 384 iter = PickleIterator(str16_pickle); |
| 385 EXPECT_TRUE(iter.ReadString16(&str16)); | 385 EXPECT_TRUE(str16_pickle.ReadString16(&iter, &str16)); |
| 386 EXPECT_EQ(1U, str16.length()); | 386 EXPECT_EQ(1U, str16.length()); |
| 387 | 387 |
| 388 // Check we don't fail in a length check with invalid String16 size. | 388 // Check we don't fail in a length check with invalid String16 size. |
| 389 // (1<<31) * sizeof(char16) == 0, so this is particularly evil. | 389 // (1<<31) * sizeof(char16) == 0, so this is particularly evil. |
| 390 Pickle bad_len; | 390 Pickle bad_len; |
| 391 EXPECT_TRUE(bad_len.WriteInt(1 << 31)); | 391 EXPECT_TRUE(bad_len.WriteInt(1 << 31)); |
| 392 iter = PickleIterator(bad_len); | 392 iter = PickleIterator(bad_len); |
| 393 EXPECT_FALSE(iter.ReadString16(&str16)); | 393 EXPECT_FALSE(bad_len.ReadString16(&iter, &str16)); |
| 394 | 394 |
| 395 // Check we don't fail in a length check with large WStrings. | 395 // Check we don't fail in a length check with large WStrings. |
| 396 Pickle big_len; | 396 Pickle big_len; |
| 397 EXPECT_TRUE(big_len.WriteInt(1 << 30)); | 397 EXPECT_TRUE(big_len.WriteInt(1 << 30)); |
| 398 iter = PickleIterator(big_len); | 398 iter = PickleIterator(big_len); |
| 399 std::wstring wstr; | 399 std::wstring wstr; |
| 400 EXPECT_FALSE(iter.ReadWString(&wstr)); | 400 EXPECT_FALSE(big_len.ReadWString(&iter, &wstr)); |
| 401 } | 401 } |
| 402 | 402 |
| 403 // Check we can write zero bytes of data and 'data' can be NULL. | 403 // Check we can write zero bytes of data and 'data' can be NULL. |
| 404 TEST(PickleTest, ZeroLength) { | 404 TEST(PickleTest, ZeroLength) { |
| 405 Pickle pickle; | 405 Pickle pickle; |
| 406 EXPECT_TRUE(pickle.WriteData(NULL, 0)); | 406 EXPECT_TRUE(pickle.WriteData(NULL, 0)); |
| 407 | 407 |
| 408 PickleIterator iter(pickle); | 408 PickleIterator iter(pickle); |
| 409 const char* outdata; | 409 const char* outdata; |
| 410 int outdatalen; | 410 int outdatalen; |
| 411 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); | 411 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
| 412 EXPECT_EQ(0, outdatalen); | 412 EXPECT_EQ(0, outdatalen); |
| 413 // We can't assert that outdata is NULL. | 413 // We can't assert that outdata is NULL. |
| 414 } | 414 } |
| 415 | 415 |
| 416 // Check that ReadBytes works properly with an iterator initialized to NULL. | 416 // Check that ReadBytes works properly with an iterator initialized to NULL. |
| 417 TEST(PickleTest, ReadBytes) { | 417 TEST(PickleTest, ReadBytes) { |
| 418 Pickle pickle; | 418 Pickle pickle; |
| 419 int data = 0x7abcd; | 419 int data = 0x7abcd; |
| 420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
| 421 | 421 |
| 422 PickleIterator iter(pickle); | 422 PickleIterator iter(pickle); |
| 423 const char* outdata_char = NULL; | 423 const char* outdata_char = NULL; |
| 424 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); | 424 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
| 425 | 425 |
| 426 int outdata; | 426 int outdata; |
| 427 memcpy(&outdata, outdata_char, sizeof(outdata)); | 427 memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 428 EXPECT_EQ(data, outdata); | 428 EXPECT_EQ(data, outdata); |
| 429 } | 429 } |
| OLD | NEW |