blob: e32ca77185e78ec7b609d90dff64a5c87b7639de [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"
18#include "rtc_base/gunit.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/ssladapter.h"
21#include "rtc_base/sslstreamadapter.h"
Bjorn Tereliusedab3012018-01-31 17:23:40 +010022#include "system_wrappers/include/field_trial_default.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/field_trial.h"
24#include "test/testsupport/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +020026#if defined(WEBRTC_IOS)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "test/ios/test_support.h"
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +020028#endif
29
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030DEFINE_bool(help, false, "prints this message");
31DEFINE_string(log, "", "logging options to use");
stefanc1aeaf02015-10-15 07:26:07 -070032DEFINE_string(
33 force_fieldtrials,
34 "",
35 "Field trials control experimental feature code which can be forced. "
36 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
37 " will assign the group Enable to field trial WebRTC-FooFeature.");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038#if defined(WEBRTC_WIN)
39DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
40DEFINE_bool(default_error_handlers, false,
41 "leave the default exception/dbg handler functions in place");
42
43void TestInvalidParameterHandler(const wchar_t* expression,
44 const wchar_t* function,
45 const wchar_t* file,
46 unsigned int line,
47 uintptr_t pReserved) {
Karl Wiberg43432732018-05-23 11:13:31 +020048 // In order to log `expression`, `function`, and `file` here, we would have
49 // to convert them to const char*. std::wcsrtombs can do that, but it's
50 // locale dependent.
Mirko Bonadei675513b2017-11-09 11:09:25 +010051 RTC_LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 exit(1);
53}
54void TestPureCallHandler() {
Mirko Bonadei675513b2017-11-09 11:09:25 +010055 RTC_LOG(LS_ERROR) << "Purecall Handler called. Exiting.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 exit(1);
57}
58int TestCrtReportHandler(int report_type, char* msg, int* retval) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010059 RTC_LOG(LS_ERROR) << "CrtReport Handler called...";
60 RTC_LOG(LS_ERROR) << msg;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 if (report_type == _CRT_ASSERT) {
62 exit(1);
63 } else {
64 *retval = 0;
65 return TRUE;
66 }
67}
Tommi0eefb4d2015-05-23 09:54:07 +020068#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
Robin Raymond1c62ffa2017-12-03 16:45:56 -050070int main(int argc, char* argv[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 testing::InitGoogleTest(&argc, argv);
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000072 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 if (FLAG_help) {
deadbeef37f5ecf2017-02-27 14:06:41 -080074 rtc::FlagList::Print(nullptr, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 return 0;
76 }
77
Niels Möllerb00dc382016-06-16 12:44:30 +020078 webrtc::test::SetExecutablePath(argv[0]);
Bjorn Tereliusedab3012018-01-31 17:23:40 +010079 webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
80 // InitFieldTrialsFromString stores the char*, so the char array must outlive
81 // the application.
82 webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
stefanc1aeaf02015-10-15 07:26:07 -070083
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084#if defined(WEBRTC_WIN)
85 if (!FLAG_default_error_handlers) {
86 // Make sure any errors don't throw dialogs hanging the test run.
87 _set_invalid_parameter_handler(TestInvalidParameterHandler);
88 _set_purecall_handler(TestPureCallHandler);
89 _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler);
90 }
91
tfarinaa41ab932015-10-30 16:08:48 -070092#if !defined(NDEBUG) // Turn on memory leak checking on Windows.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF);
94 if (FLAG_crt_break_alloc >= 0) {
95 _crtBreakAlloc = FLAG_crt_break_alloc;
96 }
tfarinaa41ab932015-10-30 16:08:48 -070097#endif
Tommi0eefb4d2015-05-23 09:54:07 +020098#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 // By default, log timestamps. Allow overrides by used of a --log flag.
101 rtc::LogMessage::LogTimestamps();
102 if (*FLAG_log != '\0') {
Tommi0eefb4d2015-05-23 09:54:07 +0200103 rtc::LogMessage::ConfigureLogging(FLAG_log);
Peter Boströmdef58202015-11-27 17:53:22 +0100104 } else if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) {
105 // Default to LS_INFO, even for release builds to provide better test
106 // logging.
107 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 }
109
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000110 // Initialize SSL which are used by several tests.
111 rtc::InitializeSSL();
deadbeef6cf94a02016-11-28 17:38:34 -0800112 rtc::SSLStreamAdapter::enable_time_callback_for_testing();
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000113
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +0200114#if defined(WEBRTC_IOS)
Edward Lemure66572b2018-01-05 15:34:09 +0100115 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false);
Kári Tristan Helgasone2baffb2017-06-09 10:31:58 +0200116 rtc::test::RunTestsFromIOSApp();
117#endif
118 const int res = RUN_ALL_TESTS();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000120 rtc::CleanupSSL();
121
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 // clean up logging so we don't appear to leak memory.
Tommi0eefb4d2015-05-23 09:54:07 +0200123 rtc::LogMessage::ConfigureLogging("");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124
125#if defined(WEBRTC_WIN)
126 // Unhook crt function so that we don't ever log after statics have been
127 // uninitialized.
128 if (!FLAG_default_error_handlers)
129 _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
130#endif
131
132 return res;
133}