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