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" |
| 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 Terelius | edab301 | 2018-01-31 17:23:40 +0100 | [diff] [blame] | 22 | #include "system_wrappers/include/field_trial_default.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "test/field_trial.h" |
| 24 | #include "test/testsupport/fileutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 26 | #if defined(WEBRTC_IOS) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "test/ios/test_support.h" |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | DEFINE_bool(help, false, "prints this message"); |
| 31 | DEFINE_string(log, "", "logging options to use"); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 32 | DEFINE_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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | #if defined(WEBRTC_WIN) |
| 39 | DEFINE_int(crt_break_alloc, -1, "memory allocation to break on"); |
| 40 | DEFINE_bool(default_error_handlers, false, |
| 41 | "leave the default exception/dbg handler functions in place"); |
| 42 | |
| 43 | void TestInvalidParameterHandler(const wchar_t* expression, |
| 44 | const wchar_t* function, |
| 45 | const wchar_t* file, |
| 46 | unsigned int line, |
| 47 | uintptr_t pReserved) { |
Karl Wiberg | 4343273 | 2018-05-23 11:13:31 +0200 | [diff] [blame] | 48 | // 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 51 | RTC_LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | exit(1); |
| 53 | } |
| 54 | void TestPureCallHandler() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 55 | RTC_LOG(LS_ERROR) << "Purecall Handler called. Exiting."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | exit(1); |
| 57 | } |
| 58 | int TestCrtReportHandler(int report_type, char* msg, int* retval) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 59 | RTC_LOG(LS_ERROR) << "CrtReport Handler called..."; |
| 60 | RTC_LOG(LS_ERROR) << msg; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | if (report_type == _CRT_ASSERT) { |
| 62 | exit(1); |
| 63 | } else { |
| 64 | *retval = 0; |
| 65 | return TRUE; |
| 66 | } |
| 67 | } |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 68 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 70 | int main(int argc, char* argv[]) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | testing::InitGoogleTest(&argc, argv); |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 72 | rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | if (FLAG_help) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 74 | rtc::FlagList::Print(nullptr, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Niels Möller | b00dc38 | 2016-06-16 12:44:30 +0200 | [diff] [blame] | 78 | webrtc::test::SetExecutablePath(argv[0]); |
Bjorn Terelius | edab301 | 2018-01-31 17:23:40 +0100 | [diff] [blame] | 79 | 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); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 83 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | #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 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 92 | #if !defined(NDEBUG) // Turn on memory leak checking on Windows. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF); |
| 94 | if (FLAG_crt_break_alloc >= 0) { |
| 95 | _crtBreakAlloc = FLAG_crt_break_alloc; |
| 96 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 97 | #endif |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 98 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | // By default, log timestamps. Allow overrides by used of a --log flag. |
| 101 | rtc::LogMessage::LogTimestamps(); |
| 102 | if (*FLAG_log != '\0') { |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 103 | rtc::LogMessage::ConfigureLogging(FLAG_log); |
Peter Boström | def5820 | 2015-11-27 17:53:22 +0100 | [diff] [blame] | 104 | } 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | } |
| 109 | |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 110 | // Initialize SSL which are used by several tests. |
| 111 | rtc::InitializeSSL(); |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 112 | rtc::SSLStreamAdapter::enable_time_callback_for_testing(); |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 113 | |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 114 | #if defined(WEBRTC_IOS) |
Edward Lemur | e66572b | 2018-01-05 15:34:09 +0100 | [diff] [blame] | 115 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, false); |
Kári Tristan Helgason | e2baffb | 2017-06-09 10:31:58 +0200 | [diff] [blame] | 116 | rtc::test::RunTestsFromIOSApp(); |
| 117 | #endif |
| 118 | const int res = RUN_ALL_TESTS(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | |
pbos@webrtc.org | 34f2a9e | 2014-09-28 11:36:45 +0000 | [diff] [blame] | 120 | rtc::CleanupSSL(); |
| 121 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | // clean up logging so we don't appear to leak memory. |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 123 | rtc::LogMessage::ConfigureLogging(""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | |
| 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 | } |