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_handle_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_string_writer_test_util.h" |
| 23 #include "minidump/test/minidump_writable_test_util.h" |
| 24 #include "util/file/string_file.h" |
| 25 |
| 26 namespace crashpad { |
| 27 namespace test { |
| 28 namespace { |
| 29 |
| 30 // The handle data stream is expected to be the only stream. |
| 31 void GetHandleDataStream( |
| 32 const std::string& file_contents, |
| 33 const MINIDUMP_HANDLE_DATA_STREAM** handle_data_stream) { |
| 34 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER); |
| 35 const size_t kHandleDataStreamOffset = |
| 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(kMinidumpStreamTypeHandleData, |
| 47 directory[kDirectoryIndex].StreamType); |
| 48 EXPECT_EQ(kHandleDataStreamOffset, directory[kDirectoryIndex].Location.Rva); |
| 49 |
| 50 *handle_data_stream = |
| 51 MinidumpWritableAtLocationDescriptor<MINIDUMP_HANDLE_DATA_STREAM>( |
| 52 file_contents, directory[kDirectoryIndex].Location); |
| 53 ASSERT_TRUE(*handle_data_stream); |
| 54 } |
| 55 |
| 56 TEST(MinidumpHandleDataWriter, Empty) { |
| 57 MinidumpFileWriter minidump_file_writer; |
| 58 auto handle_data_writer = make_scoped_ptr(new MinidumpHandleDataWriter()); |
| 59 minidump_file_writer.AddStream(handle_data_writer.Pass()); |
| 60 |
| 61 StringFile string_file; |
| 62 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| 63 |
| 64 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| 65 sizeof(MINIDUMP_HANDLE_DATA_STREAM), |
| 66 string_file.string().size()); |
| 67 |
| 68 const MINIDUMP_HANDLE_DATA_STREAM* handle_data_stream = nullptr; |
| 69 ASSERT_NO_FATAL_FAILURE( |
| 70 GetHandleDataStream(string_file.string(), &handle_data_stream)); |
| 71 |
| 72 EXPECT_EQ(0u, handle_data_stream->NumberOfDescriptors); |
| 73 } |
| 74 |
| 75 TEST(MinidumpHandleDataWriter, OneHandle) { |
| 76 MinidumpFileWriter minidump_file_writer; |
| 77 auto handle_data_writer = make_scoped_ptr(new MinidumpHandleDataWriter()); |
| 78 |
| 79 HandleSnapshot handle_snapshot; |
| 80 handle_snapshot.handle = 0x1234; |
| 81 handle_snapshot.type_name = L"Something"; |
| 82 handle_snapshot.attributes = 0x12345678; |
| 83 handle_snapshot.granted_access = 0x9abcdef0; |
| 84 handle_snapshot.pointer_count = 4567; |
| 85 handle_snapshot.handle_count = 9876; |
| 86 |
| 87 std::vector<HandleSnapshot> snapshot; |
| 88 snapshot.push_back(handle_snapshot); |
| 89 |
| 90 handle_data_writer->InitializeFromSnapshot(snapshot); |
| 91 |
| 92 minidump_file_writer.AddStream(handle_data_writer.Pass()); |
| 93 |
| 94 StringFile string_file; |
| 95 ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file)); |
| 96 |
| 97 const size_t kTypeNameStringDataLength = |
| 98 (handle_snapshot.type_name.size() + 1) * |
| 99 sizeof(handle_snapshot.type_name[0]); |
| 100 ASSERT_EQ(sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) + |
| 101 sizeof(MINIDUMP_HANDLE_DATA_STREAM) + |
| 102 sizeof(MINIDUMP_HANDLE_DESCRIPTOR) + sizeof(MINIDUMP_STRING) + |
| 103 kTypeNameStringDataLength, |
| 104 string_file.string().size()); |
| 105 |
| 106 const MINIDUMP_HANDLE_DATA_STREAM* handle_data_stream = nullptr; |
| 107 ASSERT_NO_FATAL_FAILURE( |
| 108 GetHandleDataStream(string_file.string(), &handle_data_stream)); |
| 109 |
| 110 EXPECT_EQ(1u, handle_data_stream->NumberOfDescriptors); |
| 111 const MINIDUMP_HANDLE_DESCRIPTOR* handle_descriptor = |
| 112 reinterpret_cast<const MINIDUMP_HANDLE_DESCRIPTOR*>( |
| 113 &handle_data_stream[1]); |
| 114 EXPECT_EQ(handle_snapshot.handle, handle_descriptor->Handle); |
| 115 EXPECT_EQ(handle_snapshot.type_name, |
| 116 MinidumpStringAtRVAAsString(string_file.string(), |
| 117 handle_descriptor->TypeNameRva)); |
| 118 EXPECT_EQ(0u, handle_descriptor->ObjectNameRva); |
| 119 EXPECT_EQ(handle_snapshot.attributes, handle_descriptor->Attributes); |
| 120 EXPECT_EQ(handle_snapshot.granted_access, handle_descriptor->GrantedAccess); |
| 121 EXPECT_EQ(handle_snapshot.handle_count, handle_descriptor->HandleCount); |
| 122 EXPECT_EQ(handle_snapshot.pointer_count, handle_descriptor->PointerCount); |
| 123 } |
| 124 |
| 125 } // namespace |
| 126 } // namespace test |
| 127 } // namespace crashpad |
OLD | NEW |