2014-09-10 17:30:21 -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 "util/posix/symbolic_constants_posix.h"
|
|
|
|
|
2016-10-31 15:48:24 -04:00
|
|
|
#include <signal.h>
|
2016-01-06 12:22:50 -05:00
|
|
|
#include <sys/types.h>
|
2014-09-10 17:30:21 -04:00
|
|
|
|
2019-01-04 16:57:57 -05:00
|
|
|
#include "base/stl_util.h"
|
2014-09-10 17:30:21 -04:00
|
|
|
#include "base/strings/string_piece.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
2016-01-06 12:22:50 -05:00
|
|
|
#include "build/build_config.h"
|
2014-09-10 17:30:21 -04:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2019-01-03 13:36:02 -05:00
|
|
|
#define NUL_TEST_DATA(string) \
|
2019-01-04 16:57:57 -05:00
|
|
|
{ string, base::size(string) - 1 }
|
2014-09-16 11:00:51 -04:00
|
|
|
|
2014-10-07 17:28:50 -04:00
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
2014-09-10 17:30:21 -04:00
|
|
|
namespace {
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
constexpr struct {
|
2014-09-10 17:30:21 -04:00
|
|
|
int signal;
|
|
|
|
const char* full_name;
|
|
|
|
const char* short_name;
|
|
|
|
} kSignalTestData[] = {
|
|
|
|
{SIGABRT, "SIGABRT", "ABRT"},
|
|
|
|
{SIGALRM, "SIGALRM", "ALRM"},
|
|
|
|
{SIGBUS, "SIGBUS", "BUS"},
|
|
|
|
{SIGCHLD, "SIGCHLD", "CHLD"},
|
|
|
|
{SIGCONT, "SIGCONT", "CONT"},
|
|
|
|
{SIGFPE, "SIGFPE", "FPE"},
|
|
|
|
{SIGHUP, "SIGHUP", "HUP"},
|
|
|
|
{SIGILL, "SIGILL", "ILL"},
|
|
|
|
{SIGINT, "SIGINT", "INT"},
|
|
|
|
{SIGIO, "SIGIO", "IO"},
|
|
|
|
{SIGKILL, "SIGKILL", "KILL"},
|
|
|
|
{SIGPIPE, "SIGPIPE", "PIPE"},
|
|
|
|
{SIGPROF, "SIGPROF", "PROF"},
|
|
|
|
{SIGQUIT, "SIGQUIT", "QUIT"},
|
|
|
|
{SIGSEGV, "SIGSEGV", "SEGV"},
|
|
|
|
{SIGSTOP, "SIGSTOP", "STOP"},
|
|
|
|
{SIGSYS, "SIGSYS", "SYS"},
|
|
|
|
{SIGTERM, "SIGTERM", "TERM"},
|
|
|
|
{SIGTRAP, "SIGTRAP", "TRAP"},
|
|
|
|
{SIGTSTP, "SIGTSTP", "TSTP"},
|
|
|
|
{SIGTTIN, "SIGTTIN", "TTIN"},
|
|
|
|
{SIGTTOU, "SIGTTOU", "TTOU"},
|
|
|
|
{SIGURG, "SIGURG", "URG"},
|
|
|
|
{SIGUSR1, "SIGUSR1", "USR1"},
|
|
|
|
{SIGUSR2, "SIGUSR2", "USR2"},
|
|
|
|
{SIGVTALRM, "SIGVTALRM", "VTALRM"},
|
|
|
|
{SIGWINCH, "SIGWINCH", "WINCH"},
|
|
|
|
{SIGXCPU, "SIGXCPU", "XCPU"},
|
2020-07-27 22:20:52 -04:00
|
|
|
#if defined(OS_APPLE)
|
2014-09-10 17:30:21 -04:00
|
|
|
{SIGEMT, "SIGEMT", "EMT"},
|
|
|
|
{SIGINFO, "SIGINFO", "INFO"},
|
2016-10-31 11:23:00 -04:00
|
|
|
#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
2014-09-10 17:30:21 -04:00
|
|
|
{SIGPWR, "SIGPWR", "PWR"},
|
2018-07-10 11:17:22 +02:00
|
|
|
#if !defined(ARCH_CPU_MIPS_FAMILY)
|
2014-09-10 17:30:21 -04:00
|
|
|
{SIGSTKFLT, "SIGSTKFLT", "STKFLT"},
|
|
|
|
#endif
|
2018-07-10 11:17:22 +02:00
|
|
|
#endif
|
2014-09-10 17:30:21 -04:00
|
|
|
};
|
|
|
|
|
2014-10-14 11:10:45 -04:00
|
|
|
// If |expect| is nullptr, the conversion is expected to fail. If |expect| is
|
2014-09-16 11:00:51 -04:00
|
|
|
// empty, the conversion is expected to succeed, but the precise returned string
|
|
|
|
// value is unknown. Otherwise, the conversion is expected to succeed, and
|
|
|
|
// |expect| contains the precise expected string value to be returned.
|
2014-09-10 17:30:21 -04:00
|
|
|
//
|
|
|
|
// Only set kUseFullName or kUseShortName when calling this. Other options are
|
|
|
|
// exercised directly by this function.
|
|
|
|
void TestSignalToStringOnce(int value,
|
|
|
|
const char* expect,
|
|
|
|
SymbolicConstantToStringOptions options) {
|
|
|
|
std::string actual = SignalToString(value, options | kUnknownIsEmpty);
|
|
|
|
std::string actual_numeric =
|
|
|
|
SignalToString(value, options | kUnknownIsNumeric);
|
|
|
|
if (expect) {
|
|
|
|
if (expect[0] == '\0') {
|
|
|
|
EXPECT_FALSE(actual.empty()) << "signal " << value;
|
|
|
|
} else {
|
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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 00:35:21 -04:00
|
|
|
EXPECT_EQ(actual, expect) << "signal " << value;
|
2014-09-10 17:30:21 -04:00
|
|
|
}
|
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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 00:35:21 -04:00
|
|
|
EXPECT_EQ(actual_numeric, actual) << "signal " << value;
|
2014-09-10 17:30:21 -04:00
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(actual.empty()) << "signal " << value << ", actual " << actual;
|
|
|
|
EXPECT_FALSE(actual_numeric.empty())
|
|
|
|
<< "signal " << value << ", actual_numeric " << actual_numeric;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSignalToString(int value,
|
|
|
|
const char* expect_full,
|
|
|
|
const char* expect_short) {
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("full_name");
|
|
|
|
TestSignalToStringOnce(value, expect_full, kUseFullName);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("short_name");
|
|
|
|
TestSignalToStringOnce(value, expect_short, kUseShortName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SymbolicConstantsPOSIX, SignalToString) {
|
2019-01-04 16:57:57 -05:00
|
|
|
for (size_t index = 0; index < base::size(kSignalTestData); ++index) {
|
2014-09-10 17:30:21 -04:00
|
|
|
SCOPED_TRACE(base::StringPrintf("index %zu", index));
|
|
|
|
TestSignalToString(kSignalTestData[index].signal,
|
|
|
|
kSignalTestData[index].full_name,
|
|
|
|
kSignalTestData[index].short_name);
|
|
|
|
}
|
|
|
|
|
2016-10-31 11:23:00 -04:00
|
|
|
#if defined(OS_LINUX) || defined(OS_ANDROID)
|
2014-09-10 17:30:21 -04:00
|
|
|
// NSIG is 64 to account for real-time signals.
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr int kSignalCount = 32;
|
2014-09-10 17:30:21 -04:00
|
|
|
#else
|
2017-07-25 19:15:48 -04:00
|
|
|
constexpr int kSignalCount = NSIG;
|
2014-09-10 17:30:21 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (int signal = 0; signal < kSignalCount + 8; ++signal) {
|
|
|
|
SCOPED_TRACE(base::StringPrintf("signal %d", signal));
|
|
|
|
if (signal > 0 && signal < kSignalCount) {
|
|
|
|
TestSignalToString(signal, "", "");
|
|
|
|
} else {
|
2014-10-14 11:10:45 -04:00
|
|
|
TestSignalToString(signal, nullptr, nullptr);
|
2014-09-10 17:30:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestStringToSignal(const base::StringPiece& string,
|
|
|
|
StringToSymbolicConstantOptions options,
|
|
|
|
bool expect_result,
|
|
|
|
int expect_value) {
|
|
|
|
int actual_value;
|
|
|
|
bool actual_result = StringToSignal(string, options, &actual_value);
|
|
|
|
if (expect_result) {
|
|
|
|
EXPECT_TRUE(actual_result) << "string " << string << ", options " << options
|
|
|
|
<< ", signal " << expect_value;
|
|
|
|
if (actual_result) {
|
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
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
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 00:35:21 -04:00
|
|
|
EXPECT_EQ(actual_value, expect_value) << "string " << string
|
2014-09-10 17:30:21 -04:00
|
|
|
<< ", options " << options;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EXPECT_FALSE(actual_result) << "string " << string << ", options "
|
|
|
|
<< options << ", signal " << actual_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SymbolicConstantsPOSIX, StringToSignal) {
|
2017-07-25 13:34:04 -04:00
|
|
|
static constexpr StringToSymbolicConstantOptions kOptions[] = {
|
2014-09-10 17:30:21 -04:00
|
|
|
0,
|
|
|
|
kAllowFullName,
|
|
|
|
kAllowShortName,
|
|
|
|
kAllowFullName | kAllowShortName,
|
|
|
|
kAllowNumber,
|
|
|
|
kAllowFullName | kAllowNumber,
|
|
|
|
kAllowShortName | kAllowNumber,
|
|
|
|
kAllowFullName | kAllowShortName | kAllowNumber,
|
|
|
|
};
|
|
|
|
|
2019-01-04 16:57:57 -05:00
|
|
|
for (size_t option_index = 0; option_index < base::size(kOptions);
|
2014-09-10 17:30:21 -04:00
|
|
|
++option_index) {
|
|
|
|
SCOPED_TRACE(base::StringPrintf("option_index %zu", option_index));
|
|
|
|
StringToSymbolicConstantOptions options = kOptions[option_index];
|
2019-01-04 16:57:57 -05:00
|
|
|
for (size_t index = 0; index < base::size(kSignalTestData); ++index) {
|
2014-09-10 17:30:21 -04:00
|
|
|
SCOPED_TRACE(base::StringPrintf("index %zu", index));
|
|
|
|
int signal = kSignalTestData[index].signal;
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("full_name");
|
|
|
|
TestStringToSignal(kSignalTestData[index].full_name,
|
|
|
|
options,
|
|
|
|
options & kAllowFullName,
|
|
|
|
signal);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("short_name");
|
|
|
|
TestStringToSignal(kSignalTestData[index].short_name,
|
|
|
|
options,
|
|
|
|
options & kAllowShortName,
|
|
|
|
signal);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("number");
|
|
|
|
std::string number_string = base::StringPrintf("%d", signal);
|
|
|
|
TestStringToSignal(
|
|
|
|
number_string, options, options & kAllowNumber, signal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
static constexpr const char* kNegativeTestData[] = {
|
2014-09-10 17:30:21 -04:00
|
|
|
"SIGHUP ",
|
|
|
|
" SIGINT",
|
|
|
|
"QUIT ",
|
|
|
|
" ILL",
|
|
|
|
"SIGSIGTRAP",
|
|
|
|
"SIGABRTRON",
|
|
|
|
"FPES",
|
|
|
|
"SIGGARBAGE",
|
|
|
|
"random",
|
|
|
|
"",
|
|
|
|
};
|
|
|
|
|
2019-01-04 16:57:57 -05:00
|
|
|
for (size_t index = 0; index < base::size(kNegativeTestData); ++index) {
|
2014-09-10 17:30:21 -04:00
|
|
|
SCOPED_TRACE(base::StringPrintf("index %zu", index));
|
|
|
|
TestStringToSignal(kNegativeTestData[index], options, false, 0);
|
|
|
|
}
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
static constexpr struct {
|
2014-09-10 17:30:21 -04:00
|
|
|
const char* string;
|
|
|
|
size_t length;
|
|
|
|
} kNULTestData[] = {
|
|
|
|
NUL_TEST_DATA("\0SIGBUS"),
|
|
|
|
NUL_TEST_DATA("SIG\0BUS"),
|
|
|
|
NUL_TEST_DATA("SIGB\0US"),
|
|
|
|
NUL_TEST_DATA("SIGBUS\0"),
|
|
|
|
NUL_TEST_DATA("\0BUS"),
|
|
|
|
NUL_TEST_DATA("BUS\0"),
|
|
|
|
NUL_TEST_DATA("B\0US"),
|
|
|
|
NUL_TEST_DATA("\0002"),
|
|
|
|
NUL_TEST_DATA("2\0"),
|
|
|
|
NUL_TEST_DATA("1\0002"),
|
|
|
|
};
|
|
|
|
|
2019-01-04 16:57:57 -05:00
|
|
|
for (size_t index = 0; index < base::size(kNULTestData); ++index) {
|
2014-09-10 17:30:21 -04:00
|
|
|
SCOPED_TRACE(base::StringPrintf("index %zu", index));
|
|
|
|
base::StringPiece string(kNULTestData[index].string,
|
|
|
|
kNULTestData[index].length);
|
|
|
|
TestStringToSignal(string, options, false, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a NUL is not required at the end of the string.
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("trailing_NUL_full");
|
|
|
|
TestStringToSignal(
|
|
|
|
base::StringPiece("SIGBUST", 6), kAllowFullName, true, SIGBUS);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SCOPED_TRACE("trailing_NUL_short");
|
|
|
|
TestStringToSignal(
|
|
|
|
base::StringPiece("BUST", 3), kAllowShortName, true, SIGBUS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2014-10-07 17:28:50 -04:00
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|