Chromium Code Reviews| Index: minidump/minidump_memory_info_writer_test.cc |
| diff --git a/minidump/minidump_memory_info_writer_test.cc b/minidump/minidump_memory_info_writer_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2a5ac792a577ded884ca2898a8f2128940149b44 |
| --- /dev/null |
| +++ b/minidump/minidump_memory_info_writer_test.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "minidump/minidump_memory_info_writer.h" |
| + |
| +#include <string> |
| + |
| +#include "gtest/gtest.h" |
| +#include "minidump/minidump_file_writer.h" |
| +#include "minidump/test/minidump_file_writer_test_util.h" |
| +#include "minidump/test/minidump_writable_test_util.h" |
| +#include "snapshot/test/test_memory_map_region_snapshot.h" |
| +#include "util/file/string_file.h" |
| + |
| +namespace crashpad { |
| +namespace test { |
| +namespace { |
| + |
| +const MinidumpStreamType kBogusStreamType = |
|
Mark Mentovai
2015/10/12 14:34:47
This never comes into play, you can get rid of eve
scottmg
2015/10/13 19:51:29
Done.
|
| + static_cast<MinidumpStreamType>(1234); |
| + |
| +// expected_streams is the expected number of streams in the file. The memory |
| +// info list must be the last stream. If there is another stream, it must come |
| +// first, have stream type kBogusStreamType, and have zero-length data. |
| +void GetMemoryInfoListStream(const std::string& file_contents, |
| + const MINIDUMP_MEMORY_INFO_LIST** memory_info_list, |
| + const uint32_t expected_streams) { |
| + const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER); |
| + const size_t kMemoryInfoListStreamOffset = |
| + kDirectoryOffset + expected_streams * sizeof(MINIDUMP_DIRECTORY); |
| + |
| + const MINIDUMP_DIRECTORY* directory; |
| + const MINIDUMP_HEADER* header = |
| + MinidumpHeaderAtStart(file_contents, &directory); |
| + ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, expected_streams, 0)); |
| + ASSERT_TRUE(directory); |
| + |
| + size_t directory_index = 0; |
| + if (expected_streams > 1) { |
| + ASSERT_EQ(kBogusStreamType, directory[directory_index].StreamType); |
| + ASSERT_EQ(0u, directory[directory_index].Location.DataSize); |
| + ASSERT_EQ(kMemoryInfoListStreamOffset, |
| + directory[directory_index].Location.Rva); |
| + ++directory_index; |
| + } |
| + |
| + ASSERT_EQ(kMinidumpStreamTypeMemoryInfoList, |
| + directory[directory_index].StreamType); |
| + EXPECT_EQ(kMemoryInfoListStreamOffset, |
| + directory[directory_index].Location.Rva); |
| + |
| + *memory_info_list = |
| + MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_INFO_LIST>( |
| + file_contents, directory[directory_index].Location); |
| + ASSERT_TRUE(memory_info_list); |
| +} |
| + |
| +TEST(MinidumpMemoryInfoWriter, Empty) { |
| + MinidumpFileWriter minidump_file_writer; |
| + auto memory_info_list_writer = |
| + make_scoped_ptr(new MinidumpMemoryInfoListWriter()); |
| + minidump_file_writer.AddStream(memory_info_list_writer.Pass()); |
| + |
| + StringFile string_file; |
| + ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| + |
| + ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| + sizeof(MINIDUMP_MEMORY_INFO_LIST), |
| + string_file.string().size()); |
| + |
| + const MINIDUMP_MEMORY_INFO_LIST* memory_info_list = nullptr; |
| + ASSERT_NO_FATAL_FAILURE( |
| + GetMemoryInfoListStream(string_file.string(), &memory_info_list, 1)); |
| + |
| + EXPECT_EQ(0u, memory_info_list->NumberOfEntries); |
| +} |
| + |
| +TEST(MinidumpMemoryInfoWriter, OneRegion) { |
| + MinidumpFileWriter minidump_file_writer; |
| + auto memory_info_list_writer = |
| + make_scoped_ptr(new MinidumpMemoryInfoListWriter()); |
| + |
| + auto memory_map_region = make_scoped_ptr(new TestMemoryMapRegionSnapshot()); |
| + |
| + MINIDUMP_MEMORY_INFO mmi = {0}; |
| + mmi.BaseAddress = 0x12340000; |
| + mmi.AllocationBase = 0x12000000; |
| + mmi.AllocationProtect = PAGE_READWRITE; |
| + mmi.RegionSize = 0x6000; |
| + mmi.State = MEM_COMMIT; |
| + mmi.Protect = PAGE_NOACCESS; |
| + mmi.Type = MEM_PRIVATE; |
| + memory_map_region->SetMindumpMemoryInfo(mmi); |
| + |
| + std::vector<const MemoryMapRegionSnapshot*> memory_map; |
| + memory_map.push_back(memory_map_region.get()); |
| + memory_info_list_writer->InitializeFromSnapshot(memory_map); |
| + |
| + minidump_file_writer.AddStream(memory_info_list_writer.Pass()); |
| + |
| + StringFile string_file; |
| + ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| + |
| + ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| + sizeof(MINIDUMP_MEMORY_INFO_LIST) + |
| + sizeof(MINIDUMP_MEMORY_INFO), |
| + string_file.string().size()); |
| + |
| + const MINIDUMP_MEMORY_INFO_LIST* memory_info_list = nullptr; |
| + ASSERT_NO_FATAL_FAILURE( |
| + GetMemoryInfoListStream(string_file.string(), &memory_info_list, 1)); |
| + |
| + EXPECT_EQ(1u, memory_info_list->NumberOfEntries); |
| + const MINIDUMP_MEMORY_INFO* memory_info = |
| + reinterpret_cast<const MINIDUMP_MEMORY_INFO*>(&memory_info_list[1]); |
| + EXPECT_EQ(mmi.BaseAddress, memory_info->BaseAddress); |
| + EXPECT_EQ(mmi.AllocationBase, memory_info->AllocationBase); |
| + EXPECT_EQ(mmi.AllocationProtect, memory_info->AllocationProtect); |
| + EXPECT_EQ(mmi.RegionSize, memory_info->RegionSize); |
| + EXPECT_EQ(mmi.State, memory_info->State); |
| + EXPECT_EQ(mmi.Protect, memory_info->Protect); |
| + EXPECT_EQ(mmi.Type, memory_info->Type); |
| +} |
| + |
| +} // namespace |
| +} // namespace test |
| +} // namespace crashpad |