| 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" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 // Remove when this file is in the base namespace. | 14 // Remove when this file is in the base namespace. |
| 14 using base::string16; | 15 using base::string16; |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 19 const bool testbool1 = false; |
| 20 const bool testbool2 = true; |
| 18 const int testint = 2093847192; | 21 const int testint = 2093847192; |
| 19 const std::string teststr("Hello world"); // note non-aligned string length | 22 const long testlong = 1093847192; |
| 20 const std::wstring testwstr(L"Hello, world"); | 23 const uint16 testuint16 = 32123; |
| 24 const uint32 testuint32 = 1593847192; |
| 25 const int64 testint64 = -0x7E8CA9253104BDFCLL; |
| 26 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; |
| 27 const size_t testsizet = 0xFEDC7654; |
| 28 const float testfloat = 3.1415926935f; |
| 29 const double testdouble = 2.71828182845904523; |
| 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")); |
| 21 const char testdata[] = "AAA\0BBB\0"; | 33 const char testdata[] = "AAA\0BBB\0"; |
| 22 const int testdatalen = arraysize(testdata) - 1; | 34 const int testdatalen = arraysize(testdata) - 1; |
| 23 const bool testbool1 = false; | |
| 24 const bool testbool2 = true; | |
| 25 const uint16 testuint16 = 32123; | |
| 26 const float testfloat = 3.1415926935f; | |
| 27 const double testdouble = 2.71828182845904523; | |
| 28 | 35 |
| 29 // checks that the result | 36 // checks that the result |
| 30 void VerifyResult(const Pickle& pickle) { | 37 void VerifyResult(const Pickle& pickle) { |
| 31 PickleIterator iter(pickle); | 38 PickleIterator iter(pickle); |
| 32 | 39 |
| 33 int outint; | |
| 34 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | |
| 35 EXPECT_EQ(testint, outint); | |
| 36 | |
| 37 std::string outstr; | |
| 38 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | |
| 39 EXPECT_EQ(teststr, outstr); | |
| 40 | |
| 41 std::wstring outwstr; | |
| 42 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); | |
| 43 EXPECT_EQ(testwstr, outwstr); | |
| 44 | |
| 45 bool outbool; | 40 bool outbool; |
| 46 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 41 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 47 EXPECT_FALSE(outbool); | 42 EXPECT_FALSE(outbool); |
| 48 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 43 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 49 EXPECT_TRUE(outbool); | 44 EXPECT_TRUE(outbool); |
| 50 | 45 |
| 46 int outint; |
| 47 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
| 48 EXPECT_EQ(testint, outint); |
| 49 |
| 50 long outlong; |
| 51 EXPECT_TRUE(pickle.ReadLong(&iter, &outlong)); |
| 52 EXPECT_EQ(testlong, outlong); |
| 53 |
| 51 uint16 outuint16; | 54 uint16 outuint16; |
| 52 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); | 55 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
| 53 EXPECT_EQ(testuint16, outuint16); | 56 EXPECT_EQ(testuint16, outuint16); |
| 54 | 57 |
| 58 uint32 outuint32; |
| 59 EXPECT_TRUE(pickle.ReadUInt32(&iter, &outuint32)); |
| 60 EXPECT_EQ(testuint32, outuint32); |
| 61 |
| 62 int64 outint64; |
| 63 EXPECT_TRUE(pickle.ReadInt64(&iter, &outint64)); |
| 64 EXPECT_EQ(testint64, outint64); |
| 65 |
| 66 uint64 outuint64; |
| 67 EXPECT_TRUE(pickle.ReadUInt64(&iter, &outuint64)); |
| 68 EXPECT_EQ(testuint64, outuint64); |
| 69 |
| 70 size_t outsizet; |
| 71 EXPECT_TRUE(pickle.ReadSizeT(&iter, &outsizet)); |
| 72 EXPECT_EQ(testsizet, outsizet); |
| 73 |
| 55 float outfloat; | 74 float outfloat; |
| 56 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); | 75 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); |
| 57 EXPECT_EQ(testfloat, outfloat); | 76 EXPECT_EQ(testfloat, outfloat); |
| 58 | 77 |
| 59 double outdouble; | 78 double outdouble; |
| 60 EXPECT_TRUE(pickle.ReadDouble(&iter, &outdouble)); | 79 EXPECT_TRUE(pickle.ReadDouble(&iter, &outdouble)); |
| 61 EXPECT_EQ(testdouble, outdouble); | 80 EXPECT_EQ(testdouble, outdouble); |
| 62 | 81 |
| 82 std::string outstring; |
| 83 EXPECT_TRUE(pickle.ReadString(&iter, &outstring)); |
| 84 EXPECT_EQ(teststring, outstring); |
| 85 |
| 86 std::wstring outwstring; |
| 87 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstring)); |
| 88 EXPECT_EQ(testwstring, outwstring); |
| 89 |
| 90 base::string16 outstring16; |
| 91 EXPECT_TRUE(pickle.ReadString16(&iter, &outstring16)); |
| 92 EXPECT_EQ(teststring16, outstring16); |
| 93 |
| 63 const char* outdata; | 94 const char* outdata; |
| 64 int outdatalen; | 95 int outdatalen; |
| 65 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 96 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
| 66 EXPECT_EQ(testdatalen, outdatalen); | 97 EXPECT_EQ(testdatalen, outdatalen); |
| 67 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 98 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 68 | 99 |
| 69 // reads past the end should fail | 100 // reads past the end should fail |
| 70 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); | 101 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
| 71 } | 102 } |
| 72 | 103 |
| 73 } // namespace | 104 } // namespace |
| 74 | 105 |
| 75 TEST(PickleTest, EncodeDecode) { | 106 TEST(PickleTest, EncodeDecode) { |
| 76 Pickle pickle; | 107 Pickle pickle; |
| 77 | 108 |
| 78 EXPECT_TRUE(pickle.WriteInt(testint)); | |
| 79 EXPECT_TRUE(pickle.WriteString(teststr)); | |
| 80 EXPECT_TRUE(pickle.WriteWString(testwstr)); | |
| 81 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 109 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
| 82 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 110 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
| 111 EXPECT_TRUE(pickle.WriteInt(testint)); |
| 112 EXPECT_TRUE( |
| 113 pickle.WriteLongUsingDangerousNonPortableLessPersistableForm(testlong)); |
| 83 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 114 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
| 115 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); |
| 116 EXPECT_TRUE(pickle.WriteInt64(testint64)); |
| 117 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
| 118 EXPECT_TRUE(pickle.WriteSizeT(testsizet)); |
| 84 EXPECT_TRUE(pickle.WriteFloat(testfloat)); | 119 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
| 85 EXPECT_TRUE(pickle.WriteDouble(testdouble)); | 120 EXPECT_TRUE(pickle.WriteDouble(testdouble)); |
| 121 EXPECT_TRUE(pickle.WriteString(teststring)); |
| 122 EXPECT_TRUE(pickle.WriteWString(testwstring)); |
| 123 EXPECT_TRUE(pickle.WriteString16(teststring16)); |
| 86 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 124 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
| 87 VerifyResult(pickle); | 125 VerifyResult(pickle); |
| 88 | 126 |
| 89 // test copy constructor | 127 // test copy constructor |
| 90 Pickle pickle2(pickle); | 128 Pickle pickle2(pickle); |
| 91 VerifyResult(pickle2); | 129 VerifyResult(pickle2); |
| 92 | 130 |
| 93 // test operator= | 131 // test operator= |
| 94 Pickle pickle3; | 132 Pickle pickle3; |
| 95 pickle3 = pickle; | 133 pickle3 = pickle; |
| 96 VerifyResult(pickle3); | 134 VerifyResult(pickle3); |
| 97 } | 135 } |
| 98 | 136 |
| 137 // Tests that reading/writing a size_t works correctly when the source process |
| 138 // is 64-bit. We rely on having both 32- and 64-bit trybots to validate both |
| 139 // arms of the conditional in this test. |
| 140 TEST(PickleTest, SizeTFrom64Bit) { |
| 141 Pickle pickle; |
| 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. |
| 144 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
| 145 |
| 146 PickleIterator iter(pickle); |
| 147 size_t outsizet; |
| 148 if (sizeof(size_t) < sizeof(uint64)) { |
| 149 // ReadSizeT() should return false when the original written value can't be |
| 150 // represented as a size_t. |
| 151 EXPECT_FALSE(pickle.ReadSizeT(&iter, &outsizet)); |
| 152 } else { |
| 153 EXPECT_TRUE(pickle.ReadSizeT(&iter, &outsizet)); |
| 154 EXPECT_EQ(testuint64, outsizet); |
| 155 } |
| 156 } |
| 157 |
| 99 // Tests that we can handle really small buffers. | 158 // Tests that we can handle really small buffers. |
| 100 TEST(PickleTest, SmallBuffer) { | 159 TEST(PickleTest, SmallBuffer) { |
| 101 scoped_ptr<char[]> buffer(new char[1]); | 160 scoped_ptr<char[]> buffer(new char[1]); |
| 102 | 161 |
| 103 // We should not touch the buffer. | 162 // We should not touch the buffer. |
| 104 Pickle pickle(buffer.get(), 1); | 163 Pickle pickle(buffer.get(), 1); |
| 105 | 164 |
| 106 PickleIterator iter(pickle); | 165 PickleIterator iter(pickle); |
| 107 int data; | 166 int data; |
| 108 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); | 167 EXPECT_FALSE(pickle.ReadInt(&iter, &data)); |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
| 362 | 421 |
| 363 PickleIterator iter(pickle); | 422 PickleIterator iter(pickle); |
| 364 const char* outdata_char = NULL; | 423 const char* outdata_char = NULL; |
| 365 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 424 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
| 366 | 425 |
| 367 int outdata; | 426 int outdata; |
| 368 memcpy(&outdata, outdata_char, sizeof(outdata)); | 427 memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 369 EXPECT_EQ(data, outdata); | 428 EXPECT_EQ(data, outdata); |
| 370 } | 429 } |
| OLD | NEW |