toolkit/crashreporter/google-breakpad/src/processor/binarystream_unittest.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // Copyright (c) 2010, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 #include <ios>
michael@0 31 #include <string>
michael@0 32 #include <vector>
michael@0 33
michael@0 34 #include "breakpad_googletest_includes.h"
michael@0 35 #include "common/using_std_string.h"
michael@0 36 #include "processor/binarystream.h"
michael@0 37
michael@0 38 namespace {
michael@0 39 using std::ios_base;
michael@0 40 using std::vector;
michael@0 41 using google_breakpad::binarystream;
michael@0 42
michael@0 43
michael@0 44 class BinaryStreamBasicTest : public ::testing::Test {
michael@0 45 protected:
michael@0 46 binarystream stream;
michael@0 47 };
michael@0 48
michael@0 49 TEST_F(BinaryStreamBasicTest, ReadU8) {
michael@0 50 uint8_t u8 = 0;
michael@0 51 ASSERT_FALSE(stream.eof());
michael@0 52 stream >> u8;
michael@0 53 ASSERT_TRUE(stream.eof());
michael@0 54 EXPECT_EQ(0U, u8);
michael@0 55 stream.rewind();
michael@0 56 stream.clear();
michael@0 57 stream << (uint8_t)1;
michael@0 58 ASSERT_FALSE(stream.eof());
michael@0 59 stream >> u8;
michael@0 60 EXPECT_EQ(1, u8);
michael@0 61 EXPECT_FALSE(stream.eof());
michael@0 62 }
michael@0 63
michael@0 64 TEST_F(BinaryStreamBasicTest, ReadU16) {
michael@0 65 uint16_t u16 = 0;
michael@0 66 ASSERT_FALSE(stream.eof());
michael@0 67 stream >> u16;
michael@0 68 ASSERT_TRUE(stream.eof());
michael@0 69 EXPECT_EQ(0U, u16);
michael@0 70 stream.rewind();
michael@0 71 stream.clear();
michael@0 72 stream << (uint16_t)1;
michael@0 73 ASSERT_FALSE(stream.eof());
michael@0 74 stream >> u16;
michael@0 75 EXPECT_EQ(1, u16);
michael@0 76 EXPECT_FALSE(stream.eof());
michael@0 77 }
michael@0 78
michael@0 79 TEST_F(BinaryStreamBasicTest, ReadU32) {
michael@0 80 uint32_t u32 = 0;
michael@0 81 ASSERT_FALSE(stream.eof());
michael@0 82 stream >> u32;
michael@0 83 ASSERT_TRUE(stream.eof());
michael@0 84 EXPECT_EQ(0U, u32);
michael@0 85 stream.rewind();
michael@0 86 stream.clear();
michael@0 87 stream << (uint32_t)1;
michael@0 88 ASSERT_FALSE(stream.eof());
michael@0 89 stream >> u32;
michael@0 90 EXPECT_EQ(1U, u32);
michael@0 91 EXPECT_FALSE(stream.eof());
michael@0 92 }
michael@0 93
michael@0 94 TEST_F(BinaryStreamBasicTest, ReadU64) {
michael@0 95 uint64_t u64 = 0;
michael@0 96 ASSERT_FALSE(stream.eof());
michael@0 97 stream >> u64;
michael@0 98 ASSERT_TRUE(stream.eof());
michael@0 99 EXPECT_EQ(0U, u64);
michael@0 100 stream.rewind();
michael@0 101 stream.clear();
michael@0 102 stream << (uint64_t)1;
michael@0 103 ASSERT_FALSE(stream.eof());
michael@0 104 stream >> u64;
michael@0 105 EXPECT_EQ(1U, u64);
michael@0 106 EXPECT_FALSE(stream.eof());
michael@0 107 }
michael@0 108
michael@0 109 TEST_F(BinaryStreamBasicTest, ReadString) {
michael@0 110 string s("");
michael@0 111 ASSERT_FALSE(stream.eof());
michael@0 112 stream >> s;
michael@0 113 ASSERT_TRUE(stream.eof());
michael@0 114 EXPECT_EQ("", s);
michael@0 115 // write an empty string to the stream, read it back
michael@0 116 s = "abcd";
michael@0 117 stream.rewind();
michael@0 118 stream.clear();
michael@0 119 stream << string("");
michael@0 120 stream >> s;
michael@0 121 EXPECT_EQ("", s);
michael@0 122 EXPECT_FALSE(stream.eof());
michael@0 123 stream.rewind();
michael@0 124 stream.clear();
michael@0 125 stream << string("test");
michael@0 126 ASSERT_FALSE(stream.eof());
michael@0 127 stream >> s;
michael@0 128 EXPECT_EQ("test", s);
michael@0 129 EXPECT_FALSE(stream.eof());
michael@0 130 }
michael@0 131
michael@0 132 TEST_F(BinaryStreamBasicTest, ReadEmptyString) {
michael@0 133 string s("abc");
michael@0 134 stream << string("");
michael@0 135 stream >> s;
michael@0 136 EXPECT_EQ("", s);
michael@0 137 }
michael@0 138
michael@0 139 TEST_F(BinaryStreamBasicTest, ReadMultiU8) {
michael@0 140 const uint8_t ea = 0, eb = 100, ec = 200, ed = 0xFF;
michael@0 141 uint8_t a, b, c, d, e;
michael@0 142 stream << ea << eb << ec << ed;
michael@0 143 stream >> a >> b >> c >> d;
michael@0 144 ASSERT_FALSE(stream.eof());
michael@0 145 EXPECT_EQ(ea, a);
michael@0 146 EXPECT_EQ(eb, b);
michael@0 147 EXPECT_EQ(ec, c);
michael@0 148 EXPECT_EQ(ed, d);
michael@0 149 ASSERT_FALSE(stream.eof());
michael@0 150 e = 0;
michael@0 151 stream >> e;
michael@0 152 EXPECT_EQ(0U, e);
michael@0 153 ASSERT_TRUE(stream.eof());
michael@0 154 // try reading all at once, including one past eof
michael@0 155 stream.rewind();
michael@0 156 stream.clear();
michael@0 157 ASSERT_FALSE(stream.eof());
michael@0 158 a = b = c = d = e = 0;
michael@0 159 stream << ea << eb << ec << ed;
michael@0 160 stream >> a >> b >> c >> d >> e;
michael@0 161 EXPECT_EQ(ea, a);
michael@0 162 EXPECT_EQ(eb, b);
michael@0 163 EXPECT_EQ(ec, c);
michael@0 164 EXPECT_EQ(ed, d);
michael@0 165 EXPECT_EQ(0U, e);
michael@0 166 EXPECT_TRUE(stream.eof());
michael@0 167 }
michael@0 168
michael@0 169 TEST_F(BinaryStreamBasicTest, ReadMultiU16) {
michael@0 170 const uint16_t ea = 0, eb = 0x100, ec = 0x8000, ed = 0xFFFF;
michael@0 171 uint16_t a, b, c, d, e;
michael@0 172 stream << ea << eb << ec << ed;
michael@0 173 stream >> a >> b >> c >> d;
michael@0 174 ASSERT_FALSE(stream.eof());
michael@0 175 EXPECT_EQ(ea, a);
michael@0 176 EXPECT_EQ(eb, b);
michael@0 177 EXPECT_EQ(ec, c);
michael@0 178 EXPECT_EQ(ed, d);
michael@0 179 ASSERT_FALSE(stream.eof());
michael@0 180 e = 0;
michael@0 181 stream >> e;
michael@0 182 EXPECT_EQ(0U, e);
michael@0 183 EXPECT_TRUE(stream.eof());
michael@0 184 // try reading all at once, including one past eof
michael@0 185 stream.rewind();
michael@0 186 stream.clear();
michael@0 187 ASSERT_FALSE(stream.eof());
michael@0 188 a = b = c = d = e = 0;
michael@0 189 stream << ea << eb << ec << ed;
michael@0 190 stream >> a >> b >> c >> d >> e;
michael@0 191 EXPECT_EQ(ea, a);
michael@0 192 EXPECT_EQ(eb, b);
michael@0 193 EXPECT_EQ(ec, c);
michael@0 194 EXPECT_EQ(ed, d);
michael@0 195 EXPECT_EQ(0U, e);
michael@0 196 EXPECT_TRUE(stream.eof());
michael@0 197 }
michael@0 198
michael@0 199 TEST_F(BinaryStreamBasicTest, ReadMultiU32) {
michael@0 200 const uint32_t ea = 0, eb = 0x10000, ec = 0x8000000, ed = 0xFFFFFFFF;
michael@0 201 uint32_t a, b, c, d, e;
michael@0 202 stream << ea << eb << ec << ed;
michael@0 203 stream >> a >> b >> c >> d;
michael@0 204 ASSERT_FALSE(stream.eof());
michael@0 205 EXPECT_EQ(ea, a);
michael@0 206 EXPECT_EQ(eb, b);
michael@0 207 EXPECT_EQ(ec, c);
michael@0 208 EXPECT_EQ(ed, d);
michael@0 209 ASSERT_FALSE(stream.eof());
michael@0 210 e = 0;
michael@0 211 stream >> e;
michael@0 212 EXPECT_EQ(0U, e);
michael@0 213 EXPECT_TRUE(stream.eof());
michael@0 214 // try reading all at once, including one past eof
michael@0 215 stream.rewind();
michael@0 216 stream.clear();
michael@0 217 ASSERT_FALSE(stream.eof());
michael@0 218 a = b = c = d = e = 0;
michael@0 219 stream << ea << eb << ec << ed;
michael@0 220 stream >> a >> b >> c >> d >> e;
michael@0 221 EXPECT_EQ(ea, a);
michael@0 222 EXPECT_EQ(eb, b);
michael@0 223 EXPECT_EQ(ec, c);
michael@0 224 EXPECT_EQ(ed, d);
michael@0 225 EXPECT_EQ(0U, e);
michael@0 226 EXPECT_TRUE(stream.eof());
michael@0 227 }
michael@0 228
michael@0 229 TEST_F(BinaryStreamBasicTest, ReadMultiU64) {
michael@0 230 const uint64_t ea = 0, eb = 0x10000, ec = 0x100000000ULL,
michael@0 231 ed = 0xFFFFFFFFFFFFFFFFULL;
michael@0 232 uint64_t a, b, c, d, e;
michael@0 233 stream << ea << eb << ec << ed;
michael@0 234 stream >> a >> b >> c >> d;
michael@0 235 ASSERT_FALSE(stream.eof());
michael@0 236 EXPECT_EQ(ea, a);
michael@0 237 EXPECT_EQ(eb, b);
michael@0 238 EXPECT_EQ(ec, c);
michael@0 239 EXPECT_EQ(ed, d);
michael@0 240 ASSERT_FALSE(stream.eof());
michael@0 241 e = 0;
michael@0 242 stream >> e;
michael@0 243 EXPECT_EQ(0U, e);
michael@0 244 EXPECT_TRUE(stream.eof());
michael@0 245 // try reading all at once, including one past eof
michael@0 246 stream.rewind();
michael@0 247 stream.clear();
michael@0 248 ASSERT_FALSE(stream.eof());
michael@0 249 a = b = c = d = e = 0;
michael@0 250 stream << ea << eb << ec << ed;
michael@0 251 stream >> a >> b >> c >> d >> e;
michael@0 252 EXPECT_EQ(ea, a);
michael@0 253 EXPECT_EQ(eb, b);
michael@0 254 EXPECT_EQ(ec, c);
michael@0 255 EXPECT_EQ(ed, d);
michael@0 256 EXPECT_EQ(0U, e);
michael@0 257 EXPECT_TRUE(stream.eof());
michael@0 258 }
michael@0 259
michael@0 260 TEST_F(BinaryStreamBasicTest, ReadMixed) {
michael@0 261 const uint8_t e8 = 0x10;
michael@0 262 const uint16_t e16 = 0x2020;
michael@0 263 const uint32_t e32 = 0x30303030;
michael@0 264 const uint64_t e64 = 0x4040404040404040ULL;
michael@0 265 const string es = "test";
michael@0 266 uint8_t u8 = 0;
michael@0 267 uint16_t u16 = 0;
michael@0 268 uint32_t u32 = 0;
michael@0 269 uint64_t u64 = 0;
michael@0 270 string s("test");
michael@0 271 stream << e8 << e16 << e32 << e64 << es;
michael@0 272 stream >> u8 >> u16 >> u32 >> u64 >> s;
michael@0 273 EXPECT_FALSE(stream.eof());
michael@0 274 EXPECT_EQ(e8, u8);
michael@0 275 EXPECT_EQ(e16, u16);
michael@0 276 EXPECT_EQ(e32, u32);
michael@0 277 EXPECT_EQ(e64, u64);
michael@0 278 EXPECT_EQ(es, s);
michael@0 279 }
michael@0 280
michael@0 281 TEST_F(BinaryStreamBasicTest, ReadStringMissing) {
michael@0 282 // ensure that reading a string where only the length is present fails
michael@0 283 uint16_t u16 = 8;
michael@0 284 stream << u16;
michael@0 285 stream.rewind();
michael@0 286 string s("");
michael@0 287 stream >> s;
michael@0 288 EXPECT_EQ("", s);
michael@0 289 EXPECT_TRUE(stream.eof());
michael@0 290 }
michael@0 291
michael@0 292 TEST_F(BinaryStreamBasicTest, ReadStringTruncated) {
michael@0 293 // ensure that reading a string where not all the data is present fails
michael@0 294 uint16_t u16 = 8;
michael@0 295 stream << u16;
michael@0 296 stream << (uint8_t)'t' << (uint8_t)'e' << (uint8_t)'s' << (uint8_t)'t';
michael@0 297 stream.rewind();
michael@0 298 string s("");
michael@0 299 stream >> s;
michael@0 300 EXPECT_EQ("", s);
michael@0 301 EXPECT_TRUE(stream.eof());
michael@0 302 }
michael@0 303
michael@0 304 TEST_F(BinaryStreamBasicTest, StreamByteLength) {
michael@0 305 // Test that the stream buffer contains the right amount of data
michael@0 306 stream << (uint8_t)0 << (uint16_t)1 << (uint32_t)2 << (uint64_t)3
michael@0 307 << string("test");
michael@0 308 string s = stream.str();
michael@0 309 EXPECT_EQ(21U, s.length());
michael@0 310 }
michael@0 311
michael@0 312 TEST_F(BinaryStreamBasicTest, AppendStreamResultsByteLength) {
michael@0 313 // Test that appending the str() results from two streams
michael@0 314 // gives the right byte length
michael@0 315 binarystream stream2;
michael@0 316 stream << (uint8_t)0 << (uint16_t)1;
michael@0 317 stream2 << (uint32_t)0 << (uint64_t)2
michael@0 318 << string("test");
michael@0 319 string s = stream.str();
michael@0 320 string s2 = stream2.str();
michael@0 321 s.append(s2);
michael@0 322 EXPECT_EQ(21U, s.length());
michael@0 323 }
michael@0 324
michael@0 325 TEST_F(BinaryStreamBasicTest, StreamSetStr) {
michael@0 326 const string es("test");
michael@0 327 stream << es;
michael@0 328 binarystream stream2;
michael@0 329 stream2.str(stream.str());
michael@0 330 string s;
michael@0 331 stream2 >> s;
michael@0 332 EXPECT_FALSE(stream2.eof());
michael@0 333 EXPECT_EQ("test", s);
michael@0 334 s = "";
michael@0 335 stream2.str(stream.str());
michael@0 336 stream2.rewind();
michael@0 337 stream2 >> s;
michael@0 338 EXPECT_FALSE(stream2.eof());
michael@0 339 EXPECT_EQ("test", s);
michael@0 340 }
michael@0 341
michael@0 342 class BinaryStreamU8Test : public ::testing::Test {
michael@0 343 protected:
michael@0 344 binarystream stream;
michael@0 345
michael@0 346 void SetUp() {
michael@0 347 stream << (uint8_t)1;
michael@0 348 }
michael@0 349 };
michael@0 350
michael@0 351 TEST_F(BinaryStreamU8Test, ReadU16) {
michael@0 352 uint16_t u16 = 0;
michael@0 353 ASSERT_FALSE(stream.eof());
michael@0 354 stream >> u16;
michael@0 355 ASSERT_TRUE(stream.eof());
michael@0 356 EXPECT_EQ(0U, u16);
michael@0 357 }
michael@0 358
michael@0 359 TEST_F(BinaryStreamU8Test, ReadU32) {
michael@0 360 uint32_t u32 = 0;
michael@0 361 ASSERT_FALSE(stream.eof());
michael@0 362 stream >> u32;
michael@0 363 ASSERT_TRUE(stream.eof());
michael@0 364 EXPECT_EQ(0U, u32);
michael@0 365 }
michael@0 366
michael@0 367 TEST_F(BinaryStreamU8Test, ReadU64) {
michael@0 368 uint64_t u64 = 0;
michael@0 369 ASSERT_FALSE(stream.eof());
michael@0 370 stream >> u64;
michael@0 371 ASSERT_TRUE(stream.eof());
michael@0 372 EXPECT_EQ(0U, u64);
michael@0 373 }
michael@0 374
michael@0 375 TEST_F(BinaryStreamU8Test, ReadString) {
michael@0 376 string s("");
michael@0 377 ASSERT_FALSE(stream.eof());
michael@0 378 stream >> s;
michael@0 379 ASSERT_TRUE(stream.eof());
michael@0 380 EXPECT_EQ("", s);
michael@0 381 }
michael@0 382
michael@0 383
michael@0 384 TEST(BinaryStreamTest, InitWithData) {
michael@0 385 const char *data = "abcd";
michael@0 386 binarystream stream(data);
michael@0 387 uint8_t a, b, c, d;
michael@0 388 stream >> a >> b >> c >> d;
michael@0 389 ASSERT_FALSE(stream.eof());
michael@0 390 EXPECT_EQ('a', a);
michael@0 391 EXPECT_EQ('b', b);
michael@0 392 EXPECT_EQ('c', c);
michael@0 393 EXPECT_EQ('d', d);
michael@0 394 }
michael@0 395
michael@0 396 TEST(BinaryStreamTest, InitWithDataLeadingNull) {
michael@0 397 const char *data = "\0abcd";
michael@0 398 binarystream stream(data, 5);
michael@0 399 uint8_t z, a, b, c, d;
michael@0 400 stream >> z >> a >> b >> c >> d;
michael@0 401 ASSERT_FALSE(stream.eof());
michael@0 402 EXPECT_EQ(0U, z);
michael@0 403 EXPECT_EQ('a', a);
michael@0 404 EXPECT_EQ('b', b);
michael@0 405 EXPECT_EQ('c', c);
michael@0 406 EXPECT_EQ('d', d);
michael@0 407 }
michael@0 408
michael@0 409 TEST(BinaryStreamTest, InitWithDataVector) {
michael@0 410 vector<char> data;
michael@0 411 data.push_back('a');
michael@0 412 data.push_back('b');
michael@0 413 data.push_back('c');
michael@0 414 data.push_back('d');
michael@0 415 data.push_back('e');
michael@0 416 data.resize(4);
michael@0 417 binarystream stream(&data[0], data.size());
michael@0 418 uint8_t a, b, c, d;
michael@0 419 stream >> a >> b >> c >> d;
michael@0 420 ASSERT_FALSE(stream.eof());
michael@0 421 EXPECT_EQ('a', a);
michael@0 422 EXPECT_EQ('b', b);
michael@0 423 EXPECT_EQ('c', c);
michael@0 424 EXPECT_EQ('d', d);
michael@0 425 }
michael@0 426
michael@0 427 } // namespace
michael@0 428
michael@0 429 int main(int argc, char *argv[]) {
michael@0 430 ::testing::InitGoogleTest(&argc, argv);
michael@0 431 return RUN_ALL_TESTS();
michael@0 432 }

mercurial