| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.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 | 23 |
| 23 // checks that the result | 24 // checks that the result |
| 24 void VerifyResult(const Pickle& pickle) { | 25 void VerifyResult(const Pickle& pickle) { |
| 25 void* iter = NULL; | 26 void* iter = NULL; |
| 26 | 27 |
| 27 int outint; | 28 int outint; |
| 28 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | 29 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
| 29 EXPECT_EQ(testint, outint); | 30 EXPECT_EQ(testint, outint); |
| 30 | 31 |
| 31 std::string outstr; | 32 std::string outstr; |
| 32 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 33 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
| 33 EXPECT_EQ(teststr, outstr); | 34 EXPECT_EQ(teststr, outstr); |
| 34 | 35 |
| 35 std::wstring outwstr; | 36 std::wstring outwstr; |
| 36 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); | 37 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); |
| 37 EXPECT_EQ(testwstr, outwstr); | 38 EXPECT_EQ(testwstr, outwstr); |
| 38 | 39 |
| 39 bool outbool; | 40 bool outbool; |
| 40 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 41 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 41 EXPECT_EQ(testbool1, outbool); | 42 EXPECT_EQ(testbool1, outbool); |
| 42 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 43 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
| 43 EXPECT_EQ(testbool2, outbool); | 44 EXPECT_EQ(testbool2, outbool); |
| 44 | 45 |
| 46 uint16 outuint16; |
| 47 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
| 48 EXPECT_EQ(testuint16, outuint16); |
| 49 |
| 45 const char* outdata; | 50 const char* outdata; |
| 46 int outdatalen; | 51 int outdatalen; |
| 47 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 52 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
| 48 EXPECT_EQ(testdatalen, outdatalen); | 53 EXPECT_EQ(testdatalen, outdatalen); |
| 49 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 54 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 50 | 55 |
| 51 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 56 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
| 52 EXPECT_EQ(testdatalen, outdatalen); | 57 EXPECT_EQ(testdatalen, outdatalen); |
| 53 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 58 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 54 | 59 |
| 55 // reads past the end should fail | 60 // reads past the end should fail |
| 56 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); | 61 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
| 57 } | 62 } |
| 58 | 63 |
| 59 } // namespace | 64 } // namespace |
| 60 | 65 |
| 61 TEST(PickleTest, EncodeDecode) { | 66 TEST(PickleTest, EncodeDecode) { |
| 62 Pickle pickle; | 67 Pickle pickle; |
| 63 | 68 |
| 64 EXPECT_TRUE(pickle.WriteInt(testint)); | 69 EXPECT_TRUE(pickle.WriteInt(testint)); |
| 65 EXPECT_TRUE(pickle.WriteString(teststr)); | 70 EXPECT_TRUE(pickle.WriteString(teststr)); |
| 66 EXPECT_TRUE(pickle.WriteWString(testwstr)); | 71 EXPECT_TRUE(pickle.WriteWString(testwstr)); |
| 67 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 72 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
| 68 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 73 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
| 74 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
| 69 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 75 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
| 70 | 76 |
| 71 // Over allocate BeginWriteData so we can test TrimWriteData. | 77 // Over allocate BeginWriteData so we can test TrimWriteData. |
| 72 char* dest = pickle.BeginWriteData(testdatalen + 100); | 78 char* dest = pickle.BeginWriteData(testdatalen + 100); |
| 73 EXPECT_TRUE(dest); | 79 EXPECT_TRUE(dest); |
| 74 memcpy(dest, testdata, testdatalen); | 80 memcpy(dest, testdata, testdatalen); |
| 75 | 81 |
| 76 pickle.TrimWriteData(testdatalen); | 82 pickle.TrimWriteData(testdatalen); |
| 77 | 83 |
| 78 VerifyResult(pickle); | 84 VerifyResult(pickle); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 316 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
| 311 | 317 |
| 312 void* iter = NULL; | 318 void* iter = NULL; |
| 313 const char* outdata_char; | 319 const char* outdata_char; |
| 314 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 320 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
| 315 | 321 |
| 316 int outdata; | 322 int outdata; |
| 317 memcpy(&outdata, outdata_char, sizeof(outdata)); | 323 memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 318 EXPECT_EQ(data, outdata); | 324 EXPECT_EQ(data, outdata); |
| 319 } | 325 } |
| OLD | NEW |