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> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 14 | #include <memory> |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 17 | #include "absl/flags/flag.h" |
Artem Titov | 694b74b | 2020-02-20 23:52:26 +0100 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Tommi | ec3ba73 | 2020-05-17 14:33:40 +0200 | [diff] [blame] | 19 | #include "absl/strings/match.h" |
Artem Titov | 694b74b | 2020-02-20 23:52:26 +0100 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Yves Gerey | 050e38f | 2019-08-28 13:24:00 +0200 | [diff] [blame] | 22 | #include "rtc_base/event_tracer.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Niels Möller | 04a3cc1 | 2019-05-21 13:01:58 +0200 | [diff] [blame] | 24 | #include "rtc_base/ssl_adapter.h" |
| 25 | #include "rtc_base/ssl_stream_adapter.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
| 27 | #include "system_wrappers/include/field_trial.h" |
| 28 | #include "system_wrappers/include/metrics.h" |
| 29 | #include "test/field_trial.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 30 | #include "test/gtest.h" |
Mirko Bonadei | 054ea47 | 2022-07-21 09:52:32 +0200 | [diff] [blame] | 31 | #include "test/test_flags.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 32 | #include "test/testsupport/perf_test.h" |
Patrik Höglund | 844600e | 2019-10-15 12:19:36 +0200 | [diff] [blame] | 33 | #include "test/testsupport/resources_dir_flag.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 34 | |
| 35 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "rtc_base/win32_socket_init.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #if defined(WEBRTC_IOS) |
| 40 | #include "test/ios/test_support.h" |
| 41 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 42 | ABSL_FLAG(std::string, |
| 43 | NSTreatUnknownArgumentsAsOpen, |
| 44 | "", |
| 45 | "Intentionally ignored flag intended for iOS simulator."); |
| 46 | ABSL_FLAG(std::string, |
| 47 | ApplePersistenceIgnoreState, |
| 48 | "", |
| 49 | "Intentionally ignored flag intended for iOS simulator."); |
Patrik Höglund | 3428827 | 2020-03-19 08:51:35 +0100 | [diff] [blame] | 50 | |
| 51 | // This is the cousin of isolated_script_test_perf_output, but we can't dictate |
| 52 | // where to write on iOS so the semantics of this flag are a bit different. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 53 | ABSL_FLAG( |
| 54 | bool, |
Patrik Höglund | 3428827 | 2020-03-19 08:51:35 +0100 | [diff] [blame] | 55 | write_perf_output_on_ios, |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 56 | false, |
Patrik Höglund | 3428827 | 2020-03-19 08:51:35 +0100 | [diff] [blame] | 57 | "Store the perf results in Documents/perftest_result.pb in the format " |
| 58 | "described by histogram.proto in " |
| 59 | "https://chromium.googlesource.com/catapult/."); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 60 | |
Jeremy Leconte | d15f3e1 | 2022-02-18 10:16:32 +0100 | [diff] [blame] | 61 | #endif |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 62 | |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 63 | ABSL_FLAG(std::string, |
| 64 | isolated_script_test_output, |
| 65 | "", |
| 66 | "Path to output an empty JSON file which Chromium infra requires."); |
| 67 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 68 | ABSL_FLAG(bool, logs, true, "print logs to stderr"); |
| 69 | ABSL_FLAG(bool, verbose, false, "verbose logs to stderr"); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 70 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 71 | ABSL_FLAG(std::string, |
Yves Gerey | 050e38f | 2019-08-28 13:24:00 +0200 | [diff] [blame] | 72 | trace_event, |
| 73 | "", |
| 74 | "Path to collect trace events (json file) for chrome://tracing. " |
| 75 | "If not set, events aren't captured."); |
| 76 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 77 | namespace webrtc { |
| 78 | |
| 79 | namespace { |
| 80 | |
Mirko Bonadei | 054ea47 | 2022-07-21 09:52:32 +0200 | [diff] [blame] | 81 | constexpr char kPlotAllMetrics[] = "all"; |
| 82 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 83 | class TestMainImpl : public TestMain { |
| 84 | public: |
Artem Titov | bcb42f1 | 2020-08-11 12:19:18 +0200 | [diff] [blame] | 85 | int Init(int* argc, char* argv[]) override { return Init(); } |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 86 | |
Artem Titov | bcb42f1 | 2020-08-11 12:19:18 +0200 | [diff] [blame] | 87 | int Init() override { |
Patrik Höglund | 2bc1ea0 | 2019-10-16 10:24:35 +0200 | [diff] [blame] | 88 | // Make sure we always pull in the --resources_dir flag, even if the test |
| 89 | // binary doesn't link with fileutils (downstream expects all test mains to |
| 90 | // have this flag). |
| 91 | (void)absl::GetFlag(FLAGS_resources_dir); |
Patrik Höglund | 2f28370 | 2019-10-14 10:12:18 +0200 | [diff] [blame] | 92 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 93 | // Default to LS_INFO, even for release builds to provide better test |
| 94 | // logging. |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 95 | if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) |
| 96 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 97 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 98 | if (absl::GetFlag(FLAGS_verbose)) |
Niels Möller | d0def19 | 2018-12-21 11:28:45 +0100 | [diff] [blame] | 99 | rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE); |
| 100 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 101 | rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) || |
| 102 | absl::GetFlag(FLAGS_verbose)); |
Niels Möller | d0def19 | 2018-12-21 11:28:45 +0100 | [diff] [blame] | 103 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 104 | // InitFieldTrialsFromString stores the char*, so the char array must |
| 105 | // outlive the application. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 106 | field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials); |
| 107 | webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str()); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 108 | webrtc::metrics::Enable(); |
| 109 | |
| 110 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 111 | winsock_init_ = std::make_unique<rtc::WinsockInitializer>(); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 112 | #endif |
| 113 | |
Niels Möller | 04a3cc1 | 2019-05-21 13:01:58 +0200 | [diff] [blame] | 114 | // Initialize SSL which are used by several tests. |
| 115 | rtc::InitializeSSL(); |
| 116 | rtc::SSLStreamAdapter::EnableTimeCallbackForTesting(); |
| 117 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int Run(int argc, char* argv[]) override { |
Yves Gerey | 071d025 | 2020-01-28 20:07:19 +0100 | [diff] [blame] | 122 | std::string trace_event_path = absl::GetFlag(FLAGS_trace_event); |
| 123 | const bool capture_events = !trace_event_path.empty(); |
| 124 | if (capture_events) { |
| 125 | rtc::tracing::SetupInternalTracer(); |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 126 | rtc::tracing::StartInternalCapture(trace_event_path); |
Yves Gerey | 071d025 | 2020-01-28 20:07:19 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Artem Titov | 694b74b | 2020-02-20 23:52:26 +0100 | [diff] [blame] | 129 | absl::optional<std::vector<std::string>> metrics_to_plot = |
| 130 | absl::GetFlag(FLAGS_plot); |
| 131 | |
| 132 | if (metrics_to_plot->empty()) { |
| 133 | metrics_to_plot = absl::nullopt; |
| 134 | } else { |
| 135 | if (metrics_to_plot->size() == 1 && |
| 136 | (*metrics_to_plot)[0] == kPlotAllMetrics) { |
| 137 | metrics_to_plot->clear(); |
| 138 | } |
| 139 | } |
| 140 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 141 | #if defined(WEBRTC_IOS) |
| 142 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, |
Patrik Höglund | 3428827 | 2020-03-19 08:51:35 +0100 | [diff] [blame] | 143 | absl::GetFlag(FLAGS_write_perf_output_on_ios), |
Artem Titov | 694b74b | 2020-02-20 23:52:26 +0100 | [diff] [blame] | 144 | metrics_to_plot); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 145 | rtc::test::RunTestsFromIOSApp(); |
Yves Gerey | 071d025 | 2020-01-28 20:07:19 +0100 | [diff] [blame] | 146 | int exit_code = 0; |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 147 | #else |
| 148 | int exit_code = RUN_ALL_TESTS(); |
| 149 | |
Patrik Höglund | a7a0173 | 2020-03-27 19:22:36 +0100 | [diff] [blame] | 150 | std::string perf_output_file = |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 151 | absl::GetFlag(FLAGS_isolated_script_test_perf_output); |
Patrik Höglund | a7a0173 | 2020-03-27 19:22:36 +0100 | [diff] [blame] | 152 | if (!perf_output_file.empty()) { |
| 153 | if (!webrtc::test::WritePerfResults(perf_output_file)) { |
Patrik Höglund | b8e69ef | 2020-03-12 09:39:40 +0100 | [diff] [blame] | 154 | return 1; |
| 155 | } |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 156 | } |
Artem Titov | 694b74b | 2020-02-20 23:52:26 +0100 | [diff] [blame] | 157 | if (metrics_to_plot) { |
| 158 | webrtc::test::PrintPlottableResults(*metrics_to_plot); |
Artem Titov | 087be5c | 2019-09-12 20:30:54 +0200 | [diff] [blame] | 159 | } |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 160 | |
| 161 | std::string result_filename = |
| 162 | absl::GetFlag(FLAGS_isolated_script_test_output); |
| 163 | if (!result_filename.empty()) { |
| 164 | std::ofstream result_file(result_filename); |
| 165 | result_file << "{\"version\": 3}"; |
Jeremy Leconte | 994bf45 | 2022-01-12 10:51:16 +0100 | [diff] [blame] | 166 | } |
Yves Gerey | 071d025 | 2020-01-28 20:07:19 +0100 | [diff] [blame] | 167 | #endif |
| 168 | |
| 169 | if (capture_events) { |
| 170 | rtc::tracing::StopInternalCapture(); |
| 171 | } |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 172 | |
Yves Gerey | 53347b7 | 2018-10-19 15:04:04 +0200 | [diff] [blame] | 173 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 174 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 175 | defined(UNDEFINED_SANITIZER) |
| 176 | // We want the test flagged as failed only for sanitizer defects, |
| 177 | // in which case the sanitizer will override exit code with 66. |
Yves Gerey | 071d025 | 2020-01-28 20:07:19 +0100 | [diff] [blame] | 178 | exit_code = 0; |
Yves Gerey | 53347b7 | 2018-10-19 15:04:04 +0200 | [diff] [blame] | 179 | #endif |
| 180 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 181 | return exit_code; |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ~TestMainImpl() override = default; |
| 185 | |
| 186 | private: |
| 187 | #if defined(WEBRTC_WIN) |
| 188 | std::unique_ptr<rtc::WinsockInitializer> winsock_init_; |
| 189 | #endif |
| 190 | }; |
| 191 | |
| 192 | } // namespace |
| 193 | |
| 194 | std::unique_ptr<TestMain> TestMain::Create() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 195 | return std::make_unique<TestMainImpl>(); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // namespace webrtc |