henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/flags.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/ssl_adapter.h" |
| 20 | #include "rtc_base/ssl_stream_adapter.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/field_trial.h" |
| 22 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "test/field_trial.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 24 | #include "test/gtest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
Mirko Bonadei | ba5eaee | 2018-09-17 12:20:10 +0200 | [diff] [blame] | 26 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/win32_socket_init.h" |
Mirko Bonadei | ba5eaee | 2018-09-17 12:20:10 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 30 | #if defined(WEBRTC_IOS) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "test/ios/test_support.h" |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 34 | WEBRTC_DEFINE_bool(help, false, "prints this message"); |
| 35 | WEBRTC_DEFINE_string(log, "", "logging options to use"); |
| 36 | WEBRTC_DEFINE_string( |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 37 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 43 | WEBRTC_DEFINE_int(crt_break_alloc, -1, "memory allocation to break on"); |
| 44 | WEBRTC_DEFINE_bool( |
| 45 | default_error_handlers, |
| 46 | false, |
| 47 | "leave the default exception/dbg handler functions in place"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | |
| 49 | void TestInvalidParameterHandler(const wchar_t* expression, |
| 50 | const wchar_t* function, |
| 51 | const wchar_t* file, |
| 52 | unsigned int line, |
| 53 | uintptr_t pReserved) { |
Karl Wiberg | 4343273 | 2018-05-23 11:13:31 +0200 | [diff] [blame] | 54 | // 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 57 | RTC_LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | exit(1); |
| 59 | } |
| 60 | void TestPureCallHandler() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 61 | RTC_LOG(LS_ERROR) << "Purecall Handler called. Exiting."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | exit(1); |
| 63 | } |
| 64 | int TestCrtReportHandler(int report_type, char* msg, int* retval) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 65 | RTC_LOG(LS_ERROR) << "CrtReport Handler called..."; |
| 66 | RTC_LOG(LS_ERROR) << msg; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | if (report_type == _CRT_ASSERT) { |
| 68 | exit(1); |
| 69 | } else { |
| 70 | *retval = 0; |
| 71 | return TRUE; |
| 72 | } |
| 73 | } |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 74 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 76 | int main(int argc, char* argv[]) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | testing::InitGoogleTest(&argc, argv); |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 78 | rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | if (FLAG_help) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 80 | rtc::FlagList::Print(nullptr, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
Bjorn Terelius | edab301 | 2018-01-31 17:23:40 +0100 | [diff] [blame] | 84 | 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 Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 88 | webrtc::metrics::Enable(); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 89 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | ba5eaee | 2018-09-17 12:20:10 +0200 | [diff] [blame] | 91 | rtc::WinsockInitializer winsock_init; |
| 92 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | 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 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 100 | #if !defined(NDEBUG) // Turn on memory leak checking on Windows. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | if (FLAG_crt_break_alloc >= 0) { |
| 103 | _crtBreakAlloc = FLAG_crt_break_alloc; |
| 104 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 105 | #endif |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 106 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | // By default, log timestamps. Allow overrides by used of a --log flag. |
| 109 | rtc::LogMessage::LogTimestamps(); |
| 110 | if (*FLAG_log != '\0') { |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 111 | rtc::LogMessage::ConfigureLogging(FLAG_log); |
Peter Boström | def5820 | 2015-11-27 17:53:22 +0100 | [diff] [blame] | 112 | } 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 118 | // Initialize SSL which are used by several tests. |
| 119 | rtc::InitializeSSL(); |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 120 | rtc::SSLStreamAdapter::EnableTimeCallbackForTesting(); |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 121 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 122 | #if defined(WEBRTC_IOS) |
Edward Lemur | e66572b | 2018-01-05 15:34:09 +0100 | [diff] [blame] | 123 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false); |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 124 | rtc::test::RunTestsFromIOSApp(); |
| 125 | #endif |
| 126 | const int res = RUN_ALL_TESTS(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 128 | rtc::CleanupSSL(); |
| 129 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | // clean up logging so we don't appear to leak memory. |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 131 | rtc::LogMessage::ConfigureLogging(""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | |
| 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 Gerey | 53347b7 | 2018-10-19 15:04:04 +0200 | [diff] [blame] | 140 | #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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 148 | return res; |
| 149 | } |