Add ability to disable detailed error message in RTC_CHECKs

Bug: webrtc:11133
Change-Id: I989654f1fb97b476a17956d69ee374406439ea8f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/160653
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29952}
This commit is contained in:
Artem Titov 2019-11-28 17:09:30 +01:00 committed by Commit Bot
parent 9750e84d7a
commit 9dc209a23a
21 changed files with 275 additions and 82 deletions

View File

@ -123,6 +123,10 @@ config("common_inherited_config") {
defines += [ "WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS" ] defines += [ "WEBRTC_EXCLUDE_BUILT_IN_SSL_ROOT_CERTS" ]
} }
if (rtc_disable_check_msg) {
defines += [ "RTC_DISABLE_CHECK_MSG" ]
}
# Some tests need to declare their own trace event handlers. If this define is # Some tests need to declare their own trace event handlers. If this define is
# not set, the first time TRACE_EVENT_* is called it will store the return # not set, the first time TRACE_EVENT_* is called it will store the return
# value for the current handler in an static variable, so that subsequent # value for the current handler in an static variable, so that subsequent

View File

@ -366,6 +366,7 @@ if (rtc_include_tests) {
"../rtc_base/system:arch", "../rtc_base/system:arch",
"../system_wrappers:cpu_features_api", "../system_wrappers:cpu_features_api",
"../test:fileutils", "../test:fileutils",
"../test:rtc_expect_death",
"../test:test_main", "../test:test_main",
"../test:test_support", "../test:test_support",
"//testing/gtest", "//testing/gtest",

View File

@ -11,6 +11,7 @@
#include "common_audio/channel_buffer.h" #include "common_audio/channel_buffer.h"
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
namespace webrtc { namespace webrtc {
@ -54,12 +55,12 @@ TEST(IFChannelBufferTest, SettingNumChannelsOfOneChannelBufferSetsTheOther) {
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(ChannelBufferTest, SetNumChannelsDeathTest) { TEST(ChannelBufferTest, SetNumChannelsDeathTest) {
ChannelBuffer<float> chb(kNumFrames, kMono); ChannelBuffer<float> chb(kNumFrames, kMono);
EXPECT_DEATH(chb.set_num_channels(kStereo), "num_channels"); RTC_EXPECT_DEATH(chb.set_num_channels(kStereo), "num_channels");
} }
TEST(IFChannelBufferTest, SetNumChannelsDeathTest) { TEST(IFChannelBufferTest, SetNumChannelsDeathTest) {
IFChannelBuffer ifchb(kNumFrames, kMono); IFChannelBuffer ifchb(kNumFrames, kMono);
EXPECT_DEATH(ifchb.ibuf()->set_num_channels(kStereo), "num_channels"); RTC_EXPECT_DEATH(ifchb.ibuf()->set_num_channels(kStereo), "num_channels");
} }
#endif #endif

View File

@ -12,6 +12,7 @@
#include "rtc_base/checks.h" // RTC_DCHECK_IS_ON #include "rtc_base/checks.h" // RTC_DCHECK_IS_ON
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
// Quality testing of PushResampler is handled through output_mixer_unittest.cc. // Quality testing of PushResampler is handled through output_mixer_unittest.cc.
@ -32,19 +33,20 @@ TEST(PushResamplerTest, VerifiesInputParameters) {
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(PushResamplerTest, VerifiesBadInputParameters1) { TEST(PushResamplerTest, VerifiesBadInputParameters1) {
PushResampler<int16_t> resampler; PushResampler<int16_t> resampler;
EXPECT_DEATH(resampler.InitializeIfNeeded(-1, 16000, 1), RTC_EXPECT_DEATH(resampler.InitializeIfNeeded(-1, 16000, 1),
"src_sample_rate_hz"); "src_sample_rate_hz");
} }
TEST(PushResamplerTest, VerifiesBadInputParameters2) { TEST(PushResamplerTest, VerifiesBadInputParameters2) {
PushResampler<int16_t> resampler; PushResampler<int16_t> resampler;
EXPECT_DEATH(resampler.InitializeIfNeeded(16000, -1, 1), RTC_EXPECT_DEATH(resampler.InitializeIfNeeded(16000, -1, 1),
"dst_sample_rate_hz"); "dst_sample_rate_hz");
} }
TEST(PushResamplerTest, VerifiesBadInputParameters3) { TEST(PushResamplerTest, VerifiesBadInputParameters3) {
PushResampler<int16_t> resampler; PushResampler<int16_t> resampler;
EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 0), "num_channels"); RTC_EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 0),
"num_channels");
} }
#endif #endif

View File

@ -2123,6 +2123,7 @@ if (rtc_include_tests) {
"../../test:audio_codec_mocks", "../../test:audio_codec_mocks",
"../../test:field_trial", "../../test:field_trial",
"../../test:fileutils", "../../test:fileutils",
"../../test:rtc_expect_death",
"../../test:rtp_test_utils", "../../test:rtp_test_utils",
"../../test:test_common", "../../test:test_common",
"../../test:test_support", "../../test:test_support",

View File

@ -54,6 +54,7 @@
#include "test/mock_audio_decoder.h" #include "test/mock_audio_decoder.h"
#include "test/mock_audio_encoder.h" #include "test/mock_audio_encoder.h"
#include "test/testsupport/file_utils.h" #include "test/testsupport/file_utils.h"
#include "test/testsupport/rtc_expect_death.h"
using ::testing::_; using ::testing::_;
using ::testing::AtLeast; using ::testing::AtLeast;
@ -272,8 +273,8 @@ TEST_F(AudioCodingModuleTestOldApi, VerifyOutputFrame) {
TEST_F(AudioCodingModuleTestOldApi, FailOnZeroDesiredFrequency) { TEST_F(AudioCodingModuleTestOldApi, FailOnZeroDesiredFrequency) {
AudioFrame audio_frame; AudioFrame audio_frame;
bool muted; bool muted;
EXPECT_DEATH(acm_->PlayoutData10Ms(0, &audio_frame, &muted), RTC_EXPECT_DEATH(acm_->PlayoutData10Ms(0, &audio_frame, &muted),
"dst_sample_rate_hz"); "dst_sample_rate_hz");
} }
#endif #endif

View File

@ -18,6 +18,7 @@
#include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/numerics/safe_conversions.h"
#include "test/gtest.h" #include "test/gtest.h"
#include "test/mock_audio_encoder.h" #include "test/mock_audio_encoder.h"
#include "test/testsupport/rtc_expect_death.h"
using ::testing::_; using ::testing::_;
using ::testing::InSequence; using ::testing::InSequence;
@ -441,7 +442,7 @@ class AudioEncoderCngDeathTest : public AudioEncoderCngTest {
} }
void TryWrongNumCoefficients(int num) { void TryWrongNumCoefficients(int num) {
EXPECT_DEATH( RTC_EXPECT_DEATH(
[&] { [&] {
auto config = MakeCngConfig(); auto config = MakeCngConfig();
config.num_cng_coefficients = num; config.num_cng_coefficients = num;
@ -454,9 +455,9 @@ class AudioEncoderCngDeathTest : public AudioEncoderCngTest {
TEST_F(AudioEncoderCngDeathTest, WrongFrameSize) { TEST_F(AudioEncoderCngDeathTest, WrongFrameSize) {
CreateCng(MakeCngConfig()); CreateCng(MakeCngConfig());
num_audio_samples_10ms_ *= 2; // 20 ms frame. num_audio_samples_10ms_ *= 2; // 20 ms frame.
EXPECT_DEATH(Encode(), ""); RTC_EXPECT_DEATH(Encode(), "");
num_audio_samples_10ms_ = 0; // Zero samples. num_audio_samples_10ms_ = 0; // Zero samples.
EXPECT_DEATH(Encode(), ""); RTC_EXPECT_DEATH(Encode(), "");
} }
TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsA) { TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsA) {
@ -474,16 +475,16 @@ TEST_F(AudioEncoderCngDeathTest, WrongNumCoefficientsC) {
TEST_F(AudioEncoderCngDeathTest, NullSpeechEncoder) { TEST_F(AudioEncoderCngDeathTest, NullSpeechEncoder) {
auto config = MakeCngConfig(); auto config = MakeCngConfig();
config.speech_encoder = nullptr; config.speech_encoder = nullptr;
EXPECT_DEATH(CreateCng(std::move(config)), ""); RTC_EXPECT_DEATH(CreateCng(std::move(config)), "");
} }
TEST_F(AudioEncoderCngDeathTest, StereoEncoder) { TEST_F(AudioEncoderCngDeathTest, StereoEncoder) {
EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(2)); EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(2));
EXPECT_DEATH(CreateCng(MakeCngConfig()), "Invalid configuration"); RTC_EXPECT_DEATH(CreateCng(MakeCngConfig()), "Invalid configuration");
} }
TEST_F(AudioEncoderCngDeathTest, StereoConfig) { TEST_F(AudioEncoderCngDeathTest, StereoConfig) {
EXPECT_DEATH( RTC_EXPECT_DEATH(
[&] { [&] {
auto config = MakeCngConfig(); auto config = MakeCngConfig();
config.num_channels = 2; config.num_channels = 2;
@ -498,8 +499,8 @@ TEST_F(AudioEncoderCngDeathTest, EncoderFrameSizeTooLarge) {
.WillRepeatedly(Return(7U)); .WillRepeatedly(Return(7U));
for (int i = 0; i < 6; ++i) for (int i = 0; i < 6; ++i)
Encode(); Encode();
EXPECT_DEATH(Encode(), RTC_EXPECT_DEATH(
"Frame size cannot be larger than 60 ms when using VAD/CNG."); Encode(), "Frame size cannot be larger than 60 ms when using VAD/CNG.");
} }
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_DEATH_TEST

View File

@ -17,6 +17,7 @@
#include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/numerics/safe_conversions.h"
#include "test/gtest.h" #include "test/gtest.h"
#include "test/mock_audio_encoder.h" #include "test/mock_audio_encoder.h"
#include "test/testsupport/rtc_expect_death.h"
using ::testing::_; using ::testing::_;
using ::testing::InSequence; using ::testing::InSequence;
@ -285,17 +286,17 @@ class AudioEncoderCopyRedDeathTest : public AudioEncoderCopyRedTest {
TEST_F(AudioEncoderCopyRedDeathTest, WrongFrameSize) { TEST_F(AudioEncoderCopyRedDeathTest, WrongFrameSize) {
num_audio_samples_10ms *= 2; // 20 ms frame. num_audio_samples_10ms *= 2; // 20 ms frame.
EXPECT_DEATH(Encode(), ""); RTC_EXPECT_DEATH(Encode(), "");
num_audio_samples_10ms = 0; // Zero samples. num_audio_samples_10ms = 0; // Zero samples.
EXPECT_DEATH(Encode(), ""); RTC_EXPECT_DEATH(Encode(), "");
} }
TEST_F(AudioEncoderCopyRedDeathTest, NullSpeechEncoder) { TEST_F(AudioEncoderCopyRedDeathTest, NullSpeechEncoder) {
AudioEncoderCopyRed* red = NULL; AudioEncoderCopyRed* red = NULL;
AudioEncoderCopyRed::Config config; AudioEncoderCopyRed::Config config;
config.speech_encoder = NULL; config.speech_encoder = NULL;
EXPECT_DEATH(red = new AudioEncoderCopyRed(std::move(config)), RTC_EXPECT_DEATH(red = new AudioEncoderCopyRed(std::move(config)),
"Speech encoder not provided."); "Speech encoder not provided.");
// The delete operation is needed to avoid leak reports from memcheck. // The delete operation is needed to avoid leak reports from memcheck.
delete red; delete red;
} }

View File

@ -448,6 +448,7 @@ if (rtc_include_tests) {
"../../system_wrappers", "../../system_wrappers",
"../../system_wrappers:cpu_features_api", "../../system_wrappers:cpu_features_api",
"../../test:fileutils", "../../test:fileutils",
"../../test:rtc_expect_death",
"../../test:test_support", "../../test:test_support",
"../audio_coding:neteq_input_audio_tools", "../audio_coding:neteq_input_audio_tools",
"aec:aec_core", "aec:aec_core",

View File

@ -11,7 +11,9 @@
#include "modules/audio_processing/audio_buffer.h" #include "modules/audio_processing/audio_buffer.h"
#include <cmath> #include <cmath>
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
namespace webrtc { namespace webrtc {
@ -41,7 +43,7 @@ TEST(AudioBufferTest, SetNumChannelsSetsChannelBuffersNumChannels) {
TEST(AudioBufferTest, SetNumChannelsDeathTest) { TEST(AudioBufferTest, SetNumChannelsDeathTest) {
AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz, AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz,
kMono); kMono);
EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels"); RTC_EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels");
} }
#endif #endif

View File

@ -1168,6 +1168,7 @@ if (rtc_include_tests) {
"buffer_unittest.cc", "buffer_unittest.cc",
"byte_buffer_unittest.cc", "byte_buffer_unittest.cc",
"byte_order_unittest.cc", "byte_order_unittest.cc",
"checks_unittest.cc",
"copy_on_write_buffer_unittest.cc", "copy_on_write_buffer_unittest.cc",
"critical_section_unittest.cc", "critical_section_unittest.cc",
"event_tracer_unittest.cc", "event_tracer_unittest.cc",

View File

@ -59,6 +59,7 @@ void AppendFormat(std::string* s, const char* fmt, ...) {
namespace rtc { namespace rtc {
namespace webrtc_checks_impl { namespace webrtc_checks_impl {
#if RTC_CHECK_MSG_ENABLED
// Reads one argument from args, appends it to s and advances fmt. // Reads one argument from args, appends it to s and advances fmt.
// Returns true iff an argument was sucessfully parsed. // Returns true iff an argument was sucessfully parsed.
bool ParseArg(va_list* args, const CheckArgType** fmt, std::string* s) { bool ParseArg(va_list* args, const CheckArgType** fmt, std::string* s) {
@ -162,6 +163,32 @@ RTC_NORETURN void FatalLog(const char* file,
#endif #endif
abort(); abort();
} }
#else // RTC_CHECK_MSG_ENABLED
RTC_NORETURN void FatalLog(const char* file, int line) {
std::string s;
AppendFormat(&s,
"\n\n"
"#\n"
"# Fatal error in: %s, line %d\n"
"# last system error: %u\n"
"# Check failed.\n"
"# ",
file, line, LAST_SYSTEM_ERROR);
const char* output = s.c_str();
#if defined(WEBRTC_ANDROID)
__android_log_print(ANDROID_LOG_ERROR, RTC_LOG_TAG_ANDROID, "%s\n", output);
#endif
fflush(stdout);
fprintf(stderr, "%s", output);
fflush(stderr);
#if defined(WEBRTC_WIN)
DebugBreak();
#endif
abort();
}
#endif // RTC_CHECK_MSG_ENABLED
} // namespace webrtc_checks_impl } // namespace webrtc_checks_impl
} // namespace rtc } // namespace rtc
@ -170,7 +197,11 @@ RTC_NORETURN void FatalLog(const char* file,
RTC_NORETURN void rtc_FatalMessage(const char* file, RTC_NORETURN void rtc_FatalMessage(const char* file,
int line, int line,
const char* msg) { const char* msg) {
#if RTC_CHECK_MSG_ENABLED
static constexpr rtc::webrtc_checks_impl::CheckArgType t[] = { static constexpr rtc::webrtc_checks_impl::CheckArgType t[] = {
rtc::webrtc_checks_impl::CheckArgType::kEnd}; rtc::webrtc_checks_impl::CheckArgType::kEnd};
FatalLog(file, line, msg, t); rtc::webrtc_checks_impl::FatalLog(file, line, msg, t);
#else
rtc::webrtc_checks_impl::FatalLog(file, line);
#endif
} }

View File

@ -37,6 +37,18 @@ RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg);
} // extern "C" } // extern "C"
#endif #endif
#ifdef RTC_DISABLE_CHECK_MSG
#define RTC_CHECK_MSG_ENABLED 0
#else
#define RTC_CHECK_MSG_ENABLED 1
#endif
#if RTC_CHECK_MSG_ENABLED
#define RTC_CHECK_EVAL_MESSAGE(message) message
#else
#define RTC_CHECK_EVAL_MESSAGE(message) ""
#endif
#ifdef __cplusplus #ifdef __cplusplus
// C++ version. // C++ version.
@ -109,11 +121,15 @@ enum class CheckArgType : int8_t {
kCheckOp, kCheckOp,
}; };
#if RTC_CHECK_MSG_ENABLED
RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, RTC_NORETURN RTC_EXPORT void FatalLog(const char* file,
int line, int line,
const char* message, const char* message,
const CheckArgType* fmt, const CheckArgType* fmt,
...); ...);
#else
RTC_NORETURN RTC_EXPORT void FatalLog(const char* file, int line);
#endif
// Wrapper for log arguments. Only ever make values of this type with the // Wrapper for log arguments. Only ever make values of this type with the
// MakeVal() functions. // MakeVal() functions.
@ -214,6 +230,7 @@ class LogStreamer<> final {
return LogStreamer<V>(MakeVal(arg), this); return LogStreamer<V>(MakeVal(arg), this);
} }
#if RTC_CHECK_MSG_ENABLED
template <typename... Us> template <typename... Us>
RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file, RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
const int line, const int line,
@ -232,6 +249,13 @@ class LogStreamer<> final {
CheckArgType::kEnd}; CheckArgType::kEnd};
FatalLog(file, line, message, t, args.GetVal()...); FatalLog(file, line, message, t, args.GetVal()...);
} }
#else
template <typename... Us>
RTC_NORETURN RTC_FORCE_INLINE static void Call(const char* file,
const int line) {
FatalLog(file, line);
}
#endif
}; };
// Inductive case: We've already seen at least one << argument. The most recent // Inductive case: We've already seen at least one << argument. The most recent
@ -258,6 +282,7 @@ class LogStreamer<T, Ts...> final {
return LogStreamer<V, T, Ts...>(MakeVal(arg), this); return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
} }
#if RTC_CHECK_MSG_ENABLED
template <typename... Us> template <typename... Us>
RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file, RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
const int line, const int line,
@ -273,6 +298,13 @@ class LogStreamer<T, Ts...> final {
const Us&... args) const { const Us&... args) const {
prior_->CallCheckOp(file, line, message, arg_, args...); prior_->CallCheckOp(file, line, message, arg_, args...);
} }
#else
template <typename... Us>
RTC_NORETURN RTC_FORCE_INLINE void Call(const char* file,
const int line) const {
prior_->Call(file, line);
}
#endif
private: private:
// The most recent argument. // The most recent argument.
@ -292,8 +324,12 @@ class FatalLogCall final {
template <typename... Ts> template <typename... Ts>
RTC_NORETURN RTC_FORCE_INLINE void operator&( RTC_NORETURN RTC_FORCE_INLINE void operator&(
const LogStreamer<Ts...>& streamer) { const LogStreamer<Ts...>& streamer) {
#if RTC_CHECK_MSG_ENABLED
isCheckOp ? streamer.CallCheckOp(file_, line_, message_) isCheckOp ? streamer.CallCheckOp(file_, line_, message_)
: streamer.Call(file_, line_, message_); : streamer.Call(file_, line_, message_);
#else
streamer.Call(file_, line_);
#endif
} }
private: private:
@ -301,6 +337,7 @@ class FatalLogCall final {
int line_; int line_;
const char* message_; const char* message_;
}; };
} // namespace webrtc_checks_impl } // namespace webrtc_checks_impl
// The actual stream used isn't important. We reference |ignored| in the code // The actual stream used isn't important. We reference |ignored| in the code
@ -326,19 +363,37 @@ class FatalLogCall final {
// //
// We make sure RTC_CHECK et al. always evaluates |condition|, as // We make sure RTC_CHECK et al. always evaluates |condition|, as
// doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom. // doing RTC_CHECK(FunctionWithSideEffect()) is a common idiom.
//
// RTC_CHECK_OP is a helper macro for binary operators.
// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
#if RTC_CHECK_MSG_ENABLED
#define RTC_CHECK(condition) \ #define RTC_CHECK(condition) \
while (!(condition)) \ while (!(condition)) \
rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \ rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, \
#condition) & \ #condition) & \
rtc::webrtc_checks_impl::LogStreamer<>() rtc::webrtc_checks_impl::LogStreamer<>()
// Helper macro for binary operators.
// Don't use this macro directly in your code, use RTC_CHECK_EQ et al below.
#define RTC_CHECK_OP(name, op, val1, val2) \ #define RTC_CHECK_OP(name, op, val1, val2) \
while (!rtc::Safe##name((val1), (val2))) \ while (!rtc::Safe##name((val1), (val2))) \
rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \ rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, \
#val1 " " #op " " #val2) & \ #val1 " " #op " " #val2) & \
rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2) rtc::webrtc_checks_impl::LogStreamer<>() << (val1) << (val2)
#else
#define RTC_CHECK(condition) \
while (!(condition)) \
true \
? rtc::webrtc_checks_impl::FatalLogCall<false>(__FILE__, __LINE__, "") & \
rtc::webrtc_checks_impl::LogStreamer<>() \
: rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
rtc::webrtc_checks_impl::LogStreamer<>()
#define RTC_CHECK_OP(name, op, val1, val2) \
while (!rtc::Safe##name((val1), (val2))) \
true ? rtc::webrtc_checks_impl::FatalLogCall<true>(__FILE__, __LINE__, "") & \
rtc::webrtc_checks_impl::LogStreamer<>() \
: rtc::webrtc_checks_impl::FatalLogCall<false>("", 0, "") & \
rtc::webrtc_checks_impl::LogStreamer<>()
#endif
#define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2) #define RTC_CHECK_EQ(val1, val2) RTC_CHECK_OP(Eq, ==, val1, val2)
#define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2) #define RTC_CHECK_NE(val1, val2) RTC_CHECK_OP(Ne, !=, val1, val2)
@ -391,11 +446,12 @@ inline T CheckedDivExact(T a, T b) {
// C version. Lacks many features compared to the C++ version, but usage // C version. Lacks many features compared to the C++ version, but usage
// guidelines are the same. // guidelines are the same.
#define RTC_CHECK(condition) \ #define RTC_CHECK(condition) \
do { \ do { \
if (!(condition)) { \ if (!(condition)) { \
rtc_FatalMessage(__FILE__, __LINE__, "CHECK failed: " #condition); \ rtc_FatalMessage(__FILE__, __LINE__, \
} \ RTC_CHECK_EVAL_MESSAGE("CHECK failed: " #condition)); \
} \
} while (0) } while (0)
#define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b)) #define RTC_CHECK_EQ(a, b) RTC_CHECK((a) == (b))
@ -405,11 +461,12 @@ inline T CheckedDivExact(T a, T b) {
#define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b)) #define RTC_CHECK_GE(a, b) RTC_CHECK((a) >= (b))
#define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b)) #define RTC_CHECK_GT(a, b) RTC_CHECK((a) > (b))
#define RTC_DCHECK(condition) \ #define RTC_DCHECK(condition) \
do { \ do { \
if (RTC_DCHECK_IS_ON && !(condition)) { \ if (RTC_DCHECK_IS_ON && !(condition)) { \
rtc_FatalMessage(__FILE__, __LINE__, "DCHECK failed: " #condition); \ rtc_FatalMessage(__FILE__, __LINE__, \
} \ RTC_CHECK_EVAL_MESSAGE("DCHECK failed: " #condition)); \
} \
} while (0) } while (0)
#define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b)) #define RTC_DCHECK_EQ(a, b) RTC_DCHECK((a) == (b))

View File

@ -0,0 +1,73 @@
/*
* Copyright 2019 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/checks.h"
#include "test/gtest.h"
TEST(ChecksTest, ExpressionNotEvaluatedWhenCheckPassing) {
int i = 0;
RTC_CHECK(true) << "i=" << ++i;
RTC_CHECK_EQ(i, 0) << "Previous check passed, but i was incremented!";
}
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(ChecksTest, Checks) {
#if RTC_CHECK_MSG_ENABLED
EXPECT_DEATH(FATAL() << "message",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: FATAL\\(\\)\n"
"# message");
int a = 1, b = 2;
EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: a == b \\(1 vs. 2\\)\n"
"# 12");
RTC_CHECK_EQ(5, 5);
RTC_CHECK(true) << "Shouldn't crash" << 1;
EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: false\n"
"# Hi there!");
#else
EXPECT_DEATH(FATAL() << "message",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed.\n"
"# ");
int a = 1, b = 2;
EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed.\n"
"# ");
RTC_CHECK_EQ(5, 5);
RTC_CHECK(true) << "Shouldn't crash" << 1;
EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed.\n"
"# ");
#endif // RTC_CHECK_MSG_ENABLED
}
#endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)

View File

@ -200,34 +200,6 @@ TEST(LogTest, SingleStream) {
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr)); EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
} }
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(LogTest, Checks) {
EXPECT_DEATH(FATAL() << "message",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: FATAL\\(\\)\n"
"# message");
int a = 1, b = 2;
EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: a == b \\(1 vs. 2\\)\n"
"# 12");
RTC_CHECK_EQ(5, 5);
RTC_CHECK(true) << "Shouldn't crash" << 1;
EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
"\n\n#\n"
"# Fatal error in: \\S+, line \\w+\n"
"# last system error: \\w+\n"
"# Check failed: false\n"
"# Hi there!");
}
#endif
// Test using multiple log streams. The INFO stream should get the INFO message, // Test using multiple log streams. The INFO stream should get the INFO message,
// the VERBOSE stream should get the INFO and the VERBOSE. // the VERBOSE stream should get the INFO and the VERBOSE.
// We should restore the correct global state at the end. // We should restore the correct global state at the end.

View File

@ -173,6 +173,7 @@ if (rtc_include_tests) {
":system_wrappers", ":system_wrappers",
"../rtc_base:checks", "../rtc_base:checks",
"../rtc_base:rtc_base_approved", "../rtc_base:rtc_base_approved",
"../test:rtc_expect_death",
"../test:test_main", "../test:test_main",
"../test:test_support", "../test:test_support",
"//testing/gtest", "//testing/gtest",

View File

@ -11,6 +11,7 @@
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
namespace webrtc { namespace webrtc {
namespace field_trial { namespace field_trial {
@ -28,23 +29,24 @@ TEST(FieldTrialValidationTest, AcceptsValidInputs) {
TEST(FieldTrialValidationTest, RejectsBadInputs) { TEST(FieldTrialValidationTest, RejectsBadInputs) {
// Bad delimiters // Bad delimiters
EXPECT_DEATH(InitFieldTrialsFromString("Audio/EnabledVideo/Disabled/"), RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/EnabledVideo/Disabled/"),
"Invalid field trials string:"); "Invalid field trials string:");
EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled//Video/Disabled/"), RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled//Video/Disabled/"),
"Invalid field trials string:"); "Invalid field trials string:");
EXPECT_DEATH(InitFieldTrialsFromString("/Audio/Enabled/Video/Disabled/"), RTC_EXPECT_DEATH(InitFieldTrialsFromString("/Audio/Enabled/Video/Disabled/"),
"Invalid field trials string:"); "Invalid field trials string:");
EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Video/Disabled"), RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Video/Disabled"),
"Invalid field trials string:"); "Invalid field trials string:");
EXPECT_DEATH( RTC_EXPECT_DEATH(
InitFieldTrialsFromString("Audio/Enabled/Video/Disabled/garbage"), InitFieldTrialsFromString("Audio/Enabled/Video/Disabled/garbage"),
"Invalid field trials string:"); "Invalid field trials string:");
// Duplicate trials with different values is not fine // Duplicate trials with different values is not fine
EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Audio/Disabled/"), RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Audio/Disabled/"),
"Invalid field trials string:"); "Invalid field trials string:");
EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/B/C/Audio/Disabled/"), RTC_EXPECT_DEATH(
"Invalid field trials string:"); InitFieldTrialsFromString("Audio/Enabled/B/C/Audio/Disabled/"),
"Invalid field trials string:");
} }
#endif // GTEST_HAS_DEATH_TEST && RTC_DCHECK_IS_ON && !defined(WEBRTC_ANDROID) #endif // GTEST_HAS_DEATH_TEST && RTC_DCHECK_IS_ON && !defined(WEBRTC_ANDROID)
// && !defined(WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT) // && !defined(WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT)

View File

@ -385,6 +385,7 @@ if (rtc_include_tests) {
":fileutils", ":fileutils",
":fileutils_unittests", ":fileutils_unittests",
":perf_test", ":perf_test",
":rtc_expect_death",
":rtp_test_utils", ":rtp_test_utils",
":test_main", ":test_main",
":test_support", ":test_support",
@ -714,6 +715,16 @@ rtc_library("encoder_settings") {
] ]
} }
rtc_library("rtc_expect_death") {
testonly = true
sources = [
"testsupport/rtc_expect_death.h",
]
deps = [
":test_support",
]
}
rtc_library("test_common") { rtc_library("test_common") {
testonly = true testonly = true
sources = [ sources = [

View File

@ -15,6 +15,7 @@
#include <string> #include <string>
#include "test/gtest.h" #include "test/gtest.h"
#include "test/testsupport/rtc_expect_death.h"
namespace { namespace {
@ -113,16 +114,18 @@ TEST_F(PerfDeathTest, TestFiniteResultError) {
const double kNan = std::numeric_limits<double>::quiet_NaN(); const double kNan = std::numeric_limits<double>::quiet_NaN();
const double kInf = std::numeric_limits<double>::infinity(); const double kInf = std::numeric_limits<double>::infinity();
EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit"); RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit"); RTC_EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false), ""); RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false),
EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false), ""); "");
RTC_EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false),
"");
const double kNanList[] = {kNan, kNan}; const double kNanList[] = {kNan, kNan};
EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), ""); RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
const double kInfList[] = {0, kInf}; const double kInfList[] = {0, kInf};
EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), ""); RTC_EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
} }
#endif #endif

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef TEST_TESTSUPPORT_RTC_EXPECT_DEATH_H_
#define TEST_TESTSUPPORT_RTC_EXPECT_DEATH_H_
#include "test/gtest.h"
#if RTC_CHECK_MSG_ENABLED
#define RTC_EXPECT_DEATH(statement, regex) EXPECT_DEATH(statement, regex)
#else
// If RTC_CHECKs messages are disabled we can't validate failure message
#define RTC_EXPECT_DEATH(statement, regex) EXPECT_DEATH(statement, "")
#endif
#endif // TEST_TESTSUPPORT_RTC_EXPECT_DEATH_H_

View File

@ -248,6 +248,10 @@ declare_args() {
# Set this to true to disable trace events. # Set this to true to disable trace events.
rtc_disable_trace_events = false rtc_disable_trace_events = false
# Set this to true to disable detailed error message and logging for
# RTC_CHECKs.
rtc_disable_check_msg = false
} }
# Make it possible to provide custom locations for some libraries (move these # Make it possible to provide custom locations for some libraries (move these