File: biod_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 (500 lines) | stat: -rw-r--r-- 19,672 bytes parent folder | download | duplicates (7)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2017 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/biod/biod_client.h"

#include <map>
#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/biod/messages.pb.h"
#include "chromeos/ash/components/dbus/biod/test_utils.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"

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

namespace ash {

namespace {

// Shorthand for a commonly-used constant.
const char* kInterface = biod::kBiometricsManagerInterface;

// Value used to intialize dbus::ObjectPath objects in tests to make it easier
// to determine when empty values have been assigned.
const char kInvalidTestPath[] = "/invalid/test/path";

// Value used to intialize string objects in tests to make it easier to
// determine when empty values have been assigned.
const char kInvalidString[] = "invalidString";

// Matcher that verifies that a dbus::Message has member |name|.
MATCHER_P(HasMember, name, "") {
  if (arg->GetMember() != name) {
    *result_listener << "has member " << arg->GetMember();
    return false;
  }
  return true;
}

// 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());
}

}  // namespace

class BiodClientTest : public testing::Test {
 public:
  BiodClientTest() = default;

  BiodClientTest(const BiodClientTest&) = delete;
  BiodClientTest& operator=(const BiodClientTest&) = delete;

  ~BiodClientTest() override = default;

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

    dbus::ObjectPath fpc_bio_path = dbus::ObjectPath(base::StringPrintf(
        "%s/%s", biod::kBiodServicePath, biod::kCrosFpBiometricsManagerName));
    proxy_ = new dbus::MockObjectProxy(bus_.get(), biod::kBiodServiceName,
                                       fpc_bio_path);

    // |client_|'s Init() method should request a proxy for communicating with
    // biometrics api.
    EXPECT_CALL(*bus_.get(), GetObjectProxy(biod::kBiodServiceName, _))
        .WillRepeatedly(Return(proxy_.get()));

    // Save |client_|'s signal callback.
    EXPECT_CALL(*proxy_.get(), DoConnectToSignal(kInterface, _, _, _))
        .WillRepeatedly(Invoke(this, &BiodClientTest::ConnectToSignal));

    BiodClient::Initialize(bus_.get());
    client_ = BiodClient::Get();

    // Execute callbacks posted by Init().
    base::RunLoop().RunUntilIdle();
  }

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

  void GetBiometricType(biod::BiometricType type) { biometric_type_ = type; }

 protected:
  // Add an expectation for method with |method_name| to be called. When the
  // method is called the response should match |response|.
  void AddMethodExpectation(const std::string& method_name,
                            std::unique_ptr<dbus::Response> response) {
    ASSERT_FALSE(pending_method_calls_.count(method_name));
    pending_method_calls_[method_name] = std::move(response);
    EXPECT_CALL(*proxy_.get(), DoCallMethod(HasMember(method_name), _, _))
        .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod));
  }

  // Synchronously passes |signal| to |client_|'s handler, simulating the signal
  // from biometrics.
  void EmitSignal(dbus::Signal* signal) {
    const std::string signal_name = signal->GetMember();
    const auto it = signal_callbacks_.find(signal_name);
    ASSERT_TRUE(it != signal_callbacks_.end())
        << "Client didn't register for signal " << signal_name;
    it->second.Run(signal);
  }

  // Passes a enroll scan done signal to |client_|.
  void EmitEnrollScanDoneSignal(biod::ScanResult scan_result,
                                bool enroll_session_complete,
                                int percent_complete) {
    dbus::Signal signal(kInterface,
                        biod::kBiometricsManagerEnrollScanDoneSignal);
    dbus::MessageWriter writer(&signal);
    biod::EnrollScanDone protobuf;
    protobuf.set_scan_result(scan_result);
    protobuf.set_done(enroll_session_complete);
    protobuf.set_percent_complete(percent_complete);
    writer.AppendProtoAsArrayOfBytes(protobuf);
    EmitSignal(&signal);
  }

  // Appends AuthScanMatches to |writer| message.
  void AppendMatchesArray(dbus::MessageWriter* writer,
                          const AuthScanMatches& matches) {
    dbus::MessageWriter array_writer(nullptr);
    writer->OpenArray("{sx}", &array_writer);
    for (auto& match : matches) {
      dbus::MessageWriter entry_writer(nullptr);
      array_writer.OpenDictEntry(&entry_writer);
      entry_writer.AppendString(match.first);
      entry_writer.AppendArrayOfObjectPaths(match.second);
      array_writer.CloseContainer(&entry_writer);
    }
    writer->CloseContainer(&array_writer);
  }

  // Passes a auth scan done signal to |client_|.
  void EmitAuthScanDoneSignal(const biod::FingerprintMessage& msg,
                              const AuthScanMatches& matches) {
    dbus::Signal signal(kInterface, biod::kBiometricsManagerAuthScanDoneSignal);
    dbus::MessageWriter writer(&signal);
    writer.AppendProtoAsArrayOfBytes(msg);
    AppendMatchesArray(&writer, matches);

    EmitSignal(&signal);
  }

  // Passes a scan failed signal to |client_|.
  void EmitScanFailedSignal() {
    dbus::Signal signal(kInterface,
                        biod::kBiometricsManagerSessionFailedSignal);
    EmitSignal(&signal);
  }

  std::map<std::string, std::unique_ptr<dbus::Response>> pending_method_calls_;

  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<BiodClient, DanglingUntriaged> client_;

  // Maps from biod signal name to the corresponding callback provided by
  // |client_|.
  std::map<std::string, dbus::ObjectProxy::SignalCallback> signal_callbacks_;

  biod::BiometricType biometric_type_;

 private:
  // Handles calls to |proxy_|'s ConnectToSignal() method.
  void ConnectToSignal(
      const std::string& interface_name,
      const std::string& signal_name,
      dbus::ObjectProxy::SignalCallback signal_callback,
      dbus::ObjectProxy::OnConnectedCallback* on_connected_callback) {
    EXPECT_EQ(interface_name, kInterface);
    signal_callbacks_[signal_name] = signal_callback;
    task_environment_.GetMainThreadTaskRunner()->PostTask(
        FROM_HERE,
        base::BindOnce(std::move(*on_connected_callback), interface_name,
                       signal_name, true /* success */));
  }

  // Handles calls to |proxy_|'s CallMethod().
  void OnCallMethod(dbus::MethodCall* method_call,
                    int timeout_ms,
                    dbus::ObjectProxy::ResponseCallback* callback) {
    auto it = pending_method_calls_.find(method_call->GetMember());
    ASSERT_TRUE(it != pending_method_calls_.end());
    auto pending_response = std::move(it->second);
    pending_method_calls_.erase(it);

    task_environment_.GetMainThreadTaskRunner()->PostTask(
        FROM_HERE, base::BindOnce(&RunResponseCallback, std::move(*callback),
                                  std::move(pending_response)));
  }
};

TEST_F(BiodClientTest, TestStartEnrollSession) {
  const std::string kFakeId("fakeId");
  const std::string kFakeLabel("fakeLabel");
  const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
  const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2"));

  // Verify that by sending a empty reponse or a improperly formatted one, the
  // response is an empty object path.
  AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
                       nullptr);
  dbus::ObjectPath returned_path(kInvalidTestPath);
  client_->StartEnrollSession(
      kFakeId, kFakeLabel,
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(dbus::ObjectPath(), returned_path);

  std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
  dbus::MessageWriter bad_writer(bad_response.get());
  bad_writer.AppendString("");
  AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
                       std::move(bad_response));
  returned_path = dbus::ObjectPath(kInvalidTestPath);
  client_->StartEnrollSession(
      kFakeId, kFakeLabel,
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(dbus::ObjectPath(), returned_path);

  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendObjectPath(kFakeObjectPath);

  // Create a fake response with a fake object path. The start enroll
  // call should return this object path.
  AddMethodExpectation(biod::kBiometricsManagerStartEnrollSessionMethod,
                       std::move(response));
  returned_path = dbus::ObjectPath(kInvalidTestPath);
  client_->StartEnrollSession(
      kFakeId, kFakeLabel,
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kFakeObjectPath, returned_path);
}

TEST_F(BiodClientTest, TestGetRecordsForUserWithRecords) {
  const std::string kFakeId("fakeId");
  const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
  const dbus::ObjectPath kFakeObjectPath2(std::string("/fake/object/path2"));
  const std::vector<dbus::ObjectPath> kFakeObjectPaths = {kFakeObjectPath,
                                                          kFakeObjectPath2};

  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendArrayOfObjectPaths(kFakeObjectPaths);

  // Create a fake response with an array of fake object paths. The get
  // records for user call should return this array of object paths.
  AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
                       std::move(response));
  std::vector<dbus::ObjectPath> returned_object_paths = {
      dbus::ObjectPath(kInvalidTestPath)};
  bool returned_success = false;

  client_->GetRecordsForUser(
      kFakeId,
      base::BindOnce(
          [](std::vector<dbus::ObjectPath>& returned_paths,
             bool& returned_success, const std::vector<dbus::ObjectPath>& paths,
             bool success) {
            test_utils::CopyObjectPathArray(&returned_paths, paths);
            returned_success = success;
          },
          std::ref(returned_object_paths), std::ref(returned_success)));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(returned_success, true);
  EXPECT_EQ(kFakeObjectPaths, returned_object_paths);
}

TEST_F(BiodClientTest, TestGetRecordsForUserWithoutRecords) {
  const std::string kFakeId("fakeId");
  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());

  // Create a fake response with an empty array. The response also
  // should be an empty array.
  AddMethodExpectation(biod::kBiometricsManagerGetRecordsForUserMethod,
                       std::move(response));
  std::vector<dbus::ObjectPath> returned_object_paths = {
      dbus::ObjectPath(kInvalidTestPath)};
  bool returned_success = false;

  client_->GetRecordsForUser(
      kFakeId,
      base::BindOnce(
          [](std::vector<dbus::ObjectPath>& returned_paths,
             bool& returned_success, const std::vector<dbus::ObjectPath>& paths,
             bool success) {
            test_utils::CopyObjectPathArray(&returned_paths, paths);
            returned_success = success;
          },
          std::ref(returned_object_paths), std::ref(returned_success)));

  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(returned_success, true);
  EXPECT_EQ(std::vector<dbus::ObjectPath>(), returned_object_paths);
}

TEST_F(BiodClientTest, TestDestroyAllRecords) {
  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());

  // Create an empty response to simulate success.
  AddMethodExpectation(biod::kBiometricsManagerDestroyAllRecordsMethod,
                       std::move(response));
  bool result = false;
  client_->DestroyAllRecords(
      base::BindOnce(&test_utils::CopyDBusMethodCallResult, &result));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(result);

  // Return a null response to simulate failure.
  AddMethodExpectation(biod::kBiometricsManagerDestroyAllRecordsMethod,
                       nullptr);
  result = false;
  client_->DestroyAllRecords(
      base::BindOnce(&test_utils::CopyDBusMethodCallResult, &result));
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(result);
}

TEST_F(BiodClientTest, TestRemoveRecord) {
  const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));
  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());

  // Create an empty response to simulate success.
  AddMethodExpectation(biod::kRecordRemoveMethod, std::move(response));
  bool result = false;
  client_->RemoveRecord(
      kFakeObjectPath,
      base::BindOnce(&test_utils::CopyDBusMethodCallResult, &result));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(result);

  // Return a null response to simulate failure.
  AddMethodExpectation(biod::kRecordRemoveMethod, nullptr);
  result = false;
  client_->RemoveRecord(
      kFakeObjectPath,
      base::BindOnce(&test_utils::CopyDBusMethodCallResult, &result));
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(result);
}

TEST_F(BiodClientTest, TestStartAuthentication) {
  const dbus::ObjectPath kFakeObjectPath(std::string("/fake/object/path"));

  // Verify that by sending a empty reponse or a improperly formatted one, the
  // response is an empty object path.
  AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod, nullptr);
  dbus::ObjectPath returned_path(kInvalidTestPath);
  client_->StartAuthSession(
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(dbus::ObjectPath(), returned_path);

  std::unique_ptr<dbus::Response> bad_response(dbus::Response::CreateEmpty());
  dbus::MessageWriter bad_writer(bad_response.get());
  bad_writer.AppendString("");
  AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
                       std::move(bad_response));
  returned_path = dbus::ObjectPath(kInvalidTestPath);
  client_->StartAuthSession(
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(dbus::ObjectPath(), returned_path);

  // Create a fake response with a fake object path. The start authentication
  // call should return this object path.
  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendObjectPath(kFakeObjectPath);

  AddMethodExpectation(biod::kBiometricsManagerStartAuthSessionMethod,
                       std::move(response));
  returned_path = dbus::ObjectPath(kInvalidTestPath);
  client_->StartAuthSession(
      base::BindOnce(&test_utils::CopyObjectPath, &returned_path));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kFakeObjectPath, returned_path);
}

TEST_F(BiodClientTest, TestRequestBiometricType) {
  const auto kFakeBiometricType = biod::BIOMETRIC_TYPE_FINGERPRINT;

  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendVariantOfUint32(static_cast<uint32_t>(kFakeBiometricType));

  // Create a fake response with biometric type. The get label call should
  // return this exact biometric type.
  biometric_type_ = biod::BIOMETRIC_TYPE_MAX;
  AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
  client_->RequestType(base::BindOnce(&BiodClientTest::GetBiometricType,
                                      base::Unretained(this)));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kFakeBiometricType, biometric_type_);

  // Verify that by sending a null reponse, the result is an unknown biometric
  // type.
  biometric_type_ = biod::BIOMETRIC_TYPE_MAX;
  AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
  client_->RequestType(base::BindOnce(&BiodClientTest::GetBiometricType,
                                      base::Unretained(this)));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(biod::BIOMETRIC_TYPE_UNKNOWN, biometric_type_);
}

TEST_F(BiodClientTest, TestRequestRecordLabel) {
  const std::string kFakeLabel("fakeLabel");
  const dbus::ObjectPath kFakeRecordPath(std::string("/fake/record/path"));

  std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendVariantOfString(kFakeLabel);

  // Create a fake response with string. The get label call should return this
  // exact string.
  std::string returned_label = kInvalidString;
  AddMethodExpectation(dbus::kDBusPropertiesGet, std::move(response));
  client_->RequestRecordLabel(
      kFakeRecordPath,
      base::BindOnce(&test_utils::CopyString, &returned_label));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(kFakeLabel, returned_label);

  // Verify that by sending a null reponse, the result is an empty string.
  returned_label = kInvalidString;
  AddMethodExpectation(dbus::kDBusPropertiesGet, nullptr);
  client_->RequestRecordLabel(
      kFakeRecordPath,
      base::BindOnce(&test_utils::CopyString, &returned_label));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ("", returned_label);
}

// Verify when signals are mocked, an observer will catch the signals as
// expected.
TEST_F(BiodClientTest, TestNotifyObserversFingerprintMessage) {
  test_utils::TestBiodObserver observer;
  client_->AddObserver(&observer);
  EXPECT_TRUE(client_->HasObserver(&observer));

  const biod::ScanResult scan_signal = biod::ScanResult::SCAN_RESULT_SUCCESS;
  const bool enroll_session_complete = false;
  const int percent_complete = 0;
  const AuthScanMatches test_attempt;
  biod::FingerprintMessage msg;
  msg.set_scan_result(scan_signal);
  EXPECT_EQ(0, observer.NumEnrollScansReceived());
  EXPECT_EQ(0, observer.NumAuthScansReceived());
  EXPECT_EQ(0, observer.num_failures_received());

  EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete,
                           percent_complete);
  EXPECT_EQ(1, observer.NumEnrollScansReceived());

  EmitAuthScanDoneSignal(msg, test_attempt);
  EXPECT_EQ(1, observer.NumAuthScansReceived());

  EmitScanFailedSignal();
  EXPECT_EQ(1, observer.num_failures_received());

  client_->RemoveObserver(&observer);

  EmitEnrollScanDoneSignal(scan_signal, enroll_session_complete,
                           percent_complete);
  EmitAuthScanDoneSignal(msg, test_attempt);
  EXPECT_EQ(1, observer.NumEnrollScansReceived());
  EXPECT_EQ(1, observer.NumAuthScansReceived());
  EXPECT_EQ(1, observer.num_failures_received());
}
}  // namespace ash