/thirdparty/breakpad/processor/binarystream_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 432 lines · 361 code · 33 blank · 38 comment · 0 complexity · 97371569e5e338b733897182466f9f3b MD5 · raw file

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