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

Side by Side Diff: minidump/minidump_memory_writer_test.cc

Issue 936153002: Add FileReaderInterface. Move StringFileWriter to StringFile and (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Remove unused #include Created 5 years, 10 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
OLDNEW
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,
(...skipping 11 matching lines...) Expand all
22 #include "base/format_macros.h" 22 #include "base/format_macros.h"
23 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
24 #include "gtest/gtest.h" 24 #include "gtest/gtest.h"
25 #include "minidump/minidump_extensions.h" 25 #include "minidump/minidump_extensions.h"
26 #include "minidump/minidump_file_writer.h" 26 #include "minidump/minidump_file_writer.h"
27 #include "minidump/minidump_stream_writer.h" 27 #include "minidump/minidump_stream_writer.h"
28 #include "minidump/test/minidump_file_writer_test_util.h" 28 #include "minidump/test/minidump_file_writer_test_util.h"
29 #include "minidump/test/minidump_memory_writer_test_util.h" 29 #include "minidump/test/minidump_memory_writer_test_util.h"
30 #include "minidump/test/minidump_writable_test_util.h" 30 #include "minidump/test/minidump_writable_test_util.h"
31 #include "snapshot/test/test_memory_snapshot.h" 31 #include "snapshot/test/test_memory_snapshot.h"
32 #include "util/file/string_file_writer.h" 32 #include "util/file/string_file.h"
33 #include "util/stdlib/pointer_container.h" 33 #include "util/stdlib/pointer_container.h"
34 34
35 namespace crashpad { 35 namespace crashpad {
36 namespace test { 36 namespace test {
37 namespace { 37 namespace {
38 38
39 const MinidumpStreamType kBogusStreamType = 39 const MinidumpStreamType kBogusStreamType =
40 static_cast<MinidumpStreamType>(1234); 40 static_cast<MinidumpStreamType>(1234);
41 41
42 // expected_streams is the expected number of streams in the file. The memory 42 // expected_streams is the expected number of streams in the file. The memory
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 file_contents, directory[directory_index].Location); 75 file_contents, directory[directory_index].Location);
76 ASSERT_TRUE(memory_list); 76 ASSERT_TRUE(memory_list);
77 } 77 }
78 78
79 TEST(MinidumpMemoryWriter, EmptyMemoryList) { 79 TEST(MinidumpMemoryWriter, EmptyMemoryList) {
80 MinidumpFileWriter minidump_file_writer; 80 MinidumpFileWriter minidump_file_writer;
81 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); 81 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter());
82 82
83 minidump_file_writer.AddStream(memory_list_writer.Pass()); 83 minidump_file_writer.AddStream(memory_list_writer.Pass());
84 84
85 StringFileWriter file_writer; 85 StringFile string_file;
86 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); 86 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
87 87
88 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + 88 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
89 sizeof(MINIDUMP_MEMORY_LIST), 89 sizeof(MINIDUMP_MEMORY_LIST),
90 file_writer.string().size()); 90 string_file.string().size());
91 91
92 const MINIDUMP_MEMORY_LIST* memory_list = nullptr; 92 const MINIDUMP_MEMORY_LIST* memory_list = nullptr;
93 ASSERT_NO_FATAL_FAILURE( 93 ASSERT_NO_FATAL_FAILURE(
94 GetMemoryListStream(file_writer.string(), &memory_list, 1)); 94 GetMemoryListStream(string_file.string(), &memory_list, 1));
95 95
96 EXPECT_EQ(0u, memory_list->NumberOfMemoryRanges); 96 EXPECT_EQ(0u, memory_list->NumberOfMemoryRanges);
97 } 97 }
98 98
99 TEST(MinidumpMemoryWriter, OneMemoryRegion) { 99 TEST(MinidumpMemoryWriter, OneMemoryRegion) {
100 MinidumpFileWriter minidump_file_writer; 100 MinidumpFileWriter minidump_file_writer;
101 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); 101 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter());
102 102
103 const uint64_t kBaseAddress = 0xfedcba9876543210; 103 const uint64_t kBaseAddress = 0xfedcba9876543210;
104 const uint64_t kSize = 0x1000; 104 const uint64_t kSize = 0x1000;
105 const uint8_t kValue = 'm'; 105 const uint8_t kValue = 'm';
106 106
107 auto memory_writer = make_scoped_ptr( 107 auto memory_writer = make_scoped_ptr(
108 new TestMinidumpMemoryWriter(kBaseAddress, kSize, kValue)); 108 new TestMinidumpMemoryWriter(kBaseAddress, kSize, kValue));
109 memory_list_writer->AddMemory(memory_writer.Pass()); 109 memory_list_writer->AddMemory(memory_writer.Pass());
110 110
111 minidump_file_writer.AddStream(memory_list_writer.Pass()); 111 minidump_file_writer.AddStream(memory_list_writer.Pass());
112 112
113 StringFileWriter file_writer; 113 StringFile string_file;
114 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); 114 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
115 115
116 const MINIDUMP_MEMORY_LIST* memory_list = nullptr; 116 const MINIDUMP_MEMORY_LIST* memory_list = nullptr;
117 ASSERT_NO_FATAL_FAILURE( 117 ASSERT_NO_FATAL_FAILURE(
118 GetMemoryListStream(file_writer.string(), &memory_list, 1)); 118 GetMemoryListStream(string_file.string(), &memory_list, 1));
119 119
120 MINIDUMP_MEMORY_DESCRIPTOR expected; 120 MINIDUMP_MEMORY_DESCRIPTOR expected;
121 expected.StartOfMemoryRange = kBaseAddress; 121 expected.StartOfMemoryRange = kBaseAddress;
122 expected.Memory.DataSize = kSize; 122 expected.Memory.DataSize = kSize;
123 expected.Memory.Rva = 123 expected.Memory.Rva =
124 sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + 124 sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
125 sizeof(MINIDUMP_MEMORY_LIST) + 125 sizeof(MINIDUMP_MEMORY_LIST) +
126 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR); 126 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR);
127 ExpectMinidumpMemoryDescriptorAndContents(&expected, 127 ExpectMinidumpMemoryDescriptorAndContents(&expected,
128 &memory_list->MemoryRanges[0], 128 &memory_list->MemoryRanges[0],
129 file_writer.string(), 129 string_file.string(),
130 kValue, 130 kValue,
131 true); 131 true);
132 } 132 }
133 133
134 TEST(MinidumpMemoryWriter, TwoMemoryRegions) { 134 TEST(MinidumpMemoryWriter, TwoMemoryRegions) {
135 MinidumpFileWriter minidump_file_writer; 135 MinidumpFileWriter minidump_file_writer;
136 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); 136 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter());
137 137
138 const uint64_t kBaseAddress0 = 0xc0ffee; 138 const uint64_t kBaseAddress0 = 0xc0ffee;
139 const uint64_t kSize0 = 0x0100; 139 const uint64_t kSize0 = 0x0100;
140 const uint8_t kValue0 = '6'; 140 const uint8_t kValue0 = '6';
141 const uint64_t kBaseAddress1 = 0xfac00fac; 141 const uint64_t kBaseAddress1 = 0xfac00fac;
142 const uint64_t kSize1 = 0x0200; 142 const uint64_t kSize1 = 0x0200;
143 const uint8_t kValue1 = '!'; 143 const uint8_t kValue1 = '!';
144 144
145 auto memory_writer_0 = make_scoped_ptr( 145 auto memory_writer_0 = make_scoped_ptr(
146 new TestMinidumpMemoryWriter(kBaseAddress0, kSize0, kValue0)); 146 new TestMinidumpMemoryWriter(kBaseAddress0, kSize0, kValue0));
147 memory_list_writer->AddMemory(memory_writer_0.Pass()); 147 memory_list_writer->AddMemory(memory_writer_0.Pass());
148 auto memory_writer_1 = make_scoped_ptr( 148 auto memory_writer_1 = make_scoped_ptr(
149 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1)); 149 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1));
150 memory_list_writer->AddMemory(memory_writer_1.Pass()); 150 memory_list_writer->AddMemory(memory_writer_1.Pass());
151 151
152 minidump_file_writer.AddStream(memory_list_writer.Pass()); 152 minidump_file_writer.AddStream(memory_list_writer.Pass());
153 153
154 StringFileWriter file_writer; 154 StringFile string_file;
155 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); 155 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
156 156
157 const MINIDUMP_MEMORY_LIST* memory_list = nullptr; 157 const MINIDUMP_MEMORY_LIST* memory_list = nullptr;
158 ASSERT_NO_FATAL_FAILURE( 158 ASSERT_NO_FATAL_FAILURE(
159 GetMemoryListStream(file_writer.string(), &memory_list, 1)); 159 GetMemoryListStream(string_file.string(), &memory_list, 1));
160 160
161 EXPECT_EQ(2u, memory_list->NumberOfMemoryRanges); 161 EXPECT_EQ(2u, memory_list->NumberOfMemoryRanges);
162 162
163 MINIDUMP_MEMORY_DESCRIPTOR expected; 163 MINIDUMP_MEMORY_DESCRIPTOR expected;
164 164
165 { 165 {
166 SCOPED_TRACE("region 0"); 166 SCOPED_TRACE("region 0");
167 167
168 expected.StartOfMemoryRange = kBaseAddress0; 168 expected.StartOfMemoryRange = kBaseAddress0;
169 expected.Memory.DataSize = kSize0; 169 expected.Memory.DataSize = kSize0;
170 expected.Memory.Rva = 170 expected.Memory.Rva =
171 sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + 171 sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
172 sizeof(MINIDUMP_MEMORY_LIST) + 172 sizeof(MINIDUMP_MEMORY_LIST) +
173 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR); 173 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR);
174 ExpectMinidumpMemoryDescriptorAndContents(&expected, 174 ExpectMinidumpMemoryDescriptorAndContents(&expected,
175 &memory_list->MemoryRanges[0], 175 &memory_list->MemoryRanges[0],
176 file_writer.string(), 176 string_file.string(),
177 kValue0, 177 kValue0,
178 false); 178 false);
179 } 179 }
180 180
181 { 181 {
182 SCOPED_TRACE("region 1"); 182 SCOPED_TRACE("region 1");
183 183
184 expected.StartOfMemoryRange = kBaseAddress1; 184 expected.StartOfMemoryRange = kBaseAddress1;
185 expected.Memory.DataSize = kSize1; 185 expected.Memory.DataSize = kSize1;
186 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva + 186 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva +
187 memory_list->MemoryRanges[0].Memory.DataSize; 187 memory_list->MemoryRanges[0].Memory.DataSize;
188 ExpectMinidumpMemoryDescriptorAndContents(&expected, 188 ExpectMinidumpMemoryDescriptorAndContents(&expected,
189 &memory_list->MemoryRanges[1], 189 &memory_list->MemoryRanges[1],
190 file_writer.string(), 190 string_file.string(),
191 kValue1, 191 kValue1,
192 true); 192 true);
193 } 193 }
194 } 194 }
195 195
196 class TestMemoryStream final : public internal::MinidumpStreamWriter { 196 class TestMemoryStream final : public internal::MinidumpStreamWriter {
197 public: 197 public:
198 TestMemoryStream(uint64_t base_address, size_t size, uint8_t value) 198 TestMemoryStream(uint64_t base_address, size_t size, uint8_t value)
199 : MinidumpStreamWriter(), memory_(base_address, size, value) {} 199 : MinidumpStreamWriter(), memory_(base_address, size, value) {}
200 200
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 const uint64_t kBaseAddress1 = 0x2000; 253 const uint64_t kBaseAddress1 = 0x2000;
254 const size_t kSize1 = 0x0400; 254 const size_t kSize1 = 0x0400;
255 const uint8_t kValue1 = 'm'; 255 const uint8_t kValue1 = 'm';
256 256
257 auto memory_writer = make_scoped_ptr( 257 auto memory_writer = make_scoped_ptr(
258 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1)); 258 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1));
259 memory_list_writer->AddMemory(memory_writer.Pass()); 259 memory_list_writer->AddMemory(memory_writer.Pass());
260 260
261 minidump_file_writer.AddStream(memory_list_writer.Pass()); 261 minidump_file_writer.AddStream(memory_list_writer.Pass());
262 262
263 StringFileWriter file_writer; 263 StringFile string_file;
264 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); 264 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
265 265
266 const MINIDUMP_MEMORY_LIST* memory_list = nullptr; 266 const MINIDUMP_MEMORY_LIST* memory_list = nullptr;
267 ASSERT_NO_FATAL_FAILURE( 267 ASSERT_NO_FATAL_FAILURE(
268 GetMemoryListStream(file_writer.string(), &memory_list, 2)); 268 GetMemoryListStream(string_file.string(), &memory_list, 2));
269 269
270 EXPECT_EQ(2u, memory_list->NumberOfMemoryRanges); 270 EXPECT_EQ(2u, memory_list->NumberOfMemoryRanges);
271 271
272 MINIDUMP_MEMORY_DESCRIPTOR expected; 272 MINIDUMP_MEMORY_DESCRIPTOR expected;
273 273
274 { 274 {
275 SCOPED_TRACE("region 0"); 275 SCOPED_TRACE("region 0");
276 276
277 expected.StartOfMemoryRange = kBaseAddress0; 277 expected.StartOfMemoryRange = kBaseAddress0;
278 expected.Memory.DataSize = kSize0; 278 expected.Memory.DataSize = kSize0;
279 expected.Memory.Rva = 279 expected.Memory.Rva =
280 sizeof(MINIDUMP_HEADER) + 2 * sizeof(MINIDUMP_DIRECTORY) + 280 sizeof(MINIDUMP_HEADER) + 2 * sizeof(MINIDUMP_DIRECTORY) +
281 sizeof(MINIDUMP_MEMORY_LIST) + 281 sizeof(MINIDUMP_MEMORY_LIST) +
282 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR); 282 memory_list->NumberOfMemoryRanges * sizeof(MINIDUMP_MEMORY_DESCRIPTOR);
283 ExpectMinidumpMemoryDescriptorAndContents(&expected, 283 ExpectMinidumpMemoryDescriptorAndContents(&expected,
284 &memory_list->MemoryRanges[0], 284 &memory_list->MemoryRanges[0],
285 file_writer.string(), 285 string_file.string(),
286 kValue0, 286 kValue0,
287 false); 287 false);
288 } 288 }
289 289
290 { 290 {
291 SCOPED_TRACE("region 1"); 291 SCOPED_TRACE("region 1");
292 292
293 expected.StartOfMemoryRange = kBaseAddress1; 293 expected.StartOfMemoryRange = kBaseAddress1;
294 expected.Memory.DataSize = kSize1; 294 expected.Memory.DataSize = kSize1;
295 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva + 295 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva +
296 memory_list->MemoryRanges[0].Memory.DataSize; 296 memory_list->MemoryRanges[0].Memory.DataSize;
297 ExpectMinidumpMemoryDescriptorAndContents(&expected, 297 ExpectMinidumpMemoryDescriptorAndContents(&expected,
298 &memory_list->MemoryRanges[1], 298 &memory_list->MemoryRanges[1],
299 file_writer.string(), 299 string_file.string(),
300 kValue1, 300 kValue1,
301 true); 301 true);
302 } 302 }
303 } 303 }
304 304
305 TEST(MinidumpMemoryWriter, AddFromSnapshot) { 305 TEST(MinidumpMemoryWriter, AddFromSnapshot) {
306 MINIDUMP_MEMORY_DESCRIPTOR expect_memory_descriptors[3] = {}; 306 MINIDUMP_MEMORY_DESCRIPTOR expect_memory_descriptors[3] = {};
307 uint8_t values[arraysize(expect_memory_descriptors)] = {}; 307 uint8_t values[arraysize(expect_memory_descriptors)] = {};
308 308
309 expect_memory_descriptors[0].StartOfMemoryRange = 0; 309 expect_memory_descriptors[0].StartOfMemoryRange = 0;
(...skipping 21 matching lines...) Expand all
331 memory_snapshot->SetValue(values[index]); 331 memory_snapshot->SetValue(values[index]);
332 memory_snapshots.push_back(memory_snapshot); 332 memory_snapshots.push_back(memory_snapshot);
333 } 333 }
334 334
335 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); 335 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter());
336 memory_list_writer->AddFromSnapshot(memory_snapshots); 336 memory_list_writer->AddFromSnapshot(memory_snapshots);
337 337
338 MinidumpFileWriter minidump_file_writer; 338 MinidumpFileWriter minidump_file_writer;
339 minidump_file_writer.AddStream(memory_list_writer.Pass()); 339 minidump_file_writer.AddStream(memory_list_writer.Pass());
340 340
341 StringFileWriter file_writer; 341 StringFile string_file;
342 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); 342 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
343 343
344 const MINIDUMP_MEMORY_LIST* memory_list = nullptr; 344 const MINIDUMP_MEMORY_LIST* memory_list = nullptr;
345 ASSERT_NO_FATAL_FAILURE( 345 ASSERT_NO_FATAL_FAILURE(
346 GetMemoryListStream(file_writer.string(), &memory_list, 1)); 346 GetMemoryListStream(string_file.string(), &memory_list, 1));
347 347
348 ASSERT_EQ(3u, memory_list->NumberOfMemoryRanges); 348 ASSERT_EQ(3u, memory_list->NumberOfMemoryRanges);
349 349
350 for (size_t index = 0; index < memory_list->NumberOfMemoryRanges; ++index) { 350 for (size_t index = 0; index < memory_list->NumberOfMemoryRanges; ++index) {
351 SCOPED_TRACE(base::StringPrintf("index %" PRIuS, index)); 351 SCOPED_TRACE(base::StringPrintf("index %" PRIuS, index));
352 ExpectMinidumpMemoryDescriptorAndContents( 352 ExpectMinidumpMemoryDescriptorAndContents(
353 &expect_memory_descriptors[index], 353 &expect_memory_descriptors[index],
354 &memory_list->MemoryRanges[index], 354 &memory_list->MemoryRanges[index],
355 file_writer.string(), 355 string_file.string(),
356 values[index], 356 values[index],
357 index == memory_list->NumberOfMemoryRanges - 1); 357 index == memory_list->NumberOfMemoryRanges - 1);
358 } 358 }
359 } 359 }
360 360
361 } // namespace 361 } // namespace
362 } // namespace test 362 } // namespace test
363 } // namespace crashpad 363 } // namespace crashpad
OLDNEW
« no previous file with comments | « minidump/minidump_location_descriptor_list_writer_test.cc ('k') | minidump/minidump_misc_info_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698