1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/minidump_file_writer_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +// Copyright (c) 2006, 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 +// Author: waylonis@google.com (Dan Waylonis) 1.34 + 1.35 +/* 1.36 + g++ -I../ ../common/convert_UTF.c \ 1.37 + ../common/string_conversion.cc \ 1.38 + minidump_file_writer.cc \ 1.39 + minidump_file_writer_unittest.cc \ 1.40 + -o minidump_file_writer_unittest 1.41 + */ 1.42 + 1.43 +#include <fcntl.h> 1.44 +#include <unistd.h> 1.45 + 1.46 +#include "minidump_file_writer-inl.h" 1.47 + 1.48 +using google_breakpad::MinidumpFileWriter; 1.49 + 1.50 +#define ASSERT_TRUE(cond) \ 1.51 +if (!(cond)) { \ 1.52 + fprintf(stderr, "FAILED: %s at %s:%d\n", #cond, __FILE__, __LINE__); \ 1.53 + return false; \ 1.54 +} 1.55 + 1.56 +#define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2)) 1.57 +#define ASSERT_NE(e1, e2) ASSERT_TRUE((e1) != (e2)) 1.58 + 1.59 +struct StringStructure { 1.60 + unsigned long integer_value; 1.61 + MDLocationDescriptor first_string; 1.62 + MDLocationDescriptor second_string; 1.63 +}; 1.64 + 1.65 +struct ArrayStructure { 1.66 + unsigned char char_value; 1.67 + unsigned short short_value; 1.68 + unsigned long long_value; 1.69 +}; 1.70 + 1.71 +typedef struct { 1.72 + unsigned long count; 1.73 + ArrayStructure array[0]; 1.74 +} ObjectAndArrayStructure; 1.75 + 1.76 +static bool WriteFile(const char *path) { 1.77 + MinidumpFileWriter writer; 1.78 + if (writer.Open(path)) { 1.79 + // Test a single structure 1.80 + google_breakpad::TypedMDRVA<StringStructure> strings(&writer); 1.81 + ASSERT_TRUE(strings.Allocate()); 1.82 + strings.get()->integer_value = 0xBEEF; 1.83 + const char *first = "First String"; 1.84 + ASSERT_TRUE(writer.WriteString(first, 0, &strings.get()->first_string)); 1.85 + const wchar_t *second = L"Second String"; 1.86 + ASSERT_TRUE(writer.WriteString(second, 0, &strings.get()->second_string)); 1.87 + 1.88 + // Test an array structure 1.89 + google_breakpad::TypedMDRVA<ArrayStructure> array(&writer); 1.90 + unsigned int count = 10; 1.91 + ASSERT_TRUE(array.AllocateArray(count)); 1.92 + for (unsigned char i = 0; i < count; ++i) { 1.93 + ArrayStructure local; 1.94 + local.char_value = i; 1.95 + local.short_value = i + 1; 1.96 + local.long_value = i + 2; 1.97 + ASSERT_TRUE(array.CopyIndex(i, &local)); 1.98 + } 1.99 + 1.100 + // Test an object followed by an array 1.101 + google_breakpad::TypedMDRVA<ObjectAndArrayStructure> obj_array(&writer); 1.102 + ASSERT_TRUE(obj_array.AllocateObjectAndArray(count, 1.103 + sizeof(ArrayStructure))); 1.104 + obj_array.get()->count = count; 1.105 + for (unsigned char i = 0; i < count; ++i) { 1.106 + ArrayStructure local; 1.107 + local.char_value = i; 1.108 + local.short_value = i + 1; 1.109 + local.long_value = i + 2; 1.110 + ASSERT_TRUE(obj_array.CopyIndexAfterObject(i, &local, sizeof(local))); 1.111 + } 1.112 + } 1.113 + 1.114 + return writer.Close(); 1.115 +} 1.116 + 1.117 +static bool CompareFile(const char *path) { 1.118 + unsigned long expected[] = { 1.119 +#if defined(__BIG_ENDIAN__) 1.120 + 0x0000beef, 0x0000001e, 0x00000018, 0x00000020, 0x00000038, 0x00000000, 1.121 + 0x00000018, 0x00460069, 0x00720073, 0x00740020, 0x00530074, 0x00720069, 1.122 + 0x006e0067, 0x00000000, 0x0000001a, 0x00530065, 0x0063006f, 0x006e0064, 1.123 + 0x00200053, 0x00740072, 0x0069006e, 0x00670000, 0x00000001, 0x00000002, 1.124 + 0x01000002, 0x00000003, 0x02000003, 0x00000004, 0x03000004, 0x00000005, 1.125 + 0x04000005, 0x00000006, 0x05000006, 0x00000007, 0x06000007, 0x00000008, 1.126 + 0x07000008, 0x00000009, 0x08000009, 0x0000000a, 0x0900000a, 0x0000000b, 1.127 + 0x0000000a, 0x00000001, 0x00000002, 0x01000002, 0x00000003, 0x02000003, 1.128 + 0x00000004, 0x03000004, 0x00000005, 0x04000005, 0x00000006, 0x05000006, 1.129 + 0x00000007, 0x06000007, 0x00000008, 0x07000008, 0x00000009, 0x08000009, 1.130 + 0x0000000a, 0x0900000a, 0x0000000b, 0x00000000 1.131 +#else 1.132 + 0x0000beef, 0x0000001e, 0x00000018, 0x00000020, 1.133 + 0x00000038, 0x00000000, 0x00000018, 0x00690046, 1.134 + 0x00730072, 0x00200074, 0x00740053, 0x00690072, 1.135 + 0x0067006e, 0x00000000, 0x0000001a, 0x00650053, 1.136 + 0x006f0063, 0x0064006e, 0x00530020, 0x00720074, 1.137 + 0x006e0069, 0x00000067, 0x00011e00, 0x00000002, 1.138 + 0x00021e01, 0x00000003, 0x00031e02, 0x00000004, 1.139 + 0x00041e03, 0x00000005, 0x00051e04, 0x00000006, 1.140 + 0x00061e05, 0x00000007, 0x00071e06, 0x00000008, 1.141 + 0x00081e07, 0x00000009, 0x00091e08, 0x0000000a, 1.142 + 0x000a1e09, 0x0000000b, 0x0000000a, 0x00011c00, 1.143 + 0x00000002, 0x00021c01, 0x00000003, 0x00031c02, 1.144 + 0x00000004, 0x00041c03, 0x00000005, 0x00051c04, 1.145 + 0x00000006, 0x00061c05, 0x00000007, 0x00071c06, 1.146 + 0x00000008, 0x00081c07, 0x00000009, 0x00091c08, 1.147 + 0x0000000a, 0x000a1c09, 0x0000000b, 0x00000000, 1.148 +#endif 1.149 + }; 1.150 + size_t expected_byte_count = sizeof(expected); 1.151 + int fd = open(path, O_RDONLY, 0600); 1.152 + void *buffer = malloc(expected_byte_count); 1.153 + ASSERT_NE(fd, -1); 1.154 + ASSERT_TRUE(buffer); 1.155 + ASSERT_EQ(read(fd, buffer, expected_byte_count), 1.156 + static_cast<ssize_t>(expected_byte_count)); 1.157 + 1.158 + char *b1, *b2; 1.159 + b1 = reinterpret_cast<char*>(buffer); 1.160 + b2 = reinterpret_cast<char*>(expected); 1.161 + while (*b1 == *b2) { 1.162 + b1++; 1.163 + b2++; 1.164 + } 1.165 + 1.166 + printf("%p\n", reinterpret_cast<void*>(b1 - (char*)buffer)); 1.167 + 1.168 + ASSERT_EQ(memcmp(buffer, expected, expected_byte_count), 0); 1.169 + return true; 1.170 +} 1.171 + 1.172 +static bool RunTests() { 1.173 + const char *path = "/tmp/minidump_file_writer_unittest.dmp"; 1.174 + ASSERT_TRUE(WriteFile(path)); 1.175 + ASSERT_TRUE(CompareFile(path)); 1.176 + unlink(path); 1.177 + return true; 1.178 +} 1.179 + 1.180 +extern "C" int main(int argc, const char *argv[]) { 1.181 + return RunTests() ? 0 : 1; 1.182 +}