fuzzer_test_support.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/at_exit.h"
  5. #include "base/command_line.h"
  6. #include "base/i18n/icu_util.h"
  7. #include "base/logging.h"
  8. #include "base/test/scoped_run_loop_timeout.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/test/test_timeouts.h"
  11. namespace {
  12. // Set up globals that a number of network tests use.
  13. //
  14. // Note that in general static initializers are not allowed, however this is
  15. // just being used by test code.
  16. struct InitGlobals {
  17. InitGlobals() {
  18. base::CommandLine::Init(0, nullptr);
  19. // |test| instances uses TaskEnvironment, which needs TestTimeouts.
  20. TestTimeouts::Initialize();
  21. task_environment = std::make_unique<base::test::TaskEnvironment>(
  22. base::test::TaskEnvironment::MainThreadType::IO);
  23. increased_timeout_ = std::make_unique<base::test::ScopedRunLoopTimeout>(
  24. FROM_HERE, TestTimeouts::action_max_timeout());
  25. // Set up ICU. ICU is used internally by GURL, which is used throughout the
  26. // //net code. Initializing ICU is important to prevent fuzztests from
  27. // asserting when handling non-ASCII urls.
  28. CHECK(base::i18n::InitializeICU());
  29. // Disable noisy logging as per "libFuzzer in Chrome" documentation:
  30. // testing/libfuzzer/getting_started.md#Disable-noisy-error-message-logging.
  31. logging::SetMinLogLevel(logging::LOG_FATAL);
  32. }
  33. // A number of tests use async code which depends on there being a
  34. // TaskEnvironment. Setting one up here allows tests to reuse the
  35. // TaskEnvironment between runs.
  36. std::unique_ptr<base::test::TaskEnvironment> task_environment;
  37. // Fuzzing tests often need to Run() for longer than action_timeout().
  38. std::unique_ptr<base::test::ScopedRunLoopTimeout> increased_timeout_;
  39. base::AtExitManager at_exit_manager;
  40. };
  41. InitGlobals* init_globals = new InitGlobals();
  42. } // namespace