OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_memory_info_writer.h" |
| 16 |
| 17 #include <string> |
| 18 |
| 19 #include "gtest/gtest.h" |
| 20 #include "minidump/minidump_file_writer.h" |
| 21 #include "minidump/test/minidump_file_writer_test_util.h" |
| 22 #include "minidump/test/minidump_writable_test_util.h" |
| 23 #include "snapshot/test/test_memory_map_region_snapshot.h" |
| 24 #include "util/file/string_file.h" |
| 25 |
| 26 namespace crashpad { |
| 27 namespace test { |
| 28 namespace { |
| 29 |
| 30 // The memory info list is expected to be the only stream. |
| 31 void GetMemoryInfoListStream( |
| 32 const std::string& file_contents, |
| 33 const MINIDUMP_MEMORY_INFO_LIST** memory_info_list) { |
| 34 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER); |
| 35 const size_t kMemoryInfoListStreamOffset = |
| 36 kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY); |
| 37 |
| 38 const MINIDUMP_DIRECTORY* directory; |
| 39 const MINIDUMP_HEADER* header = |
| 40 MinidumpHeaderAtStart(file_contents, &directory); |
| 41 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0)); |
| 42 ASSERT_TRUE(directory); |
| 43 |
| 44 const size_t kDirectoryIndex = 0; |
| 45 |
| 46 ASSERT_EQ(kMinidumpStreamTypeMemoryInfoList, |
| 47 directory[kDirectoryIndex].StreamType); |
| 48 EXPECT_EQ(kMemoryInfoListStreamOffset, |
| 49 directory[kDirectoryIndex].Location.Rva); |
| 50 |
| 51 *memory_info_list = |
| 52 MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_INFO_LIST>( |
| 53 file_contents, directory[kDirectoryIndex].Location); |
| 54 ASSERT_TRUE(memory_info_list); |
| 55 } |
| 56 |
| 57 TEST(MinidumpMemoryInfoWriter, Empty) { |
| 58 MinidumpFileWriter minidump_file_writer; |
| 59 auto memory_info_list_writer = |
| 60 make_scoped_ptr(new MinidumpMemoryInfoListWriter()); |
| 61 minidump_file_writer.AddStream(memory_info_list_writer.Pass()); |
| 62 |
| 63 StringFile string_file; |
| 64 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| 65 |
| 66 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| 67 sizeof(MINIDUMP_MEMORY_INFO_LIST), |
| 68 string_file.string().size()); |
| 69 |
| 70 const MINIDUMP_MEMORY_INFO_LIST* memory_info_list = nullptr; |
| 71 ASSERT_NO_FATAL_FAILURE( |
| 72 GetMemoryInfoListStream(string_file.string(), &memory_info_list)); |
| 73 |
| 74 EXPECT_EQ(0u, memory_info_list->NumberOfEntries); |
| 75 } |
| 76 |
| 77 TEST(MinidumpMemoryInfoWriter, OneRegion) { |
| 78 MinidumpFileWriter minidump_file_writer; |
| 79 auto memory_info_list_writer = |
| 80 make_scoped_ptr(new MinidumpMemoryInfoListWriter()); |
| 81 |
| 82 auto memory_map_region = make_scoped_ptr(new TestMemoryMapRegionSnapshot()); |
| 83 |
| 84 MINIDUMP_MEMORY_INFO mmi = {0}; |
| 85 mmi.BaseAddress = 0x12340000; |
| 86 mmi.AllocationBase = 0x12000000; |
| 87 mmi.AllocationProtect = PAGE_READWRITE; |
| 88 mmi.RegionSize = 0x6000; |
| 89 mmi.State = MEM_COMMIT; |
| 90 mmi.Protect = PAGE_NOACCESS; |
| 91 mmi.Type = MEM_PRIVATE; |
| 92 memory_map_region->SetMindumpMemoryInfo(mmi); |
| 93 |
| 94 std::vector<const MemoryMapRegionSnapshot*> memory_map; |
| 95 memory_map.push_back(memory_map_region.get()); |
| 96 memory_info_list_writer->InitializeFromSnapshot(memory_map); |
| 97 |
| 98 minidump_file_writer.AddStream(memory_info_list_writer.Pass()); |
| 99 |
| 100 StringFile string_file; |
| 101 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| 102 |
| 103 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| 104 sizeof(MINIDUMP_MEMORY_INFO_LIST) + |
| 105 sizeof(MINIDUMP_MEMORY_INFO), |
| 106 string_file.string().size()); |
| 107 |
| 108 const MINIDUMP_MEMORY_INFO_LIST* memory_info_list = nullptr; |
| 109 ASSERT_NO_FATAL_FAILURE( |
| 110 GetMemoryInfoListStream(string_file.string(), &memory_info_list)); |
| 111 |
| 112 EXPECT_EQ(1u, memory_info_list->NumberOfEntries); |
| 113 const MINIDUMP_MEMORY_INFO* memory_info = |
| 114 reinterpret_cast<const MINIDUMP_MEMORY_INFO*>(&memory_info_list[1]); |
| 115 EXPECT_EQ(mmi.BaseAddress, memory_info->BaseAddress); |
| 116 EXPECT_EQ(mmi.AllocationBase, memory_info->AllocationBase); |
| 117 EXPECT_EQ(mmi.AllocationProtect, memory_info->AllocationProtect); |
| 118 EXPECT_EQ(mmi.RegionSize, memory_info->RegionSize); |
| 119 EXPECT_EQ(mmi.State, memory_info->State); |
| 120 EXPECT_EQ(mmi.Protect, memory_info->Protect); |
| 121 EXPECT_EQ(mmi.Type, memory_info->Type); |
| 122 } |
| 123 |
| 124 } // namespace |
| 125 } // namespace test |
| 126 } // namespace crashpad |
OLD | NEW |