crashpad/minidump/minidump_unloaded_module_writer_test.cc
Mark Mentovai 4b450c8137 test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.

This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.

EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.

References:

https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
77d6b17338
https://github.com/google/googletest/pull/713

Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 12:34:24 +00:00

166 lines
6.3 KiB
C++

// Copyright 2016 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_unloaded_module_writer.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "gtest/gtest.h"
#include "minidump/minidump_file_writer.h"
#include "minidump/test/minidump_file_writer_test_util.h"
#include "minidump/test/minidump_string_writer_test_util.h"
#include "minidump/test/minidump_writable_test_util.h"
#include "util/file/string_file.h"
namespace crashpad {
namespace test {
namespace {
void ExpectUnloadedModule(const MINIDUMP_UNLOADED_MODULE* expected,
const MINIDUMP_UNLOADED_MODULE* observed,
const std::string& file_contents,
const std::string& expected_module_name) {
EXPECT_EQ(observed->BaseOfImage, expected->BaseOfImage);
EXPECT_EQ(observed->SizeOfImage, expected->SizeOfImage);
EXPECT_EQ(observed->CheckSum, expected->CheckSum);
EXPECT_EQ(observed->TimeDateStamp, expected->TimeDateStamp);
EXPECT_NE(observed->ModuleNameRva, 0u);
base::string16 observed_module_name_utf16 =
MinidumpStringAtRVAAsString(file_contents, observed->ModuleNameRva);
base::string16 expected_module_name_utf16 =
base::UTF8ToUTF16(expected_module_name);
EXPECT_EQ(observed_module_name_utf16, expected_module_name_utf16);
}
void GetUnloadedModuleListStream(
const std::string& file_contents,
const MINIDUMP_UNLOADED_MODULE_LIST** unloaded_module_list) {
const size_t kDirectoryOffset = sizeof(MINIDUMP_HEADER);
const size_t kUnloadedModuleListStreamOffset =
kDirectoryOffset + sizeof(MINIDUMP_DIRECTORY);
const size_t kUnloadedModulesOffset =
kUnloadedModuleListStreamOffset + sizeof(MINIDUMP_UNLOADED_MODULE_LIST);
ASSERT_GE(file_contents.size(), kUnloadedModulesOffset);
const MINIDUMP_DIRECTORY* directory;
const MINIDUMP_HEADER* header =
MinidumpHeaderAtStart(file_contents, &directory);
ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
ASSERT_TRUE(directory);
ASSERT_EQ(directory[0].StreamType, kMinidumpStreamTypeUnloadedModuleList);
EXPECT_EQ(directory[0].Location.Rva, kUnloadedModuleListStreamOffset);
*unloaded_module_list =
MinidumpWritableAtLocationDescriptor<MINIDUMP_UNLOADED_MODULE_LIST>(
file_contents, directory[0].Location);
ASSERT_TRUE(unloaded_module_list);
}
TEST(MinidumpUnloadedModuleWriter, EmptyModule) {
MinidumpFileWriter minidump_file_writer;
auto unloaded_module_list_writer =
base::WrapUnique(new MinidumpUnloadedModuleListWriter());
const char kModuleName[] = "test_dll";
auto unloaded_module_writer =
base::WrapUnique(new MinidumpUnloadedModuleWriter());
unloaded_module_writer->SetName(kModuleName);
unloaded_module_list_writer->AddUnloadedModule(
std::move(unloaded_module_writer));
ASSERT_TRUE(
minidump_file_writer.AddStream(std::move(unloaded_module_list_writer)));
StringFile string_file;
ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
ASSERT_GT(string_file.string().size(),
sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
sizeof(MINIDUMP_UNLOADED_MODULE_LIST) +
1 * sizeof(MINIDUMP_UNLOADED_MODULE));
const MINIDUMP_UNLOADED_MODULE_LIST* unloaded_module_list = nullptr;
ASSERT_NO_FATAL_FAILURE(
GetUnloadedModuleListStream(string_file.string(), &unloaded_module_list));
EXPECT_EQ(unloaded_module_list->NumberOfEntries, 1u);
MINIDUMP_UNLOADED_MODULE expected = {};
ASSERT_NO_FATAL_FAILURE(
ExpectUnloadedModule(&expected,
reinterpret_cast<const MINIDUMP_UNLOADED_MODULE*>(
&unloaded_module_list[1]),
string_file.string(),
kModuleName));
}
TEST(MinidumpUnloadedModuleWriter, OneModule) {
MinidumpFileWriter minidump_file_writer;
auto unloaded_module_list_writer =
base::WrapUnique(new MinidumpUnloadedModuleListWriter());
const char kModuleName[] = "statically_linked";
const uint64_t kModuleBase = 0x10da69000;
const uint32_t kModuleSize = 0x1000;
const uint32_t kChecksum = 0x76543210;
const time_t kTimestamp = 0x386d4380;
auto unloaded_module_writer =
base::WrapUnique(new MinidumpUnloadedModuleWriter());
unloaded_module_writer->SetName(kModuleName);
unloaded_module_writer->SetImageBaseAddress(kModuleBase);
unloaded_module_writer->SetImageSize(kModuleSize);
unloaded_module_writer->SetChecksum(kChecksum);
unloaded_module_writer->SetTimestamp(kTimestamp);
unloaded_module_list_writer->AddUnloadedModule(
std::move(unloaded_module_writer));
ASSERT_TRUE(
minidump_file_writer.AddStream(std::move(unloaded_module_list_writer)));
StringFile string_file;
ASSERT_TRUE(minidump_file_writer.WriteEverything(&string_file));
ASSERT_GT(string_file.string().size(),
sizeof(MINIDUMP_HEADER) + sizeof(MINIDUMP_DIRECTORY) +
sizeof(MINIDUMP_UNLOADED_MODULE_LIST) +
1 * sizeof(MINIDUMP_UNLOADED_MODULE));
const MINIDUMP_UNLOADED_MODULE_LIST* unloaded_module_list = nullptr;
ASSERT_NO_FATAL_FAILURE(
GetUnloadedModuleListStream(string_file.string(), &unloaded_module_list));
EXPECT_EQ(unloaded_module_list->NumberOfEntries, 1u);
MINIDUMP_UNLOADED_MODULE expected = {};
expected.BaseOfImage = kModuleBase;
expected.SizeOfImage = kModuleSize;
expected.CheckSum = kChecksum;
expected.TimeDateStamp = kTimestamp;
ASSERT_NO_FATAL_FAILURE(
ExpectUnloadedModule(&expected,
reinterpret_cast<const MINIDUMP_UNLOADED_MODULE*>(
&unloaded_module_list[1]),
string_file.string(),
kModuleName));
}
} // namespace
} // namespace test
} // namespace crashpad