Commit 0cf2e56e authored by Omar Morsi's avatar Omar Morsi Committed by Commit Bot

Extract platform keys service test util

This CL extracts platform keys service function execution waiters and
the scoped softoken key attributes mapping implementation into a
separate test util file so as it can be used by key permissions manager
later.

Bug: 1127284
Change-Id: Iff156918660e6eb2340a27c3f4e3ddd2ab6c6e9b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2456787Reviewed-by: default avatarPavol Marko <pmarko@chromium.org>
Commit-Queue: Omar Morsi <omorsi@google.com>
Cr-Commit-Position: refs/heads/master@{#815199}
parent a49d219d
// Copyright 2020 The Chromium 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 "chrome/browser/chromeos/platform_keys/platform_keys_service_test_util.h"
#include "chrome/browser/chromeos/platform_keys/platform_keys_service.h"
namespace chromeos {
namespace platform_keys {
namespace test_util {
ScopedSoftokenAttrsMapping::ScopedSoftokenAttrsMapping(
PlatformKeysService* platform_keys_service)
: platform_keys_service_(platform_keys_service) {
platform_keys_service_->SetMapToSoftokenAttrsForTesting(true);
}
ScopedSoftokenAttrsMapping::~ScopedSoftokenAttrsMapping() {
DCHECK(platform_keys_service_);
platform_keys_service_->SetMapToSoftokenAttrsForTesting(false);
}
GetTokensExecutionWaiter::GetTokensExecutionWaiter() = default;
GetTokensExecutionWaiter::~GetTokensExecutionWaiter() = default;
GenerateKeyExecutionWaiter::GenerateKeyExecutionWaiter() = default;
GenerateKeyExecutionWaiter::~GenerateKeyExecutionWaiter() = default;
SignExecutionWaiter::SignExecutionWaiter() = default;
SignExecutionWaiter::~SignExecutionWaiter() = default;
GetCertificatesExecutionWaiter::GetCertificatesExecutionWaiter() = default;
GetCertificatesExecutionWaiter::~GetCertificatesExecutionWaiter() = default;
SetAttributeForKeyExecutionWaiter::SetAttributeForKeyExecutionWaiter() =
default;
SetAttributeForKeyExecutionWaiter::~SetAttributeForKeyExecutionWaiter() =
default;
GetAttributeForKeyExecutionWaiter::GetAttributeForKeyExecutionWaiter() =
default;
GetAttributeForKeyExecutionWaiter::~GetAttributeForKeyExecutionWaiter() =
default;
RemoveKeyExecutionWaiter::RemoveKeyExecutionWaiter() = default;
RemoveKeyExecutionWaiter::~RemoveKeyExecutionWaiter() = default;
GetAllKeysExecutionWaiter::GetAllKeysExecutionWaiter() = default;
GetAllKeysExecutionWaiter::~GetAllKeysExecutionWaiter() = default;
IsKeyOnTokenExecutionWaiter::IsKeyOnTokenExecutionWaiter() = default;
IsKeyOnTokenExecutionWaiter::~IsKeyOnTokenExecutionWaiter() = default;
} // namespace test_util
} // namespace platform_keys
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_TEST_UTIL_H_
#define CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_TEST_UTIL_H_
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/platform_keys/platform_keys.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace platform_keys {
class PlatformKeysService;
namespace test_util {
// Softoken NSS PKCS11 module (used for testing) allows only predefined key
// attributes to be set and retrieved. Chaps supports setting and retrieving
// custom attributes.
// This helper is created to allow setting and retrieving attributes
// supported by PlatformKeysService. For the lifetime of an instance of this
// helper, PlatformKeysService will be configured to use softoken key attribute
// mapping for the lifetime of the helper.
class ScopedSoftokenAttrsMapping {
public:
explicit ScopedSoftokenAttrsMapping(
PlatformKeysService* platform_keys_service);
ScopedSoftokenAttrsMapping(const ScopedSoftokenAttrsMapping& other) = delete;
ScopedSoftokenAttrsMapping& operator=(
const ScopedSoftokenAttrsMapping& other) = delete;
~ScopedSoftokenAttrsMapping();
private:
PlatformKeysService* const platform_keys_service_;
};
// A helper that waits until execution of an asynchronous PlatformKeysService
// operation has finished and provides access to the results.
// Note: all PlatformKeysService operations have a trailing status argument that
// is filled in case of an error.
template <typename... ResultCallbackArgs>
class ExecutionWaiter {
public:
ExecutionWaiter() = default;
~ExecutionWaiter() = default;
ExecutionWaiter(const ExecutionWaiter& other) = delete;
ExecutionWaiter& operator=(const ExecutionWaiter& other) = delete;
// Returns the callback to be passed to the PlatformKeysService operation
// invocation.
base::RepeatingCallback<void(ResultCallbackArgs... result_callback_args,
Status status)>
GetCallback() {
return base::BindRepeating(&ExecutionWaiter::OnExecutionDone,
weak_ptr_factory_.GetWeakPtr());
}
// Waits until the callback returned by GetCallback() has been called.
void Wait() { run_loop_.Run(); }
// Returns the status passed to the callback.
Status status() const {
EXPECT_TRUE(done_);
return status_;
}
protected:
// A std::tuple that is capable of storing the arguments passed to the result
// callback.
using ResultCallbackArgsTuple =
std::tuple<std::decay_t<ResultCallbackArgs>...>;
// Access to the arguments passed to the callback.
const ResultCallbackArgsTuple& result_callback_args() const {
EXPECT_TRUE(done_);
return result_callback_args_;
}
private:
void OnExecutionDone(ResultCallbackArgs... result_callback_args,
Status status) {
EXPECT_FALSE(done_);
done_ = true;
result_callback_args_ = ResultCallbackArgsTuple(
std::forward<ResultCallbackArgs>(result_callback_args)...);
status_ = status;
run_loop_.Quit();
}
base::RunLoop run_loop_;
bool done_ = false;
ResultCallbackArgsTuple result_callback_args_;
Status status_ = Status::kSuccess;
base::WeakPtrFactory<ExecutionWaiter> weak_ptr_factory_{this};
};
// Supports waiting for the result of PlatformKeysService::GetTokens.
class GetTokensExecutionWaiter
: public ExecutionWaiter<std::unique_ptr<std::vector<TokenId>>> {
public:
GetTokensExecutionWaiter();
~GetTokensExecutionWaiter();
const std::unique_ptr<std::vector<TokenId>>& token_ids() const {
return std::get<0>(result_callback_args());
}
};
// Supports waiting for the result of the PlatformKeysService::GenerateKey*
// function family.
class GenerateKeyExecutionWaiter : public ExecutionWaiter<const std::string&> {
public:
GenerateKeyExecutionWaiter();
~GenerateKeyExecutionWaiter();
const std::string& public_key_spki_der() const {
return std::get<0>(result_callback_args());
}
};
// Supports waiting for the result of the PlatformKeysService::Sign* function
// family.
class SignExecutionWaiter : public ExecutionWaiter<const std::string&> {
public:
SignExecutionWaiter();
~SignExecutionWaiter();
const std::string& signature() const {
return std::get<0>(result_callback_args());
}
};
// Supports waiting for the result of the PlatformKeysService::GetCertificates.
class GetCertificatesExecutionWaiter
: public ExecutionWaiter<std::unique_ptr<net::CertificateList>> {
public:
GetCertificatesExecutionWaiter();
~GetCertificatesExecutionWaiter();
const net::CertificateList& matches() const {
return *std::get<0>(result_callback_args());
}
};
// Supports waiting for the result of the
// PlatformKeysService::SetAttributeForKey.
class SetAttributeForKeyExecutionWaiter : public ExecutionWaiter<> {
public:
SetAttributeForKeyExecutionWaiter();
~SetAttributeForKeyExecutionWaiter();
};
// Supports waiting for the result of the
// PlatformKeysService::GetAttributeForKey.
class GetAttributeForKeyExecutionWaiter
: public ExecutionWaiter<const base::Optional<std::string>&> {
public:
GetAttributeForKeyExecutionWaiter();
~GetAttributeForKeyExecutionWaiter();
const base::Optional<std::string>& attribute_value() const {
return std::get<0>(result_callback_args());
}
};
// Supports waiting for the result of the PlatformKeysService::RemoveKey.
class RemoveKeyExecutionWaiter : public ExecutionWaiter<> {
public:
RemoveKeyExecutionWaiter();
~RemoveKeyExecutionWaiter();
};
class GetAllKeysExecutionWaiter
: public ExecutionWaiter<std::vector<std::string>> {
public:
GetAllKeysExecutionWaiter();
~GetAllKeysExecutionWaiter();
const std::vector<std::string>& public_keys() const {
return std::get<0>(result_callback_args());
}
};
class IsKeyOnTokenExecutionWaiter
: public ExecutionWaiter<base::Optional<bool>> {
public:
IsKeyOnTokenExecutionWaiter();
~IsKeyOnTokenExecutionWaiter();
base::Optional<bool> on_slot() const {
return std::get<0>(result_callback_args());
}
};
} // namespace test_util
} // namespace platform_keys
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_TEST_UTIL_H_
...@@ -2581,6 +2581,8 @@ if (!is_android) { ...@@ -2581,6 +2581,8 @@ if (!is_android) {
"../browser/chromeos/net/network_portal_detector_impl_browsertest.cc", "../browser/chromeos/net/network_portal_detector_impl_browsertest.cc",
"../browser/chromeos/network_change_manager_client_browsertest.cc", "../browser/chromeos/network_change_manager_client_browsertest.cc",
"../browser/chromeos/platform_keys/platform_keys_service_browsertest.cc", "../browser/chromeos/platform_keys/platform_keys_service_browsertest.cc",
"../browser/chromeos/platform_keys/platform_keys_service_test_util.cc",
"../browser/chromeos/platform_keys/platform_keys_service_test_util.h",
"../browser/chromeos/plugin_vm/plugin_vm_test_helper.cc", "../browser/chromeos/plugin_vm/plugin_vm_test_helper.cc",
"../browser/chromeos/policy/affiliation_test_helper.cc", "../browser/chromeos/policy/affiliation_test_helper.cc",
"../browser/chromeos/policy/affiliation_test_helper.h", "../browser/chromeos/policy/affiliation_test_helper.h",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment