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