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: // Original author: Jim Blandy michael@0: michael@0: // synth_minidump_unittest.cc: Unit tests for google_breakpad::SynthMinidump michael@0: // classes. michael@0: 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 "google_breakpad/common/minidump_format.h" michael@0: #include "processor/synth_minidump.h" michael@0: #include "processor/synth_minidump_unittest_data.h" michael@0: michael@0: using google_breakpad::SynthMinidump::Context; michael@0: using google_breakpad::SynthMinidump::Dump; michael@0: using google_breakpad::SynthMinidump::Exception; michael@0: using google_breakpad::SynthMinidump::List; michael@0: using google_breakpad::SynthMinidump::Memory; michael@0: using google_breakpad::SynthMinidump::Module; michael@0: using google_breakpad::SynthMinidump::Section; michael@0: using google_breakpad::SynthMinidump::Stream; michael@0: using google_breakpad::SynthMinidump::String; michael@0: using google_breakpad::SynthMinidump::SystemInfo; michael@0: using google_breakpad::test_assembler::kBigEndian; michael@0: using google_breakpad::test_assembler::kLittleEndian; michael@0: using google_breakpad::test_assembler::Label; michael@0: michael@0: TEST(Section, Simple) { michael@0: Dump dump(0); michael@0: Section section(dump); michael@0: section.L32(0x12345678); michael@0: section.Finish(0); michael@0: string contents; michael@0: ASSERT_TRUE(section.GetContents(&contents)); michael@0: EXPECT_EQ(string("\x78\x56\x34\x12", 4), contents); michael@0: } michael@0: michael@0: TEST(Section, CiteLocationIn) { michael@0: Dump dump(0, kBigEndian); michael@0: Section section1(dump), section2(dump); michael@0: section1.Append("order"); michael@0: section2.Append("mayhem"); michael@0: section2.Finish(0x32287ec2); michael@0: section2.CiteLocationIn(§ion1); michael@0: string contents; michael@0: ASSERT_TRUE(section1.GetContents(&contents)); michael@0: string expected("order\0\0\0\x06\x32\x28\x7e\xc2", 13); michael@0: EXPECT_EQ(expected, contents); michael@0: } michael@0: michael@0: TEST(Stream, CiteStreamIn) { michael@0: Dump dump(0, kLittleEndian); michael@0: Stream stream(dump, 0x40cae2b3); michael@0: Section section(dump); michael@0: stream.Append("stream contents"); michael@0: section.Append("section contents"); michael@0: stream.Finish(0x41424344); michael@0: stream.CiteStreamIn(§ion); michael@0: string contents; michael@0: ASSERT_TRUE(section.GetContents(&contents)); michael@0: string expected("section contents" michael@0: "\xb3\xe2\xca\x40" michael@0: "\x0f\0\0\0" michael@0: "\x44\x43\x42\x41", michael@0: 16 + 4 + 4 + 4); michael@0: EXPECT_EQ(expected, contents); michael@0: } michael@0: michael@0: TEST(Memory, CiteMemoryIn) { michael@0: Dump dump(0, kBigEndian); michael@0: Memory memory(dump, 0x76d010874ab019f9ULL); michael@0: Section section(dump); michael@0: memory.Append("memory contents"); michael@0: section.Append("section contents"); michael@0: memory.Finish(0x51525354); michael@0: memory.CiteMemoryIn(§ion); michael@0: string contents; michael@0: ASSERT_TRUE(section.GetContents(&contents)); michael@0: string expected("section contents" michael@0: "\x76\xd0\x10\x87\x4a\xb0\x19\xf9" michael@0: "\0\0\0\x0f" michael@0: "\x51\x52\x53\x54", michael@0: 16 + 8 + 4 + 4); michael@0: EXPECT_EQ(contents, expected); michael@0: } michael@0: michael@0: TEST(Memory, Here) { michael@0: Dump dump(0, kBigEndian); michael@0: Memory memory(dump, 0x89979731eb060ed4ULL); michael@0: memory.Append(1729, 42); michael@0: Label l = memory.Here(); michael@0: ASSERT_EQ(0x89979731eb060ed4ULL + 1729, l.Value()); michael@0: } michael@0: michael@0: TEST(Context, X86) { michael@0: Dump dump(0, kLittleEndian); michael@0: assert(x86_raw_context.context_flags & MD_CONTEXT_X86); michael@0: Context context(dump, x86_raw_context); michael@0: string contents; michael@0: ASSERT_TRUE(context.GetContents(&contents)); michael@0: EXPECT_EQ(sizeof(x86_expected_contents), contents.size()); michael@0: EXPECT_TRUE(memcmp(contents.data(), x86_expected_contents, contents.size()) michael@0: == 0); michael@0: } michael@0: michael@0: TEST(Context, ARM) { michael@0: Dump dump(0, kLittleEndian); michael@0: assert(arm_raw_context.context_flags & MD_CONTEXT_ARM); michael@0: Context context(dump, arm_raw_context); michael@0: string contents; michael@0: ASSERT_TRUE(context.GetContents(&contents)); michael@0: EXPECT_EQ(sizeof(arm_expected_contents), contents.size()); michael@0: EXPECT_TRUE(memcmp(contents.data(), arm_expected_contents, contents.size()) michael@0: == 0); michael@0: } michael@0: michael@0: TEST(ContextDeathTest, X86BadFlags) { michael@0: Dump dump(0, kLittleEndian); michael@0: MDRawContextX86 raw; michael@0: raw.context_flags = MD_CONTEXT_AMD64; michael@0: ASSERT_DEATH(Context context(dump, raw);, michael@0: "context\\.context_flags & (0x[0-9a-f]+|MD_CONTEXT_X86)"); michael@0: } michael@0: michael@0: TEST(ContextDeathTest, X86BadEndianness) { michael@0: Dump dump(0, kBigEndian); michael@0: MDRawContextX86 raw; michael@0: raw.context_flags = MD_CONTEXT_X86; michael@0: ASSERT_DEATH(Context context(dump, raw);, michael@0: "dump\\.endianness\\(\\) == kLittleEndian"); michael@0: } michael@0: michael@0: TEST(Thread, Simple) { michael@0: Dump dump(0, kLittleEndian); michael@0: Context context(dump, x86_raw_context); michael@0: context.Finish(0x8665da0c); michael@0: Memory stack(dump, 0xaad55a93cc3c0efcULL); michael@0: stack.Append("stack contents"); michael@0: stack.Finish(0xe08cdbd1); michael@0: google_breakpad::SynthMinidump::Thread thread( michael@0: dump, 0x3d7ec360, stack, context, michael@0: 0x3593f44d, // suspend count michael@0: 0xab352b82, // priority class michael@0: 0x2753d838, // priority michael@0: 0xeb2de4be3f29e3e9ULL); // thread environment block michael@0: string contents; michael@0: ASSERT_TRUE(thread.GetContents(&contents)); michael@0: static const uint8_t expected_bytes[] = { michael@0: 0x60, 0xc3, 0x7e, 0x3d, // thread id michael@0: 0x4d, 0xf4, 0x93, 0x35, // suspend count michael@0: 0x82, 0x2b, 0x35, 0xab, // priority class michael@0: 0x38, 0xd8, 0x53, 0x27, // priority michael@0: 0xe9, 0xe3, 0x29, 0x3f, 0xbe, 0xe4, 0x2d, 0xeb, // thread environment block michael@0: 0xfc, 0x0e, 0x3c, 0xcc, 0x93, 0x5a, 0xd5, 0xaa, // stack address michael@0: 0x0e, 0x00, 0x00, 0x00, // stack size michael@0: 0xd1, 0xdb, 0x8c, 0xe0, // stack MDRVA michael@0: 0xcc, 0x02, 0x00, 0x00, // context size michael@0: 0x0c, 0xda, 0x65, 0x86 // context MDRVA michael@0: }; michael@0: EXPECT_EQ(sizeof(expected_bytes), contents.size()); michael@0: EXPECT_TRUE(memcmp(contents.data(), expected_bytes, contents.size()) == 0); michael@0: } michael@0: michael@0: TEST(Exception, Simple) { michael@0: Dump dump(0, kLittleEndian); michael@0: Context context(dump, x86_raw_context); michael@0: context.Finish(0x8665da0c); michael@0: michael@0: Exception exception(dump, context, michael@0: 0x1234abcd, // thread id michael@0: 0xdcba4321, // exception code michael@0: 0xf0e0d0c0, // exception flags michael@0: 0x0919a9b9c9d9e9f9ULL); // exception address michael@0: string contents; michael@0: ASSERT_TRUE(exception.GetContents(&contents)); michael@0: static const uint8_t expected_bytes[] = { michael@0: 0xcd, 0xab, 0x34, 0x12, // thread id michael@0: 0x00, 0x00, 0x00, 0x00, // __align michael@0: 0x21, 0x43, 0xba, 0xdc, // exception code michael@0: 0xc0, 0xd0, 0xe0, 0xf0, // exception flags michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception record michael@0: 0xf9, 0xe9, 0xd9, 0xc9, 0xb9, 0xa9, 0x19, 0x09, // exception address michael@0: 0x00, 0x00, 0x00, 0x00, // number parameters michael@0: 0x00, 0x00, 0x00, 0x00, // __align michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exception_information michael@0: 0xcc, 0x02, 0x00, 0x00, // context size michael@0: 0x0c, 0xda, 0x65, 0x86 // context MDRVA michael@0: }; michael@0: EXPECT_EQ(sizeof(expected_bytes), contents.size()); michael@0: EXPECT_TRUE(memcmp(contents.data(), expected_bytes, contents.size()) == 0); michael@0: } michael@0: michael@0: TEST(String, Simple) { michael@0: Dump dump(0, kBigEndian); michael@0: String s(dump, "All mimsy were the borogoves"); michael@0: string contents; michael@0: ASSERT_TRUE(s.GetContents(&contents)); michael@0: static const char expected[] = michael@0: "\x00\x00\x00\x38\0A\0l\0l\0 \0m\0i\0m\0s\0y\0 \0w\0e\0r\0e" michael@0: "\0 \0t\0h\0e\0 \0b\0o\0r\0o\0g\0o\0v\0e\0s"; michael@0: string expected_string(expected, sizeof(expected) - 1); michael@0: EXPECT_EQ(expected_string, contents); michael@0: } michael@0: michael@0: TEST(String, CiteStringIn) { michael@0: Dump dump(0, kLittleEndian); michael@0: String s(dump, "and the mome wraths outgrabe"); michael@0: Section section(dump); michael@0: section.Append("initial"); michael@0: s.CiteStringIn(§ion); michael@0: s.Finish(0xdc2bb469); michael@0: string contents; michael@0: ASSERT_TRUE(section.GetContents(&contents)); michael@0: EXPECT_EQ(string("initial\x69\xb4\x2b\xdc", 7 + 4), contents); michael@0: } michael@0: michael@0: TEST(List, Empty) { michael@0: Dump dump(0, kBigEndian); michael@0: List
list(dump, 0x2442779c); michael@0: EXPECT_TRUE(list.Empty()); michael@0: list.Finish(0x84e09808); michael@0: string contents; michael@0: ASSERT_TRUE(list.GetContents(&contents)); michael@0: EXPECT_EQ(string("\0\0\0\0", 4), contents); michael@0: } michael@0: michael@0: TEST(List, Two) { michael@0: Dump dump(0, kBigEndian); michael@0: List
list(dump, 0x26c9f498); michael@0: Section section1(dump); michael@0: section1.Append("section one contents"); michael@0: EXPECT_TRUE(list.Empty()); michael@0: list.Add(§ion1); michael@0: EXPECT_FALSE(list.Empty()); michael@0: Section section2(dump); michael@0: section2.Append("section two contents"); michael@0: list.Add(§ion2); michael@0: list.Finish(0x1e5bb60e); michael@0: string contents; michael@0: ASSERT_TRUE(list.GetContents(&contents)); michael@0: EXPECT_EQ(string("\0\0\0\x02section one contentssection two contents", 44), michael@0: contents); michael@0: } michael@0: michael@0: TEST(Dump, Header) { michael@0: Dump dump(0x9f738b33685cc84cULL, kLittleEndian, 0xb3817faf, 0x2c741c0a); michael@0: dump.Finish(); michael@0: string contents; michael@0: ASSERT_TRUE(dump.GetContents(&contents)); michael@0: ASSERT_EQ(string("\x4d\x44\x4d\x50" // signature michael@0: "\xaf\x7f\x81\xb3" // version michael@0: "\0\0\0\0" // stream count michael@0: "\x20\0\0\0" // directory RVA (could be anything) michael@0: "\0\0\0\0" // checksum michael@0: "\x0a\x1c\x74\x2c" // time_date_stamp michael@0: "\x4c\xc8\x5c\x68\x33\x8b\x73\x9f", // flags michael@0: 32), michael@0: contents); michael@0: } michael@0: michael@0: TEST(Dump, HeaderBigEndian) { michael@0: Dump dump(0x206ce3cc6fb8e0f0ULL, kBigEndian, 0x161693e2, 0x35667744); michael@0: dump.Finish(); michael@0: string contents; michael@0: ASSERT_TRUE(dump.GetContents(&contents)); michael@0: ASSERT_EQ(string("\x50\x4d\x44\x4d" // signature michael@0: "\x16\x16\x93\xe2" // version michael@0: "\0\0\0\0" // stream count michael@0: "\0\0\0\x20" // directory RVA (could be anything) michael@0: "\0\0\0\0" // checksum michael@0: "\x35\x66\x77\x44" // time_date_stamp michael@0: "\x20\x6c\xe3\xcc\x6f\xb8\xe0\xf0", // flags michael@0: 32), michael@0: contents); michael@0: } michael@0: michael@0: TEST(Dump, OneSection) { michael@0: Dump dump(0, kLittleEndian); michael@0: Section section(dump); michael@0: section.Append("section contents"); michael@0: dump.Add(§ion); michael@0: dump.Finish(); michael@0: string dump_contents; michael@0: // Just check for undefined labels; don't worry about the contents. michael@0: ASSERT_TRUE(dump.GetContents(&dump_contents)); michael@0: michael@0: Section referencing_section(dump); michael@0: section.CiteLocationIn(&referencing_section); michael@0: string contents; michael@0: ASSERT_TRUE(referencing_section.GetContents(&contents)); michael@0: ASSERT_EQ(string("\x10\0\0\0\x20\0\0\0", 8), contents); michael@0: }