File: machine_certificate_uploader_impl_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 (411 lines) | stat: -rw-r--r-- 15,269 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
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
// Copyright 2019 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/ash/attestation/machine_certificate_uploader_impl.h"

#include <stdint.h>

#include <optional>
#include <string>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/ash/attestation/attestation_key_payload.pb.h"
#include "chrome/browser/ash/settings/scoped_cros_settings_test_helper.h"
#include "chromeos/ash/components/attestation/fake_certificate.h"
#include "chromeos/ash/components/attestation/mock_attestation_flow.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/attestation/fake_attestation_client.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Invoke;
using testing::StrictMock;
using testing::WithArgs;

namespace ash {
namespace attestation {

namespace {

constexpr int64_t kCertValid = 90;
constexpr int64_t kCertExpiringSoon = 20;
constexpr int64_t kCertExpired = -20;
constexpr int kRetryLimit = 3;
constexpr char kFakeCertificate[] = "fake_cert";

void CertCallbackUnspecifiedFailure(
    AttestationFlow::CertificateCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), ATTESTATION_UNSPECIFIED_FAILURE, ""));
}

void CertCallbackBadRequestFailure(
    AttestationFlow::CertificateCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback),
                                ATTESTATION_SERVER_BAD_REQUEST_FAILURE, ""));
}

void CertCallbackNotAvailableFailure(
    AttestationFlow::CertificateCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), ATTESTATION_NOT_AVAILABLE, ""));
}

void ResultCallbackSuccess(policy::CloudPolicyClient::ResultCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), policy::CloudPolicyClient::Result(
                                              policy::DM_STATUS_SUCCESS)));
}

void ResultCallbackNotRegistered(
    policy::CloudPolicyClient::ResultCallback callback) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback),
                     policy::CloudPolicyClient::Result(
                         policy::CloudPolicyClient::NotRegistered())));
}

class CallbackObserver {
 public:
  MOCK_METHOD(void, Callback, (bool state));

  auto GetCallback() {
    return base::BindOnce(&CallbackObserver::Callback, base::Unretained(this));
  }
};

class MockableFakeAttestationFlow : public MockAttestationFlow {
 public:
  MockableFakeAttestationFlow() {
    ON_CALL(*this, GetCertificate(_, _, _, _, _, _, _, _))
        .WillByDefault(
            Invoke(this, &MockableFakeAttestationFlow::GetCertificateInternal));
  }
  ~MockableFakeAttestationFlow() override = default;
  void set_status(AttestationStatus status) { status_ = status; }

 private:
  void GetCertificateInternal(
      AttestationCertificateProfile certificate_profile,
      const AccountId& account_id,
      const std::string& request_origin,
      bool force_new_key,
      ::attestation::KeyType key_crypto_type,
      const std::string& key_name,
      const std::optional<AttestationFlow::CertProfileSpecificData>&
          profile_specific_data,
      CertificateCallback callback) {
    std::string certificate;
    if (status_ == ATTESTATION_SUCCESS) {
      certificate = certificate_;
      AttestationClient::Get()
          ->GetTestInterface()
          ->GetMutableKeyInfoReply(cryptohome::Identification(account_id).id(),
                                   kEnterpriseMachineKey)
          ->set_certificate(certificate_);
    }
    std::move(callback).Run(status_, certificate);
  }
  AttestationStatus status_ = ATTESTATION_SUCCESS;
  const std::string certificate_ = kFakeCertificate;
};

}  // namespace

class MachineCertificateUploaderTestBase : public ::testing::Test {
 public:
  MachineCertificateUploaderTestBase() {
    AttestationClient::InitializeFake();
    settings_helper_.ReplaceDeviceSettingsProviderWithStub();
    policy_client_.SetDMToken("fake_dm_token");
  }
  ~MachineCertificateUploaderTestBase() override {
    AttestationClient::Shutdown();
  }

 protected:
  enum MockOptions {
    MOCK_KEY_EXISTS = 1,           // Configure so a certified key exists.
    MOCK_KEY_UPLOADED = (1 << 1),  // Configure so an upload has occurred.
    MOCK_NEW_KEY = (1 << 2),       // Configure expecting new key generation.
    MOCK_UNREGISTERED_CLIENT =
        (1 << 3)  // Configure to fake an unregistered cloud policy client.
  };

  // The derived fixture has different needs to control this function's
  // behavior.
  virtual bool GetShouldRefreshCert() const = 0;

  // Configures mock expectations according to |mock_options|.  If options
  // require that a certificate exists, |certificate| will be used.
  void SetupMocks(int mock_options, const std::string& certificate) {
    bool key_exists = (mock_options & MOCK_KEY_EXISTS);
    // Setup expected key / cert queries.
    if (key_exists) {
      ::attestation::GetKeyInfoReply* key_info =
          AttestationClient::Get()->GetTestInterface()->GetMutableKeyInfoReply(
              /*username=*/"", kEnterpriseMachineKey);
      key_info->set_certificate(certificate);
    }

    // Setup expected key payload queries.
    bool key_uploaded = (mock_options & MOCK_KEY_UPLOADED);
    if (key_uploaded) {
      ::attestation::GetKeyInfoReply* key_info =
          AttestationClient::Get()->GetTestInterface()->GetMutableKeyInfoReply(
              /*username=*/"", kEnterpriseMachineKey);
      key_info->set_payload(key_uploaded ? CreatePayload() : std::string());
    }

    // Setup expected key uploads.  Use WillOnce() so StrictMock will trigger an
    // error if our expectations are not met exactly.  We want to verify that
    // during a single run through the uploader only one upload operation occurs
    // (because it is costly) and similarly, that the writing of the uploaded
    // status in the key payload matches the upload operation.
    bool new_key = GetShouldRefreshCert() || (mock_options & MOCK_NEW_KEY);
    if (new_key || !key_uploaded) {
      if (mock_options & MOCK_UNREGISTERED_CLIENT) {
        EXPECT_CALL(policy_client_,
                    UploadEnterpriseMachineCertificate(
                        new_key ? kFakeCertificate : certificate, _))
            .WillOnce(WithArgs<1>(Invoke(ResultCallbackNotRegistered)));
      } else {
        EXPECT_CALL(policy_client_,
                    UploadEnterpriseMachineCertificate(
                        new_key ? kFakeCertificate : certificate, _))
            .WillOnce(WithArgs<1>(Invoke(ResultCallbackSuccess)));
      }
    }

    // Setup expected key generations.  Again use WillOnce().  Key generation is
    // another costly operation and if it gets triggered more than once during
    // a single pass this indicates a logical problem in the observer.
    if (new_key) {
      EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _, _, _, _));
    }
  }

  void RunUploader(
      base::OnceCallback<void(bool)> upload_callback = base::DoNothing()) {
    MachineCertificateUploaderImpl uploader(&policy_client_,
                                            &attestation_flow_);
    uploader.set_retry_limit_for_testing(kRetryLimit);
    uploader.set_retry_delay_for_testing(base::TimeDelta());
    if (GetShouldRefreshCert())
      uploader.RefreshAndUploadCertificate(base::DoNothing());
    else
      uploader.UploadCertificateIfNeeded(std::move(upload_callback));

    base::RunLoop().RunUntilIdle();
  }

  std::string CreatePayload() {
    AttestationKeyPayload proto;
    proto.set_is_certificate_uploaded(true);
    std::string serialized;
    proto.SerializeToString(&serialized);
    return serialized;
  }

  content::BrowserTaskEnvironment task_environment_;
  ScopedCrosSettingsTestHelper settings_helper_;
  StrictMock<MockableFakeAttestationFlow> attestation_flow_;
  StrictMock<policy::MockCloudPolicyClient> policy_client_;
};

class MachineCertificateUploaderTest
    : public MachineCertificateUploaderTestBase,
      public ::testing::WithParamInterface<bool> {
 public:
  bool GetShouldRefreshCert() const final { return GetParam(); }
};

TEST_P(MachineCertificateUploaderTest, UnregisteredPolicyClient) {
  policy_client_.SetDMToken("");
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, GetCertificateUnspecifiedFailure) {
  EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _, _, _, _))
      .WillRepeatedly(WithArgs<7>(Invoke(CertCallbackUnspecifiedFailure)));
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, GetCertificateBadRequestFailure) {
  EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _, _, _, _))
      .WillOnce(WithArgs<7>(Invoke(CertCallbackBadRequestFailure)));
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, GetCertificateNotAvailableFailure) {
  EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _, _, _, _))
      .WillOnce(WithArgs<7>(Invoke(CertCallbackNotAvailableFailure)));
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, NewCertificate) {
  SetupMocks(MOCK_NEW_KEY, "");
  RunUploader();
  EXPECT_EQ(CreatePayload(),
            AttestationClient::Get()
                ->GetTestInterface()
                ->GetMutableKeyInfoReply(/*username=*/"", kEnterpriseMachineKey)
                ->payload());
}

TEST_P(MachineCertificateUploaderTest, WaitForUploadComplete) {
  SetupMocks(MOCK_NEW_KEY, "");

  StrictMock<CallbackObserver> waiting_callback_observer;
  MachineCertificateUploaderImpl uploader(&policy_client_, &attestation_flow_);

  uploader.WaitForUploadComplete(waiting_callback_observer.GetCallback());
  EXPECT_CALL(waiting_callback_observer, Callback(true));

  StrictMock<CallbackObserver> direct_callback_observer;
  EXPECT_CALL(direct_callback_observer, Callback(true));

  if (GetShouldRefreshCert()) {
    uploader.RefreshAndUploadCertificate(
        direct_callback_observer.GetCallback());
  } else {
    uploader.UploadCertificateIfNeeded(direct_callback_observer.GetCallback());
  }

  base::RunLoop().RunUntilIdle();

  testing::Mock::VerifyAndClearExpectations(&waiting_callback_observer);
  EXPECT_CALL(waiting_callback_observer, Callback(true));
  uploader.WaitForUploadComplete(waiting_callback_observer.GetCallback());

  EXPECT_EQ(CreatePayload(),
            AttestationClient::Get()
                ->GetTestInterface()
                ->GetMutableKeyInfoReply(/*username=*/"", kEnterpriseMachineKey)
                ->payload());
}

TEST_P(MachineCertificateUploaderTest, WaitForUploadFail) {
  EXPECT_CALL(attestation_flow_, GetCertificate(_, _, _, _, _, _, _, _))
      .WillOnce(WithArgs<7>(Invoke(CertCallbackBadRequestFailure)));

  StrictMock<CallbackObserver> waiting_callback_observer;
  MachineCertificateUploaderImpl uploader(&policy_client_, &attestation_flow_);

  uploader.WaitForUploadComplete(waiting_callback_observer.GetCallback());
  EXPECT_CALL(waiting_callback_observer, Callback(false));

  StrictMock<CallbackObserver> direct_callback_observer;
  EXPECT_CALL(direct_callback_observer, Callback(false));

  if (GetShouldRefreshCert()) {
    uploader.RefreshAndUploadCertificate(
        direct_callback_observer.GetCallback());
  } else {
    uploader.UploadCertificateIfNeeded(direct_callback_observer.GetCallback());
  }

  base::RunLoop().RunUntilIdle();
  testing::Mock::VerifyAndClearExpectations(&waiting_callback_observer);

  // Consequent calls to `WaitForUploadComplete` will return false until
  // somebody else retries to upload the cert again.
  EXPECT_CALL(waiting_callback_observer, Callback(false));
  uploader.WaitForUploadComplete(waiting_callback_observer.GetCallback());
  base::RunLoop().RunUntilIdle();
}

TEST_P(MachineCertificateUploaderTest, KeyExistsNotUploaded) {
  std::string certificate;
  ASSERT_TRUE(GetFakeCertificatePEM(base::Days(kCertValid), &certificate));
  SetupMocks(MOCK_KEY_EXISTS, certificate);
  RunUploader();
  EXPECT_EQ(CreatePayload(),
            AttestationClient::Get()
                ->GetTestInterface()
                ->GetMutableKeyInfoReply(/*username=*/"", kEnterpriseMachineKey)
                ->payload());
}

TEST_P(MachineCertificateUploaderTest, KeyExistsAlreadyUploaded) {
  std::string certificate;
  ASSERT_TRUE(GetFakeCertificatePEM(base::Days(kCertValid), &certificate));
  SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED, certificate);
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, KeyExistsCertExpiringSoon) {
  std::string certificate;
  ASSERT_TRUE(
      GetFakeCertificatePEM(base::Days(kCertExpiringSoon), &certificate));
  SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED | MOCK_NEW_KEY, certificate);
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, KeyExistsCertExpired) {
  std::string certificate;
  ASSERT_TRUE(GetFakeCertificatePEM(base::Days(kCertExpired), &certificate));
  SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED | MOCK_NEW_KEY, certificate);
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest, IgnoreUnknownCertFormat) {
  SetupMocks(MOCK_KEY_EXISTS | MOCK_KEY_UPLOADED, "unsupported");
  RunUploader();
}

TEST_P(MachineCertificateUploaderTest,
       UnregisterPolicyClientDuringCallsReturnsUploadFailure) {
  // We might get unregistered during asynchronous calls. Fake that behaviour
  // here by letting the mock return unregistered.
  SetupMocks(MOCK_NEW_KEY | MOCK_UNREGISTERED_CLIENT, "");

  bool upload_success = false;
  RunUploader(base::BindOnce(
      [](bool* upload_success, bool success) { *upload_success = success; },
      (&upload_success)));

  EXPECT_FALSE(upload_success);
}

INSTANTIATE_TEST_SUITE_P(All,
                         MachineCertificateUploaderTest,
                         testing::Values(false, true));

class MachineCertificateUploaderTestNoRefresh
    : public MachineCertificateUploaderTestBase {
 public:
  bool GetShouldRefreshCert() const final { return false; }
};

TEST_F(MachineCertificateUploaderTestNoRefresh, DBusFailureRetry) {
  SetupMocks(MOCK_NEW_KEY, "");
  AttestationClient::Get()->GetTestInterface()->set_key_info_dbus_error_count(
      kRetryLimit - 1);
  RunUploader();
  EXPECT_EQ(CreatePayload(),
            AttestationClient::Get()
                ->GetTestInterface()
                ->GetMutableKeyInfoReply(/*username=*/"", kEnterpriseMachineKey)
                ->payload());
  EXPECT_EQ(
      AttestationClient::Get()->GetTestInterface()->key_info_dbus_error_count(),
      0);
}

}  // namespace attestation
}  // namespace ash