crashpad/handler/crashpad_handler_test.cc
Mark Mentovai 281be63d00 Standardize on static constexpr for arrays when possible
This uses “static” at function scope to avoid making local copies, even
in cases where the compiler can’t see that the local copy is
unnecessary. “constexpr” adds additional safety in that it prevents
global state from being initialized from any runtime dependencies, which
would be undesirable.

At namespace scope, “constexpr” is also used where appropriate.

For the most part, this was a mechanical transformation for things
matching '(^| )const [^=]*\['.

Similar transformations could be applied to non-arrays in some cases,
but there’s limited practical impact in most non-array cases relative to
arrays, there are far more use sites, and much more manual intervention
would be required.

Change-Id: I3513b739ee8b0be026f8285475cddc5f9cc81152
Reviewed-on: https://chromium-review.googlesource.com/583997
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Leonard Mosescu <mosescu@chromium.org>
2017-07-25 17:40:51 +00:00

133 lines
4.2 KiB
C++

// Copyright 2017 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 <windows.h>
#include <dbghelp.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "client/crash_report_database.h"
#include "client/crashpad_client.h"
#include "gtest/gtest.h"
#include "minidump/test/minidump_file_writer_test_util.h"
#include "test/test_paths.h"
#include "test/win/win_multiprocess_with_temp_dir.h"
#include "util/file/file_reader.h"
namespace crashpad {
namespace test {
namespace {
void StartAndCrashWithExtendedHandler(const base::FilePath& temp_dir) {
base::FilePath handler_path = TestPaths::Executable().DirName().Append(
FILE_PATH_LITERAL("crashpad_handler_test_extended_handler.exe"));
CrashpadClient client;
ASSERT_TRUE(client.StartHandler(handler_path,
temp_dir,
base::FilePath(),
"",
std::map<std::string, std::string>(),
std::vector<std::string>(),
false,
false));
__debugbreak();
}
class CrashWithExtendedHandler final : public WinMultiprocessWithTempDir {
public:
CrashWithExtendedHandler() : WinMultiprocessWithTempDir() {}
~CrashWithExtendedHandler() {}
private:
void ValidateGeneratedDump();
void WinMultiprocessParent() override {
SetExpectedChildExitCode(EXCEPTION_BREAKPOINT);
}
void WinMultiprocessChild() override {
StartAndCrashWithExtendedHandler(GetTempDirPath());
}
void WinMultiprocessParentAfterChild(HANDLE child) override {
// At this point the child has exited, which means the crash report should
// have been written.
ValidateGeneratedDump();
// Delegate the cleanup to the superclass.
WinMultiprocessWithTempDir::WinMultiprocessParentAfterChild(child);
}
};
void CrashWithExtendedHandler::ValidateGeneratedDump() {
// Open the database and find the sole dump that should have been created.
std::unique_ptr<CrashReportDatabase> database(
CrashReportDatabase::Initialize(GetTempDirPath()));
ASSERT_TRUE(database);
std::vector<CrashReportDatabase::Report> reports;
ASSERT_EQ(database->GetCompletedReports(&reports),
CrashReportDatabase::kNoError);
ASSERT_EQ(reports.size(), 1u);
// Open the dump and validate that it has the extension stream with the
// expected contents.
FileReader reader;
ASSERT_TRUE(reader.Open(reports[0].file_path));
// Read the header.
MINIDUMP_HEADER header = {};
ASSERT_TRUE(reader.ReadExactly(&header, sizeof(header)));
// Read the directory.
std::vector<MINIDUMP_DIRECTORY> directory(header.NumberOfStreams);
ASSERT_TRUE(reader.SeekSet(header.StreamDirectoryRva));
ASSERT_TRUE(reader.ReadExactly(directory.data(),
directory.size() * sizeof(directory[0])));
// Search for the extension stream.
size_t found_extension_streams = 0;
for (const auto& entry : directory) {
if (entry.StreamType == 0xCAFEBABE) {
++found_extension_streams;
ASSERT_TRUE(reader.SeekSet(entry.Location.Rva));
std::vector<char> data;
data.resize(entry.Location.DataSize);
ASSERT_TRUE(reader.ReadExactly(data.data(), data.size()));
static constexpr char kExpectedData[] = "Injected extension stream!";
EXPECT_EQ(memcmp(kExpectedData, data.data(), sizeof(kExpectedData)), 0);
}
}
EXPECT_EQ(found_extension_streams, 1u);
}
TEST(CrashpadHandler, ExtensibilityCalloutsWork) {
WinMultiprocessWithTempDir::Run<CrashWithExtendedHandler>();
}
} // namespace
} // namespace test
} // namespace crashpad