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

Side by Side Diff: minidump/minidump_crashpad_info_writer_test.cc

Issue 654573003: Add MinidumpCrashpadInfoWriter and its test (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
OLDNEW
(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_crashpad_info_writer.h"
16
17 #include <dbghelp.h>
18
19 #include "gtest/gtest.h"
20 #include "minidump/minidump_extensions.h"
21 #include "minidump/minidump_file_writer.h"
22 #include "minidump/minidump_file_writer_test_util.h"
23 #include "minidump/minidump_simple_string_dictionary_writer.h"
24 #include "minidump/minidump_string_writer_test_util.h"
25 #include "util/file/string_file_writer.h"
26
27 namespace crashpad {
28 namespace test {
29 namespace {
30
31 void GetCrashpadInfoStream(
32 const std::string& file_contents,
33 const MinidumpCrashpadInfo** crashpad_info,
34 const MinidumpSimpleStringDictionary** simple_annotations) {
35 const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
36 const size_t kCrashpadInfoStreamOffset =
37 kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
38 size_t end = kCrashpadInfoStreamOffset + sizeof(MinidumpCrashpadInfo);
39 const size_t kSimpleAnnotationsOffset = simple_annotations ? end : 0;
40 if (simple_annotations) {
41 end += sizeof(MinidumpSimpleStringDictionary);
42 }
43 const size_t kFileSize = end;
44
45 if (!simple_annotations) {
46 ASSERT_EQ(kFileSize, file_contents.size());
47 } else {
48 EXPECT_GE(file_contents.size(), kFileSize);
49 }
50
51 const MINIDUMP_HEADER* header =
52 reinterpret_cast<const MINIDUMP_HEADER*>(&file_contents[0]);
53
54 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
55
56 const MINIDUMP_DIRECTORY* directory =
57 reinterpret_cast<const MINIDUMP_DIRECTORY*>(
58 &file_contents[kDirectoryOffset]);
59
60 ASSERT_EQ(kMinidumpStreamTypeCrashpadInfo, directory->StreamType);
61 ASSERT_EQ(sizeof(MinidumpCrashpadInfo), directory->Location.DataSize);
62 ASSERT_EQ(kCrashpadInfoStreamOffset, directory->Location.Rva);
63
64 *crashpad_info = reinterpret_cast<const MinidumpCrashpadInfo*>(
65 &file_contents[kCrashpadInfoStreamOffset]);
66
67 if (simple_annotations) {
68 ASSERT_GE((*crashpad_info)->simple_annotations.DataSize,
69 sizeof(MinidumpSimpleStringDictionary));
70 ASSERT_EQ(kSimpleAnnotationsOffset,
71 (*crashpad_info)->simple_annotations.Rva);
72 *simple_annotations =
73 reinterpret_cast<const MinidumpSimpleStringDictionary*>(
74 &file_contents[kSimpleAnnotationsOffset]);
75 } else {
76 ASSERT_EQ(0u, (*crashpad_info)->simple_annotations.DataSize);
77 ASSERT_EQ(0u, (*crashpad_info)->simple_annotations.Rva);
78 }
79 }
80
81 TEST(MinidumpCrashpadInfoWriter, Empty) {
82 MinidumpFileWriter minidump_file_writer;
83 MinidumpCrashpadInfoWriter crashpad_info_writer;
84
85 minidump_file_writer.AddStream(&crashpad_info_writer);
86
87 StringFileWriter file_writer;
88 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
89
90 const MinidumpCrashpadInfo* crashpad_info;
91
92 ASSERT_NO_FATAL_FAILURE(
93 GetCrashpadInfoStream(file_writer.string(), &crashpad_info, nullptr));
94
95 EXPECT_EQ(sizeof(*crashpad_info), crashpad_info->size);
96 EXPECT_EQ(1u, crashpad_info->version);
97 EXPECT_EQ(0u, crashpad_info->simple_annotations.DataSize);
98 EXPECT_EQ(0u, crashpad_info->simple_annotations.Rva);
99 }
100
101 TEST(MinidumpCrashpadInfoWriter, SimpleAnnotations) {
102 MinidumpFileWriter minidump_file_writer;
103 MinidumpCrashpadInfoWriter crashpad_info_writer;
104
105 minidump_file_writer.AddStream(&crashpad_info_writer);
106
107 MinidumpSimpleStringDictionaryWriter simple_annotations_writer;
108
109 // Set a key and value before adding the simple_annotations_writer to
110 // crashpad_info_writer, and another one after.
111 const char kKey0[] = "k0";
112 const char kValue0[] = "v";
113 const char kKey1[] = "KEY1";
114 const char kValue1[] = "";
115 MinidumpSimpleStringDictionaryEntryWriter entry_0;
116 entry_0.SetKeyValue(kKey0, kValue0);
117 simple_annotations_writer.AddEntry(&entry_0);
118
119 crashpad_info_writer.SetSimpleAnnotations(&simple_annotations_writer);
120
121 MinidumpSimpleStringDictionaryEntryWriter entry_1;
122 entry_1.SetKeyValue(kKey1, kValue1);
123 simple_annotations_writer.AddEntry(&entry_1);
124
125 StringFileWriter file_writer;
126 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
127
128 const MinidumpCrashpadInfo* crashpad_info;
129 const MinidumpSimpleStringDictionary* simple_annotations;
130
131 ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream(
132 file_writer.string(), &crashpad_info, &simple_annotations));
133
134 EXPECT_EQ(sizeof(*crashpad_info), crashpad_info->size);
135 EXPECT_EQ(1u, crashpad_info->version);
136
137 ASSERT_EQ(2u, simple_annotations->count);
138
139 EXPECT_EQ(
140 kKey1,
141 MinidumpUTF8StringAtRVA(file_writer, simple_annotations->entries[0].key));
142 EXPECT_EQ(kValue1,
143 MinidumpUTF8StringAtRVA(file_writer,
144 simple_annotations->entries[0].value));
145 EXPECT_EQ(
146 kKey0,
147 MinidumpUTF8StringAtRVA(file_writer, simple_annotations->entries[1].key));
148 EXPECT_EQ(kValue0,
149 MinidumpUTF8StringAtRVA(file_writer,
150 simple_annotations->entries[1].value));
151 }
152
153 } // namespace
154 } // namespace test
155 } // namespace crashpad
OLDNEW
« no previous file with comments | « minidump/minidump_crashpad_info_writer.cc ('k') | minidump/minidump_simple_string_dictionary_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698