henrike@webrtc.org | f7795df | 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 | |
| 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 | #include "webrtc/base/pathutils.h" |
| 22 | |
| 23 | DEFINE_bool(help, false, "prints this message"); |
| 24 | DEFINE_string(log, "", "logging options to use"); |
| 25 | #if defined(WEBRTC_WIN) |
| 26 | DEFINE_int(crt_break_alloc, -1, "memory allocation to break on"); |
| 27 | DEFINE_bool(default_error_handlers, false, |
| 28 | "leave the default exception/dbg handler functions in place"); |
| 29 | |
| 30 | void TestInvalidParameterHandler(const wchar_t* expression, |
| 31 | const wchar_t* function, |
| 32 | const wchar_t* file, |
| 33 | unsigned int line, |
| 34 | uintptr_t pReserved) { |
| 35 | LOG(LS_ERROR) << "InvalidParameter Handler called. Exiting."; |
| 36 | LOG(LS_ERROR) << expression << std::endl << function << std::endl << file |
| 37 | << std::endl << line; |
| 38 | exit(1); |
| 39 | } |
| 40 | void TestPureCallHandler() { |
| 41 | LOG(LS_ERROR) << "Purecall Handler called. Exiting."; |
| 42 | exit(1); |
| 43 | } |
| 44 | int TestCrtReportHandler(int report_type, char* msg, int* retval) { |
| 45 | LOG(LS_ERROR) << "CrtReport Handler called..."; |
| 46 | LOG(LS_ERROR) << msg; |
| 47 | if (report_type == _CRT_ASSERT) { |
| 48 | exit(1); |
| 49 | } else { |
| 50 | *retval = 0; |
| 51 | return TRUE; |
| 52 | } |
| 53 | } |
| 54 | #endif // WEBRTC_WIN |
| 55 | |
| 56 | rtc::Pathname GetTalkDirectory() { |
| 57 | // Locate talk directory. |
| 58 | rtc::Pathname path = rtc::Filesystem::GetCurrentDirectory(); |
| 59 | std::string talk_folder_name("talk"); |
| 60 | talk_folder_name += path.folder_delimiter(); |
| 61 | while (path.folder_name() != talk_folder_name && !path.empty()) { |
| 62 | path.SetFolder(path.parent_folder()); |
| 63 | } |
| 64 | |
| 65 | // If not running inside "talk" folder, then assume running in its parent |
| 66 | // folder. |
| 67 | if (path.empty()) { |
| 68 | path = rtc::Filesystem::GetCurrentDirectory(); |
| 69 | path.AppendFolder("talk"); |
| 70 | // Make sure the folder exist. |
| 71 | if (!rtc::Filesystem::IsFolder(path)) { |
| 72 | path.clear(); |
| 73 | } |
| 74 | } |
| 75 | return path; |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char** argv) { |
| 79 | testing::InitGoogleTest(&argc, argv); |
| 80 | FlagList::SetFlagsFromCommandLine(&argc, argv, false); |
| 81 | if (FLAG_help) { |
| 82 | FlagList::Print(NULL, false); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | #if defined(WEBRTC_WIN) |
| 87 | if (!FLAG_default_error_handlers) { |
| 88 | // Make sure any errors don't throw dialogs hanging the test run. |
| 89 | _set_invalid_parameter_handler(TestInvalidParameterHandler); |
| 90 | _set_purecall_handler(TestPureCallHandler); |
| 91 | _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestCrtReportHandler); |
| 92 | } |
| 93 | |
| 94 | #ifdef _DEBUG // Turn on memory leak checking on Windows. |
| 95 | _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF |_CRTDBG_LEAK_CHECK_DF); |
| 96 | if (FLAG_crt_break_alloc >= 0) { |
| 97 | _crtBreakAlloc = FLAG_crt_break_alloc; |
| 98 | } |
| 99 | #endif // _DEBUG |
| 100 | #endif // WEBRTC_WIN |
| 101 | |
| 102 | rtc::Filesystem::SetOrganizationName("google"); |
| 103 | rtc::Filesystem::SetApplicationName("unittest"); |
| 104 | |
| 105 | // By default, log timestamps. Allow overrides by used of a --log flag. |
| 106 | rtc::LogMessage::LogTimestamps(); |
| 107 | if (*FLAG_log != '\0') { |
| 108 | rtc::LogMessage::ConfigureLogging(FLAG_log, "unittest.log"); |
| 109 | } |
| 110 | |
| 111 | int res = RUN_ALL_TESTS(); |
| 112 | |
| 113 | // clean up logging so we don't appear to leak memory. |
| 114 | rtc::LogMessage::ConfigureLogging("", ""); |
| 115 | |
| 116 | #if defined(WEBRTC_WIN) |
| 117 | // Unhook crt function so that we don't ever log after statics have been |
| 118 | // uninitialized. |
| 119 | if (!FLAG_default_error_handlers) |
| 120 | _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler); |
| 121 | #endif |
| 122 | |
| 123 | return res; |
| 124 | } |