blob: 66a9a73309f1c942d24ab302be3f22bedeb4d6bd [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
17#include "webrtc/base/flags.h"
18#include "webrtc/base/fileutils.h"
19#include "webrtc/base/gunit.h"
20#include "webrtc/base/logging.h"
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +000021#include "webrtc/base/ssladapter.h"
stefanc1aeaf02015-10-15 07:26:07 -070022#include "webrtc/test/field_trial.h"
Niels Möllerb00dc382016-06-16 12:44:30 +020023#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25DEFINE_bool(help, false, "prints this message");
26DEFINE_string(log, "", "logging options to use");
stefanc1aeaf02015-10-15 07:26:07 -070027DEFINE_string(
28 force_fieldtrials,
29 "",
30 "Field trials control experimental feature code which can be forced. "
31 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
32 " will assign the group Enable to field trial WebRTC-FooFeature.");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033#if defined(WEBRTC_WIN)
34DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
35DEFINE_bool(default_error_handlers, false,
36 "leave the default exception/dbg handler functions in place");
37
38void TestInvalidParameterHandler(const wchar_t* expression,
39 const wchar_t* function,
40 const wchar_t* file,
41 unsigned int line,
42 uintptr_t pReserved) {
43 LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting.";
44 LOG(LS_ERROR) << expression << std::endl << function << std::endl << file
45 << std::endl << line;
46 exit(1);
47}
48void TestPureCallHandler() {
49 LOG(LS_ERROR) << "Purecall Handler called. Exiting.";
50 exit(1);
51}
52int TestCrtReportHandler(int report_type, char* msg, int* retval) {
53 LOG(LS_ERROR) << "CrtReport Handler called...";
54 LOG(LS_ERROR) << msg;
55 if (report_type == _CRT_ASSERT) {
56 exit(1);
57 } else {
58 *retval = 0;
59 return TRUE;
60 }
61}
Tommi0eefb4d2015-05-23 09:54:07 +020062#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064int main(int argc, char** argv) {
65 testing::InitGoogleTest(&argc, argv);
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000066 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 if (FLAG_help) {
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000068 rtc::FlagList::Print(NULL, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 return 0;
70 }
71
Niels Möllerb00dc382016-06-16 12:44:30 +020072 webrtc::test::SetExecutablePath(argv[0]);
stefanc1aeaf02015-10-15 07:26:07 -070073 webrtc::test::InitFieldTrialsFromString(FLAG_force_fieldtrials);
74
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075#if defined(WEBRTC_WIN)
76 if (!FLAG_default_error_handlers) {
77 // Make sure any errors don't throw dialogs hanging the test run.
78 _set_invalid_parameter_handler(TestInvalidParameterHandler);
79 _set_purecall_handler(TestPureCallHandler);
80 _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler);
81 }
82
tfarinaa41ab932015-10-30 16:08:48 -070083#if !defined(NDEBUG) // Turn on memory leak checking on Windows.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF);
85 if (FLAG_crt_break_alloc >= 0) {
86 _crtBreakAlloc = FLAG_crt_break_alloc;
87 }
tfarinaa41ab932015-10-30 16:08:48 -070088#endif
Tommi0eefb4d2015-05-23 09:54:07 +020089#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090
91 rtc::Filesystem::SetOrganizationName("google");
92 rtc::Filesystem::SetApplicationName("unittest");
93
94 // By default, log timestamps. Allow overrides by used of a --log flag.
95 rtc::LogMessage::LogTimestamps();
96 if (*FLAG_log != '\0') {
Tommi0eefb4d2015-05-23 09:54:07 +020097 rtc::LogMessage::ConfigureLogging(FLAG_log);
Peter Boströmdef58202015-11-27 17:53:22 +010098 } else if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) {
99 // Default to LS_INFO, even for release builds to provide better test
100 // logging.
101 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 }
103
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000104 // Initialize SSL which are used by several tests.
105 rtc::InitializeSSL();
106
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 int res = RUN_ALL_TESTS();
108
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000109 rtc::CleanupSSL();
110
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 // clean up logging so we don't appear to leak memory.
Tommi0eefb4d2015-05-23 09:54:07 +0200112 rtc::LogMessage::ConfigureLogging("");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113
114#if defined(WEBRTC_WIN)
115 // Unhook crt function so that we don't ever log after statics have been
116 // uninitialized.
117 if (!FLAG_default_error_handlers)
118 _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
119#endif
120
121 return res;
122}