michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "breakpad_googletest_includes.h" michael@0: #include "common/using_std_string.h" michael@0: #include "processor/binarystream.h" michael@0: michael@0: namespace { michael@0: using std::ios_base; michael@0: using std::vector; michael@0: using google_breakpad::binarystream; michael@0: michael@0: michael@0: class BinaryStreamBasicTest : public ::testing::Test { michael@0: protected: michael@0: binarystream stream; michael@0: }; michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadU8) { michael@0: uint8_t u8 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u8; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u8); michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << (uint8_t)1; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u8; michael@0: EXPECT_EQ(1, u8); michael@0: EXPECT_FALSE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadU16) { michael@0: uint16_t u16 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u16; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u16); michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << (uint16_t)1; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u16; michael@0: EXPECT_EQ(1, u16); michael@0: EXPECT_FALSE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadU32) { michael@0: uint32_t u32 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u32; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u32); michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << (uint32_t)1; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u32; michael@0: EXPECT_EQ(1U, u32); michael@0: EXPECT_FALSE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadU64) { michael@0: uint64_t u64 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u64; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u64); michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << (uint64_t)1; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u64; michael@0: EXPECT_EQ(1U, u64); michael@0: EXPECT_FALSE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadString) { michael@0: string s(""); michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> s; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ("", s); michael@0: // write an empty string to the stream, read it back michael@0: s = "abcd"; michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << string(""); michael@0: stream >> s; michael@0: EXPECT_EQ("", s); michael@0: EXPECT_FALSE(stream.eof()); michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: stream << string("test"); michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> s; michael@0: EXPECT_EQ("test", s); michael@0: EXPECT_FALSE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadEmptyString) { michael@0: string s("abc"); michael@0: stream << string(""); michael@0: stream >> s; michael@0: EXPECT_EQ("", s); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadMultiU8) { michael@0: const uint8_t ea = 0, eb = 100, ec = 200, ed = 0xFF; michael@0: uint8_t a, b, c, d, e; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: ASSERT_FALSE(stream.eof()); michael@0: e = 0; michael@0: stream >> e; michael@0: EXPECT_EQ(0U, e); michael@0: ASSERT_TRUE(stream.eof()); michael@0: // try reading all at once, including one past eof michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: ASSERT_FALSE(stream.eof()); michael@0: a = b = c = d = e = 0; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d >> e; michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadMultiU16) { michael@0: const uint16_t ea = 0, eb = 0x100, ec = 0x8000, ed = 0xFFFF; michael@0: uint16_t a, b, c, d, e; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: ASSERT_FALSE(stream.eof()); michael@0: e = 0; michael@0: stream >> e; michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: // try reading all at once, including one past eof michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: ASSERT_FALSE(stream.eof()); michael@0: a = b = c = d = e = 0; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d >> e; michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadMultiU32) { michael@0: const uint32_t ea = 0, eb = 0x10000, ec = 0x8000000, ed = 0xFFFFFFFF; michael@0: uint32_t a, b, c, d, e; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: ASSERT_FALSE(stream.eof()); michael@0: e = 0; michael@0: stream >> e; michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: // try reading all at once, including one past eof michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: ASSERT_FALSE(stream.eof()); michael@0: a = b = c = d = e = 0; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d >> e; michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadMultiU64) { michael@0: const uint64_t ea = 0, eb = 0x10000, ec = 0x100000000ULL, michael@0: ed = 0xFFFFFFFFFFFFFFFFULL; michael@0: uint64_t a, b, c, d, e; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: ASSERT_FALSE(stream.eof()); michael@0: e = 0; michael@0: stream >> e; michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: // try reading all at once, including one past eof michael@0: stream.rewind(); michael@0: stream.clear(); michael@0: ASSERT_FALSE(stream.eof()); michael@0: a = b = c = d = e = 0; michael@0: stream << ea << eb << ec << ed; michael@0: stream >> a >> b >> c >> d >> e; michael@0: EXPECT_EQ(ea, a); michael@0: EXPECT_EQ(eb, b); michael@0: EXPECT_EQ(ec, c); michael@0: EXPECT_EQ(ed, d); michael@0: EXPECT_EQ(0U, e); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadMixed) { michael@0: const uint8_t e8 = 0x10; michael@0: const uint16_t e16 = 0x2020; michael@0: const uint32_t e32 = 0x30303030; michael@0: const uint64_t e64 = 0x4040404040404040ULL; michael@0: const string es = "test"; michael@0: uint8_t u8 = 0; michael@0: uint16_t u16 = 0; michael@0: uint32_t u32 = 0; michael@0: uint64_t u64 = 0; michael@0: string s("test"); michael@0: stream << e8 << e16 << e32 << e64 << es; michael@0: stream >> u8 >> u16 >> u32 >> u64 >> s; michael@0: EXPECT_FALSE(stream.eof()); michael@0: EXPECT_EQ(e8, u8); michael@0: EXPECT_EQ(e16, u16); michael@0: EXPECT_EQ(e32, u32); michael@0: EXPECT_EQ(e64, u64); michael@0: EXPECT_EQ(es, s); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadStringMissing) { michael@0: // ensure that reading a string where only the length is present fails michael@0: uint16_t u16 = 8; michael@0: stream << u16; michael@0: stream.rewind(); michael@0: string s(""); michael@0: stream >> s; michael@0: EXPECT_EQ("", s); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, ReadStringTruncated) { michael@0: // ensure that reading a string where not all the data is present fails michael@0: uint16_t u16 = 8; michael@0: stream << u16; michael@0: stream << (uint8_t)'t' << (uint8_t)'e' << (uint8_t)'s' << (uint8_t)'t'; michael@0: stream.rewind(); michael@0: string s(""); michael@0: stream >> s; michael@0: EXPECT_EQ("", s); michael@0: EXPECT_TRUE(stream.eof()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, StreamByteLength) { michael@0: // Test that the stream buffer contains the right amount of data michael@0: stream << (uint8_t)0 << (uint16_t)1 << (uint32_t)2 << (uint64_t)3 michael@0: << string("test"); michael@0: string s = stream.str(); michael@0: EXPECT_EQ(21U, s.length()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, AppendStreamResultsByteLength) { michael@0: // Test that appending the str() results from two streams michael@0: // gives the right byte length michael@0: binarystream stream2; michael@0: stream << (uint8_t)0 << (uint16_t)1; michael@0: stream2 << (uint32_t)0 << (uint64_t)2 michael@0: << string("test"); michael@0: string s = stream.str(); michael@0: string s2 = stream2.str(); michael@0: s.append(s2); michael@0: EXPECT_EQ(21U, s.length()); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamBasicTest, StreamSetStr) { michael@0: const string es("test"); michael@0: stream << es; michael@0: binarystream stream2; michael@0: stream2.str(stream.str()); michael@0: string s; michael@0: stream2 >> s; michael@0: EXPECT_FALSE(stream2.eof()); michael@0: EXPECT_EQ("test", s); michael@0: s = ""; michael@0: stream2.str(stream.str()); michael@0: stream2.rewind(); michael@0: stream2 >> s; michael@0: EXPECT_FALSE(stream2.eof()); michael@0: EXPECT_EQ("test", s); michael@0: } michael@0: michael@0: class BinaryStreamU8Test : public ::testing::Test { michael@0: protected: michael@0: binarystream stream; michael@0: michael@0: void SetUp() { michael@0: stream << (uint8_t)1; michael@0: } michael@0: }; michael@0: michael@0: TEST_F(BinaryStreamU8Test, ReadU16) { michael@0: uint16_t u16 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u16; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u16); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamU8Test, ReadU32) { michael@0: uint32_t u32 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u32; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u32); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamU8Test, ReadU64) { michael@0: uint64_t u64 = 0; michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> u64; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ(0U, u64); michael@0: } michael@0: michael@0: TEST_F(BinaryStreamU8Test, ReadString) { michael@0: string s(""); michael@0: ASSERT_FALSE(stream.eof()); michael@0: stream >> s; michael@0: ASSERT_TRUE(stream.eof()); michael@0: EXPECT_EQ("", s); michael@0: } michael@0: michael@0: michael@0: TEST(BinaryStreamTest, InitWithData) { michael@0: const char *data = "abcd"; michael@0: binarystream stream(data); michael@0: uint8_t a, b, c, d; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ('a', a); michael@0: EXPECT_EQ('b', b); michael@0: EXPECT_EQ('c', c); michael@0: EXPECT_EQ('d', d); michael@0: } michael@0: michael@0: TEST(BinaryStreamTest, InitWithDataLeadingNull) { michael@0: const char *data = "\0abcd"; michael@0: binarystream stream(data, 5); michael@0: uint8_t z, a, b, c, d; michael@0: stream >> z >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ(0U, z); michael@0: EXPECT_EQ('a', a); michael@0: EXPECT_EQ('b', b); michael@0: EXPECT_EQ('c', c); michael@0: EXPECT_EQ('d', d); michael@0: } michael@0: michael@0: TEST(BinaryStreamTest, InitWithDataVector) { michael@0: vector data; michael@0: data.push_back('a'); michael@0: data.push_back('b'); michael@0: data.push_back('c'); michael@0: data.push_back('d'); michael@0: data.push_back('e'); michael@0: data.resize(4); michael@0: binarystream stream(&data[0], data.size()); michael@0: uint8_t a, b, c, d; michael@0: stream >> a >> b >> c >> d; michael@0: ASSERT_FALSE(stream.eof()); michael@0: EXPECT_EQ('a', a); michael@0: EXPECT_EQ('b', b); michael@0: EXPECT_EQ('c', c); michael@0: EXPECT_EQ('d', d); michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: int main(int argc, char *argv[]) { michael@0: ::testing::InitGoogleTest(&argc, argv); michael@0: return RUN_ALL_TESTS(); michael@0: }