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

Side by Side Diff: minidump/minidump_file_writer_test.cc

Issue 670853002: minidump: Migrate the rest of the tests to MinidumpWritableAtLocationDescriptor<>() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review feedback Created 6 years, 2 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
« no previous file with comments | « minidump/minidump_exception_writer_test.cc ('k') | minidump/minidump_memory_writer_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "minidump/minidump_file_writer.h" 15 #include "minidump/minidump_file_writer.h"
16 16
17 #include <dbghelp.h> 17 #include <dbghelp.h>
18 18
19 #include <string> 19 #include <string>
20 20
21 #include "base/basictypes.h" 21 #include "base/basictypes.h"
22 #include "gtest/gtest.h" 22 #include "gtest/gtest.h"
23 #include "minidump/minidump_stream_writer.h" 23 #include "minidump/minidump_stream_writer.h"
24 #include "minidump/minidump_writable.h" 24 #include "minidump/minidump_writable.h"
25 #include "minidump/test/minidump_file_writer_test_util.h" 25 #include "minidump/test/minidump_file_writer_test_util.h"
26 #include "minidump/test/minidump_writable_test_util.h"
26 #include "util/file/file_writer.h" 27 #include "util/file/file_writer.h"
27 #include "util/file/string_file_writer.h" 28 #include "util/file/string_file_writer.h"
28 29
29 namespace crashpad { 30 namespace crashpad {
30 namespace test { 31 namespace test {
31 namespace { 32 namespace {
32 33
33 TEST(MinidumpFileWriter, Empty) { 34 TEST(MinidumpFileWriter, Empty) {
34 MinidumpFileWriter minidump_file; 35 MinidumpFileWriter minidump_file;
35 StringFileWriter file_writer; 36 StringFileWriter file_writer;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 const MINIDUMP_DIRECTORY* directory; 100 const MINIDUMP_DIRECTORY* directory;
100 const MINIDUMP_HEADER* header = 101 const MINIDUMP_HEADER* header =
101 MinidumpHeaderAtStart(file_writer.string(), &directory); 102 MinidumpHeaderAtStart(file_writer.string(), &directory);
102 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, kTimestamp)); 103 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, kTimestamp));
103 ASSERT_TRUE(directory); 104 ASSERT_TRUE(directory);
104 105
105 EXPECT_EQ(kStreamType, directory[0].StreamType); 106 EXPECT_EQ(kStreamType, directory[0].StreamType);
106 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize); 107 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
107 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva); 108 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
108 109
109 const uint8_t* stream_data = 110 const uint8_t* stream_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
110 reinterpret_cast<const uint8_t*>(&file_writer.string()[kStreamOffset]); 111 file_writer.string(), directory[0].Location);
112 ASSERT_TRUE(stream_data);
111 113
112 std::string expected_stream(kStreamSize, kStreamValue); 114 std::string expected_stream(kStreamSize, kStreamValue);
113 EXPECT_EQ(0, memcmp(stream_data, expected_stream.c_str(), kStreamSize)); 115 EXPECT_EQ(0, memcmp(stream_data, expected_stream.c_str(), kStreamSize));
114 } 116 }
115 117
116 TEST(MinidumpFileWriter, ThreeStreams) { 118 TEST(MinidumpFileWriter, ThreeStreams) {
117 MinidumpFileWriter minidump_file; 119 MinidumpFileWriter minidump_file;
118 const time_t kTimestamp = 0x155d2fb8; 120 const time_t kTimestamp = 0x155d2fb8;
119 minidump_file.SetTimestamp(kTimestamp); 121 minidump_file.SetTimestamp(kTimestamp);
120 122
121 const size_t kStream1Size = 5; 123 const size_t kStream0Size = 5;
122 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x6d); 124 const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x6d);
123 const uint8_t kStream1Value = 0x5a; 125 const uint8_t kStream0Value = 0x5a;
124 TestStream stream1(kStream1Type, kStream1Size, kStream1Value); 126 TestStream stream0(kStream0Type, kStream0Size, kStream0Value);
125 minidump_file.AddStream(&stream1); 127 minidump_file.AddStream(&stream0);
126 128
127 // Make the second stream’s type be a smaller quantity than the first stream’s 129 // Make the second stream’s type be a smaller quantity than the first stream’s
128 // to test that the streams show up in the order that they were added, not in 130 // to test that the streams show up in the order that they were added, not in
129 // numeric order. 131 // numeric order.
130 const size_t kStream2Size = 3; 132 const size_t kStream1Size = 3;
131 const MinidumpStreamType kStream2Type = static_cast<MinidumpStreamType>(0x4d); 133 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
132 const uint8_t kStream2Value = 0xa5; 134 const uint8_t kStream1Value = 0xa5;
135 TestStream stream1(kStream1Type, kStream1Size, kStream1Value);
136 minidump_file.AddStream(&stream1);
137
138 const size_t kStream2Size = 1;
139 const MinidumpStreamType kStream2Type = static_cast<MinidumpStreamType>(0x7e);
140 const uint8_t kStream2Value = 0x36;
133 TestStream stream2(kStream2Type, kStream2Size, kStream2Value); 141 TestStream stream2(kStream2Type, kStream2Size, kStream2Value);
134 minidump_file.AddStream(&stream2); 142 minidump_file.AddStream(&stream2);
135 143
136 const size_t kStream3Size = 1;
137 const MinidumpStreamType kStream3Type = static_cast<MinidumpStreamType>(0x7e);
138 const uint8_t kStream3Value = 0x36;
139 TestStream stream3(kStream3Type, kStream3Size, kStream3Value);
140 minidump_file.AddStream(&stream3);
141
142 StringFileWriter file_writer; 144 StringFileWriter file_writer;
143 ASSERT_TRUE(minidump_file.WriteEverything(&file_writer)); 145 ASSERT_TRUE(minidump_file.WriteEverything(&file_writer));
144 146
145 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER); 147 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
146 const size_t kStream1Offset = 148 const size_t kStream0Offset =
147 kDirectoryOffset + 3 * sizeof(MINIDUMP_DIRECTORY); 149 kDirectoryOffset + 3 * sizeof(MINIDUMP_DIRECTORY);
148 const size_t kStream2Padding = 3; 150 const size_t kStream1Padding = 3;
151 const size_t kStream1Offset = kStream0Offset + kStream0Size + kStream1Padding;
152 const size_t kStream2Padding = 1;
149 const size_t kStream2Offset = kStream1Offset + kStream1Size + kStream2Padding; 153 const size_t kStream2Offset = kStream1Offset + kStream1Size + kStream2Padding;
150 const size_t kStream3Padding = 1; 154 const size_t kFileSize = kStream2Offset + kStream2Size;
151 const size_t kStream3Offset = kStream2Offset + kStream2Size + kStream3Padding;
152 const size_t kFileSize = kStream3Offset + kStream3Size;
153 155
154 ASSERT_EQ(kFileSize, file_writer.string().size()); 156 ASSERT_EQ(kFileSize, file_writer.string().size());
155 157
156 const MINIDUMP_DIRECTORY* directory; 158 const MINIDUMP_DIRECTORY* directory;
157 const MINIDUMP_HEADER* header = 159 const MINIDUMP_HEADER* header =
158 MinidumpHeaderAtStart(file_writer.string(), &directory); 160 MinidumpHeaderAtStart(file_writer.string(), &directory);
159 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 3, kTimestamp)); 161 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 3, kTimestamp));
160 ASSERT_TRUE(directory); 162 ASSERT_TRUE(directory);
161 163
162 EXPECT_EQ(kStream1Type, directory[0].StreamType); 164 EXPECT_EQ(kStream0Type, directory[0].StreamType);
163 EXPECT_EQ(kStream1Size, directory[0].Location.DataSize); 165 EXPECT_EQ(kStream0Size, directory[0].Location.DataSize);
164 EXPECT_EQ(kStream1Offset, directory[0].Location.Rva); 166 EXPECT_EQ(kStream0Offset, directory[0].Location.Rva);
165 EXPECT_EQ(kStream2Type, directory[1].StreamType); 167 EXPECT_EQ(kStream1Type, directory[1].StreamType);
166 EXPECT_EQ(kStream2Size, directory[1].Location.DataSize); 168 EXPECT_EQ(kStream1Size, directory[1].Location.DataSize);
167 EXPECT_EQ(kStream2Offset, directory[1].Location.Rva); 169 EXPECT_EQ(kStream1Offset, directory[1].Location.Rva);
168 EXPECT_EQ(kStream3Type, directory[2].StreamType); 170 EXPECT_EQ(kStream2Type, directory[2].StreamType);
169 EXPECT_EQ(kStream3Size, directory[2].Location.DataSize); 171 EXPECT_EQ(kStream2Size, directory[2].Location.DataSize);
170 EXPECT_EQ(kStream3Offset, directory[2].Location.Rva); 172 EXPECT_EQ(kStream2Offset, directory[2].Location.Rva);
171 173
172 const uint8_t* stream1_data = 174 const uint8_t* stream0_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
173 reinterpret_cast<const uint8_t*>(&file_writer.string()[kStream1Offset]); 175 file_writer.string(), directory[0].Location);
176 ASSERT_TRUE(stream0_data);
177
178 std::string expected_stream0(kStream0Size, kStream0Value);
179 EXPECT_EQ(0, memcmp(stream0_data, expected_stream0.c_str(), kStream0Size));
180
181 const int kZeroes[16] = {};
182 ASSERT_GE(sizeof(kZeroes), kStream1Padding);
183 EXPECT_EQ(0, memcmp(stream0_data + kStream0Size, kZeroes, kStream1Padding));
184
185 const uint8_t* stream1_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
186 file_writer.string(), directory[1].Location);
187 ASSERT_TRUE(stream1_data);
174 188
175 std::string expected_stream1(kStream1Size, kStream1Value); 189 std::string expected_stream1(kStream1Size, kStream1Value);
176 EXPECT_EQ(0, memcmp(stream1_data, expected_stream1.c_str(), kStream1Size)); 190 EXPECT_EQ(0, memcmp(stream1_data, expected_stream1.c_str(), kStream1Size));
177 191
178 const int kZeroes[16] = {};
179 ASSERT_GE(sizeof(kZeroes), kStream2Padding); 192 ASSERT_GE(sizeof(kZeroes), kStream2Padding);
180 EXPECT_EQ(0, memcmp(stream1_data + kStream1Size, kZeroes, kStream2Padding)); 193 EXPECT_EQ(0, memcmp(stream1_data + kStream1Size, kZeroes, kStream2Padding));
181 194
182 const uint8_t* stream2_data = 195 const uint8_t* stream2_data = MinidumpWritableAtLocationDescriptor<uint8_t>(
183 reinterpret_cast<const uint8_t*>(&file_writer.string()[kStream2Offset]); 196 file_writer.string(), directory[2].Location);
197 ASSERT_TRUE(stream2_data);
184 198
185 std::string expected_stream2(kStream2Size, kStream2Value); 199 std::string expected_stream2(kStream2Size, kStream2Value);
186 EXPECT_EQ(0, memcmp(stream2_data, expected_stream2.c_str(), kStream2Size)); 200 EXPECT_EQ(0, memcmp(stream2_data, expected_stream2.c_str(), kStream2Size));
187
188 ASSERT_GE(sizeof(kZeroes), kStream3Padding);
189 EXPECT_EQ(0, memcmp(stream2_data + kStream2Size, kZeroes, kStream3Padding));
190
191 const uint8_t* stream3_data =
192 reinterpret_cast<const uint8_t*>(&file_writer.string()[kStream3Offset]);
193
194 std::string expected_stream3(kStream3Size, kStream3Value);
195 EXPECT_EQ(0, memcmp(stream3_data, expected_stream3.c_str(), kStream3Size));
196 } 201 }
197 202
198 TEST(MinidumpFileWriter, ZeroLengthStream) { 203 TEST(MinidumpFileWriter, ZeroLengthStream) {
199 MinidumpFileWriter minidump_file; 204 MinidumpFileWriter minidump_file;
200 205
201 const size_t kStreamSize = 0; 206 const size_t kStreamSize = 0;
202 const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d); 207 const MinidumpStreamType kStreamType = static_cast<MinidumpStreamType>(0x4d);
203 TestStream stream(kStreamType, kStreamSize, 0); 208 TestStream stream(kStreamType, kStreamSize, 0);
204 minidump_file.AddStream(&stream); 209 minidump_file.AddStream(&stream);
205 210
(...skipping 13 matching lines...) Expand all
219 ASSERT_TRUE(directory); 224 ASSERT_TRUE(directory);
220 225
221 EXPECT_EQ(kStreamType, directory[0].StreamType); 226 EXPECT_EQ(kStreamType, directory[0].StreamType);
222 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize); 227 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
223 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva); 228 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
224 } 229 }
225 230
226 TEST(MinidumpFileWriterDeathTest, SameStreamType) { 231 TEST(MinidumpFileWriterDeathTest, SameStreamType) {
227 MinidumpFileWriter minidump_file; 232 MinidumpFileWriter minidump_file;
228 233
229 const size_t kStream1Size = 5; 234 const size_t kStream0Size = 5;
230 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d); 235 const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x4d);
231 const uint8_t kStream1Value = 0x5a; 236 const uint8_t kStream0Value = 0x5a;
232 TestStream stream1(kStream1Type, kStream1Size, kStream1Value); 237 TestStream stream0(kStream0Type, kStream0Size, kStream0Value);
233 minidump_file.AddStream(&stream1); 238 minidump_file.AddStream(&stream0);
234 239
235 // It is an error to add a second stream of the same type. 240 // It is an error to add a second stream of the same type.
236 const size_t kStream2Size = 3; 241 const size_t kStream1Size = 3;
237 const MinidumpStreamType kStream2Type = static_cast<MinidumpStreamType>(0x4d); 242 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
238 const uint8_t kStream2Value = 0xa5; 243 const uint8_t kStream1Value = 0xa5;
239 TestStream stream2(kStream2Type, kStream2Size, kStream2Value); 244 TestStream stream1(kStream1Type, kStream1Size, kStream1Value);
240 ASSERT_DEATH(minidump_file.AddStream(&stream2), "already present"); 245 ASSERT_DEATH(minidump_file.AddStream(&stream1), "already present");
241 } 246 }
242 247
243 } // namespace 248 } // namespace
244 } // namespace test 249 } // namespace test
245 } // namespace crashpad 250 } // namespace crashpad
OLDNEW
« no previous file with comments | « minidump/minidump_exception_writer_test.cc ('k') | minidump/minidump_memory_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698