File: device_authenticator_android_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 (230 lines) | stat: -rw-r--r-- 9,248 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
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
// 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 "chrome/browser/device_reauth/android/device_authenticator_android.h"

#include <memory>

#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chrome/browser/device_reauth/android/device_authenticator_bridge.h"
#include "components/device_reauth/device_authenticator.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using base::Bucket;
using base::test::RunOnceCallback;
using device_reauth::BiometricsAvailability;
using device_reauth::DeviceAuthenticator;
using device_reauth::DeviceAuthUIResult;
using testing::_;
using testing::ElementsAre;
using testing::Return;

class MockDeviceAuthenticatorBridge : public DeviceAuthenticatorBridge {
 public:
  MOCK_METHOD(BiometricsAvailability,
              CanAuthenticateWithBiometric,
              (),
              (override));
  MOCK_METHOD(bool, CanAuthenticateWithBiometricOrScreenLock, (), (override));
  MOCK_METHOD(void,
              Authenticate,
              (base::OnceCallback<void(device_reauth::DeviceAuthUIResult)>
                   response_callback),
              (override));
  MOCK_METHOD(void, Cancel, (), (override));
};

}  // namespace

class DeviceAuthenticatorAndroidTest : public testing::Test {
 public:
  DeviceAuthenticatorAndroidTest()
      : device_authenticator_params_(
            base::Seconds(60),
            device_reauth::DeviceAuthSource::kPasswordManager) {}

  void SetUp() override {
    std::unique_ptr<MockDeviceAuthenticatorBridge> bridge =
        std::make_unique<MockDeviceAuthenticatorBridge>();
    bridge_ = bridge.get();
    authenticator_ = std::make_unique<DeviceAuthenticatorAndroid>(
        std::move(bridge), &proxy_, device_authenticator_params_);
  }

  DeviceAuthenticatorAndroid* authenticator() { return authenticator_.get(); }

  MockDeviceAuthenticatorBridge& bridge() { return *bridge_; }

  base::test::TaskEnvironment& task_environment() { return task_environment_; }

 private:
  DeviceAuthenticatorProxy proxy_;
  device_reauth::DeviceAuthParams device_authenticator_params_;
  std::unique_ptr<DeviceAuthenticatorAndroid> authenticator_;
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};

  // This is owned by the authenticator.
  raw_ptr<MockDeviceAuthenticatorBridge> bridge_ = nullptr;
};

TEST_F(DeviceAuthenticatorAndroidTest, CanAuthenticateCallsBridge) {
  base::HistogramTester histogram_tester;

  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kAvailable));
  EXPECT_TRUE(authenticator()->CanAuthenticateWithBiometrics());

  histogram_tester.ExpectUniqueSample(
      "Android.DeviceAuthenticator.CanAuthenticateWithBiometrics",
      BiometricsAvailability::kAvailable, 1);
}

TEST_F(
    DeviceAuthenticatorAndroidTest,
    CanAuthenticateDoesNotReecordHistogramForNonPasswordManagerForIncognito) {
  base::HistogramTester histogram_tester;

  EXPECT_CALL(bridge(), CanAuthenticateWithBiometricOrScreenLock)
      .WillOnce(Return(true));
  EXPECT_TRUE(authenticator()->CanAuthenticateWithBiometricOrScreenLock());

  histogram_tester.ExpectTotalCount(
      "Android.DeviceAuthenticator.CanAuthenticateWithBiometrics", 0);
}

TEST_F(DeviceAuthenticatorAndroidTest, AuthenticateRecordsSource) {
  base::HistogramTester histogram_tester;

  authenticator()->AuthenticateWithMessage(u"", base::DoNothing());

  histogram_tester.ExpectUniqueSample(
      "Android.DeviceAuthenticator.AuthSource",
      device_reauth::DeviceAuthSource::kPasswordManager, 1);
}

TEST_F(DeviceAuthenticatorAndroidTest, DoesntTriggerAuthIfWithin60Seconds) {
  // Simulate a previous successful authentication
  base::HistogramTester histogram_tester;
  EXPECT_CALL(bridge(), Authenticate)
      .WillOnce(RunOnceCallback<0>(DeviceAuthUIResult::kSuccessWithBiometrics));
  authenticator()->AuthenticateWithMessage(u"", base::DoNothing());

  // The next call to `Authenticate()` should not re-trigger an authentication.
  EXPECT_CALL(bridge(), Authenticate(_)).Times(0);
  base::MockCallback<DeviceAuthenticator::AuthenticateCallback> result_callback;
  EXPECT_CALL(result_callback, Run(/*auth_succeeded=*/true));
  authenticator()->AuthenticateWithMessage(u"", result_callback.Get());
  EXPECT_THAT(
      histogram_tester.GetAllSamples(
          "PasswordManager.BiometricAuthPwdFill.AuthResult"),
      ElementsAre(
          Bucket(
              static_cast<int>(DeviceAuthFinalResult::kSuccessWithBiometrics),
              1),
          Bucket(static_cast<int>(DeviceAuthFinalResult::kAuthStillValid), 1)));
}

TEST_F(DeviceAuthenticatorAndroidTest, TriggersAuthIfMoreThan60Seconds) {
  base::HistogramTester histogram_tester;
  // Simulate a previous successful authentication
  EXPECT_CALL(bridge(), Authenticate)
      .WillOnce(RunOnceCallback<0>(DeviceAuthUIResult::kSuccessWithBiometrics));
  authenticator()->AuthenticateWithMessage(u"", base::DoNothing());

  task_environment().FastForwardBy(base::Seconds(60));

  // The next call to `Authenticate()` should re-trigger an authentication.
  EXPECT_CALL(bridge(), Authenticate(_))
      .WillOnce(RunOnceCallback<0>(DeviceAuthUIResult::kFailed));
  base::MockCallback<DeviceAuthenticator::AuthenticateCallback> result_callback;
  EXPECT_CALL(result_callback, Run(/*auth_succeeded=*/false));
  authenticator()->AuthenticateWithMessage(u"", result_callback.Get());

  EXPECT_THAT(
      histogram_tester.GetAllSamples(
          "PasswordManager.BiometricAuthPwdFill.AuthResult"),
      ElementsAre(Bucket(static_cast<int>(
                             DeviceAuthFinalResult::kSuccessWithBiometrics),
                         1),
                  Bucket(static_cast<int>(DeviceAuthFinalResult::kFailed), 1)));
}

TEST_F(DeviceAuthenticatorAndroidTest, TriggersAuthIfPreviousFailed) {
  base::HistogramTester histogram_tester;
  // Simulate a previous failed authentication
  EXPECT_CALL(bridge(), Authenticate)
      .WillOnce(RunOnceCallback<0>(DeviceAuthUIResult::kFailed));
  authenticator()->AuthenticateWithMessage(u"", base::DoNothing());

  // The next call to `Authenticate()` should re-trigger an authentication.
  EXPECT_CALL(bridge(), Authenticate(_))
      .WillOnce(RunOnceCallback<0>(DeviceAuthUIResult::kSuccessWithBiometrics));
  base::MockCallback<DeviceAuthenticator::AuthenticateCallback> result_callback;
  EXPECT_CALL(result_callback, Run(/*auth_succeeded=*/true));
  authenticator()->AuthenticateWithMessage(u"", result_callback.Get());

  EXPECT_THAT(
      histogram_tester.GetAllSamples(
          "PasswordManager.BiometricAuthPwdFill.AuthResult"),
      ElementsAre(Bucket(static_cast<int>(
                             DeviceAuthFinalResult::kSuccessWithBiometrics),
                         1),
                  Bucket(static_cast<int>(DeviceAuthFinalResult::kFailed), 1)));
}

TEST_F(DeviceAuthenticatorAndroidTest, GetBiometricAvailabilityStatusRequired) {
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kRequired));
  EXPECT_EQ(device_reauth::BiometricStatus::kRequired,
            authenticator()->GetBiometricAvailabilityStatus());
}

TEST_F(DeviceAuthenticatorAndroidTest,
       GetBiometricAvailabilityStatusRequiredButHasErrors) {
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kRequiredButHasError));
  EXPECT_EQ(device_reauth::BiometricStatus::kRequired,
            authenticator()->GetBiometricAvailabilityStatus());
}

TEST_F(DeviceAuthenticatorAndroidTest,
       GetBiometricAvailabilityStatusAvailable) {
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kAvailable));
  EXPECT_EQ(device_reauth::BiometricStatus::kBiometricsAvailable,
            authenticator()->GetBiometricAvailabilityStatus());
}

TEST_F(DeviceAuthenticatorAndroidTest,
       GetBiometricAvailabilityStatusLSKFAvailable) {
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kNotEnrolled));
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometricOrScreenLock)
      .WillOnce(Return(true));
  EXPECT_EQ(device_reauth::BiometricStatus::kOnlyLskfAvailable,
            authenticator()->GetBiometricAvailabilityStatus());
}

TEST_F(DeviceAuthenticatorAndroidTest,
       GetBiometricAvailabilityStatusUnavailable) {
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometric)
      .WillOnce(Return(BiometricsAvailability::kNotEnrolled));
  EXPECT_CALL(bridge(), CanAuthenticateWithBiometricOrScreenLock)
      .WillOnce(Return(false));
  EXPECT_EQ(device_reauth::BiometricStatus::kUnavailable,
            authenticator()->GetBiometricAvailabilityStatus());
}