summaryrefslogtreecommitdiff
path: root/chromium/services/tracing/public/cpp/system_tracing_service_unittest.cc
blob: 7124dee969b40170df819158442072317c16725b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <unistd.h>

#include "base/files/scoped_temp_dir.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

#include "services/tracing/perfetto/system_test_utils.h"
#include "services/tracing/public/cpp/perfetto/perfetto_traced_process.h"
#include "services/tracing/public/cpp/system_tracing_service.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace tracing {

const char* kProducerSockEnvName = "PERFETTO_PRODUCER_SOCK_NAME";

namespace {

class SystemTracingServiceTest : public testing::Test {
 public:
  SystemTracingServiceTest()
      : task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}

  void SetUp() override {
    // Disable system producer since the tests will exercise producer socket
    // connection.
    PerfettoTracedProcess::SetSystemProducerEnabledForTesting(false);

    // The test connects to the mock system service.
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    system_service_ = std::make_unique<MockSystemService>(temp_dir_);

    // Override the default system producer socket.
    saved_producer_sock_env_ = getenv(kProducerSockEnvName);
    ASSERT_EQ(0, setenv(kProducerSockEnvName,
                        system_service_->producer().c_str(), 1));

    // Use the current thread as the Perfetto task runner.
    tracing::PerfettoTracedProcess::ResetTaskRunnerForTesting(
        base::ThreadTaskRunnerHandle::Get());
  }

  void TearDown() override {
    // Restore the value of Perfetto producer socket name env variable.
    if (saved_producer_sock_env_) {
      ASSERT_EQ(0,
                setenv(kProducerSockEnvName, saved_producer_sock_env_, true));
    } else {
      ASSERT_EQ(0, unsetenv(kProducerSockEnvName));
    }
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  base::ScopedTempDir temp_dir_;
  std::unique_ptr<MockSystemService> system_service_;
  const char* saved_producer_sock_env_ = nullptr;
};

// Test the OpenProducerSocket implementation. Expect a valid socket file
// descriptor returned in the callback.
TEST_F(SystemTracingServiceTest, OpenProducerSocket) {
  auto sts = std::make_unique<SystemTracingService>();
  bool callback_called = false;

  base::RunLoop run_loop;
  auto callback = base::BindLambdaForTesting([&](base::File file) {
    callback_called = true;
    ASSERT_TRUE(file.IsValid());
    run_loop.Quit();
  });

  absl::optional<bool> socket_connected;
  auto on_connect_callback = base::BindLambdaForTesting(
      [&](bool connected) { socket_connected = connected; });

  sts->OpenProducerSocketForTesting(std::move(callback),
                                    std::move(on_connect_callback));
  ASSERT_FALSE(callback_called);
  run_loop.Run();
  ASSERT_TRUE(callback_called);
  ASSERT_TRUE(socket_connected.has_value());
  ASSERT_TRUE(*socket_connected);
}

// Test the OpenProducerSocket implementation with a nonexistent socket. Expect
// an invalid socket file descriptor returned in the callback.
TEST_F(SystemTracingServiceTest, OpenProducerSocket_Nonexistent) {
  auto sts = std::make_unique<SystemTracingService>();
  bool callback_called = false;

  // Set the producer socket name to a nonexistent path.
  saved_producer_sock_env_ = getenv(kProducerSockEnvName);
  ASSERT_EQ(0, setenv(kProducerSockEnvName, "nonexistent_socket", 1));

  base::RunLoop run_loop;
  auto callback = base::BindLambdaForTesting([&](base::File file) {
    callback_called = true;
    // OpenProducerSocket fails and returns an invalid socket file descriptor.
    ASSERT_FALSE(file.IsValid());
    run_loop.Quit();
  });

  absl::optional<bool> socket_connected;
  auto on_connect_callback = base::BindLambdaForTesting(
      [&](bool connected) { socket_connected = connected; });
  sts->OpenProducerSocketForTesting(std::move(callback),
                                    std::move(on_connect_callback));

  ASSERT_FALSE(callback_called);
  run_loop.Run();
  ASSERT_TRUE(callback_called);
  ASSERT_TRUE(socket_connected.has_value());
  ASSERT_FALSE(*socket_connected);
}

// Test the OpenProducerSocket implementation through mojo. Expect that the
// callback runs when invoked through mojo.
TEST_F(SystemTracingServiceTest, BindAndPassPendingRemote) {
  auto sts = std::make_unique<SystemTracingService>();
  bool callback_called = false;

  // Bind the pending remote on the current thread.
  mojo::Remote<mojom::SystemTracingService> remote;
  remote.Bind(sts->BindAndPassPendingRemote(), nullptr);

  base::RunLoop run_loop;
  auto callback = base::BindLambdaForTesting([&](base::File file) {
    callback_called = true;
    ASSERT_TRUE(file.IsValid());
    run_loop.Quit();
  });

  remote->OpenProducerSocket(std::move(callback));
  ASSERT_FALSE(callback_called);
  run_loop.Run();
  ASSERT_TRUE(callback_called);
}

// Test the OpenProducerSocket implementation through mojo with a nonexistent
// socket. Expect that the callback runs with an invalid socket file descriptor
TEST_F(SystemTracingServiceTest, BindAndPassPendingRemote_Nonexistent) {
  auto sts = std::make_unique<SystemTracingService>();
  bool callback_called = false;

  // Set the producer socket name to a nonexistent path.
  saved_producer_sock_env_ = getenv(kProducerSockEnvName);
  ASSERT_EQ(0, setenv(kProducerSockEnvName, "nonexistent_socket", 1));

  // Bind the pending remote on the current thread.
  mojo::Remote<mojom::SystemTracingService> remote;
  remote.Bind(sts->BindAndPassPendingRemote(), nullptr);

  base::RunLoop run_loop;
  auto callback = base::BindLambdaForTesting([&](base::File file) {
    callback_called = true;
    ASSERT_FALSE(file.IsValid());
    run_loop.Quit();
  });

  remote->OpenProducerSocket(std::move(callback));
  ASSERT_FALSE(callback_called);
  run_loop.Run();
  ASSERT_TRUE(callback_called);
}

}  // namespace
}  // namespace tracing