blob: def010465d2d82c40ef75cbe33494230d3bcb587 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10//
11// A reuseable entry point for gunit tests.
12
13#if defined(WEBRTC_WIN)
14#include <crtdbg.h>
15#endif
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/flags.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/ssl_adapter.h"
20#include "rtc_base/ssl_stream_adapter.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020021#include "system_wrappers/include/field_trial.h"
22#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/field_trial.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Mirko Bonadeiba5eaee2018-09-17 12:20:10 +020026#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/win32_socket_init.h"
Mirko Bonadeiba5eaee2018-09-17 12:20:10 +020028#endif
29
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +020030#if defined(WEBRTC_IOS)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "test/ios/test_support.h"
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +020032#endif
33
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020034WEBRTC_DEFINE_bool(help, false, "prints this message");
35WEBRTC_DEFINE_string(log, "", "logging options to use");
36WEBRTC_DEFINE_string(
stefanc1aeaf02015-10-15 07:26:07 -070037 force_fieldtrials,
38 "",
39 "Field trials control experimental feature code which can be forced. "
40 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
41 " will assign the group Enable to field trial WebRTC-FooFeature.");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042#if defined(WEBRTC_WIN)
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020043WEBRTC_DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
44WEBRTC_DEFINE_bool(
45 default_error_handlers,
46 false,
47 "leave the default exception/dbg handler functions in place");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49void TestInvalidParameterHandler(const wchar_t* expression,
50 const wchar_t* function,
51 const wchar_t* file,
52 unsigned int line,
53 uintptr_t pReserved) {
Karl Wiberg43432732018-05-23 11:13:31 +020054 // In order to log `expression`, `function`, and `file` here, we would have
55 // to convert them to const char*. std::wcsrtombs can do that, but it's
56 // locale dependent.
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 exit(1);
59}
60void TestPureCallHandler() {
Mirko Bonadei675513b2017-11-09 11:09:25 +010061 RTC_LOG(LS_ERROR) << "Purecall Handler called. Exiting.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 exit(1);
63}
64int TestCrtReportHandler(int report_type, char* msg, int* retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010065 RTC_LOG(LS_ERROR) << "CrtReport Handler called...";
66 RTC_LOG(LS_ERROR) << msg;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 if (report_type == _CRT_ASSERT) {
68 exit(1);
69 } else {
70 *retval = 0;
71 return TRUE;
72 }
73}
Tommi0eefb4d2015-05-23 09:54:07 +020074#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075
Robin Raymond1c62ffa2017-12-03 16:45:56 -050076int main(int argc, char* argv[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 testing::InitGoogleTest(&argc, argv);
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000078 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 if (FLAG_help) {
deadbeef37f5ecf2017-02-27 14:06:41 -080080 rtc::FlagList::Print(nullptr, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 return 0;
82 }
83
Bjorn Tereliusedab3012018-01-31 17:23:40 +010084 webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
85 // InitFieldTrialsFromString stores the char*, so the char array must outlive
86 // the application.
87 webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
Qingsi Wang7fc821d2018-07-12 12:54:53 -070088 webrtc::metrics::Enable();
stefanc1aeaf02015-10-15 07:26:07 -070089
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090#if defined(WEBRTC_WIN)
Mirko Bonadeiba5eaee2018-09-17 12:20:10 +020091 rtc::WinsockInitializer winsock_init;
92
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 if (!FLAG_default_error_handlers) {
94 // Make sure any errors don't throw dialogs hanging the test run.
95 _set_invalid_parameter_handler(TestInvalidParameterHandler);
96 _set_purecall_handler(TestPureCallHandler);
97 _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler);
98 }
99
tfarinaa41ab932015-10-30 16:08:48 -0700100#if !defined(NDEBUG) // Turn on memory leak checking on Windows.
Yves Gerey665174f2018-06-19 15:03:05 +0200101 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 if (FLAG_crt_break_alloc >= 0) {
103 _crtBreakAlloc = FLAG_crt_break_alloc;
104 }
tfarinaa41ab932015-10-30 16:08:48 -0700105#endif
Tommi0eefb4d2015-05-23 09:54:07 +0200106#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 // By default, log timestamps. Allow overrides by used of a --log flag.
109 rtc::LogMessage::LogTimestamps();
110 if (*FLAG_log != '\0') {
Tommi0eefb4d2015-05-23 09:54:07 +0200111 rtc::LogMessage::ConfigureLogging(FLAG_log);
Peter Boströmdef58202015-11-27 17:53:22 +0100112 } else if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) {
113 // Default to LS_INFO, even for release builds to provide better test
114 // logging.
115 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 }
117
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000118 // Initialize SSL which are used by several tests.
119 rtc::InitializeSSL();
Benjamin Wrightb19b4972018-10-25 10:46:49 -0700120 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000121
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +0200122#if defined(WEBRTC_IOS)
Edward Lemure66572b2018-01-05 15:34:09 +0100123 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false);
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +0200124 rtc::test::RunTestsFromIOSApp();
125#endif
126 const int res = RUN_ALL_TESTS();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000128 rtc::CleanupSSL();
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 // clean up logging so we don't appear to leak memory.
Tommi0eefb4d2015-05-23 09:54:07 +0200131 rtc::LogMessage::ConfigureLogging("");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132
133#if defined(WEBRTC_WIN)
134 // Unhook crt function so that we don't ever log after statics have been
135 // uninitialized.
136 if (!FLAG_default_error_handlers)
137 _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
138#endif
139
Yves Gerey53347b72018-10-19 15:04:04 +0200140#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
141 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
142 defined(UNDEFINED_SANITIZER)
143 // We want the test flagged as failed only for sanitizer defects,
144 // in which case the sanitizer will override exit code with 66.
145 return 0;
146#endif
147
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 return res;
149}