1 package civitas.crypto.parameters.tests;
2
3 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5 import static org.junit.jupiter.api.Assertions.assertThrows;
6
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.mockito.InjectMocks;
10
11 import civitas.crypto.CryptoError;
12 import civitas.crypto.parameters.CheckGroup;
13 import civitas.crypto.parameters.ElGamalParameters;
14 import civitas.util.CivitasBigInteger;
15 import civitas.util.CivitasBigIntegerFactory;
16 import io.github.magwas.konveyor.testing.TestBase;
17
18 class CheckGroupTest extends TestBase implements ElGamalParametersTestData {
19
20 @InjectMocks
21 CheckGroup checkGroup;
22
23 @Test
24 @DisplayName("checkGroup does nothing if the parameters are a group")
25 void test() {
26 assertDoesNotThrow(() -> checkGroup.apply(EL_GAMAL_PARAMETERS));
27 }
28
29 @Test
30 @DisplayName("checkGroup throws CryptoError if parameters are not a group")
31 void test_1() {
32 assertThrows(CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(BIGINT_A, BIGINT_B, BIGINT_C)));
33 }
34
35 @Test
36 @DisplayName("checkGroup throws CryptoException if q does not divide p-1")
37 void checkGroupTest() {
38 CryptoError t = assertThrows(
39 CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(BIGINT_P, BIGINT_P, GENERATOR_OTHER)));
40 assertEquals("q does not divide p-1", t.getMessage());
41 }
42
43 @Test
44 @DisplayName("checkGroup throws CryptoException if p is not prime")
45 void checkGroupTest1() {
46 CivitasBigInteger p;
47 p = BIGINT_Q.multiply(CivitasBigIntegerFactory.obtain(2)).add(ONE);
48 CryptoError t = assertThrows(
49 CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(p, BIGINT_Q, GENERATOR_FOR_UNPRIME_P)));
50 assertEquals("p is not prime", t.getMessage());
51 }
52
53 @Test
54 @DisplayName("checkGroup throws CryptoException if q is not prime")
55 void checkGroupTest5() {
56 CryptoError t = assertThrows(
57 CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(BIGINT_P, BIGINT_A, BIGINT_G)));
58 assertEquals("q is not prime", t.getMessage());
59 }
60
61 @Test
62 @DisplayName("checkGroup throws CryptoException if q is not generator")
63 void checkGroupTest2() {
64 CryptoError t = assertThrows(
65 CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(BIGINT_P, BIGINT_Q, BIGINT_A)));
66 assertEquals("g is not order q", t.getMessage());
67 }
68
69 @Test
70 @DisplayName("checkGroup throws CryptoException if q is not prime")
71 void checkGroupTest3() {
72 CivitasBigInteger q = BIGINT_A;
73 CivitasBigInteger p = q.multiply(TWO).add(ONE);
74
75 CryptoError t = assertThrows(
76 CryptoError.class, () -> checkGroup.apply(new ElGamalParameters(p, q, GENERATOR_FOR_UNPRIME_Q)));
77 assertEquals("p is not prime", t.getMessage());
78 }
79 }