Skip to content

Commit 663c00f

Browse files
authored
Merge pull request #57 from selfmadecode/unit-tests
Tests: add rsa tests
2 parents 104ac6b + f5793a4 commit 663c00f

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/SafeCrypt.UnitTests/AesTests/EncryptionDecryption.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using SafeCrypt.Helpers;
22
using SafeCrypt.Models;
33
using SafeCrypt.AES;
4-
using SafeCrypt.RsaEncryption.Models;
54

65
namespace SafeCrypt.UnitTests.AesTests;
76

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using SafeCrypt.Helpers;
2+
using SafeCrypt.RsaEncryption;
3+
4+
namespace SafeCrypt.UnitTests.RsaTests;
5+
6+
public class RsaTests
7+
{
8+
private readonly string PublicKey;
9+
private readonly string PrivateKey;
10+
public RsaTests()
11+
{
12+
(PublicKey, PrivateKey) = KeyGenerators.GenerateRsaKeys(2048);
13+
}
14+
15+
[Fact]
16+
public async Task EncryptAsync_DecryptAsync_ValidParameters_ReturnsOriginalData()
17+
{
18+
// Arrange
19+
var model = new RsaEncryptionParameters
20+
{
21+
DataToEncrypt = "Hello, RSA!",
22+
PublicKey = PublicKey
23+
};
24+
25+
// Act
26+
var encrypt = await Rsa.EncryptAsync(model);
27+
28+
var decryptModel = new RsaDecryptionParameters
29+
{
30+
DataToDecrypt = encrypt.EncryptedData,
31+
PrivateKey = PrivateKey
32+
};
33+
34+
var decrypt = await Rsa.DecryptAsync(decryptModel);
35+
36+
// Assert
37+
Assert.Equal(model.DataToEncrypt, decrypt.DecryptedData);
38+
Assert.NotNull(encrypt.EncryptedData);
39+
Assert.Empty(encrypt.Errors);
40+
Assert.NotNull(decrypt.DecryptedData);
41+
Assert.Empty(decrypt.Errors);
42+
}
43+
44+
[Fact]
45+
public async Task EncryptAsync_InvalidData_ReturnsErrors()
46+
{
47+
// Arrange
48+
var model = new RsaEncryptionParameters
49+
{
50+
DataToEncrypt = "",
51+
PublicKey = PublicKey
52+
};
53+
54+
// Act
55+
var result = await Rsa.EncryptAsync(model);
56+
57+
// Assert
58+
Assert.Null(result.EncryptedData);
59+
Assert.NotEmpty(result.Errors);
60+
}
61+
}

0 commit comments

Comments
 (0)