blob: fa0441b106a75ed3e62aad623ef260bf1822cb33 [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <queue>
#include <string>
#include <base/callback.h>
#include <brillo/secure_blob.h>
#include <gmock/gmock.h>
#include "cryptohome/key.pb.h"
#include "cryptohome/key_challenge_service.h"
#include "cryptohome/rpc.pb.h"
namespace cryptohome {
class MockKeyChallengeService : public KeyChallengeService {
~MockKeyChallengeService() override;
(const AccountIdentifier&,
const KeyChallengeRequest&,
void ChallengeKey(const AccountIdentifier& account_id,
const KeyChallengeRequest& key_challenge_request,
ResponseCallback response_callback) override {
// Note: this method contains a move-only argument and thus cannot be mocked
// directly. Use ChallengeKeyMovable for all mocking needs.
ChallengeKeyMovable(account_id, key_challenge_request, &response_callback);
// Helper class for simplifying the use of MockKeyChallengeService.
// It encapsulates setting up a mock expectation and execution of the callback
// with which the mocked method was called. Intended usage: first call
// ExpectSignatureChallenge(), and then, after the mocked method gets executed,
// call one of the Simulate*() methods.
class KeyChallengeServiceMockController final {
explicit KeyChallengeServiceMockController(
MockKeyChallengeService* mock_service);
// Sets up a mock expectation on ChallengeKey(). This mock expectation doesn't
// run the passed ResponseCallback, but remembers it in
// |intercepted_response_callback_|, allowing the later call of a Simulate*()
// method.
void ExpectSignatureChallenge(
const std::string& expected_username,
const brillo::Blob& expected_public_key_spki_der,
const brillo::Blob& expected_challenge_value,
ChallengeSignatureAlgorithm expected_signature_algorithm);
// Whether the mocked ChallengeKey() has been called.
// It's allowed to call the Simulate*() methods only after this returns true.
bool is_challenge_requested() const {
return !intercepted_response_callbacks_.empty();
// Simulates a successful response for the given challenge request.
void SimulateSignatureChallengeResponse(const brillo::Blob& signature_value);
// Simulates a failed response for the given challenge request.
void SimulateFailureResponse();
// Not owned.
MockKeyChallengeService* const mock_service_;
// The accumulated response callbacks that was passed to the mocked
// ChallengeKey() method.
} // namespace cryptohome