1 package civitas.crypto.rsapublickey.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertFalse;
4 import static org.junit.jupiter.api.Assertions.assertTrue;
5 import static org.mockito.Mockito.verify;
6
7 import org.bouncycastle.crypto.CryptoException;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import org.mockito.InjectMocks;
11 import org.mockito.Mock;
12
13 import civitas.crypto.algorithms.CreateFreshNonceBase64;
14 import civitas.crypto.rsaprivatekey.tests.PrivateKeyTestData;
15 import civitas.crypto.rsapublickey.IsPublicKeyAuthorized;
16 import civitas.crypto.rsapublickey.VerifyPublicKeySignature;
17 import civitas.crypto.signature.SignWithPublicKey;
18 import civitas.crypto.signature.tests.SignatureTestData;
19 import civitas.util.tests.BasicValuesTestData;
20 import io.github.magwas.konveyor.testing.TestBase;
21
22 class IsPublicKeyAuthorizedTest extends TestBase
23 implements PublicKeyTestData, BasicValuesTestData, SignatureTestData, PrivateKeyTestData {
24
25 @InjectMocks
26 IsPublicKeyAuthorized isPublicKeyAuthorized;
27
28 @Mock
29 CreateFreshNonceBase64 createFreshNonceBase64;
30
31 @Mock
32 SignWithPublicKey signWithPublicKey;
33
34 @Mock
35 VerifyPublicKeySignature verifyPublicKeySignature;
36
37 @Test
38 @DisplayName(
39 """
40 isAuthorized checks if the private key is belonging to the public key
41 - creates a new base64 nonce
42 - signs the nonce with the private key
43 - verifies that the signature is verifiable with the public key
44 """)
45 void test2() throws CryptoException, IllegalAccessException {
46 boolean actual = isPublicKeyAuthorized.apply(PUBLIC_KEY, PRIVATE_KEY);
47 verify(createFreshNonceBase64).apply(AUTHENTICATION_NONCE_LENGTH);
48 verify(signWithPublicKey).apply(PRIVATE_KEY, PUBLIC_KEY, AUTHENTICATION_NONCE);
49 verify(verifyPublicKeySignature).apply(SIGNATURE_OF_AUTH_NONCE_WITH_KEY, PUBLIC_KEY, AUTHENTICATION_NONCE);
50 assertTrue(actual);
51 }
52
53 @Test
54 @DisplayName("isAuthorized is false for other private key")
55 void test2_2() throws CryptoException {
56 assertFalse(isPublicKeyAuthorized.apply(PUBLIC_KEY, PRIVATE_KEY2));
57 }
58 }