1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/binarystream_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,432 @@ 1.4 +// Copyright (c) 2010, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +#include <ios> 1.34 +#include <string> 1.35 +#include <vector> 1.36 + 1.37 +#include "breakpad_googletest_includes.h" 1.38 +#include "common/using_std_string.h" 1.39 +#include "processor/binarystream.h" 1.40 + 1.41 +namespace { 1.42 +using std::ios_base; 1.43 +using std::vector; 1.44 +using google_breakpad::binarystream; 1.45 + 1.46 + 1.47 +class BinaryStreamBasicTest : public ::testing::Test { 1.48 +protected: 1.49 + binarystream stream; 1.50 +}; 1.51 + 1.52 +TEST_F(BinaryStreamBasicTest, ReadU8) { 1.53 + uint8_t u8 = 0; 1.54 + ASSERT_FALSE(stream.eof()); 1.55 + stream >> u8; 1.56 + ASSERT_TRUE(stream.eof()); 1.57 + EXPECT_EQ(0U, u8); 1.58 + stream.rewind(); 1.59 + stream.clear(); 1.60 + stream << (uint8_t)1; 1.61 + ASSERT_FALSE(stream.eof()); 1.62 + stream >> u8; 1.63 + EXPECT_EQ(1, u8); 1.64 + EXPECT_FALSE(stream.eof()); 1.65 +} 1.66 + 1.67 +TEST_F(BinaryStreamBasicTest, ReadU16) { 1.68 + uint16_t u16 = 0; 1.69 + ASSERT_FALSE(stream.eof()); 1.70 + stream >> u16; 1.71 + ASSERT_TRUE(stream.eof()); 1.72 + EXPECT_EQ(0U, u16); 1.73 + stream.rewind(); 1.74 + stream.clear(); 1.75 + stream << (uint16_t)1; 1.76 + ASSERT_FALSE(stream.eof()); 1.77 + stream >> u16; 1.78 + EXPECT_EQ(1, u16); 1.79 + EXPECT_FALSE(stream.eof()); 1.80 +} 1.81 + 1.82 +TEST_F(BinaryStreamBasicTest, ReadU32) { 1.83 + uint32_t u32 = 0; 1.84 + ASSERT_FALSE(stream.eof()); 1.85 + stream >> u32; 1.86 + ASSERT_TRUE(stream.eof()); 1.87 + EXPECT_EQ(0U, u32); 1.88 + stream.rewind(); 1.89 + stream.clear(); 1.90 + stream << (uint32_t)1; 1.91 + ASSERT_FALSE(stream.eof()); 1.92 + stream >> u32; 1.93 + EXPECT_EQ(1U, u32); 1.94 + EXPECT_FALSE(stream.eof()); 1.95 +} 1.96 + 1.97 +TEST_F(BinaryStreamBasicTest, ReadU64) { 1.98 + uint64_t u64 = 0; 1.99 + ASSERT_FALSE(stream.eof()); 1.100 + stream >> u64; 1.101 + ASSERT_TRUE(stream.eof()); 1.102 + EXPECT_EQ(0U, u64); 1.103 + stream.rewind(); 1.104 + stream.clear(); 1.105 + stream << (uint64_t)1; 1.106 + ASSERT_FALSE(stream.eof()); 1.107 + stream >> u64; 1.108 + EXPECT_EQ(1U, u64); 1.109 + EXPECT_FALSE(stream.eof()); 1.110 +} 1.111 + 1.112 +TEST_F(BinaryStreamBasicTest, ReadString) { 1.113 + string s(""); 1.114 + ASSERT_FALSE(stream.eof()); 1.115 + stream >> s; 1.116 + ASSERT_TRUE(stream.eof()); 1.117 + EXPECT_EQ("", s); 1.118 + // write an empty string to the stream, read it back 1.119 + s = "abcd"; 1.120 + stream.rewind(); 1.121 + stream.clear(); 1.122 + stream << string(""); 1.123 + stream >> s; 1.124 + EXPECT_EQ("", s); 1.125 + EXPECT_FALSE(stream.eof()); 1.126 + stream.rewind(); 1.127 + stream.clear(); 1.128 + stream << string("test"); 1.129 + ASSERT_FALSE(stream.eof()); 1.130 + stream >> s; 1.131 + EXPECT_EQ("test", s); 1.132 + EXPECT_FALSE(stream.eof()); 1.133 +} 1.134 + 1.135 +TEST_F(BinaryStreamBasicTest, ReadEmptyString) { 1.136 + string s("abc"); 1.137 + stream << string(""); 1.138 + stream >> s; 1.139 + EXPECT_EQ("", s); 1.140 +} 1.141 + 1.142 +TEST_F(BinaryStreamBasicTest, ReadMultiU8) { 1.143 + const uint8_t ea = 0, eb = 100, ec = 200, ed = 0xFF; 1.144 + uint8_t a, b, c, d, e; 1.145 + stream << ea << eb << ec << ed; 1.146 + stream >> a >> b >> c >> d; 1.147 + ASSERT_FALSE(stream.eof()); 1.148 + EXPECT_EQ(ea, a); 1.149 + EXPECT_EQ(eb, b); 1.150 + EXPECT_EQ(ec, c); 1.151 + EXPECT_EQ(ed, d); 1.152 + ASSERT_FALSE(stream.eof()); 1.153 + e = 0; 1.154 + stream >> e; 1.155 + EXPECT_EQ(0U, e); 1.156 + ASSERT_TRUE(stream.eof()); 1.157 + // try reading all at once, including one past eof 1.158 + stream.rewind(); 1.159 + stream.clear(); 1.160 + ASSERT_FALSE(stream.eof()); 1.161 + a = b = c = d = e = 0; 1.162 + stream << ea << eb << ec << ed; 1.163 + stream >> a >> b >> c >> d >> e; 1.164 + EXPECT_EQ(ea, a); 1.165 + EXPECT_EQ(eb, b); 1.166 + EXPECT_EQ(ec, c); 1.167 + EXPECT_EQ(ed, d); 1.168 + EXPECT_EQ(0U, e); 1.169 + EXPECT_TRUE(stream.eof()); 1.170 +} 1.171 + 1.172 +TEST_F(BinaryStreamBasicTest, ReadMultiU16) { 1.173 + const uint16_t ea = 0, eb = 0x100, ec = 0x8000, ed = 0xFFFF; 1.174 + uint16_t a, b, c, d, e; 1.175 + stream << ea << eb << ec << ed; 1.176 + stream >> a >> b >> c >> d; 1.177 + ASSERT_FALSE(stream.eof()); 1.178 + EXPECT_EQ(ea, a); 1.179 + EXPECT_EQ(eb, b); 1.180 + EXPECT_EQ(ec, c); 1.181 + EXPECT_EQ(ed, d); 1.182 + ASSERT_FALSE(stream.eof()); 1.183 + e = 0; 1.184 + stream >> e; 1.185 + EXPECT_EQ(0U, e); 1.186 + EXPECT_TRUE(stream.eof()); 1.187 + // try reading all at once, including one past eof 1.188 + stream.rewind(); 1.189 + stream.clear(); 1.190 + ASSERT_FALSE(stream.eof()); 1.191 + a = b = c = d = e = 0; 1.192 + stream << ea << eb << ec << ed; 1.193 + stream >> a >> b >> c >> d >> e; 1.194 + EXPECT_EQ(ea, a); 1.195 + EXPECT_EQ(eb, b); 1.196 + EXPECT_EQ(ec, c); 1.197 + EXPECT_EQ(ed, d); 1.198 + EXPECT_EQ(0U, e); 1.199 + EXPECT_TRUE(stream.eof()); 1.200 +} 1.201 + 1.202 +TEST_F(BinaryStreamBasicTest, ReadMultiU32) { 1.203 + const uint32_t ea = 0, eb = 0x10000, ec = 0x8000000, ed = 0xFFFFFFFF; 1.204 + uint32_t a, b, c, d, e; 1.205 + stream << ea << eb << ec << ed; 1.206 + stream >> a >> b >> c >> d; 1.207 + ASSERT_FALSE(stream.eof()); 1.208 + EXPECT_EQ(ea, a); 1.209 + EXPECT_EQ(eb, b); 1.210 + EXPECT_EQ(ec, c); 1.211 + EXPECT_EQ(ed, d); 1.212 + ASSERT_FALSE(stream.eof()); 1.213 + e = 0; 1.214 + stream >> e; 1.215 + EXPECT_EQ(0U, e); 1.216 + EXPECT_TRUE(stream.eof()); 1.217 + // try reading all at once, including one past eof 1.218 + stream.rewind(); 1.219 + stream.clear(); 1.220 + ASSERT_FALSE(stream.eof()); 1.221 + a = b = c = d = e = 0; 1.222 + stream << ea << eb << ec << ed; 1.223 + stream >> a >> b >> c >> d >> e; 1.224 + EXPECT_EQ(ea, a); 1.225 + EXPECT_EQ(eb, b); 1.226 + EXPECT_EQ(ec, c); 1.227 + EXPECT_EQ(ed, d); 1.228 + EXPECT_EQ(0U, e); 1.229 + EXPECT_TRUE(stream.eof()); 1.230 +} 1.231 + 1.232 +TEST_F(BinaryStreamBasicTest, ReadMultiU64) { 1.233 + const uint64_t ea = 0, eb = 0x10000, ec = 0x100000000ULL, 1.234 + ed = 0xFFFFFFFFFFFFFFFFULL; 1.235 + uint64_t a, b, c, d, e; 1.236 + stream << ea << eb << ec << ed; 1.237 + stream >> a >> b >> c >> d; 1.238 + ASSERT_FALSE(stream.eof()); 1.239 + EXPECT_EQ(ea, a); 1.240 + EXPECT_EQ(eb, b); 1.241 + EXPECT_EQ(ec, c); 1.242 + EXPECT_EQ(ed, d); 1.243 + ASSERT_FALSE(stream.eof()); 1.244 + e = 0; 1.245 + stream >> e; 1.246 + EXPECT_EQ(0U, e); 1.247 + EXPECT_TRUE(stream.eof()); 1.248 + // try reading all at once, including one past eof 1.249 + stream.rewind(); 1.250 + stream.clear(); 1.251 + ASSERT_FALSE(stream.eof()); 1.252 + a = b = c = d = e = 0; 1.253 + stream << ea << eb << ec << ed; 1.254 + stream >> a >> b >> c >> d >> e; 1.255 + EXPECT_EQ(ea, a); 1.256 + EXPECT_EQ(eb, b); 1.257 + EXPECT_EQ(ec, c); 1.258 + EXPECT_EQ(ed, d); 1.259 + EXPECT_EQ(0U, e); 1.260 + EXPECT_TRUE(stream.eof()); 1.261 +} 1.262 + 1.263 +TEST_F(BinaryStreamBasicTest, ReadMixed) { 1.264 + const uint8_t e8 = 0x10; 1.265 + const uint16_t e16 = 0x2020; 1.266 + const uint32_t e32 = 0x30303030; 1.267 + const uint64_t e64 = 0x4040404040404040ULL; 1.268 + const string es = "test"; 1.269 + uint8_t u8 = 0; 1.270 + uint16_t u16 = 0; 1.271 + uint32_t u32 = 0; 1.272 + uint64_t u64 = 0; 1.273 + string s("test"); 1.274 + stream << e8 << e16 << e32 << e64 << es; 1.275 + stream >> u8 >> u16 >> u32 >> u64 >> s; 1.276 + EXPECT_FALSE(stream.eof()); 1.277 + EXPECT_EQ(e8, u8); 1.278 + EXPECT_EQ(e16, u16); 1.279 + EXPECT_EQ(e32, u32); 1.280 + EXPECT_EQ(e64, u64); 1.281 + EXPECT_EQ(es, s); 1.282 +} 1.283 + 1.284 +TEST_F(BinaryStreamBasicTest, ReadStringMissing) { 1.285 + // ensure that reading a string where only the length is present fails 1.286 + uint16_t u16 = 8; 1.287 + stream << u16; 1.288 + stream.rewind(); 1.289 + string s(""); 1.290 + stream >> s; 1.291 + EXPECT_EQ("", s); 1.292 + EXPECT_TRUE(stream.eof()); 1.293 +} 1.294 + 1.295 +TEST_F(BinaryStreamBasicTest, ReadStringTruncated) { 1.296 + // ensure that reading a string where not all the data is present fails 1.297 + uint16_t u16 = 8; 1.298 + stream << u16; 1.299 + stream << (uint8_t)'t' << (uint8_t)'e' << (uint8_t)'s' << (uint8_t)'t'; 1.300 + stream.rewind(); 1.301 + string s(""); 1.302 + stream >> s; 1.303 + EXPECT_EQ("", s); 1.304 + EXPECT_TRUE(stream.eof()); 1.305 +} 1.306 + 1.307 +TEST_F(BinaryStreamBasicTest, StreamByteLength) { 1.308 + // Test that the stream buffer contains the right amount of data 1.309 + stream << (uint8_t)0 << (uint16_t)1 << (uint32_t)2 << (uint64_t)3 1.310 + << string("test"); 1.311 + string s = stream.str(); 1.312 + EXPECT_EQ(21U, s.length()); 1.313 +} 1.314 + 1.315 +TEST_F(BinaryStreamBasicTest, AppendStreamResultsByteLength) { 1.316 + // Test that appending the str() results from two streams 1.317 + // gives the right byte length 1.318 + binarystream stream2; 1.319 + stream << (uint8_t)0 << (uint16_t)1; 1.320 + stream2 << (uint32_t)0 << (uint64_t)2 1.321 + << string("test"); 1.322 + string s = stream.str(); 1.323 + string s2 = stream2.str(); 1.324 + s.append(s2); 1.325 + EXPECT_EQ(21U, s.length()); 1.326 +} 1.327 + 1.328 +TEST_F(BinaryStreamBasicTest, StreamSetStr) { 1.329 + const string es("test"); 1.330 + stream << es; 1.331 + binarystream stream2; 1.332 + stream2.str(stream.str()); 1.333 + string s; 1.334 + stream2 >> s; 1.335 + EXPECT_FALSE(stream2.eof()); 1.336 + EXPECT_EQ("test", s); 1.337 + s = ""; 1.338 + stream2.str(stream.str()); 1.339 + stream2.rewind(); 1.340 + stream2 >> s; 1.341 + EXPECT_FALSE(stream2.eof()); 1.342 + EXPECT_EQ("test", s); 1.343 +} 1.344 + 1.345 +class BinaryStreamU8Test : public ::testing::Test { 1.346 +protected: 1.347 + binarystream stream; 1.348 + 1.349 + void SetUp() { 1.350 + stream << (uint8_t)1; 1.351 + } 1.352 +}; 1.353 + 1.354 +TEST_F(BinaryStreamU8Test, ReadU16) { 1.355 + uint16_t u16 = 0; 1.356 + ASSERT_FALSE(stream.eof()); 1.357 + stream >> u16; 1.358 + ASSERT_TRUE(stream.eof()); 1.359 + EXPECT_EQ(0U, u16); 1.360 +} 1.361 + 1.362 +TEST_F(BinaryStreamU8Test, ReadU32) { 1.363 + uint32_t u32 = 0; 1.364 + ASSERT_FALSE(stream.eof()); 1.365 + stream >> u32; 1.366 + ASSERT_TRUE(stream.eof()); 1.367 + EXPECT_EQ(0U, u32); 1.368 +} 1.369 + 1.370 +TEST_F(BinaryStreamU8Test, ReadU64) { 1.371 + uint64_t u64 = 0; 1.372 + ASSERT_FALSE(stream.eof()); 1.373 + stream >> u64; 1.374 + ASSERT_TRUE(stream.eof()); 1.375 + EXPECT_EQ(0U, u64); 1.376 +} 1.377 + 1.378 +TEST_F(BinaryStreamU8Test, ReadString) { 1.379 + string s(""); 1.380 + ASSERT_FALSE(stream.eof()); 1.381 + stream >> s; 1.382 + ASSERT_TRUE(stream.eof()); 1.383 + EXPECT_EQ("", s); 1.384 +} 1.385 + 1.386 + 1.387 +TEST(BinaryStreamTest, InitWithData) { 1.388 + const char *data = "abcd"; 1.389 + binarystream stream(data); 1.390 + uint8_t a, b, c, d; 1.391 + stream >> a >> b >> c >> d; 1.392 + ASSERT_FALSE(stream.eof()); 1.393 + EXPECT_EQ('a', a); 1.394 + EXPECT_EQ('b', b); 1.395 + EXPECT_EQ('c', c); 1.396 + EXPECT_EQ('d', d); 1.397 +} 1.398 + 1.399 +TEST(BinaryStreamTest, InitWithDataLeadingNull) { 1.400 + const char *data = "\0abcd"; 1.401 + binarystream stream(data, 5); 1.402 + uint8_t z, a, b, c, d; 1.403 + stream >> z >> a >> b >> c >> d; 1.404 + ASSERT_FALSE(stream.eof()); 1.405 + EXPECT_EQ(0U, z); 1.406 + EXPECT_EQ('a', a); 1.407 + EXPECT_EQ('b', b); 1.408 + EXPECT_EQ('c', c); 1.409 + EXPECT_EQ('d', d); 1.410 +} 1.411 + 1.412 +TEST(BinaryStreamTest, InitWithDataVector) { 1.413 + vector<char> data; 1.414 + data.push_back('a'); 1.415 + data.push_back('b'); 1.416 + data.push_back('c'); 1.417 + data.push_back('d'); 1.418 + data.push_back('e'); 1.419 + data.resize(4); 1.420 + binarystream stream(&data[0], data.size()); 1.421 + uint8_t a, b, c, d; 1.422 + stream >> a >> b >> c >> d; 1.423 + ASSERT_FALSE(stream.eof()); 1.424 + EXPECT_EQ('a', a); 1.425 + EXPECT_EQ('b', b); 1.426 + EXPECT_EQ('c', c); 1.427 + EXPECT_EQ('d', d); 1.428 +} 1.429 + 1.430 +} // namespace 1.431 + 1.432 +int main(int argc, char *argv[]) { 1.433 + ::testing::InitGoogleTest(&argc, argv); 1.434 + return RUN_ALL_TESTS(); 1.435 +}