summaryrefslogtreecommitdiffstats
path: root/base/pickle_unittest.cc
diff options
context:
space:
mode:
authorinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 21:49:38 +0000
committerinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 21:49:38 +0000
commitd7cae12696b96500c05dd2d430f6238922c20c96 (patch)
treeecff27b367735535b2a66477f8cd89d3c462a6c0 /base/pickle_unittest.cc
parentee2815e28d408216cf94e874825b6bcf76c69083 (diff)
downloadchromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.zip
chromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.tar.gz
chromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.tar.bz2
Add base to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/pickle_unittest.cc')
-rw-r--r--base/pickle_unittest.cc238
1 files changed, 238 insertions, 0 deletions
diff --git a/base/pickle_unittest.cc b/base/pickle_unittest.cc
new file mode 100644
index 0000000..6780e21
--- /dev/null
+++ b/base/pickle_unittest.cc
@@ -0,0 +1,238 @@
+// Copyright 2008, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#include <windows.h>
+#include <string.h>
+
+#include "base/basictypes.h"
+#include "base/check_handler.h"
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "base/pickle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const int testint = 2093847192;
+const std::string teststr("Hello world"); // note non-aligned string length
+const std::wstring testwstr(L"Hello, world");
+const char testdata[] = "AAA\0BBB\0";
+const int testdatalen = arraysize(testdata) - 1;
+const bool testbool1 = false;
+const bool testbool2 = true;
+
+// checks that the result
+void VerifyResult(const Pickle& pickle) {
+ void* iter = NULL;
+
+ int outint;
+ EXPECT_TRUE(pickle.ReadInt(&iter, &outint));
+ EXPECT_EQ(testint, outint);
+
+ std::string outstr;
+ EXPECT_TRUE(pickle.ReadString(&iter, &outstr));
+ EXPECT_EQ(teststr, outstr);
+
+ std::wstring outwstr;
+ EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr));
+ EXPECT_EQ(testwstr, outwstr);
+
+ bool outbool;
+ EXPECT_TRUE(pickle.ReadBool(&iter, &outbool));
+ EXPECT_EQ(testbool1, outbool);
+ EXPECT_TRUE(pickle.ReadBool(&iter, &outbool));
+ EXPECT_EQ(testbool2, outbool);
+
+ const char* outdata;
+ int outdatalen;
+ EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen));
+ EXPECT_EQ(testdatalen, outdatalen);
+ EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0);
+
+ EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen));
+ EXPECT_EQ(testdatalen, outdatalen);
+ EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0);
+
+ // reads past the end should fail
+ EXPECT_FALSE(pickle.ReadInt(&iter, &outint));
+}
+
+} // namespace
+
+TEST(PickleTest, EncodeDecode) {
+ Pickle pickle;
+
+ EXPECT_TRUE(pickle.WriteInt(testint));
+ EXPECT_TRUE(pickle.WriteString(teststr));
+ EXPECT_TRUE(pickle.WriteWString(testwstr));
+ EXPECT_TRUE(pickle.WriteBool(testbool1));
+ EXPECT_TRUE(pickle.WriteBool(testbool2));
+ EXPECT_TRUE(pickle.WriteData(testdata, testdatalen));
+
+ char* dest = pickle.BeginWriteData(testdatalen);
+ EXPECT_TRUE(dest);
+ memcpy(dest, testdata, testdatalen);
+
+ VerifyResult(pickle);
+
+ // test copy constructor
+ Pickle pickle2(pickle);
+ VerifyResult(pickle2);
+
+ // test operator=
+ Pickle pickle3;
+ pickle3 = pickle;
+ VerifyResult(pickle3);
+}
+
+TEST(PickleTest, ZeroLenStr) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteString(""));
+
+ void* iter = NULL;
+ std::string outstr;
+ EXPECT_TRUE(pickle.ReadString(&iter, &outstr));
+ EXPECT_EQ("", outstr);
+}
+
+TEST(PickleTest, ZeroLenWStr) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteWString(L""));
+
+ void* iter = NULL;
+ std::string outstr;
+ EXPECT_TRUE(pickle.ReadString(&iter, &outstr));
+ EXPECT_EQ("", outstr);
+}
+
+TEST(PickleTest, BadLenStr) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteInt(-2));
+
+ void* iter = NULL;
+ std::string outstr;
+ EXPECT_FALSE(pickle.ReadString(&iter, &outstr));
+}
+
+TEST(PickleTest, BadLenWStr) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteInt(-1));
+
+ void* iter = NULL;
+ std::wstring woutstr;
+ EXPECT_FALSE(pickle.ReadWString(&iter, &woutstr));
+}
+
+TEST(PickleTest, FindNext) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteInt(1));
+ EXPECT_TRUE(pickle.WriteString("Domo"));
+
+ const char* start = reinterpret_cast<const char*>(pickle.data());
+ const char* end = start + pickle.size();
+
+ EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end));
+ EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1));
+ EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1));
+}
+
+TEST(PickleTest, IteratorHasRoom) {
+ Pickle pickle;
+ EXPECT_TRUE(pickle.WriteInt(1));
+ EXPECT_TRUE(pickle.WriteInt(2));
+
+ const void* iter = 0;
+ EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, 1));
+ iter = pickle.payload();
+ EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, 0));
+ EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, 1));
+ EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, -1));
+ EXPECT_TRUE(pickle.IteratorHasRoomFor(iter, sizeof(int) * 2));
+ EXPECT_FALSE(pickle.IteratorHasRoomFor(iter, (sizeof(int) * 2) + 1));
+}
+
+TEST(PickleTest, Resize) {
+ int unit = Pickle::kPayloadUnit;
+ scoped_array<char> data(new char[unit]);
+ char* data_ptr = data.get();
+ for (int i = 0; i < unit; i++)
+ data_ptr[i] = 'G';
+
+ // construct a message that will be exactly the size of one payload unit,
+ // note that any data will have a 4-byte header indicating the size
+ const int payload_size_after_header = unit - sizeof(uint32);
+ Pickle pickle;
+ pickle.WriteData(data_ptr, payload_size_after_header - sizeof(uint32));
+ int cur_payload = payload_size_after_header;
+
+ EXPECT_EQ(pickle.capacity(), unit);
+ EXPECT_EQ(pickle.payload_size(), payload_size_after_header);
+
+ // fill out a full page (noting data header)
+ pickle.WriteData(data_ptr, unit - sizeof(uint32));
+ cur_payload += unit;
+ EXPECT_EQ(unit*2, pickle.capacity());
+ EXPECT_EQ(cur_payload, pickle.payload_size());
+
+ // one more byte should expand the capacity by one unit
+ pickle.WriteData(data_ptr, 1);
+ cur_payload += 5;
+ EXPECT_EQ(unit * 3, pickle.capacity());
+ EXPECT_EQ(cur_payload, pickle.payload_size());
+}
+
+TEST(PickleTest, HeaderPadding) {
+ struct CustomHeader : Pickle::Header {
+ int blah;
+ };
+
+ const uint32 kMagic = 0x12345678;
+
+ Pickle pickle(sizeof(CustomHeader));
+ pickle.WriteInt(kMagic);
+
+ // this should not overwrite the 'int' payload
+ pickle.headerT<CustomHeader>()->blah = 10;
+
+ void* iter = NULL;
+ int result;
+ ASSERT_TRUE(pickle.ReadInt(&iter, &result));
+
+ EXPECT_EQ(result, kMagic);
+}
+
+TEST(PickleTest, EqualsOperator) {
+ Pickle source;
+ source.WriteInt(1);
+
+ Pickle copy_refs_source_buffer(static_cast<const char*>(source.data()),
+ source.size());
+ Pickle copy;
+ copy = copy_refs_source_buffer;
+ ASSERT_EQ(source.size(), copy.size());
+} \ No newline at end of file