blob: 204af03ca31ad8ac4d09026a088377e8640c1aa9 [file] [log] [blame]
Artem Titov40a7a352018-10-15 15:25:34 +02001/*
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 Bonadei317a1f02019-09-17 17:06:18 +020014#include <memory>
Artem Titov40a7a352018-10-15 15:25:34 +020015#include <string>
16
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020017#include "absl/flags/flag.h"
Artem Titov694b74b2020-02-20 23:52:26 +010018#include "absl/memory/memory.h"
Tommiec3ba732020-05-17 14:33:40 +020019#include "absl/strings/match.h"
Artem Titov694b74b2020-02-20 23:52:26 +010020#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
Yves Gerey050e38f2019-08-28 13:24:00 +020022#include "rtc_base/event_tracer.h"
Artem Titov40a7a352018-10-15 15:25:34 +020023#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 13:01:58 +020024#include "rtc_base/ssl_adapter.h"
25#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 15:25:34 +020026#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 Titov40a7a352018-10-15 15:25:34 +020030#include "test/gtest.h"
Mirko Bonadei054ea472022-07-21 09:52:32 +020031#include "test/test_flags.h"
Artem Titov40a7a352018-10-15 15:25:34 +020032#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 12:19:36 +020033#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 15:25:34 +020034
35#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020037#endif
38
39#if defined(WEBRTC_IOS)
40#include "test/ios/test_support.h"
41
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020042ABSL_FLAG(std::string,
43 NSTreatUnknownArgumentsAsOpen,
44 "",
45 "Intentionally ignored flag intended for iOS simulator.");
46ABSL_FLAG(std::string,
47 ApplePersistenceIgnoreState,
48 "",
49 "Intentionally ignored flag intended for iOS simulator.");
Patrik Höglund34288272020-03-19 08:51:35 +010050
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 Bonadei2ab97f62019-07-18 13:44:12 +020053ABSL_FLAG(
54 bool,
Patrik Höglund34288272020-03-19 08:51:35 +010055 write_perf_output_on_ios,
Artem Titov40a7a352018-10-15 15:25:34 +020056 false,
Patrik Höglund34288272020-03-19 08:51:35 +010057 "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 Titov40a7a352018-10-15 15:25:34 +020060
Jeremy Leconted15f3e12022-02-18 10:16:32 +010061#endif
Artem Titov40a7a352018-10-15 15:25:34 +020062
Jeremy Leconte994bf452022-01-12 10:51:16 +010063ABSL_FLAG(std::string,
64 isolated_script_test_output,
65 "",
66 "Path to output an empty JSON file which Chromium infra requires.");
67
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020068ABSL_FLAG(bool, logs, true, "print logs to stderr");
69ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020070
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020071ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020072 trace_event,
73 "",
74 "Path to collect trace events (json file) for chrome://tracing. "
75 "If not set, events aren't captured.");
76
Artem Titov40a7a352018-10-15 15:25:34 +020077namespace webrtc {
78
79namespace {
80
Mirko Bonadei054ea472022-07-21 09:52:32 +020081constexpr char kPlotAllMetrics[] = "all";
82
Artem Titov40a7a352018-10-15 15:25:34 +020083class TestMainImpl : public TestMain {
84 public:
Artem Titovbcb42f12020-08-11 12:19:18 +020085 int Init(int* argc, char* argv[]) override { return Init(); }
Artem Titov40a7a352018-10-15 15:25:34 +020086
Artem Titovbcb42f12020-08-11 12:19:18 +020087 int Init() override {
Patrik Höglund2bc1ea02019-10-16 10:24:35 +020088 // 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öglund2f283702019-10-14 10:12:18 +020092
Artem Titov40a7a352018-10-15 15:25:34 +020093 // Default to LS_INFO, even for release builds to provide better test
94 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +020095 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
96 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
97
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020098 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +010099 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
100
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200101 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
102 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100103
Artem Titov40a7a352018-10-15 15:25:34 +0200104 // InitFieldTrialsFromString stores the char*, so the char array must
105 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200106 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
107 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200108 webrtc::metrics::Enable();
109
110#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200111 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200112#endif
113
Niels Möller04a3cc12019-05-21 13:01:58 +0200114 // Initialize SSL which are used by several tests.
115 rtc::InitializeSSL();
116 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
117
Artem Titov40a7a352018-10-15 15:25:34 +0200118 return 0;
119 }
120
121 int Run(int argc, char* argv[]) override {
Yves Gerey071d0252020-01-28 20:07:19 +0100122 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 Tofigh2ab914c2022-04-13 12:55:15 +0200126 rtc::tracing::StartInternalCapture(trace_event_path);
Yves Gerey071d0252020-01-28 20:07:19 +0100127 }
128
Artem Titov694b74b2020-02-20 23:52:26 +0100129 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 Titov40a7a352018-10-15 15:25:34 +0200141#if defined(WEBRTC_IOS)
142 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Patrik Höglund34288272020-03-19 08:51:35 +0100143 absl::GetFlag(FLAGS_write_perf_output_on_ios),
Artem Titov694b74b2020-02-20 23:52:26 +0100144 metrics_to_plot);
Artem Titov40a7a352018-10-15 15:25:34 +0200145 rtc::test::RunTestsFromIOSApp();
Yves Gerey071d0252020-01-28 20:07:19 +0100146 int exit_code = 0;
Artem Titov40a7a352018-10-15 15:25:34 +0200147#else
148 int exit_code = RUN_ALL_TESTS();
149
Patrik Höglunda7a01732020-03-27 19:22:36 +0100150 std::string perf_output_file =
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200151 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Patrik Höglunda7a01732020-03-27 19:22:36 +0100152 if (!perf_output_file.empty()) {
153 if (!webrtc::test::WritePerfResults(perf_output_file)) {
Patrik Höglundb8e69ef2020-03-12 09:39:40 +0100154 return 1;
155 }
Artem Titov40a7a352018-10-15 15:25:34 +0200156 }
Artem Titov694b74b2020-02-20 23:52:26 +0100157 if (metrics_to_plot) {
158 webrtc::test::PrintPlottableResults(*metrics_to_plot);
Artem Titov087be5c2019-09-12 20:30:54 +0200159 }
Jeremy Leconte994bf452022-01-12 10:51:16 +0100160
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 Leconte994bf452022-01-12 10:51:16 +0100166 }
Yves Gerey071d0252020-01-28 20:07:19 +0100167#endif
168
169 if (capture_events) {
170 rtc::tracing::StopInternalCapture();
171 }
Artem Titov40a7a352018-10-15 15:25:34 +0200172
Yves Gerey53347b72018-10-19 15:04:04 +0200173#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 Gerey071d0252020-01-28 20:07:19 +0100178 exit_code = 0;
Yves Gerey53347b72018-10-19 15:04:04 +0200179#endif
180
Artem Titov40a7a352018-10-15 15:25:34 +0200181 return exit_code;
Artem Titov40a7a352018-10-15 15:25:34 +0200182 }
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
194std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200195 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200196}
197
198} // namespace webrtc