2014-10-17 18:00:41 -04:00
|
|
|
|
// Copyright 2014 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_crashpad_info_writer.h"
|
|
|
|
|
|
|
|
|
|
#include <dbghelp.h>
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "minidump/minidump_extensions.h"
|
|
|
|
|
#include "minidump/minidump_file_writer.h"
|
2014-10-24 14:58:53 -04:00
|
|
|
|
#include "minidump/minidump_module_crashpad_info_writer.h"
|
2014-10-20 12:11:14 -04:00
|
|
|
|
#include "minidump/test/minidump_file_writer_test_util.h"
|
2014-11-07 11:38:13 -05:00
|
|
|
|
#include "minidump/test/minidump_string_writer_test_util.h"
|
2014-10-22 18:35:18 -04:00
|
|
|
|
#include "minidump/test/minidump_writable_test_util.h"
|
2014-11-07 11:38:13 -05:00
|
|
|
|
#include "snapshot/test/test_module_snapshot.h"
|
|
|
|
|
#include "snapshot/test/test_process_snapshot.h"
|
2014-10-17 18:00:41 -04:00
|
|
|
|
#include "util/file/string_file_writer.h"
|
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2014-10-24 14:44:55 -04:00
|
|
|
|
void GetCrashpadInfoStream(const std::string& file_contents,
|
|
|
|
|
const MinidumpCrashpadInfo** crashpad_info,
|
|
|
|
|
const MinidumpModuleCrashpadInfoList** module_list) {
|
2014-10-21 14:15:07 -04:00
|
|
|
|
const MINIDUMP_DIRECTORY* directory;
|
2014-10-17 18:00:41 -04:00
|
|
|
|
const MINIDUMP_HEADER* header =
|
2014-10-21 14:15:07 -04:00
|
|
|
|
MinidumpHeaderAtStart(file_contents, &directory);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
|
2014-10-21 14:15:07 -04:00
|
|
|
|
ASSERT_TRUE(directory);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-21 14:15:07 -04:00
|
|
|
|
ASSERT_EQ(kMinidumpStreamTypeCrashpadInfo, directory[0].StreamType);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-22 18:35:18 -04:00
|
|
|
|
*crashpad_info = MinidumpWritableAtLocationDescriptor<MinidumpCrashpadInfo>(
|
|
|
|
|
file_contents, directory[0].Location);
|
|
|
|
|
ASSERT_TRUE(*crashpad_info);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-24 14:44:55 -04:00
|
|
|
|
*module_list =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MinidumpModuleCrashpadInfoList>(
|
|
|
|
|
file_contents, (*crashpad_info)->module_list);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MinidumpCrashpadInfoWriter, Empty) {
|
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
2014-10-27 15:01:39 -04:00
|
|
|
|
auto crashpad_info_writer = make_scoped_ptr(new MinidumpCrashpadInfoWriter());
|
2014-11-07 11:38:13 -05:00
|
|
|
|
EXPECT_FALSE(crashpad_info_writer->IsUseful());
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
minidump_file_writer.AddStream(crashpad_info_writer.Pass());
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
|
|
|
|
StringFileWriter file_writer;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
|
|
|
|
|
|
|
|
|
|
const MinidumpCrashpadInfo* crashpad_info;
|
2014-10-24 14:44:55 -04:00
|
|
|
|
const MinidumpModuleCrashpadInfoList* module_list;
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-24 14:44:55 -04:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream(
|
|
|
|
|
file_writer.string(), &crashpad_info, &module_list));
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-24 14:44:55 -04:00
|
|
|
|
EXPECT_EQ(MinidumpCrashpadInfo::kVersion, crashpad_info->version);
|
|
|
|
|
EXPECT_FALSE(module_list);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 14:44:55 -04:00
|
|
|
|
TEST(MinidumpCrashpadInfoWriter, CrashpadModuleList) {
|
|
|
|
|
const uint32_t kMinidumpModuleListIndex = 3;
|
|
|
|
|
|
2014-10-17 18:00:41 -04:00
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
2014-10-27 15:01:39 -04:00
|
|
|
|
auto crashpad_info_writer = make_scoped_ptr(new MinidumpCrashpadInfoWriter());
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
auto module_list_writer =
|
|
|
|
|
make_scoped_ptr(new MinidumpModuleCrashpadInfoListWriter());
|
|
|
|
|
auto module_writer = make_scoped_ptr(new MinidumpModuleCrashpadInfoWriter());
|
|
|
|
|
module_writer->SetMinidumpModuleListIndex(kMinidumpModuleListIndex);
|
|
|
|
|
module_list_writer->AddModule(module_writer.Pass());
|
|
|
|
|
crashpad_info_writer->SetModuleList(module_list_writer.Pass());
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
2014-11-07 11:38:13 -05:00
|
|
|
|
EXPECT_TRUE(crashpad_info_writer->IsUseful());
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
minidump_file_writer.AddStream(crashpad_info_writer.Pass());
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
|
|
|
|
StringFileWriter file_writer;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
|
|
|
|
|
|
|
|
|
|
const MinidumpCrashpadInfo* crashpad_info;
|
2014-10-24 14:44:55 -04:00
|
|
|
|
const MinidumpModuleCrashpadInfoList* module_list;
|
2014-10-17 18:00:41 -04:00
|
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream(
|
2014-10-24 14:44:55 -04:00
|
|
|
|
file_writer.string(), &crashpad_info, &module_list));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(MinidumpCrashpadInfo::kVersion, crashpad_info->version);
|
|
|
|
|
ASSERT_TRUE(module_list);
|
|
|
|
|
ASSERT_EQ(1u, module_list->count);
|
|
|
|
|
|
|
|
|
|
const MinidumpModuleCrashpadInfo* module =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MinidumpModuleCrashpadInfo>(
|
2014-11-06 16:58:37 -05:00
|
|
|
|
file_writer.string(), module_list->children[0]);
|
2014-10-24 14:44:55 -04:00
|
|
|
|
ASSERT_TRUE(module);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(MinidumpModuleCrashpadInfo::kVersion, module->version);
|
|
|
|
|
EXPECT_EQ(kMinidumpModuleListIndex, module->minidump_module_list_index);
|
2014-11-07 11:38:13 -05:00
|
|
|
|
EXPECT_EQ(0u, module->list_annotations.DataSize);
|
|
|
|
|
EXPECT_EQ(0u, module->list_annotations.Rva);
|
2014-10-24 14:44:55 -04:00
|
|
|
|
EXPECT_EQ(0u, module->simple_annotations.DataSize);
|
|
|
|
|
EXPECT_EQ(0u, module->simple_annotations.Rva);
|
2014-10-17 18:00:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:38:13 -05:00
|
|
|
|
TEST(MinidumpCrashpadInfoWriter, InitializeFromSnapshot) {
|
|
|
|
|
const char kEntry[] = "This is a simple annotation in a list.";
|
|
|
|
|
|
|
|
|
|
// Test with a useless module, one that doesn’t carry anything that would
|
|
|
|
|
// require MinidumpCrashpadInfo or any child object.
|
|
|
|
|
auto process_snapshot = make_scoped_ptr(new TestProcessSnapshot());
|
|
|
|
|
|
|
|
|
|
auto module_snapshot = make_scoped_ptr(new TestModuleSnapshot());
|
|
|
|
|
process_snapshot->AddModule(module_snapshot.Pass());
|
|
|
|
|
|
|
|
|
|
auto info_writer = make_scoped_ptr(new MinidumpCrashpadInfoWriter());
|
|
|
|
|
info_writer->InitializeFromSnapshot(process_snapshot.get());
|
|
|
|
|
EXPECT_FALSE(info_writer->IsUseful());
|
|
|
|
|
|
|
|
|
|
// Try again with a useful module.
|
|
|
|
|
process_snapshot.reset(new TestProcessSnapshot());
|
|
|
|
|
|
|
|
|
|
module_snapshot.reset(new TestModuleSnapshot());
|
|
|
|
|
std::vector<std::string> annotations_list(1, std::string(kEntry));
|
|
|
|
|
module_snapshot->SetAnnotationsVector(annotations_list);
|
|
|
|
|
process_snapshot->AddModule(module_snapshot.Pass());
|
|
|
|
|
|
|
|
|
|
info_writer.reset(new MinidumpCrashpadInfoWriter());
|
|
|
|
|
info_writer->InitializeFromSnapshot(process_snapshot.get());
|
|
|
|
|
EXPECT_TRUE(info_writer->IsUseful());
|
|
|
|
|
|
|
|
|
|
MinidumpFileWriter minidump_file_writer;
|
|
|
|
|
minidump_file_writer.AddStream(info_writer.Pass());
|
|
|
|
|
|
|
|
|
|
StringFileWriter file_writer;
|
|
|
|
|
ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
|
|
|
|
|
|
|
|
|
|
const MinidumpCrashpadInfo* info;
|
|
|
|
|
const MinidumpModuleCrashpadInfoList* module_list;
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(GetCrashpadInfoStream(
|
|
|
|
|
file_writer.string(), &info, &module_list));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(MinidumpCrashpadInfo::kVersion, info->version);
|
|
|
|
|
ASSERT_TRUE(module_list);
|
|
|
|
|
ASSERT_EQ(1u, module_list->count);
|
|
|
|
|
|
|
|
|
|
const MinidumpModuleCrashpadInfo* module =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MinidumpModuleCrashpadInfo>(
|
|
|
|
|
file_writer.string(), module_list->children[0]);
|
|
|
|
|
ASSERT_TRUE(module);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(MinidumpModuleCrashpadInfo::kVersion, module->version);
|
|
|
|
|
EXPECT_EQ(0u, module->minidump_module_list_index);
|
|
|
|
|
|
|
|
|
|
const MinidumpRVAList* list_annotations =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MinidumpRVAList>(
|
|
|
|
|
file_writer.string(), module->list_annotations);
|
|
|
|
|
ASSERT_TRUE(list_annotations);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(1u, list_annotations->count);
|
|
|
|
|
EXPECT_EQ(kEntry,
|
|
|
|
|
MinidumpUTF8StringAtRVAAsString(
|
|
|
|
|
file_writer.string(), list_annotations->children[0]));
|
|
|
|
|
|
|
|
|
|
const MinidumpSimpleStringDictionary* simple_annotations =
|
|
|
|
|
MinidumpWritableAtLocationDescriptor<MinidumpSimpleStringDictionary>(
|
|
|
|
|
file_writer.string(), module->simple_annotations);
|
|
|
|
|
EXPECT_FALSE(simple_annotations);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-17 18:00:41 -04:00
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|