OLD | NEW |
(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_location_descriptor_list_writer.h" |
| 16 |
| 17 #include "base/strings/stringprintf.h" |
| 18 #include "gtest/gtest.h" |
| 19 #include "minidump/test/minidump_writable_test_util.h" |
| 20 #include "util/file/string_file_writer.h" |
| 21 |
| 22 namespace crashpad { |
| 23 namespace test { |
| 24 namespace { |
| 25 |
| 26 class TestMinidumpWritable final : public internal::MinidumpWritable { |
| 27 public: |
| 28 explicit TestMinidumpWritable(uint32_t value) |
| 29 : MinidumpWritable(), |
| 30 value_(value) { |
| 31 } |
| 32 |
| 33 ~TestMinidumpWritable() override {} |
| 34 |
| 35 protected: |
| 36 // MinidumpWritable: |
| 37 |
| 38 size_t SizeOfObject() override { return sizeof(value_); } |
| 39 |
| 40 bool WriteObject(FileWriterInterface* file_writer) override { |
| 41 return file_writer->Write(&value_, sizeof(value_)); |
| 42 } |
| 43 |
| 44 private: |
| 45 uint32_t value_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(TestMinidumpWritable); |
| 48 }; |
| 49 |
| 50 class TestMinidumpLocationDescriptorListWriter final |
| 51 : public MinidumpLocationDescriptorListWriter { |
| 52 public: |
| 53 TestMinidumpLocationDescriptorListWriter() |
| 54 : MinidumpLocationDescriptorListWriter() { |
| 55 } |
| 56 |
| 57 ~TestMinidumpLocationDescriptorListWriter() {} |
| 58 |
| 59 void AddChild(scoped_ptr<TestMinidumpWritable> child) { |
| 60 MinidumpLocationDescriptorListWriter::AddChild(child.Pass()); |
| 61 } |
| 62 |
| 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(TestMinidumpLocationDescriptorListWriter); |
| 65 }; |
| 66 |
| 67 const MinidumpLocationDescriptorList* MinidumpLocationDescriptorListAtStart( |
| 68 const std::string& file_contents, |
| 69 size_t count) { |
| 70 MINIDUMP_LOCATION_DESCRIPTOR location_descriptor; |
| 71 location_descriptor.DataSize = sizeof(MinidumpLocationDescriptorList) + |
| 72 count * sizeof(MINIDUMP_LOCATION_DESCRIPTOR); |
| 73 location_descriptor.Rva = 0; |
| 74 return MinidumpWritableAtLocationDescriptor<MinidumpLocationDescriptorList>( |
| 75 file_contents, location_descriptor); |
| 76 } |
| 77 |
| 78 TEST(MinidumpLocationDescriptorListWriter, Empty) { |
| 79 TestMinidumpLocationDescriptorListWriter list_writer; |
| 80 |
| 81 StringFileWriter file_writer; |
| 82 |
| 83 ASSERT_TRUE(list_writer.WriteEverything(&file_writer)); |
| 84 EXPECT_EQ(sizeof(MinidumpLocationDescriptorList), |
| 85 file_writer.string().size()); |
| 86 |
| 87 const MinidumpLocationDescriptorList* list = |
| 88 MinidumpLocationDescriptorListAtStart(file_writer.string(), 0); |
| 89 ASSERT_TRUE(list); |
| 90 |
| 91 EXPECT_EQ(0u, list->count); |
| 92 } |
| 93 |
| 94 TEST(MinidumpLocationDescriptorListWriter, OneChild) { |
| 95 TestMinidumpLocationDescriptorListWriter list_writer; |
| 96 |
| 97 const uint32_t kValue = 0; |
| 98 auto child_writable = make_scoped_ptr(new TestMinidumpWritable(kValue)); |
| 99 list_writer.AddChild(child_writable.Pass()); |
| 100 |
| 101 StringFileWriter file_writer; |
| 102 |
| 103 ASSERT_TRUE(list_writer.WriteEverything(&file_writer)); |
| 104 |
| 105 const MinidumpLocationDescriptorList* list = |
| 106 MinidumpLocationDescriptorListAtStart(file_writer.string(), 1); |
| 107 ASSERT_TRUE(list); |
| 108 |
| 109 ASSERT_EQ(1u, list->count); |
| 110 |
| 111 const uint32_t* child = MinidumpWritableAtLocationDescriptor<uint32_t>( |
| 112 file_writer.string(), list->children[0]); |
| 113 ASSERT_TRUE(child); |
| 114 EXPECT_EQ(kValue, *child); |
| 115 } |
| 116 |
| 117 TEST(MinidumpLocationDescriptorListWriter, ThreeChildren) { |
| 118 TestMinidumpLocationDescriptorListWriter list_writer; |
| 119 |
| 120 const uint32_t kValues[] = { 0x80000000, 0x55555555, 0x66006600 }; |
| 121 |
| 122 auto child_writable_0 = make_scoped_ptr(new TestMinidumpWritable(kValues[0])); |
| 123 list_writer.AddChild(child_writable_0.Pass()); |
| 124 |
| 125 auto child_writable_1 = make_scoped_ptr(new TestMinidumpWritable(kValues[1])); |
| 126 list_writer.AddChild(child_writable_1.Pass()); |
| 127 |
| 128 auto child_writable_2 = make_scoped_ptr(new TestMinidumpWritable(kValues[2])); |
| 129 list_writer.AddChild(child_writable_2.Pass()); |
| 130 |
| 131 StringFileWriter file_writer; |
| 132 |
| 133 ASSERT_TRUE(list_writer.WriteEverything(&file_writer)); |
| 134 |
| 135 const MinidumpLocationDescriptorList* list = |
| 136 MinidumpLocationDescriptorListAtStart(file_writer.string(), 3); |
| 137 ASSERT_TRUE(list); |
| 138 |
| 139 ASSERT_EQ(arraysize(kValues), list->count); |
| 140 |
| 141 for (size_t index = 0; index < arraysize(kValues); ++index) { |
| 142 SCOPED_TRACE(base::StringPrintf("index %zu", index)); |
| 143 |
| 144 const uint32_t* child = MinidumpWritableAtLocationDescriptor<uint32_t>( |
| 145 file_writer.string(), list->children[index]); |
| 146 ASSERT_TRUE(child); |
| 147 EXPECT_EQ(kValues[index], *child); |
| 148 } |
| 149 } |
| 150 |
| 151 } // namespace |
| 152 } // namespace test |
| 153 } // namespace crashpad |
OLD | NEW |