blob: 7170163346a3ccfbf377d8c4f6a6478544dfbd03 [file] [log] [blame]
Artem Titov40a7a352018-10-15 13:25:341/*
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 15:06:1814#include <memory>
Artem Titov40a7a352018-10-15 13:25:3415#include <string>
16
Mirko Bonadei2ab97f62019-07-18 11:44:1217#include "absl/flags/flag.h"
Artem Titov694b74b2020-02-20 22:52:2618#include "absl/memory/memory.h"
Tommiec3ba732020-05-17 12:33:4019#include "absl/strings/match.h"
Artem Titov694b74b2020-02-20 22:52:2620#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 15:47:4921#include "rtc_base/checks.h"
Yves Gerey050e38f2019-08-28 11:24:0022#include "rtc_base/event_tracer.h"
Artem Titov40a7a352018-10-15 13:25:3423#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 11:01:5824#include "rtc_base/ssl_adapter.h"
25#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 13:25:3426#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 13:25:3430#include "test/gtest.h"
Artem Titov40a7a352018-10-15 13:25:3431#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 10:19:3632#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 13:25:3433
34#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 17:11:0035#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 13:25:3436#endif
37
38#if defined(WEBRTC_IOS)
39#include "test/ios/test_support.h"
40
Mirko Bonadei2ab97f62019-07-18 11:44:1241ABSL_FLAG(std::string,
42 NSTreatUnknownArgumentsAsOpen,
43 "",
44 "Intentionally ignored flag intended for iOS simulator.");
45ABSL_FLAG(std::string,
46 ApplePersistenceIgnoreState,
47 "",
48 "Intentionally ignored flag intended for iOS simulator.");
Patrik Höglund34288272020-03-19 07:51:3549
50// This is the cousin of isolated_script_test_perf_output, but we can't dictate
51// where to write on iOS so the semantics of this flag are a bit different.
Mirko Bonadei2ab97f62019-07-18 11:44:1252ABSL_FLAG(
53 bool,
Patrik Höglund34288272020-03-19 07:51:3554 write_perf_output_on_ios,
Artem Titov40a7a352018-10-15 13:25:3455 false,
Patrik Höglund34288272020-03-19 07:51:3556 "Store the perf results in Documents/perftest_result.pb in the format "
57 "described by histogram.proto in "
58 "https://chromium.googlesource.com/catapult/.");
Artem Titov40a7a352018-10-15 13:25:3459
60#else
61
Mirko Bonadei2ab97f62019-07-18 11:44:1262ABSL_FLAG(
63 std::string,
Artem Titov40a7a352018-10-15 13:25:3464 isolated_script_test_perf_output,
65 "",
Patrik Höglund34288272020-03-19 07:51:3566 "Path where the perf results should be stored in proto format described "
67 "described by histogram.proto in "
68 "https://chromium.googlesource.com/catapult/.");
69
Artem Titov694b74b2020-02-20 22:52:2670#endif
Artem Titov40a7a352018-10-15 13:25:3471
Artem Titov087be5c2019-09-12 18:30:5472constexpr char kPlotAllMetrics[] = "all";
73ABSL_FLAG(std::vector<std::string>,
74 plot,
75 {},
76 "List of metrics that should be exported for plotting (if they are "
77 "available). Example: psnr,ssim,encode_time. To plot all available "
78 " metrics pass 'all' as flag value");
79
Mirko Bonadei2ab97f62019-07-18 11:44:1280ABSL_FLAG(bool, logs, true, "print logs to stderr");
81ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 13:25:3482
Mirko Bonadei2ab97f62019-07-18 11:44:1283ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 11:24:0084 trace_event,
85 "",
86 "Path to collect trace events (json file) for chrome://tracing. "
87 "If not set, events aren't captured.");
88
89ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 11:44:1290 force_fieldtrials,
91 "",
92 "Field trials control experimental feature code which can be forced. "
93 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
94 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 13:25:3495
96namespace webrtc {
97
98namespace {
99
100class TestMainImpl : public TestMain {
101 public:
Tommi9b7232a2020-05-15 08:09:27102 // In order to set up a fresh rtc::Thread state for each test and avoid
103 // accidentally carrying over pending tasks that might be sent from one test
104 // and executed while another test is running, we inject a TestListener
105 // that sets up a new rtc::Thread instance for the main thread, per test.
106 class TestListener : public ::testing::EmptyTestEventListener {
107 public:
108 TestListener() = default;
109
110 private:
Tommiec3ba732020-05-17 12:33:40111 bool IsDeathTest(const char* test_case_name, const char* test_name) {
112 // Workaround to avoid wrapping the main thread when we run death tests.
113 // The approach we take for detecting death tests is essentially the same
114 // as gtest does internally. Gtest does this:
115 //
116 // static const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
117 // ::testing::internal::UnitTestOptions::MatchesFilter(
118 // test_case_name, kDeathTestCaseFilter);
119 //
120 // Our approach is a little more straight forward.
121 if (absl::EndsWith(test_case_name, "DeathTest"))
122 return true;
123
124 return absl::EndsWith(test_name, "DeathTest");
125 }
126
Tommi9b7232a2020-05-15 08:09:27127 void OnTestStart(const ::testing::TestInfo& test_info) override {
Tommiec3ba732020-05-17 12:33:40128 if (!IsDeathTest(test_info.test_suite_name(), test_info.name())) {
129 // Ensure that main thread gets wrapped as an rtc::Thread.
130 // TODO(bugs.webrtc.org/9714): It might be better to avoid wrapping the
131 // main thread, or leave it to individual tests that need it. But as
132 // long as we have automatic thread wrapping, we need this to avoid that
133 // some other random thread (which one depending on which tests are run)
134 // gets automatically wrapped.
135 thread_ = rtc::Thread::CreateWithSocketServer();
136 thread_->WrapCurrent();
137 RTC_DCHECK_EQ(rtc::Thread::Current(), thread_.get());
138 } else {
139 RTC_LOG(LS_INFO) << "No thread auto wrap for death test.";
140 }
Tommi9b7232a2020-05-15 08:09:27141 }
142
143 void OnTestEnd(const ::testing::TestInfo& test_info) override {
144 // Terminate the message loop. Note that if the test failed to clean
145 // up pending messages, this may execute part of the test. Ideally we
146 // should print a warning message here, or even fail the test if it leaks.
Tommiec3ba732020-05-17 12:33:40147 if (thread_) {
148 thread_->Quit(); // Signal quit.
149 thread_->Run(); // Flush + process Quit signal.
150 thread_->UnwrapCurrent();
151 thread_ = nullptr;
152 }
Tommi9b7232a2020-05-15 08:09:27153 }
154
155 std::unique_ptr<rtc::Thread> thread_;
156 };
157
Artem Titovbcb42f12020-08-11 10:19:18158 int Init(int* argc, char* argv[]) override { return Init(); }
Artem Titov40a7a352018-10-15 13:25:34159
Artem Titovbcb42f12020-08-11 10:19:18160 int Init() override {
Patrik Höglund2bc1ea02019-10-16 08:24:35161 // Make sure we always pull in the --resources_dir flag, even if the test
162 // binary doesn't link with fileutils (downstream expects all test mains to
163 // have this flag).
164 (void)absl::GetFlag(FLAGS_resources_dir);
Patrik Höglund2f283702019-10-14 08:12:18165
Artem Titov40a7a352018-10-15 13:25:34166 // Default to LS_INFO, even for release builds to provide better test
167 // logging.
Artem Titov40a7a352018-10-15 13:25:34168 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
169 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
170
Mirko Bonadei2ab97f62019-07-18 11:44:12171 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 10:28:45172 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
173
Mirko Bonadei2ab97f62019-07-18 11:44:12174 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
175 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 10:28:45176
Artem Titov40a7a352018-10-15 13:25:34177 // InitFieldTrialsFromString stores the char*, so the char array must
178 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 11:44:12179 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
180 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 13:25:34181 webrtc::metrics::Enable();
182
183#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 15:06:18184 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 13:25:34185#endif
186
Niels Möller04a3cc12019-05-21 11:01:58187 // Initialize SSL which are used by several tests.
188 rtc::InitializeSSL();
189 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
190
Tommi9b7232a2020-05-15 08:09:27191 ::testing::UnitTest::GetInstance()->listeners().Append(new TestListener());
Yves Gerey050e38f2019-08-28 11:24:00192
Artem Titov40a7a352018-10-15 13:25:34193 return 0;
194 }
195
196 int Run(int argc, char* argv[]) override {
Yves Gerey071d0252020-01-28 19:07:19197 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
198 const bool capture_events = !trace_event_path.empty();
199 if (capture_events) {
200 rtc::tracing::SetupInternalTracer();
201 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
202 }
203
Artem Titov694b74b2020-02-20 22:52:26204 absl::optional<std::vector<std::string>> metrics_to_plot =
205 absl::GetFlag(FLAGS_plot);
206
207 if (metrics_to_plot->empty()) {
208 metrics_to_plot = absl::nullopt;
209 } else {
210 if (metrics_to_plot->size() == 1 &&
211 (*metrics_to_plot)[0] == kPlotAllMetrics) {
212 metrics_to_plot->clear();
213 }
214 }
215
Artem Titov40a7a352018-10-15 13:25:34216#if defined(WEBRTC_IOS)
217 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Patrik Höglund34288272020-03-19 07:51:35218 absl::GetFlag(FLAGS_write_perf_output_on_ios),
Artem Titov694b74b2020-02-20 22:52:26219 metrics_to_plot);
Artem Titov40a7a352018-10-15 13:25:34220 rtc::test::RunTestsFromIOSApp();
Yves Gerey071d0252020-01-28 19:07:19221 int exit_code = 0;
Artem Titov40a7a352018-10-15 13:25:34222#else
223 int exit_code = RUN_ALL_TESTS();
224
Patrik Höglunda7a01732020-03-27 18:22:36225 std::string perf_output_file =
Mirko Bonadei2ab97f62019-07-18 11:44:12226 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Patrik Höglunda7a01732020-03-27 18:22:36227 if (!perf_output_file.empty()) {
228 if (!webrtc::test::WritePerfResults(perf_output_file)) {
Patrik Höglundb8e69ef2020-03-12 08:39:40229 return 1;
230 }
Artem Titov40a7a352018-10-15 13:25:34231 }
Artem Titov694b74b2020-02-20 22:52:26232 if (metrics_to_plot) {
233 webrtc::test::PrintPlottableResults(*metrics_to_plot);
Artem Titov087be5c2019-09-12 18:30:54234 }
Yves Gerey071d0252020-01-28 19:07:19235#endif
236
237 if (capture_events) {
238 rtc::tracing::StopInternalCapture();
239 }
Artem Titov40a7a352018-10-15 13:25:34240
Yves Gerey53347b72018-10-19 13:04:04241#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
242 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
243 defined(UNDEFINED_SANITIZER)
244 // We want the test flagged as failed only for sanitizer defects,
245 // in which case the sanitizer will override exit code with 66.
Yves Gerey071d0252020-01-28 19:07:19246 exit_code = 0;
Yves Gerey53347b72018-10-19 13:04:04247#endif
248
Artem Titov40a7a352018-10-15 13:25:34249 return exit_code;
Artem Titov40a7a352018-10-15 13:25:34250 }
251
252 ~TestMainImpl() override = default;
253
254 private:
255#if defined(WEBRTC_WIN)
256 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
257#endif
258};
259
260} // namespace
261
262std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 15:06:18263 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 13:25:34264}
265
266} // namespace webrtc