Home | History | Annotate | Download | only in base
      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/ssladapter.h"
     22 #include "webrtc/test/field_trial.h"
     23 
     24 DEFINE_bool(help, false, "prints this message");
     25 DEFINE_string(log, "", "logging options to use");
     26 DEFINE_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.");
     32 #if defined(WEBRTC_WIN)
     33 DEFINE_int(crt_break_alloc, -1, "memory allocation to break on");
     34 DEFINE_bool(default_error_handlers, false,
     35             "leave the default exception/dbg handler functions in place");
     36 
     37 void 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 }
     47 void TestPureCallHandler() {
     48   LOG(LS_ERROR) << "Purecall Handler called.  Exiting.";
     49   exit(1);
     50 }
     51 int 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 }
     61 #endif  // WEBRTC_WIN
     62 
     63 int main(int argc, char** argv) {
     64   testing::InitGoogleTest(&argc, argv);
     65   rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, false);
     66   if (FLAG_help) {
     67     rtc::FlagList::Print(NULL, false);
     68     return 0;
     69   }
     70 
     71   webrtc::test::InitFieldTrialsFromString(FLAG_force_fieldtrials);
     72 
     73 #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 #if !defined(NDEBUG)  // 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
     87 #endif  // WEBRTC_WIN
     88 
     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') {
     95     rtc::LogMessage::ConfigureLogging(FLAG_log);
     96   } else if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) {
     97     // Default to LS_INFO, even for release builds to provide better test
     98     // logging.
     99     rtc::LogMessage::LogToDebug(rtc::LS_INFO);
    100   }
    101 
    102   // Initialize SSL which are used by several tests.
    103   rtc::InitializeSSL();
    104 
    105   int res = RUN_ALL_TESTS();
    106 
    107   rtc::CleanupSSL();
    108 
    109   // clean up logging so we don't appear to leak memory.
    110   rtc::LogMessage::ConfigureLogging("");
    111 
    112 #if defined(WEBRTC_WIN)
    113   // Unhook crt function so that we don't ever log after statics have been
    114   // uninitialized.
    115   if (!FLAG_default_error_handlers)
    116     _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestCrtReportHandler);
    117 #endif
    118 
    119   return res;
    120 }
    121