Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(972)

Side by Side Diff: minidump/minidump_system_info_writer_test.cc

Issue 435243002: Add MinidumpSystemInfoWriter and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Fix release-mode death test Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « minidump/minidump_system_info_writer.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "minidump/minidump_system_info_writer.h"
16
17 #include <dbghelp.h>
18 #include <string.h>
19
20 #include <string>
21
22 #include "gtest/gtest.h"
23 #include "minidump/minidump_file_writer.h"
24 #include "util/file/string_file_writer.h"
25
26 namespace {
27
28 using namespace crashpad;
29
30 void GetSystemInfoStream(const std::string& file_contents,
31 size_t csd_version_length,
32 const MINIDUMP_SYSTEM_INFO** system_info,
33 const MINIDUMP_STRING** csd_version) {
34 // The expected number of bytes for the CSD version’s MINIDUMP_STRING::Buffer.
35 const size_t kCSDVersionBytes =
36 csd_version_length * sizeof(MINIDUMP_STRING::Buffer[0]);
37 const size_t kCSDVersionBytesWithNUL =
38 kCSDVersionBytes + sizeof(MINIDUMP_STRING::Buffer[0]);
39
40 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
41 const size_t kSystemInfoStreamOffset =
42 kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
43 const size_t kCSDVersionOffset =
44 kSystemInfoStreamOffset + sizeof(MINIDUMP_SYSTEM_INFO);
45 const size_t kFileSize =
46 kCSDVersionOffset + sizeof(MINIDUMP_STRING) + kCSDVersionBytesWithNUL;
47
48 ASSERT_EQ(kFileSize, file_contents.size());
49
50 const MINIDUMP_HEADER* header =
51 reinterpret_cast<const MINIDUMP_HEADER*>(&file_contents[0]);
52
53 EXPECT_EQ(static_cast<uint32_t>(MINIDUMP_SIGNATURE), header->Signature);
54 EXPECT_EQ(static_cast<uint32_t>(MINIDUMP_VERSION), header->Version);
55 ASSERT_EQ(1u, header->NumberOfStreams);
56 ASSERT_EQ(kDirectoryOffset, header->StreamDirectoryRva);
57 EXPECT_EQ(0u, header->CheckSum);
58 EXPECT_EQ(0u, header->TimeDateStamp);
59 EXPECT_EQ(MiniDumpNormal, header->Flags);
60
61 const MINIDUMP_DIRECTORY* directory =
62 reinterpret_cast<const MINIDUMP_DIRECTORY*>(
63 &file_contents[kDirectoryOffset]);
64
65 ASSERT_EQ(kMinidumpStreamTypeSystemInfo, directory->StreamType);
66 ASSERT_EQ(sizeof(MINIDUMP_SYSTEM_INFO), directory->Location.DataSize);
67 ASSERT_EQ(kSystemInfoStreamOffset, directory->Location.Rva);
68
69 *system_info = reinterpret_cast<const MINIDUMP_SYSTEM_INFO*>(
70 &file_contents[kSystemInfoStreamOffset]);
71
72 ASSERT_EQ(kCSDVersionOffset, (*system_info)->CSDVersionRva);
73
74 *csd_version = reinterpret_cast<const MINIDUMP_STRING*>(
75 &file_contents[kCSDVersionOffset]);
76
77 ASSERT_EQ(kCSDVersionBytes, (*csd_version)->Length);
78 }
79
80 TEST(MinidumpSystemInfoWriter, Empty) {
81 MinidumpFileWriter minidump_file_writer;
82 MinidumpSystemInfoWriter system_info_writer;
83
84 system_info_writer.SetCSDVersion(std::string());
85
86 minidump_file_writer.AddStream(&system_info_writer);
87
88 StringFileWriter file_writer;
89 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
90
91 const MINIDUMP_SYSTEM_INFO* system_info;
92 const MINIDUMP_STRING* csd_version;
93
94 GetSystemInfoStream(file_writer.string(), 0, &system_info, &csd_version);
95
96 EXPECT_EQ(kMinidumpCPUArchitectureUnknown,
97 system_info->ProcessorArchitecture);
98 EXPECT_EQ(0u, system_info->ProcessorLevel);
99 EXPECT_EQ(0u, system_info->ProcessorRevision);
100 EXPECT_EQ(0u, system_info->NumberOfProcessors);
101 EXPECT_EQ(0u, system_info->ProductType);
102 EXPECT_EQ(0u, system_info->MajorVersion);
103 EXPECT_EQ(0u, system_info->MinorVersion);
104 EXPECT_EQ(0u, system_info->BuildNumber);
105 EXPECT_EQ(0u, system_info->PlatformId);
106 EXPECT_EQ(0u, system_info->SuiteMask);
107 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.VendorId[0]);
108 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.VendorId[1]);
109 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.VendorId[2]);
110 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.VersionInformation);
111 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.FeatureInformation);
112 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.AMDExtendedCpuFeatures);
113 EXPECT_EQ(0u, system_info->Cpu.OtherCpuInfo.ProcessorFeatures[0]);
114 EXPECT_EQ(0u, system_info->Cpu.OtherCpuInfo.ProcessorFeatures[1]);
115
116 EXPECT_EQ('\0', csd_version->Buffer[0]);
117 }
118
119 TEST(MinidumpSystemInfoWriter, X86_Win) {
120 MinidumpFileWriter minidump_file_writer;
121 MinidumpSystemInfoWriter system_info_writer;
122
123 const MinidumpCPUArchitecture kCPUArchitecture = kMinidumpCPUArchitectureX86;
124 const uint16_t kCPULevel = 0x0010;
125 const uint16_t kCPURevision = 0x0602;
126 const uint8_t kCPUCount = 1;
127 const MinidumpOS kOS = kMinidumpOSWin32NT;
128 const MinidumpOSType kOSType = kMinidumpOSTypeWorkstation;
129 const uint32_t kOSVersionMajor = 6;
130 const uint32_t kOSVersionMinor = 1;
131 const uint32_t kOSVersionBuild = 7601;
132 const char kCSDVersion[] = "Service Pack 1";
133 const uint16_t kSuiteMask = VER_SUITE_SINGLEUSERTS;
134 const char kCPUVendor[] = "AuthenticAMD";
135 const uint32_t kCPUVersion = 0x00100f62;
136 const uint32_t kCPUFeatures = 0x078bfbff;
137 const uint32_t kAMDFeatures = 0xefd3fbff;
138
139 uint32_t cpu_vendor_registers[3];
140 ASSERT_EQ(sizeof(cpu_vendor_registers), strlen(kCPUVendor));
141 memcpy(cpu_vendor_registers, kCPUVendor, sizeof(cpu_vendor_registers));
142
143 system_info_writer.SetCPUArchitecture(kCPUArchitecture);
144 system_info_writer.SetCPULevelAndRevision(kCPULevel, kCPURevision);
145 system_info_writer.SetCPUCount(kCPUCount);
146 system_info_writer.SetOS(kOS);
147 system_info_writer.SetOSType(kMinidumpOSTypeWorkstation);
148 system_info_writer.SetOSVersion(
149 kOSVersionMajor, kOSVersionMinor, kOSVersionBuild);
150 system_info_writer.SetCSDVersion(kCSDVersion);
151 system_info_writer.SetSuiteMask(kSuiteMask);
152 system_info_writer.SetCPUX86VendorString(kCPUVendor);
153 system_info_writer.SetCPUX86VersionAndFeatures(kCPUVersion, kCPUFeatures);
154 system_info_writer.SetCPUX86AMDExtendedFeatures(kAMDFeatures);
155
156 minidump_file_writer.AddStream(&system_info_writer);
157
158 StringFileWriter file_writer;
159 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
160
161 const MINIDUMP_SYSTEM_INFO* system_info;
162 const MINIDUMP_STRING* csd_version;
163
164 GetSystemInfoStream(
165 file_writer.string(), strlen(kCSDVersion), &system_info, &csd_version);
166
167 EXPECT_EQ(kCPUArchitecture, system_info->ProcessorArchitecture);
168 EXPECT_EQ(kCPULevel, system_info->ProcessorLevel);
169 EXPECT_EQ(kCPURevision, system_info->ProcessorRevision);
170 EXPECT_EQ(kCPUCount, system_info->NumberOfProcessors);
171 EXPECT_EQ(kOSType, system_info->ProductType);
172 EXPECT_EQ(kOSVersionMajor, system_info->MajorVersion);
173 EXPECT_EQ(kOSVersionMinor, system_info->MinorVersion);
174 EXPECT_EQ(kOSVersionBuild, system_info->BuildNumber);
175 EXPECT_EQ(kOS, system_info->PlatformId);
176 EXPECT_EQ(kSuiteMask, system_info->SuiteMask);
177 EXPECT_EQ(cpu_vendor_registers[0], system_info->Cpu.X86CpuInfo.VendorId[0]);
178 EXPECT_EQ(cpu_vendor_registers[1], system_info->Cpu.X86CpuInfo.VendorId[1]);
179 EXPECT_EQ(cpu_vendor_registers[2], system_info->Cpu.X86CpuInfo.VendorId[2]);
180 EXPECT_EQ(kCPUVersion, system_info->Cpu.X86CpuInfo.VersionInformation);
181 EXPECT_EQ(kCPUFeatures, system_info->Cpu.X86CpuInfo.FeatureInformation);
182 EXPECT_EQ(kAMDFeatures, system_info->Cpu.X86CpuInfo.AMDExtendedCpuFeatures);
183
184 for (size_t index = 0; index < strlen(kCSDVersion); ++index) {
185 EXPECT_EQ(kCSDVersion[index], csd_version->Buffer[index]) << index;
186 }
187 }
188
189 TEST(MinidumpSystemInfoWriter, X86_64_Mac) {
190 MinidumpFileWriter minidump_file_writer;
191 MinidumpSystemInfoWriter system_info_writer;
192
193 const MinidumpCPUArchitecture kCPUArchitecture =
194 kMinidumpCPUArchitectureAMD64;
195 const uint16_t kCPULevel = 0x0006;
196 const uint16_t kCPURevision = 0x3a09;
197 const uint8_t kCPUCount = 8;
198 const MinidumpOS kOS = kMinidumpOSMacOSX;
199 const MinidumpOSType kOSType = kMinidumpOSTypeWorkstation;
200 const uint32_t kOSVersionMajor = 10;
201 const uint32_t kOSVersionMinor = 9;
202 const uint32_t kOSVersionBuild = 4;
203 const char kCSDVersion[] = "13E28";
204 const uint64_t kCPUFeatures[2] = {0x10427f4c, 0x00000000};
205
206 system_info_writer.SetCPUArchitecture(kCPUArchitecture);
207 system_info_writer.SetCPULevelAndRevision(kCPULevel, kCPURevision);
208 system_info_writer.SetCPUCount(kCPUCount);
209 system_info_writer.SetOS(kOS);
210 system_info_writer.SetOSType(kMinidumpOSTypeWorkstation);
211 system_info_writer.SetOSVersion(
212 kOSVersionMajor, kOSVersionMinor, kOSVersionBuild);
213 system_info_writer.SetCSDVersion(kCSDVersion);
214 system_info_writer.SetCPUOtherFeatures(kCPUFeatures[0], kCPUFeatures[1]);
215
216 minidump_file_writer.AddStream(&system_info_writer);
217
218 StringFileWriter file_writer;
219 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
220
221 const MINIDUMP_SYSTEM_INFO* system_info;
222 const MINIDUMP_STRING* csd_version;
223
224 GetSystemInfoStream(
225 file_writer.string(), strlen(kCSDVersion), &system_info, &csd_version);
226
227 EXPECT_EQ(kCPUArchitecture, system_info->ProcessorArchitecture);
228 EXPECT_EQ(kCPULevel, system_info->ProcessorLevel);
229 EXPECT_EQ(kCPURevision, system_info->ProcessorRevision);
230 EXPECT_EQ(kCPUCount, system_info->NumberOfProcessors);
231 EXPECT_EQ(kOSType, system_info->ProductType);
232 EXPECT_EQ(kOSVersionMajor, system_info->MajorVersion);
233 EXPECT_EQ(kOSVersionMinor, system_info->MinorVersion);
234 EXPECT_EQ(kOSVersionBuild, system_info->BuildNumber);
235 EXPECT_EQ(kOS, system_info->PlatformId);
236 EXPECT_EQ(0u, system_info->SuiteMask);
237 EXPECT_EQ(kCPUFeatures[0],
238 system_info->Cpu.OtherCpuInfo.ProcessorFeatures[0]);
239 EXPECT_EQ(kCPUFeatures[1],
240 system_info->Cpu.OtherCpuInfo.ProcessorFeatures[1]);
241 }
242
243 TEST(MinidumpSystemInfoWriter, X86_CPUVendorFromRegisters) {
244 // MinidumpSystemInfoWriter.X86_Win already tested SetCPUX86VendorString().
245 // This test exercises SetCPUX86Vendor() to set the vendor from register
246 // values.
247 MinidumpFileWriter minidump_file_writer;
248 MinidumpSystemInfoWriter system_info_writer;
249
250 const MinidumpCPUArchitecture kCPUArchitecture = kMinidumpCPUArchitectureX86;
251 const uint32_t kCPUVendor[] = {'uneG', 'Ieni', 'letn'};
252
253 system_info_writer.SetCPUArchitecture(kCPUArchitecture);
254 system_info_writer.SetCPUX86Vendor(
255 kCPUVendor[0], kCPUVendor[1], kCPUVendor[2]);
256 system_info_writer.SetCSDVersion(std::string());
257
258 minidump_file_writer.AddStream(&system_info_writer);
259
260 StringFileWriter file_writer;
261 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
262
263 const MINIDUMP_SYSTEM_INFO* system_info;
264 const MINIDUMP_STRING* csd_version;
265
266 GetSystemInfoStream(file_writer.string(), 0, &system_info, &csd_version);
267
268 EXPECT_EQ(kCPUArchitecture, system_info->ProcessorArchitecture);
269 EXPECT_EQ(0u, system_info->ProcessorLevel);
270 EXPECT_EQ(kCPUVendor[0], system_info->Cpu.X86CpuInfo.VendorId[0]);
271 EXPECT_EQ(kCPUVendor[1], system_info->Cpu.X86CpuInfo.VendorId[1]);
272 EXPECT_EQ(kCPUVendor[2], system_info->Cpu.X86CpuInfo.VendorId[2]);
273 EXPECT_EQ(0u, system_info->Cpu.X86CpuInfo.VersionInformation);
274 }
275
276 TEST(MinidumpSystemInfoWriterDeathTest, NoCSDVersion) {
277 MinidumpFileWriter minidump_file_writer;
278 MinidumpSystemInfoWriter system_info_writer;
279 minidump_file_writer.AddStream(&system_info_writer);
280
281 StringFileWriter file_writer;
282 ASSERT_DEATH(minidump_file_writer.WriteEverything(&file_writer),
283 "csd_version_");
284 }
285
286 } // namespace
OLDNEW
« no previous file with comments | « minidump/minidump_system_info_writer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698