View Javadoc
1   package civitas.bboard.common.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.bboard.common.VerifyBBPost;
14  import civitas.common.electoralroll.tests.ElectoralRollCapabilitySharesTestData;
15  import civitas.common.tests.RandomAwareTestBase;
16  import civitas.crypto.messagedigest.CryptoHash;
17  import civitas.crypto.rsapublickey.VerifyPublicKeySignature;
18  import civitas.crypto.rsapublickey.tests.PublicKeyTestData;
19  
20  class VerifyBBPostTest extends RandomAwareTestBase
21  		implements BBPostTestData, ElectoralRollCapabilitySharesTestData, PublicKeyTestData {
22  
23  	@InjectMocks
24  	VerifyBBPost verifyBBPost;
25  
26  	@Mock
27  	CryptoHash cryptoHash;
28  
29  	@Mock
30  	VerifyPublicKeySignature verifyPublicKeySignature;
31  
32  	@Test
33  	@DisplayName("verifies if the signature matches the xml form of the message. returns true if it does"
34  			+ "- converts the message to xml" + "- computes the hash of the xml"
35  			+ "- verifies that the signature is the signature of the hash using the key")
36  	void test() throws CryptoException {
37  		assertTrue(verifyBBPost.apply(BBPOST));
38  		verify(cryptoHash).apply(ELECTORAL_ROLL_CAPABILITY_SHARES_XML.getBytes());
39  		verify(verifyPublicKeySignature).apply(BBPOST.sig, ELECTORAL_ROLL_CAPABILITY_SHARES_XML_HASH);
40  	}
41  
42  	@Test
43  	@DisplayName("if the signature does not verify, returns false")
44  	void test1() throws CryptoException {
45  		assertFalse(verifyBBPost.apply(BBPOST_BAD_SIG));
46  	}
47  }