michael@0: // Copyright (c) 2006, 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: // Author: waylonis@google.com (Dan Waylonis) michael@0: michael@0: /* michael@0: g++ -I../ ../common/convert_UTF.c \ michael@0: ../common/string_conversion.cc \ michael@0: minidump_file_writer.cc \ michael@0: minidump_file_writer_unittest.cc \ michael@0: -o minidump_file_writer_unittest michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "minidump_file_writer-inl.h" michael@0: michael@0: using google_breakpad::MinidumpFileWriter; michael@0: michael@0: #define ASSERT_TRUE(cond) \ michael@0: if (!(cond)) { \ michael@0: fprintf(stderr, "FAILED: %s at %s:%d\n", #cond, __FILE__, __LINE__); \ michael@0: return false; \ michael@0: } michael@0: michael@0: #define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2)) michael@0: #define ASSERT_NE(e1, e2) ASSERT_TRUE((e1) != (e2)) michael@0: michael@0: struct StringStructure { michael@0: unsigned long integer_value; michael@0: MDLocationDescriptor first_string; michael@0: MDLocationDescriptor second_string; michael@0: }; michael@0: michael@0: struct ArrayStructure { michael@0: unsigned char char_value; michael@0: unsigned short short_value; michael@0: unsigned long long_value; michael@0: }; michael@0: michael@0: typedef struct { michael@0: unsigned long count; michael@0: ArrayStructure array[0]; michael@0: } ObjectAndArrayStructure; michael@0: michael@0: static bool WriteFile(const char *path) { michael@0: MinidumpFileWriter writer; michael@0: if (writer.Open(path)) { michael@0: // Test a single structure michael@0: google_breakpad::TypedMDRVA strings(&writer); michael@0: ASSERT_TRUE(strings.Allocate()); michael@0: strings.get()->integer_value = 0xBEEF; michael@0: const char *first = "First String"; michael@0: ASSERT_TRUE(writer.WriteString(first, 0, &strings.get()->first_string)); michael@0: const wchar_t *second = L"Second String"; michael@0: ASSERT_TRUE(writer.WriteString(second, 0, &strings.get()->second_string)); michael@0: michael@0: // Test an array structure michael@0: google_breakpad::TypedMDRVA array(&writer); michael@0: unsigned int count = 10; michael@0: ASSERT_TRUE(array.AllocateArray(count)); michael@0: for (unsigned char i = 0; i < count; ++i) { michael@0: ArrayStructure local; michael@0: local.char_value = i; michael@0: local.short_value = i + 1; michael@0: local.long_value = i + 2; michael@0: ASSERT_TRUE(array.CopyIndex(i, &local)); michael@0: } michael@0: michael@0: // Test an object followed by an array michael@0: google_breakpad::TypedMDRVA obj_array(&writer); michael@0: ASSERT_TRUE(obj_array.AllocateObjectAndArray(count, michael@0: sizeof(ArrayStructure))); michael@0: obj_array.get()->count = count; michael@0: for (unsigned char i = 0; i < count; ++i) { michael@0: ArrayStructure local; michael@0: local.char_value = i; michael@0: local.short_value = i + 1; michael@0: local.long_value = i + 2; michael@0: ASSERT_TRUE(obj_array.CopyIndexAfterObject(i, &local, sizeof(local))); michael@0: } michael@0: } michael@0: michael@0: return writer.Close(); michael@0: } michael@0: michael@0: static bool CompareFile(const char *path) { michael@0: unsigned long expected[] = { michael@0: #if defined(__BIG_ENDIAN__) michael@0: 0x0000beef, 0x0000001e, 0x00000018, 0x00000020, 0x00000038, 0x00000000, michael@0: 0x00000018, 0x00460069, 0x00720073, 0x00740020, 0x00530074, 0x00720069, michael@0: 0x006e0067, 0x00000000, 0x0000001a, 0x00530065, 0x0063006f, 0x006e0064, michael@0: 0x00200053, 0x00740072, 0x0069006e, 0x00670000, 0x00000001, 0x00000002, michael@0: 0x01000002, 0x00000003, 0x02000003, 0x00000004, 0x03000004, 0x00000005, michael@0: 0x04000005, 0x00000006, 0x05000006, 0x00000007, 0x06000007, 0x00000008, michael@0: 0x07000008, 0x00000009, 0x08000009, 0x0000000a, 0x0900000a, 0x0000000b, michael@0: 0x0000000a, 0x00000001, 0x00000002, 0x01000002, 0x00000003, 0x02000003, michael@0: 0x00000004, 0x03000004, 0x00000005, 0x04000005, 0x00000006, 0x05000006, michael@0: 0x00000007, 0x06000007, 0x00000008, 0x07000008, 0x00000009, 0x08000009, michael@0: 0x0000000a, 0x0900000a, 0x0000000b, 0x00000000 michael@0: #else michael@0: 0x0000beef, 0x0000001e, 0x00000018, 0x00000020, michael@0: 0x00000038, 0x00000000, 0x00000018, 0x00690046, michael@0: 0x00730072, 0x00200074, 0x00740053, 0x00690072, michael@0: 0x0067006e, 0x00000000, 0x0000001a, 0x00650053, michael@0: 0x006f0063, 0x0064006e, 0x00530020, 0x00720074, michael@0: 0x006e0069, 0x00000067, 0x00011e00, 0x00000002, michael@0: 0x00021e01, 0x00000003, 0x00031e02, 0x00000004, michael@0: 0x00041e03, 0x00000005, 0x00051e04, 0x00000006, michael@0: 0x00061e05, 0x00000007, 0x00071e06, 0x00000008, michael@0: 0x00081e07, 0x00000009, 0x00091e08, 0x0000000a, michael@0: 0x000a1e09, 0x0000000b, 0x0000000a, 0x00011c00, michael@0: 0x00000002, 0x00021c01, 0x00000003, 0x00031c02, michael@0: 0x00000004, 0x00041c03, 0x00000005, 0x00051c04, michael@0: 0x00000006, 0x00061c05, 0x00000007, 0x00071c06, michael@0: 0x00000008, 0x00081c07, 0x00000009, 0x00091c08, michael@0: 0x0000000a, 0x000a1c09, 0x0000000b, 0x00000000, michael@0: #endif michael@0: }; michael@0: size_t expected_byte_count = sizeof(expected); michael@0: int fd = open(path, O_RDONLY, 0600); michael@0: void *buffer = malloc(expected_byte_count); michael@0: ASSERT_NE(fd, -1); michael@0: ASSERT_TRUE(buffer); michael@0: ASSERT_EQ(read(fd, buffer, expected_byte_count), michael@0: static_cast(expected_byte_count)); michael@0: michael@0: char *b1, *b2; michael@0: b1 = reinterpret_cast(buffer); michael@0: b2 = reinterpret_cast(expected); michael@0: while (*b1 == *b2) { michael@0: b1++; michael@0: b2++; michael@0: } michael@0: michael@0: printf("%p\n", reinterpret_cast(b1 - (char*)buffer)); michael@0: michael@0: ASSERT_EQ(memcmp(buffer, expected, expected_byte_count), 0); michael@0: return true; michael@0: } michael@0: michael@0: static bool RunTests() { michael@0: const char *path = "/tmp/minidump_file_writer_unittest.dmp"; michael@0: ASSERT_TRUE(WriteFile(path)); michael@0: ASSERT_TRUE(CompareFile(path)); michael@0: unlink(path); michael@0: return true; michael@0: } michael@0: michael@0: extern "C" int main(int argc, const char *argv[]) { michael@0: return RunTests() ? 0 : 1; michael@0: }