1 package civitas.crypto.signature.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import static org.mockito.Mockito.verify;
6
7 import java.security.InvalidKeyException;
8 import java.security.SignatureException;
9
10 import org.bouncycastle.crypto.CryptoException;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Test;
13 import org.mockito.InjectMocks;
14 import org.mockito.Mock;
15
16 import civitas.common.tests.RandomAwareTestBase;
17 import civitas.crypto.CryptoBase;
18 import civitas.crypto.messagedigest.CryptoHash;
19 import civitas.crypto.rsapublickey.ConvertPublicKeyToString;
20 import civitas.crypto.rsapublickey.tests.PublicKeyTestData;
21 import civitas.crypto.signature.SignWithPublicKey;
22 import civitas.util.tests.BasicValuesTestData;
23
24 class SignWithPublicKeyTest extends RandomAwareTestBase
25 implements PublicKeyTestData, SignatureTestData, BasicValuesTestData {
26
27 @InjectMocks
28 SignWithPublicKey signWithPublicKey;
29
30 @Mock
31 ConvertPublicKeyToString convertPublicKeyToString;
32
33 @Mock
34 CryptoBase cryptoBase;
35
36 @Mock
37 CryptoHash cryptoHash;
38
39 @Test
40 @DisplayName(
41 """
42 signs with public key
43 - initializes the rsa signer with the private key
44 - updates the rsa signer with the byte array to sign
45 - obtains the signature using the rsa signer
46 - converts the public key to string
47 - returns the signature containing the signature and the string version of the public key
48 """)
49 void test() throws CryptoException, InvalidKeyException, SignatureException {
50 assertEquals(
51 SIGNATURE_OF_AUTH_NONCE_WITH_KEY,
52 signWithPublicKey.apply(PRIVATE_KEY, PUBLIC_KEY, AUTHENTICATION_NONCE.getBytes()));
53 verify(convertPublicKeyToString).apply(PUBLIC_KEY);
54 verify(cryptoBase.rsaSigner).initSign(PRIVATE_KEY);
55 verify(cryptoBase.rsaSigner).update(AUTHENTICATION_NONCE.getBytes());
56 }
57
58 @Test
59 @DisplayName("when the private key is bad, a CryptoException is thrown\n")
60 void test2() {
61 assertThrows(
62 CryptoException.class,
63 () -> signWithPublicKey.apply(PRIVATE_KEY_BAD, PUBLIC_KEY, AUTHENTICATION_NONCE.getBytes()));
64 }
65
66 @Test
67 @DisplayName(
68 """
69 when signing a string, sings the hash of the string
70 - computes the hash of the string
71 - initializes the rsa signer with the private key
72 - updates the rsa signer with the hash
73 - obtains the signature using the rsa signer
74 - converts the public key to string
75 - returns the signature containing the signature and the string version of the public key
76 """)
77 void test1() throws CryptoException, InvalidKeyException, SignatureException {
78 assertEquals(SIGNATURE_OF_AUTH_NONCE_WITH_KEY, signWithPublicKey.apply(PRIVATE_KEY, PUBLIC_KEY, SOMESTRING));
79 verify(cryptoHash).apply(SOMESTRING.getBytes());
80 verify(convertPublicKeyToString).apply(PUBLIC_KEY);
81 verify(cryptoBase.rsaSigner).initSign(PRIVATE_KEY);
82 verify(cryptoBase.rsaSigner).update(SOMESTRING_HASH);
83 }
84 }