File: check_for_service_and_start_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (194 lines) | stat: -rw-r--r-- 7,221 bytes parent folder | download | duplicates (6)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/dbus/utils/check_for_service_and_start.h"

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;

namespace dbus_utils {
namespace {

MATCHER_P2(MatchMethod, interface, member, "") {
  return arg->GetInterface() == interface && arg->GetMember() == member;
}

class CheckForServiceAndStartTest : public testing::Test {
 public:
  void SetUp() override {
    mock_bus_ = base::MakeRefCounted<dbus::MockBus>(dbus::Bus::Options());

    mock_dbus_proxy_ = base::MakeRefCounted<dbus::MockObjectProxy>(
        mock_bus_.get(), DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS));

    EXPECT_CALL(*mock_bus_, GetObjectProxy(DBUS_SERVICE_DBUS,
                                           dbus::ObjectPath(DBUS_PATH_DBUS)))
        .WillRepeatedly(Return(mock_dbus_proxy_.get()));
  }

  void TearDown() override {
    mock_bus_.reset();
    mock_dbus_proxy_.reset();
  }

 protected:
  void ExpectNameHasOwner(const std::string& service_name,
                          bool has_owner,
                          bool error = false) {
    EXPECT_CALL(
        *mock_dbus_proxy_,
        DoCallMethod(MatchMethod(DBUS_INTERFACE_DBUS, "NameHasOwner"), _, _))
        .WillOnce(Invoke([this, service_name, has_owner, error](
                             dbus::MethodCall* method_call, int timeout_ms,
                             dbus::ObjectProxy::ResponseCallback* callback) {
          dbus::MessageReader reader(method_call);
          std::string received_service_name;
          EXPECT_TRUE(reader.PopString(&received_service_name));
          EXPECT_EQ(received_service_name, service_name);

          if (error) {
            // Simulate an error by calling the callback with a null response
            task_environment_.GetMainThreadTaskRunner()->PostTask(
                FROM_HERE, base::BindOnce(std::move(*callback), nullptr));
          } else {
            response_ = dbus::Response::CreateEmpty();
            dbus::MessageWriter writer(response_.get());
            writer.AppendBool(has_owner);
            task_environment_.GetMainThreadTaskRunner()->PostTask(
                FROM_HERE,
                base::BindOnce(std::move(*callback), response_.get()));
          }
        }));
  }

  void ExpectListActivatableNames(
      const std::vector<std::string>& activatable_names) {
    EXPECT_CALL(
        *mock_dbus_proxy_,
        DoCallMethod(MatchMethod(DBUS_INTERFACE_DBUS, "ListActivatableNames"),
                     _, _))
        .WillOnce(Invoke([this, activatable_names](
                             dbus::MethodCall* method_call, int timeout_ms,
                             dbus::ObjectProxy::ResponseCallback* callback) {
          response_ = dbus::Response::CreateEmpty();
          dbus::MessageWriter writer(response_.get());
          writer.AppendArrayOfStrings(activatable_names);
          task_environment_.GetMainThreadTaskRunner()->PostTask(
              FROM_HERE, base::BindOnce(std::move(*callback), response_.get()));
        }));
  }

  void ExpectStartServiceByName(const std::string& service_name,
                                uint32_t reply_code) {
    EXPECT_CALL(
        *mock_dbus_proxy_,
        DoCallMethodWithErrorResponse(
            MatchMethod(DBUS_INTERFACE_DBUS, "StartServiceByName"), _, _))
        .WillOnce(
            Invoke([this, service_name, reply_code](
                       dbus::MethodCall* method_call, int timeout_ms,
                       dbus::ObjectProxy::ResponseOrErrorCallback* callback) {
              dbus::MessageReader reader(method_call);
              std::string received_service_name;
              uint32_t flags;
              EXPECT_TRUE(reader.PopString(&received_service_name));
              EXPECT_TRUE(reader.PopUint32(&flags));
              EXPECT_EQ(received_service_name, service_name);
              EXPECT_EQ(flags, 0U);

              response_ = dbus::Response::CreateEmpty();
              dbus::MessageWriter writer(response_.get());
              writer.AppendUint32(reply_code);
              task_environment_.GetMainThreadTaskRunner()->PostTask(
                  FROM_HERE, base::BindOnce(std::move(*callback),
                                            response_.get(), nullptr));
            }));
  }

  void RunCheckForServiceAndStart(const std::string& service_name,
                                  std::optional<bool>* service_started_out) {
    base::RunLoop run_loop;
    CheckForServiceAndStartCallback callback = base::BindOnce(
        [](std::optional<bool>* service_started_out,
           base::OnceClosure quit_closure,
           std::optional<bool> service_started_in) {
          *service_started_out = service_started_in;
          std::move(quit_closure).Run();
        },
        service_started_out, run_loop.QuitClosure());

    CheckForServiceAndStart(mock_bus_, service_name, std::move(callback));

    run_loop.Run();
  }

  base::test::SingleThreadTaskEnvironment task_environment_;
  std::unique_ptr<dbus::Response> response_;
  scoped_refptr<dbus::MockBus> mock_bus_;
  scoped_refptr<dbus::MockObjectProxy> mock_dbus_proxy_;
};

TEST_F(CheckForServiceAndStartTest, ServiceAlreadyRunning) {
  const std::string kServiceName = "org.example.Service";

  ExpectNameHasOwner(kServiceName, true);

  std::optional<bool> service_started;
  RunCheckForServiceAndStart(kServiceName, &service_started);

  EXPECT_TRUE(service_started.has_value());
  EXPECT_TRUE(service_started.value());
}

TEST_F(CheckForServiceAndStartTest, ServiceNotRunningButActivatable) {
  const std::string kServiceName = "org.example.Service";

  ExpectNameHasOwner(kServiceName, false);
  ExpectListActivatableNames({kServiceName, "org.other.Service"});
  ExpectStartServiceByName(kServiceName, DBUS_START_REPLY_SUCCESS);

  std::optional<bool> service_started;
  RunCheckForServiceAndStart(kServiceName, &service_started);

  ASSERT_TRUE(service_started.has_value());
  EXPECT_TRUE(service_started.value());
}

TEST_F(CheckForServiceAndStartTest, ServiceNotRunningAndNotActivatable) {
  const std::string kServiceName = "org.example.Service";

  ExpectNameHasOwner(kServiceName, false);
  ExpectListActivatableNames({"org.other.Service"});

  std::optional<bool> service_started;
  RunCheckForServiceAndStart(kServiceName, &service_started);

  ASSERT_TRUE(service_started.has_value());
  EXPECT_FALSE(service_started.value());
}

TEST_F(CheckForServiceAndStartTest, NameHasOwnerError) {
  const std::string kServiceName = "org.example.Service";

  ExpectNameHasOwner(kServiceName, false, true);  // Simulate an error

  std::optional<bool> service_started;
  RunCheckForServiceAndStart(kServiceName, &service_started);

  EXPECT_FALSE(service_started.has_value());
}

}  // namespace
}  // namespace dbus_utils