system_test_utils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2019 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. #ifndef SERVICES_TRACING_PERFETTO_SYSTEM_TEST_UTILS_H_
  5. #define SERVICES_TRACING_PERFETTO_SYSTEM_TEST_UTILS_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "services/tracing/public/cpp/perfetto/posix_system_producer.h"
  9. namespace base {
  10. class ScopedTempDir;
  11. }
  12. namespace perfetto {
  13. class ServiceIPCHost;
  14. class TracingService;
  15. namespace base {
  16. class TaskRunner;
  17. }
  18. } // namespace perfetto
  19. namespace tracing {
  20. class MockSystemService {
  21. public:
  22. MockSystemService(const std::string& consumer_socket,
  23. const std::string& producer_socket);
  24. explicit MockSystemService(const base::ScopedTempDir& tmp_dir);
  25. MockSystemService(const base::ScopedTempDir& tmp_dir,
  26. std::unique_ptr<perfetto::base::TaskRunner>);
  27. ~MockSystemService();
  28. perfetto::TracingService* GetService();
  29. const std::string& consumer() const;
  30. const std::string& producer() const;
  31. private:
  32. void StartService();
  33. const bool used_tmpdir_;
  34. const char* old_tmpdir_ = nullptr;
  35. std::string consumer_;
  36. std::string producer_;
  37. std::unique_ptr<perfetto::ServiceIPCHost> service_;
  38. std::unique_ptr<perfetto::base::TaskRunner> task_runner_;
  39. };
  40. class MockPosixSystemProducer : public PosixSystemProducer {
  41. public:
  42. MockPosixSystemProducer(
  43. const std::string& socket,
  44. bool check_sdk_level = false,
  45. uint32_t num_data_sources = 0,
  46. base::OnceClosure data_source_enabled_callback = base::OnceClosure(),
  47. base::OnceClosure data_source_disabled_callback = base::OnceClosure(),
  48. bool sandbox_forbids_socket_connection = false);
  49. ~MockPosixSystemProducer() override;
  50. void StartDataSource(
  51. perfetto::DataSourceInstanceID id,
  52. const perfetto::DataSourceConfig& data_source_config) override;
  53. void StopDataSource(perfetto::DataSourceInstanceID id) override;
  54. void SetDataSourceEnabledCallback(
  55. base::OnceClosure data_source_enabled_callback);
  56. void SetDataSourceDisabledCallback(
  57. base::OnceClosure data_source_disabled_callback);
  58. protected:
  59. // Override for testing.
  60. bool SandboxForbidsSocketConnection() override;
  61. private:
  62. uint32_t num_data_sources_expected_;
  63. uint32_t num_data_sources_active_ = 0;
  64. base::OnceClosure data_source_enabled_callback_;
  65. base::OnceClosure data_source_disabled_callback_;
  66. std::unique_ptr<SystemProducer> old_producer_;
  67. bool sandbox_forbids_socket_connection_;
  68. };
  69. } // namespace tracing
  70. #endif // SERVICES_TRACING_PERFETTO_SYSTEM_TEST_UTILS_H_