File: cryptohome_misc_client_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (326 lines) | stat: -rw-r--r-- 13,216 bytes parent folder | download | duplicates (3)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/userdataauth/cryptohome_misc_client.h"

#include <string>
#include <utility>

#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/cryptohome/UserDataAuth.pb.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/cryptohome/dbus-constants.h"

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

namespace ash {

namespace {

// Runs |callback| with |response|. Needed due to ResponseCallback expecting a
// bare pointer rather than an std::unique_ptr.
void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback,
                         std::unique_ptr<dbus::Response> response) {
  std::move(callback).Run(response.get());
}

bool ProtobufEquals(const google::protobuf::MessageLite& a,
                    const google::protobuf::MessageLite& b) {
  std::string a_serialized, b_serialized;
  a.SerializeToString(&a_serialized);
  b.SerializeToString(&b_serialized);
  return a_serialized == b_serialized;
}

// FakeTaskRunner will run all tasks posted to it immediately in the PostTask()
// call. This class is a helper to ensure that BlockingMethodCaller would work
// correctly in the unit test. Note that Mock is not used because
// SingleThreadTaskRunner is refcounted and it doesn't play well with Mock.
class FakeTaskRunner : public base::SingleThreadTaskRunner {
 public:
  // Yes, this task runner runs everything in sequence.
  bool RunsTasksInCurrentSequence() const override { return true; }

  // Run all tasks immediately, no delay is allowed.
  bool PostDelayedTask(const base::Location& location,
                       base::OnceClosure closure,
                       base::TimeDelta delta) override {
    // Since we are running it now, we can't accept any delay.
    CHECK(delta.is_zero());
    std::move(closure).Run();
    return true;
  }

  // Non nestable task not supported.
  bool PostNonNestableDelayedTask(const base::Location& location,
                                  base::OnceClosure closure,
                                  base::TimeDelta delta) override {
    // Can't run non-nested stuff.
    NOTIMPLEMENTED();
    return false;
  }

 private:
  // For reference counting.
  ~FakeTaskRunner() override {}
};

// Create a callback that would copy the input argument passed to it into |out|.
// This is used mostly to create a callback that would catch the reply from
// dbus.
template <typename T>
base::OnceCallback<void(T)> CreateCopyCallback(T* out) {
  return base::BindOnce([](T* out, T result) { *out = result; }, out);
}

}  // namespace

class CryptohomeMiscClientTest : public testing::Test {
 public:
  CryptohomeMiscClientTest() = default;
  ~CryptohomeMiscClientTest() override = default;

  void SetUp() override {
    dbus::Bus::Options options;
    options.bus_type = dbus::Bus::SYSTEM;
    bus_ = new dbus::MockBus(options);

    dbus::ObjectPath userdataauth_object_path =
        dbus::ObjectPath(::user_data_auth::kUserDataAuthServicePath);
    proxy_ = new dbus::MockObjectProxy(
        bus_.get(), ::user_data_auth::kUserDataAuthServiceName,
        userdataauth_object_path);

    // Makes sure `GetObjectProxy()` is called with the correct service name and
    // path.
    EXPECT_CALL(*bus_.get(),
                GetObjectProxy(::user_data_auth::kUserDataAuthServiceName,
                               userdataauth_object_path))
        .WillRepeatedly(Return(proxy_.get()));

    EXPECT_CALL(*proxy_.get(), DoCallMethod(_, _, _))
        .WillRepeatedly(Invoke(this, &CryptohomeMiscClientTest::OnCallMethod));
    EXPECT_CALL(*proxy_.get(), CallMethodAndBlock(_, _))
        .WillRepeatedly(
            Invoke(this, &CryptohomeMiscClientTest::OnBlockingCallMethod));

    CryptohomeMiscClient::Initialize(bus_.get());

    // Execute callbacks posted by `client_->Init()`.
    base::RunLoop().RunUntilIdle();

    client_ = CryptohomeMiscClient::Get();
  }

  void TearDown() override { CryptohomeMiscClient::Shutdown(); }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;

  // Mock bus and proxy for simulating calls.
  scoped_refptr<dbus::MockBus> bus_;
  scoped_refptr<dbus::MockObjectProxy> proxy_;

  // Convenience pointer to the global instance.
  raw_ptr<CryptohomeMiscClient, DanglingUntriaged> client_;

  // The expected replies to the respective D-Bus calls.
  ::user_data_auth::GetSystemSaltReply expected_get_system_salt_reply_;
  ::user_data_auth::GetSanitizedUsernameReply
      expected_get_sanitized_username_reply_;
  ::user_data_auth::GetLoginStatusReply expected_get_login_status_reply_;
  ::user_data_auth::LockToSingleUserMountUntilRebootReply
      expected_lock_to_single_user_mount_until_reboot_reply_;
  ::user_data_auth::GetRsuDeviceIdReply expected_get_rsu_device_id_reply_;

  // The expected replies to the respective blocking D-Bus calls.
  ::user_data_auth::GetSanitizedUsernameReply
      expected_blocking_get_sanitized_username_reply_;

  // When it is set `true`, an invalid array of bytes that cannot be parsed will
  // be the response.
  bool shall_message_parsing_fail_ = false;

 private:
  // Handles calls to |proxy_|'s `CallMethod()`.
  void OnCallMethod(dbus::MethodCall* method_call,
                    int timeout_ms,
                    dbus::ObjectProxy::ResponseCallback* callback) {
    std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    dbus::MessageWriter writer(response.get());
    if (shall_message_parsing_fail_) {
      // 0x02 => Field 0, Type String
      // (0xFF)*6 => Varint, the size of the string, it is not terminated and is
      // a very large value so the parsing will fail.
      constexpr uint8_t invalid_protobuf[] = {0x02, 0xFF, 0xFF, 0xFF,
                                              0xFF, 0xFF, 0xFF};
      writer.AppendArrayOfBytes(invalid_protobuf);
    } else if (method_call->GetMember() == ::user_data_auth::kGetSystemSalt) {
      writer.AppendProtoAsArrayOfBytes(expected_get_system_salt_reply_);
    } else if (method_call->GetMember() ==
               ::user_data_auth::kGetSanitizedUsername) {
      writer.AppendProtoAsArrayOfBytes(expected_get_sanitized_username_reply_);
    } else if (method_call->GetMember() == ::user_data_auth::kGetLoginStatus) {
      writer.AppendProtoAsArrayOfBytes(expected_get_login_status_reply_);
    } else if (method_call->GetMember() ==
               ::user_data_auth::kLockToSingleUserMountUntilReboot) {
      writer.AppendProtoAsArrayOfBytes(
          expected_lock_to_single_user_mount_until_reboot_reply_);
    } else if (method_call->GetMember() == ::user_data_auth::kGetRsuDeviceId) {
      writer.AppendProtoAsArrayOfBytes(expected_get_rsu_device_id_reply_);
    } else {
      ASSERT_FALSE(true) << "Unrecognized member: " << method_call->GetMember();
    }
    task_environment_.GetMainThreadTaskRunner()->PostTask(
        FROM_HERE, base::BindOnce(RunResponseCallback, std::move(*callback),
                                  std::move(response)));
  }

  // Handles blocking call to |proxy_|'s `CallMethodAndBlock`.
  base::expected<std::unique_ptr<dbus::Response>, dbus::Error>
  OnBlockingCallMethod(dbus::MethodCall* method_call, int timeout_ms) {
    std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    dbus::MessageWriter writer(response.get());
    if (shall_message_parsing_fail_) {
      // 0x02 => Field 0, Type String
      // (0xFF)*6 => Varint, the size of the string, it is not terminated and is
      // a very large value so the parsing will fail.
      constexpr uint8_t invalid_protobuf[] = {0x02, 0xFF, 0xFF, 0xFF,
                                              0xFF, 0xFF, 0xFF};
      writer.AppendArrayOfBytes(invalid_protobuf);
    } else if (method_call->GetMember() ==
               ::user_data_auth::kGetSanitizedUsername) {
      writer.AppendProtoAsArrayOfBytes(
          expected_blocking_get_sanitized_username_reply_);
    } else {
      LOG(FATAL) << "Unrecognized member: " << method_call->GetMember();
    }
    return base::ok(std::move(response));
  }
};

TEST_F(CryptohomeMiscClientTest, GetSystemSalt) {
  constexpr char kSalt[] = "example_salt";
  expected_get_system_salt_reply_.set_salt(std::string(kSalt));
  std::optional<::user_data_auth::GetSystemSaltReply> result_reply;

  client_->GetSystemSalt(::user_data_auth::GetSystemSaltRequest(),
                         CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(
      ProtobufEquals(result_reply.value(), expected_get_system_salt_reply_));
}

TEST_F(CryptohomeMiscClientTest, GetSystemSaltInvalidProtobuf) {
  shall_message_parsing_fail_ = true;
  std::optional<::user_data_auth::GetSystemSaltReply> result_reply =
      ::user_data_auth::GetSystemSaltReply();

  client_->GetSystemSalt(::user_data_auth::GetSystemSaltRequest(),
                         CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_EQ(result_reply, std::nullopt);
}

TEST_F(CryptohomeMiscClientTest, GetSanitizedUsername) {
  constexpr char kAccountId[] = "test1234@example.com";
  expected_get_sanitized_username_reply_.set_sanitized_username(
      std::string(kAccountId));
  std::optional<::user_data_auth::GetSanitizedUsernameReply> result_reply;

  client_->GetSanitizedUsername(::user_data_auth::GetSanitizedUsernameRequest(),
                                CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(ProtobufEquals(result_reply.value(),
                             expected_get_sanitized_username_reply_));
}

TEST_F(CryptohomeMiscClientTest, GetLoginStatus) {
  expected_get_login_status_reply_.set_error(
      user_data_auth::CryptohomeErrorCode::CRYPTOHOME_ERROR_TPM_DEFEND_LOCK);
  std::optional<::user_data_auth::GetLoginStatusReply> result_reply;

  client_->GetLoginStatus(::user_data_auth::GetLoginStatusRequest(),
                          CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(
      ProtobufEquals(result_reply.value(), expected_get_login_status_reply_));
}

TEST_F(CryptohomeMiscClientTest, LockToSingleUserMountUntilReboot) {
  expected_lock_to_single_user_mount_until_reboot_reply_.set_error(
      user_data_auth::CryptohomeErrorCode::CRYPTOHOME_ERROR_TPM_DEFEND_LOCK);
  std::optional<::user_data_auth::LockToSingleUserMountUntilRebootReply>
      result_reply;

  client_->LockToSingleUserMountUntilReboot(
      ::user_data_auth::LockToSingleUserMountUntilRebootRequest(),
      CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(
      ProtobufEquals(result_reply.value(),
                     expected_lock_to_single_user_mount_until_reboot_reply_));
}

TEST_F(CryptohomeMiscClientTest, GetRsuDeviceId) {
  expected_get_rsu_device_id_reply_.set_error(
      user_data_auth::CryptohomeErrorCode::CRYPTOHOME_ERROR_TPM_DEFEND_LOCK);
  std::optional<::user_data_auth::GetRsuDeviceIdReply> result_reply;

  client_->GetRsuDeviceId(::user_data_auth::GetRsuDeviceIdRequest(),
                          CreateCopyCallback(&result_reply));
  base::RunLoop().RunUntilIdle();
  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(
      ProtobufEquals(result_reply.value(), expected_get_rsu_device_id_reply_));
}

TEST_F(CryptohomeMiscClientTest, BlockingGetSanitizedUsername) {
  constexpr char kAccountId[] = "test1234@example.com";
  expected_blocking_get_sanitized_username_reply_.set_sanitized_username(
      std::string(kAccountId));
  std::optional<::user_data_auth::GetSanitizedUsernameReply> result_reply;

  scoped_refptr<FakeTaskRunner> runner = new FakeTaskRunner;
  EXPECT_CALL(*bus_.get(), GetDBusTaskRunner())
      .WillRepeatedly(Return(runner.get()));

  result_reply = client_->BlockingGetSanitizedUsername(
      ::user_data_auth::GetSanitizedUsernameRequest());

  ASSERT_NE(result_reply, std::nullopt);
  EXPECT_TRUE(ProtobufEquals(result_reply.value(),
                             expected_blocking_get_sanitized_username_reply_));
}

TEST_F(CryptohomeMiscClientTest, BlockingGetSanitizedUsernameInvalidProtobuf) {
  shall_message_parsing_fail_ = true;
  std::optional<::user_data_auth::GetSanitizedUsernameReply> result_reply =
      ::user_data_auth::GetSanitizedUsernameReply();

  scoped_refptr<FakeTaskRunner> runner = new FakeTaskRunner;
  EXPECT_CALL(*bus_.get(), GetDBusTaskRunner())
      .WillRepeatedly(Return(runner.get()));

  result_reply = client_->BlockingGetSanitizedUsername(
      ::user_data_auth::GetSanitizedUsernameRequest());

  EXPECT_EQ(result_reply, std::nullopt);
}

}  // namespace ash