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
17 #include "webrtc/base/flags.h"
18 #include "webrtc/base/fileutils.h"
19 #include "webrtc/base/gunit.h"
20 #include "webrtc/base/logging.h"
21
22 DEFINE_bool(help, false, "prints this message");
23 DEFINE_string(log, "", "logging options to use");
24 #if defined(WEBRTC_WIN)
25 DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
26 DEFINE_bool(default_error_handlers, false,
27 "leave the default exception/dbg handler functions in place");
28
TestInvalidParameterHandler(const wchar_t * expression,const wchar_t * function,const wchar_t * file,unsigned int line,uintptr_t pReserved)29 void TestInvalidParameterHandler(const wchar_t* expression,
30 const wchar_t* function,
31 const wchar_t* file,
32 unsigned int line,
33 uintptr_t pReserved) {
34 LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting.";
35 LOG(LS_ERROR) << expression << std::endl << function << std::endl << file
36 << std::endl << line;
37 exit(1);
38 }
TestPureCallHandler()39 void TestPureCallHandler() {
40 LOG(LS_ERROR) << "Purecall Handler called. Exiting.";
41 exit(1);
42 }
TestCrtReportHandler(int report_type,char * msg,int * retval)43 int TestCrtReportHandler(int report_type, char* msg, int* retval) {
44 LOG(LS_ERROR) << "CrtReport Handler called...";
45 LOG(LS_ERROR) << msg;
46 if (report_type == _CRT_ASSERT) {
47 exit(1);
48 } else {
49 *retval = 0;
50 return TRUE;
51 }
52 }
53 #endif // WEBRTC_WIN
54
main(int argc,char ** argv)55 int main(int argc, char** argv) {
56 testing::InitGoogleTest(&argc, argv);
57 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
58 if (FLAG_help) {
59 rtc::FlagList::Print(NULL, false);
60 return 0;
61 }
62
63 #if defined(WEBRTC_WIN)
64 if (!FLAG_default_error_handlers) {
65 // Make sure any errors don't throw dialogs hanging the test run.
66 _set_invalid_parameter_handler(TestInvalidParameterHandler);
67 _set_purecall_handler(TestPureCallHandler);
68 _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler);
69 }
70
71 #ifdef _DEBUG // Turn on memory leak checking on Windows.
72 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF);
73 if (FLAG_crt_break_alloc >= 0) {
74 _crtBreakAlloc = FLAG_crt_break_alloc;
75 }
76 #endif // _DEBUG
77 #endif // WEBRTC_WIN
78
79 rtc::Filesystem::SetOrganizationName("google");
80 rtc::Filesystem::SetApplicationName("unittest");
81
82 // By default, log timestamps. Allow overrides by used of a --log flag.
83 rtc::LogMessage::LogTimestamps();
84 if (*FLAG_log != '\0') {
85 rtc::LogMessage::ConfigureLogging(FLAG_log, "unittest.log");
86 }
87
88 int res = RUN_ALL_TESTS();
89
90 // clean up logging so we don't appear to leak memory.
91 rtc::LogMessage::ConfigureLogging("", "");
92
93 #if defined(WEBRTC_WIN)
94 // Unhook crt function so that we don't ever log after statics have been
95 // uninitialized.
96 if (!FLAG_default_error_handlers)
97 _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
98 #endif
99
100 return res;
101 }
102