1 // Copyright (c) 2006, 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 
30 // Author: waylonis@google.com (Dan Waylonis)
31 
32 /*
33  g++ -I../ ../common/convert_UTF.c \
34  ../common/string_conversion.cc \
35  minidump_file_writer.cc \
36  minidump_file_writer_unittest.cc \
37  -o minidump_file_writer_unittest
38  */
39 
40 #include <fcntl.h>
41 #include <unistd.h>
42 
43 #include "minidump_file_writer-inl.h"
44 
45 using google_breakpad::MinidumpFileWriter;
46 
47 #define ASSERT_TRUE(cond) \
48 if (!(cond)) { \
49   fprintf(stderr, "FAILED: %s at %s:%d\n", #cond, __FILE__, __LINE__); \
50     return false; \
51 }
52 
53 #define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2))
54 #define ASSERT_NE(e1, e2) ASSERT_TRUE((e1) != (e2))
55 
56 struct StringStructure {
57   unsigned long integer_value;
58   MDLocationDescriptor first_string;
59   MDLocationDescriptor second_string;
60 };
61 
62 struct ArrayStructure {
63   unsigned char char_value;
64   unsigned short short_value;
65   unsigned long long_value;
66 };
67 
68 typedef struct {
69   unsigned long count;
70   ArrayStructure array[0];
71 } ObjectAndArrayStructure;
72 
WriteFile(const char * path)73 static bool WriteFile(const char *path) {
74   MinidumpFileWriter writer;
75   if (writer.Open(path)) {
76     // Test a single structure
77     google_breakpad::TypedMDRVA<StringStructure> strings(&writer);
78     ASSERT_TRUE(strings.Allocate());
79     strings.get()->integer_value = 0xBEEF;
80     const char *first = "First String";
81     ASSERT_TRUE(writer.WriteString(first, 0, &strings.get()->first_string));
82     const wchar_t *second = L"Second String";
83     ASSERT_TRUE(writer.WriteString(second, 0, &strings.get()->second_string));
84 
85     // Test an array structure
86     google_breakpad::TypedMDRVA<ArrayStructure> array(&writer);
87     unsigned int count = 10;
88     ASSERT_TRUE(array.AllocateArray(count));
89     for (unsigned char i = 0; i < count; ++i) {
90       ArrayStructure local;
91       local.char_value = i;
92       local.short_value = i + 1;
93       local.long_value = i + 2;
94       ASSERT_TRUE(array.CopyIndex(i, &local));
95     }
96 
97     // Test an object followed by an array
98     google_breakpad::TypedMDRVA<ObjectAndArrayStructure> obj_array(&writer);
99     ASSERT_TRUE(obj_array.AllocateObjectAndArray(count,
100                                                  sizeof(ArrayStructure)));
101     obj_array.get()->count = count;
102     for (unsigned char i = 0; i < count; ++i) {
103       ArrayStructure local;
104       local.char_value = i;
105       local.short_value = i + 1;
106       local.long_value = i + 2;
107       ASSERT_TRUE(obj_array.CopyIndexAfterObject(i, &local, sizeof(local)));
108     }
109   }
110 
111   return writer.Close();
112 }
113 
CompareFile(const char * path)114 static bool CompareFile(const char *path) {
115   unsigned long expected[] = {
116 #if defined(__BIG_ENDIAN__)
117     0x0000beef, 0x0000001e, 0x00000018, 0x00000020, 0x00000038, 0x00000000,
118     0x00000018, 0x00460069, 0x00720073, 0x00740020, 0x00530074, 0x00720069,
119     0x006e0067, 0x00000000, 0x0000001a, 0x00530065, 0x0063006f, 0x006e0064,
120     0x00200053, 0x00740072, 0x0069006e, 0x00670000, 0x00000001, 0x00000002,
121     0x01000002, 0x00000003, 0x02000003, 0x00000004, 0x03000004, 0x00000005,
122     0x04000005, 0x00000006, 0x05000006, 0x00000007, 0x06000007, 0x00000008,
123     0x07000008, 0x00000009, 0x08000009, 0x0000000a, 0x0900000a, 0x0000000b,
124     0x0000000a, 0x00000001, 0x00000002, 0x01000002, 0x00000003, 0x02000003,
125     0x00000004, 0x03000004, 0x00000005, 0x04000005, 0x00000006, 0x05000006,
126     0x00000007, 0x06000007, 0x00000008, 0x07000008, 0x00000009, 0x08000009,
127     0x0000000a, 0x0900000a, 0x0000000b, 0x00000000
128 #else
129     0x0000beef, 0x0000001e, 0x00000018, 0x00000020,
130     0x00000038, 0x00000000, 0x00000018, 0x00690046,
131     0x00730072, 0x00200074, 0x00740053, 0x00690072,
132     0x0067006e, 0x00000000, 0x0000001a, 0x00650053,
133     0x006f0063, 0x0064006e, 0x00530020, 0x00720074,
134     0x006e0069, 0x00000067, 0x00011e00, 0x00000002,
135     0x00021e01, 0x00000003, 0x00031e02, 0x00000004,
136     0x00041e03, 0x00000005, 0x00051e04, 0x00000006,
137     0x00061e05, 0x00000007, 0x00071e06, 0x00000008,
138     0x00081e07, 0x00000009, 0x00091e08, 0x0000000a,
139     0x000a1e09, 0x0000000b, 0x0000000a, 0x00011c00,
140     0x00000002, 0x00021c01, 0x00000003, 0x00031c02,
141     0x00000004, 0x00041c03, 0x00000005, 0x00051c04,
142     0x00000006, 0x00061c05, 0x00000007, 0x00071c06,
143     0x00000008, 0x00081c07, 0x00000009, 0x00091c08,
144     0x0000000a, 0x000a1c09, 0x0000000b, 0x00000000,
145 #endif
146   };
147   size_t expected_byte_count = sizeof(expected);
148   int fd = open(path, O_RDONLY, 0600);
149   void *buffer = malloc(expected_byte_count);
150   ASSERT_NE(fd, -1);
151   ASSERT_TRUE(buffer);
152   ASSERT_EQ(read(fd, buffer, expected_byte_count),
153             static_cast<ssize_t>(expected_byte_count));
154 
155   char *b1, *b2;
156   b1 = reinterpret_cast<char*>(buffer);
157   b2 = reinterpret_cast<char*>(expected);
158   while (*b1 == *b2) {
159     b1++;
160     b2++;
161   }
162 
163   printf("%p\n", reinterpret_cast<void*>(b1 - (char*)buffer));
164 
165   ASSERT_EQ(memcmp(buffer, expected, expected_byte_count), 0);
166   return true;
167 }
168 
RunTests()169 static bool RunTests() {
170   const char *path = "/tmp/minidump_file_writer_unittest.dmp";
171   ASSERT_TRUE(WriteFile(path));
172   ASSERT_TRUE(CompareFile(path));
173   unlink(path);
174   return true;
175 }
176 
main(int argc,const char * argv[])177 extern "C" int main(int argc, const char *argv[]) {
178   return RunTests() ? 0 : 1;
179 }
180