2026-02-08 11:57:57 +01:00
|
|
|
using IdentityShroud.Core.Contracts;
|
|
|
|
|
using IdentityShroud.Core.Model;
|
|
|
|
|
|
|
|
|
|
namespace IdentityShroud.Core.Tests.Model;
|
|
|
|
|
|
2026-02-14 14:50:06 +01:00
|
|
|
public class KeyTests
|
2026-02-08 11:57:57 +01:00
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SetNewKey()
|
|
|
|
|
{
|
|
|
|
|
byte[] privateKey = [5, 6, 7, 8];
|
|
|
|
|
byte[] encryptedPrivateKey = [1, 2, 3, 4];
|
|
|
|
|
|
|
|
|
|
var encryptionService = Substitute.For<IEncryptionService>();
|
|
|
|
|
encryptionService
|
|
|
|
|
.Encrypt(Arg.Any<byte[]>())
|
|
|
|
|
.Returns(x => encryptedPrivateKey);
|
|
|
|
|
|
2026-02-14 14:50:06 +01:00
|
|
|
Key key = new();
|
|
|
|
|
key.SetPrivateKey(encryptionService, privateKey);
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
// should be able to return original without calling decrypt
|
2026-02-14 14:50:06 +01:00
|
|
|
Assert.Equal(privateKey, key.GetPrivateKey(encryptionService));
|
|
|
|
|
Assert.Equal(encryptedPrivateKey, key.PrivateKeyEncrypted);
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
encryptionService.Received(1).Encrypt(privateKey);
|
|
|
|
|
encryptionService.DidNotReceive().Decrypt(Arg.Any<byte[]>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetDecryptedKey()
|
|
|
|
|
{
|
|
|
|
|
byte[] privateKey = [5, 6, 7, 8];
|
|
|
|
|
byte[] encryptedPrivateKey = [1, 2, 3, 4];
|
|
|
|
|
|
|
|
|
|
var encryptionService = Substitute.For<IEncryptionService>();
|
|
|
|
|
encryptionService
|
|
|
|
|
.Decrypt(encryptedPrivateKey)
|
|
|
|
|
.Returns(x => privateKey);
|
|
|
|
|
|
2026-02-14 14:50:06 +01:00
|
|
|
Key key = new();
|
|
|
|
|
key.PrivateKeyEncrypted = encryptedPrivateKey;
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
// should be able to return original without calling decrypt
|
2026-02-14 14:50:06 +01:00
|
|
|
Assert.Equal(privateKey, key.GetPrivateKey(encryptionService));
|
|
|
|
|
Assert.Equal(encryptedPrivateKey, key.PrivateKeyEncrypted);
|
2026-02-08 11:57:57 +01:00
|
|
|
|
|
|
|
|
encryptionService.Received(1).Decrypt(encryptedPrivateKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|