Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include "test/test_main_lib.h" |
| 12 | |
| 13 | #include <fstream> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 16 | #include "absl/flags/flag.h" |
| 17 | #include "absl/flags/parse.h" |
Mirko Bonadei | bc6a06c | 2018-10-19 08:43:53 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 20 | #include "rtc_base/logging.h" |
Niels Möller | 04a3cc1 | 2019-05-21 13:01:58 +0200 | [diff] [blame] | 21 | #include "rtc_base/ssl_adapter.h" |
| 22 | #include "rtc_base/ssl_stream_adapter.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 23 | #include "rtc_base/thread.h" |
| 24 | #include "system_wrappers/include/field_trial.h" |
| 25 | #include "system_wrappers/include/metrics.h" |
| 26 | #include "test/field_trial.h" |
| 27 | #include "test/gmock.h" |
| 28 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "test/testsupport/file_utils.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 30 | #include "test/testsupport/perf_test.h" |
| 31 | |
| 32 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 33 | #include "rtc_base/win32_socket_init.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #if defined(WEBRTC_IOS) |
| 37 | #include "test/ios/test_support.h" |
| 38 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 39 | ABSL_FLAG(std::string, |
| 40 | NSTreatUnknownArgumentsAsOpen, |
| 41 | "", |
| 42 | "Intentionally ignored flag intended for iOS simulator."); |
| 43 | ABSL_FLAG(std::string, |
| 44 | ApplePersistenceIgnoreState, |
| 45 | "", |
| 46 | "Intentionally ignored flag intended for iOS simulator."); |
| 47 | ABSL_FLAG( |
| 48 | bool, |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 49 | save_chartjson_result, |
| 50 | false, |
| 51 | "Store the perf results in Documents/perf_result.json in the format " |
| 52 | "described by " |
| 53 | "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/" |
| 54 | "data-format.md."); |
| 55 | |
| 56 | #else |
| 57 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 58 | ABSL_FLAG(std::string, |
| 59 | isolated_script_test_output, |
| 60 | "", |
| 61 | "Path to output an empty JSON file which Chromium infra requires."); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 62 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 63 | ABSL_FLAG( |
| 64 | std::string, |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 65 | isolated_script_test_perf_output, |
| 66 | "", |
| 67 | "Path where the perf results should be stored in the JSON format described " |
| 68 | "by " |
| 69 | "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/" |
| 70 | "data-format.md."); |
| 71 | |
| 72 | #endif |
| 73 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 74 | ABSL_FLAG(bool, logs, true, "print logs to stderr"); |
| 75 | ABSL_FLAG(bool, verbose, false, "verbose logs to stderr"); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 76 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 77 | ABSL_FLAG(std::string, |
| 78 | force_fieldtrials, |
| 79 | "", |
| 80 | "Field trials control experimental feature code which can be forced. " |
| 81 | "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/" |
| 82 | " will assign the group Enable to field trial WebRTC-FooFeature."); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 83 | |
| 84 | namespace webrtc { |
| 85 | |
| 86 | namespace { |
| 87 | |
| 88 | class TestMainImpl : public TestMain { |
| 89 | public: |
Artem Titov | b5541a0 | 2018-10-17 17:37:47 +0200 | [diff] [blame] | 90 | int Init(int* argc, char* argv[]) override { |
| 91 | ::testing::InitGoogleMock(argc, argv); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 92 | absl::ParseCommandLine(*argc, argv); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 93 | |
| 94 | // Default to LS_INFO, even for release builds to provide better test |
| 95 | // logging. |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 96 | if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) |
| 97 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 98 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 99 | if (absl::GetFlag(FLAGS_verbose)) |
Niels Möller | d0def19 | 2018-12-21 11:28:45 +0100 | [diff] [blame] | 100 | rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE); |
| 101 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 102 | rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) || |
| 103 | absl::GetFlag(FLAGS_verbose)); |
Niels Möller | d0def19 | 2018-12-21 11:28:45 +0100 | [diff] [blame] | 104 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 105 | // TODO(bugs.webrtc.org/9792): we need to reference something from |
| 106 | // fileutils.h so that our downstream hack where we replace fileutils.cc |
| 107 | // works. Otherwise the downstream flag implementation will take over and |
| 108 | // botch the flag introduced by the hack. Remove this awful thing once the |
| 109 | // downstream implementation has been eliminated. |
| 110 | (void)webrtc::test::JoinFilename("horrible", "hack"); |
| 111 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 112 | // InitFieldTrialsFromString stores the char*, so the char array must |
| 113 | // outlive the application. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 114 | field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials); |
| 115 | webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str()); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 116 | webrtc::metrics::Enable(); |
| 117 | |
| 118 | #if defined(WEBRTC_WIN) |
| 119 | winsock_init_ = absl::make_unique<rtc::WinsockInitializer>(); |
| 120 | #endif |
| 121 | |
Niels Möller | 04a3cc1 | 2019-05-21 13:01:58 +0200 | [diff] [blame] | 122 | // Initialize SSL which are used by several tests. |
| 123 | rtc::InitializeSSL(); |
| 124 | rtc::SSLStreamAdapter::EnableTimeCallbackForTesting(); |
| 125 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 126 | // Ensure that main thread gets wrapped as an rtc::Thread. |
| 127 | // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main |
| 128 | // thread, or leave it to individual tests that need it. But as long as we |
| 129 | // have automatic thread wrapping, we need this to avoid that some other |
| 130 | // random thread (which one depending on which tests are run) gets |
| 131 | // automatically wrapped. |
| 132 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 133 | RTC_CHECK(rtc::Thread::Current()); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int Run(int argc, char* argv[]) override { |
| 138 | #if defined(WEBRTC_IOS) |
| 139 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 140 | absl::GetFlag(FLAGS_save_chartjson_result)); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 141 | rtc::test::RunTestsFromIOSApp(); |
| 142 | return 0; |
| 143 | #else |
| 144 | int exit_code = RUN_ALL_TESTS(); |
| 145 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 146 | std::string chartjson_result_file = |
| 147 | absl::GetFlag(FLAGS_isolated_script_test_perf_output); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 148 | if (!chartjson_result_file.empty()) { |
| 149 | webrtc::test::WritePerfResults(chartjson_result_file); |
| 150 | } |
| 151 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 152 | std::string result_filename = |
| 153 | absl::GetFlag(FLAGS_isolated_script_test_output); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 154 | if (!result_filename.empty()) { |
| 155 | std::ofstream result_file(result_filename); |
| 156 | result_file << "{\"version\": 3}"; |
| 157 | result_file.close(); |
| 158 | } |
| 159 | |
Yves Gerey | 53347b7 | 2018-10-19 15:04:04 +0200 | [diff] [blame] | 160 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 161 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 162 | defined(UNDEFINED_SANITIZER) |
| 163 | // We want the test flagged as failed only for sanitizer defects, |
| 164 | // in which case the sanitizer will override exit code with 66. |
| 165 | return 0; |
| 166 | #endif |
| 167 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 168 | return exit_code; |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | ~TestMainImpl() override = default; |
| 173 | |
| 174 | private: |
| 175 | #if defined(WEBRTC_WIN) |
| 176 | std::unique_ptr<rtc::WinsockInitializer> winsock_init_; |
| 177 | #endif |
| 178 | }; |
| 179 | |
| 180 | } // namespace |
| 181 | |
| 182 | std::unique_ptr<TestMain> TestMain::Create() { |
| 183 | return absl::make_unique<TestMainImpl>(); |
| 184 | } |
| 185 | |
| 186 | } // namespace webrtc |