namespace DodoSSH.Client.Session.Tests;
/// A device key store that keeps its key in a field.
///
///
/// Stands in for whatever guards the key on a real machine. The gesture is the entire security value of the
/// real thing, so what this fake models is the two ways the gesture ends: it hands the key over, or it does
/// not. is a cancelled prompt and an invalidated key at once, which is exactly how much
/// the caller is allowed to know.
///
internal sealed class FakeDeviceKeyStore : IDeviceKeyStore
{
private byte[]? key;
/// When set, the next load refuses, as a cancelled gesture does.
internal bool Decline { get; set; }
/// Whether this machine can keep a key at all.
internal bool IsAvailable { get; set; } = true;
/// Reads the stored key without a gesture, for assertions only.
internal byte[]? Peek() => key;
/// Replaces the stored key, standing in for a rotated or corrupted keystore entry.
internal void Overwrite(byte[] replacement) => key = replacement;
///
public ValueTask IsAvailableAsync(CancellationToken cancellationToken) =>
ValueTask.FromResult(IsAvailable);
///
public ValueTask SaveAsync(ReadOnlyMemory devicePrivateKey, CancellationToken cancellationToken)
{
key = devicePrivateKey.ToArray();
return ValueTask.CompletedTask;
}
///
public ValueTask TryLoadAsync(CancellationToken cancellationToken) =>
ValueTask.FromResult(Decline ? null : key?.ToArray());
///
public ValueTask ForgetAsync(CancellationToken cancellationToken)
{
key = null;
return ValueTask.CompletedTask;
}
}