blob: f952b2d54745223cc388e7ca31168ca5f54b3f1f [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24DEFINE_bool(help, false, "prints this message");
25DEFINE_string(log, "", "logging options to use");
stefanc1aeaf02015-10-15 07:26:07 -070026DEFINE_string(
27 force_fieldtrials,
28 "",
29 "Field trials control experimental feature code which can be forced. "
30 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
31 " will assign the group Enable to field trial WebRTC-FooFeature.");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#if defined(WEBRTC_WIN)
33DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
34DEFINE_bool(default_error_handlers, false,
35 "leave the default exception/dbg handler functions in place");
36
37void TestInvalidParameterHandler(const wchar_t* expression,
38 const wchar_t* function,
39 const wchar_t* file,
40 unsigned int line,
41 uintptr_t pReserved) {
42 LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting.";
43 LOG(LS_ERROR) << expression << std::endl << function << std::endl << file
44 << std::endl << line;
45 exit(1);
46}
47void TestPureCallHandler() {
48 LOG(LS_ERROR) << "Purecall Handler called. Exiting.";
49 exit(1);
50}
51int TestCrtReportHandler(int report_type, char* msg, int* retval) {
52 LOG(LS_ERROR) << "CrtReport Handler called...";
53 LOG(LS_ERROR) << msg;
54 if (report_type == _CRT_ASSERT) {
55 exit(1);
56 } else {
57 *retval = 0;
58 return TRUE;
59 }
60}
Tommi0eefb4d2015-05-23 09:54:07 +020061#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063int main(int argc, char** argv) {
64 testing::InitGoogleTest(&argc, argv);
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000065 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 if (FLAG_help) {
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000067 rtc::FlagList::Print(NULL, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 return 0;
69 }
70
stefanc1aeaf02015-10-15 07:26:07 -070071 webrtc::test::InitFieldTrialsFromString(FLAG_force_fieldtrials);
72
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073#if defined(WEBRTC_WIN)
74 if (!FLAG_default_error_handlers) {
75 // Make sure any errors don't throw dialogs hanging the test run.
76 _set_invalid_parameter_handler(TestInvalidParameterHandler);
77 _set_purecall_handler(TestPureCallHandler);
78 _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler);
79 }
80
81#ifdef _DEBUG // Turn on memory leak checking on Windows.
82 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF);
83 if (FLAG_crt_break_alloc >= 0) {
84 _crtBreakAlloc = FLAG_crt_break_alloc;
85 }
86#endif // _DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020087#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088
89 rtc::Filesystem::SetOrganizationName("google");
90 rtc::Filesystem::SetApplicationName("unittest");
91
92 // By default, log timestamps. Allow overrides by used of a --log flag.
93 rtc::LogMessage::LogTimestamps();
94 if (*FLAG_log != '\0') {
Tommi0eefb4d2015-05-23 09:54:07 +020095 rtc::LogMessage::ConfigureLogging(FLAG_log);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 }
97
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +000098 // Initialize SSL which are used by several tests.
99 rtc::InitializeSSL();
100
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 int res = RUN_ALL_TESTS();
102
pbos@webrtc.org34f2a9e2014-09-28 11:36:45 +0000103 rtc::CleanupSSL();
104
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 // clean up logging so we don't appear to leak memory.
Tommi0eefb4d2015-05-23 09:54:07 +0200106 rtc::LogMessage::ConfigureLogging("");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107
108#if defined(WEBRTC_WIN)
109 // Unhook crt function so that we don't ever log after statics have been
110 // uninitialized.
111 if (!FLAG_default_error_handlers)
112 _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
113#endif
114
115 return res;
116}