OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "minidump/minidump_crashpad_info_writer.h" | 15 #include "minidump/minidump_crashpad_info_writer.h" |
16 | 16 |
17 #include <dbghelp.h> | 17 #include <dbghelp.h> |
18 | 18 |
19 #include "gtest/gtest.h" | 19 #include "gtest/gtest.h" |
| 20 #include "minidump/minidump_crashpad_module_writer.h" |
20 #include "minidump/minidump_extensions.h" | 21 #include "minidump/minidump_extensions.h" |
21 #include "minidump/minidump_file_writer.h" | 22 #include "minidump/minidump_file_writer.h" |
22 #include "minidump/minidump_simple_string_dictionary_writer.h" | |
23 #include "minidump/test/minidump_file_writer_test_util.h" | 23 #include "minidump/test/minidump_file_writer_test_util.h" |
24 #include "minidump/test/minidump_string_writer_test_util.h" | |
25 #include "minidump/test/minidump_writable_test_util.h" | 24 #include "minidump/test/minidump_writable_test_util.h" |
26 #include "util/file/string_file_writer.h" | 25 #include "util/file/string_file_writer.h" |
27 | 26 |
28 namespace crashpad { | 27 namespace crashpad { |
29 namespace test { | 28 namespace test { |
30 namespace { | 29 namespace { |
31 | 30 |
32 void GetCrashpadInfoStream( | 31 void GetCrashpadInfoStream(const std::string& file_contents, |
33 const std::string& file_contents, | 32 const MinidumpCrashpadInfo** crashpad_info, |
34 const MinidumpCrashpadInfo** crashpad_info, | 33 const MinidumpModuleCrashpadInfoList** module_list) { |
35 const MinidumpSimpleStringDictionary** simple_annotations) { | |
36 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER); | |
37 const size_t kCrashpadInfoStreamOffset = | |
38 kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY); | |
39 size_t end = kCrashpadInfoStreamOffset + sizeof(MinidumpCrashpadInfo); | |
40 const size_t kSimpleAnnotationsOffset = simple_annotations ? end : 0; | |
41 if (simple_annotations) { | |
42 end += sizeof(MinidumpSimpleStringDictionary); | |
43 } | |
44 const size_t kFileSize = end; | |
45 | |
46 if (!simple_annotations) { | |
47 ASSERT_EQ(kFileSize, file_contents.size()); | |
48 } else { | |
49 EXPECT_GE(file_contents.size(), kFileSize); | |
50 } | |
51 | |
52 const MINIDUMP_DIRECTORY* directory; | 34 const MINIDUMP_DIRECTORY* directory; |
53 const MINIDUMP_HEADER* header = | 35 const MINIDUMP_HEADER* header = |
54 MinidumpHeaderAtStart(file_contents, &directory); | 36 MinidumpHeaderAtStart(file_contents, &directory); |
55 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0)); | 37 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0)); |
56 ASSERT_TRUE(directory); | 38 ASSERT_TRUE(directory); |
57 | 39 |
58 ASSERT_EQ(kMinidumpStreamTypeCrashpadInfo, directory[0].StreamType); | 40 ASSERT_EQ(kMinidumpStreamTypeCrashpadInfo, directory[0].StreamType); |
59 EXPECT_EQ(kCrashpadInfoStreamOffset, directory[0].Location.Rva); | |
60 | 41 |
61 *crashpad_info = MinidumpWritableAtLocationDescriptor<MinidumpCrashpadInfo>( | 42 *crashpad_info = MinidumpWritableAtLocationDescriptor<MinidumpCrashpadInfo>( |
62 file_contents, directory[0].Location); | 43 file_contents, directory[0].Location); |
63 ASSERT_TRUE(*crashpad_info); | 44 ASSERT_TRUE(*crashpad_info); |
64 | 45 |
65 if (simple_annotations) { | 46 *module_list = |
66 EXPECT_EQ(kSimpleAnnotationsOffset, | 47 MinidumpWritableAtLocationDescriptor<MinidumpModuleCrashpadInfoList>( |
67 (*crashpad_info)->simple_annotations.Rva); | 48 file_contents, (*crashpad_info)->module_list); |
68 *simple_annotations = | |
69 MinidumpWritableAtLocationDescriptor<MinidumpSimpleStringDictionary>( | |
70 file_contents, (*crashpad_info)->simple_annotations); | |
71 ASSERT_TRUE(*simple_annotations); | |
72 } else { | |
73 ASSERT_EQ(0u, (*crashpad_info)->simple_annotations.DataSize); | |
74 ASSERT_EQ(0u, (*crashpad_info)->simple_annotations.Rva); | |
75 } | |
76 } | 49 } |
77 | 50 |
78 TEST(MinidumpCrashpadInfoWriter, Empty) { | 51 TEST(MinidumpCrashpadInfoWriter, Empty) { |
79 MinidumpFileWriter minidump_file_writer; | 52 MinidumpFileWriter minidump_file_writer; |
80 MinidumpCrashpadInfoWriter crashpad_info_writer; | 53 MinidumpCrashpadInfoWriter crashpad_info_writer; |
81 | 54 |
82 minidump_file_writer.AddStream(&crashpad_info_writer); | 55 minidump_file_writer.AddStream(&crashpad_info_writer); |
83 | 56 |
84 StringFileWriter file_writer; | 57 StringFileWriter file_writer; |
85 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); | 58 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); |
86 | 59 |
87 const MinidumpCrashpadInfo* crashpad_info; | 60 const MinidumpCrashpadInfo* crashpad_info; |
| 61 const MinidumpModuleCrashpadInfoList* module_list; |
88 | 62 |
89 ASSERT_NO_FATAL_FAILURE( | 63 ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream( |
90 GetCrashpadInfoStream(file_writer.string(), &crashpad_info, nullptr)); | 64 file_writer.string(), &crashpad_info, &module_list)); |
91 | 65 |
92 EXPECT_EQ(sizeof(*crashpad_info), crashpad_info->size); | 66 EXPECT_EQ(MinidumpCrashpadInfo::kVersion, crashpad_info->version); |
93 EXPECT_EQ(1u, crashpad_info->version); | 67 EXPECT_FALSE(module_list); |
94 EXPECT_EQ(0u, crashpad_info->simple_annotations.DataSize); | |
95 EXPECT_EQ(0u, crashpad_info->simple_annotations.Rva); | |
96 } | 68 } |
97 | 69 |
98 TEST(MinidumpCrashpadInfoWriter, SimpleAnnotations) { | 70 TEST(MinidumpCrashpadInfoWriter, CrashpadModuleList) { |
| 71 const uint32_t kMinidumpModuleListIndex = 3; |
| 72 |
99 MinidumpFileWriter minidump_file_writer; | 73 MinidumpFileWriter minidump_file_writer; |
100 MinidumpCrashpadInfoWriter crashpad_info_writer; | 74 MinidumpCrashpadInfoWriter crashpad_info_writer; |
101 | 75 |
102 minidump_file_writer.AddStream(&crashpad_info_writer); | 76 minidump_file_writer.AddStream(&crashpad_info_writer); |
103 | 77 |
104 MinidumpSimpleStringDictionaryWriter simple_annotations_writer; | 78 MinidumpModuleCrashpadInfoListWriter module_list_writer; |
105 | 79 MinidumpModuleCrashpadInfoWriter module_writer; |
106 // Set a key and value before adding the simple_annotations_writer to | 80 module_writer.SetMinidumpModuleListIndex(kMinidumpModuleListIndex); |
107 // crashpad_info_writer, and another one after. | 81 module_list_writer.AddModule(&module_writer); |
108 const char kKey0[] = "k0"; | 82 crashpad_info_writer.SetModuleList(&module_list_writer); |
109 const char kValue0[] = "v"; | |
110 const char kKey1[] = "KEY1"; | |
111 const char kValue1[] = ""; | |
112 MinidumpSimpleStringDictionaryEntryWriter entry_0; | |
113 entry_0.SetKeyValue(kKey0, kValue0); | |
114 simple_annotations_writer.AddEntry(&entry_0); | |
115 | |
116 crashpad_info_writer.SetSimpleAnnotations(&simple_annotations_writer); | |
117 | |
118 MinidumpSimpleStringDictionaryEntryWriter entry_1; | |
119 entry_1.SetKeyValue(kKey1, kValue1); | |
120 simple_annotations_writer.AddEntry(&entry_1); | |
121 | 83 |
122 StringFileWriter file_writer; | 84 StringFileWriter file_writer; |
123 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); | 85 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); |
124 | 86 |
125 const MinidumpCrashpadInfo* crashpad_info; | 87 const MinidumpCrashpadInfo* crashpad_info; |
126 const MinidumpSimpleStringDictionary* simple_annotations; | 88 const MinidumpModuleCrashpadInfoList* module_list; |
127 | 89 |
128 ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream( | 90 ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream( |
129 file_writer.string(), &crashpad_info, &simple_annotations)); | 91 file_writer.string(), &crashpad_info, &module_list)); |
130 | 92 |
131 EXPECT_EQ(sizeof(*crashpad_info), crashpad_info->size); | 93 EXPECT_EQ(MinidumpCrashpadInfo::kVersion, crashpad_info->version); |
132 EXPECT_EQ(1u, crashpad_info->version); | 94 ASSERT_TRUE(module_list); |
| 95 ASSERT_EQ(1u, module_list->count); |
133 | 96 |
134 ASSERT_EQ(2u, simple_annotations->count); | 97 const MinidumpModuleCrashpadInfo* module = |
| 98 MinidumpWritableAtLocationDescriptor<MinidumpModuleCrashpadInfo>( |
| 99 file_writer.string(), module_list->modules[0]); |
| 100 ASSERT_TRUE(module); |
135 | 101 |
136 EXPECT_EQ(kKey1, | 102 EXPECT_EQ(MinidumpModuleCrashpadInfo::kVersion, module->version); |
137 MinidumpUTF8StringAtRVAAsString( | 103 EXPECT_EQ(kMinidumpModuleListIndex, module->minidump_module_list_index); |
138 file_writer.string(), simple_annotations->entries[0].key)); | 104 EXPECT_EQ(0u, module->simple_annotations.DataSize); |
139 EXPECT_EQ(kValue1, | 105 EXPECT_EQ(0u, module->simple_annotations.Rva); |
140 MinidumpUTF8StringAtRVAAsString( | |
141 file_writer.string(), simple_annotations->entries[0].value)); | |
142 EXPECT_EQ(kKey0, | |
143 MinidumpUTF8StringAtRVAAsString( | |
144 file_writer.string(), simple_annotations->entries[1].key)); | |
145 EXPECT_EQ(kValue0, | |
146 MinidumpUTF8StringAtRVAAsString( | |
147 file_writer.string(), simple_annotations->entries[1].value)); | |
148 } | 106 } |
149 | 107 |
150 } // namespace | 108 } // namespace |
151 } // namespace test | 109 } // namespace test |
152 } // namespace crashpad | 110 } // namespace crashpad |
OLD | NEW |