diff --git a/pom.xml b/pom.xml index c026642..eb31a0d 100644 --- a/pom.xml +++ b/pom.xml @@ -50,17 +50,30 @@ 0.8.7 2.0.0 - - 4.13.2 + + 5.8.1 + 3.21.0 - junit - junit + org.junit.jupiter + junit-jupiter-api ${junit.version} test + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + diff --git a/src/test/java/io/carbynestack/mpspdz/integration/MpSpdzIntegrationUtilsTest.java b/src/test/java/io/carbynestack/mpspdz/integration/MpSpdzIntegrationUtilsTest.java index 814e98e..285722d 100644 --- a/src/test/java/io/carbynestack/mpspdz/integration/MpSpdzIntegrationUtilsTest.java +++ b/src/test/java/io/carbynestack/mpspdz/integration/MpSpdzIntegrationUtilsTest.java @@ -7,30 +7,26 @@ package io.carbynestack.mpspdz.integration; import static io.carbynestack.mpspdz.integration.TestTriple.loadFromResources; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.math.BigInteger; import java.util.List; import java.util.Random; -import org.hamcrest.CoreMatchers; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class MpSpdzIntegrationUtilsTest { - private static final Random RANDOM = new Random(42); private static final int REPETITIONS = 100000; private static final BigInteger PRIME = new BigInteger("198766463529478683931867765928436695041"); private static final BigInteger R = new BigInteger("141515903391459779531506841503331516415"); private static final BigInteger R_INV = new BigInteger("133854242216446749056083838363708373830"); - - private List gfpTestData; - private final MpSpdzIntegrationUtils mpSpdzIntegrationUtils = MpSpdzIntegrationUtils.of(PRIME, R, R_INV); + private List gfpTestData; - @Before + @BeforeEach public void loadGfpData() throws Exception { gfpTestData = loadFromResources("/GfpTestData", "/BigIntTestData", mpSpdzIntegrationUtils.getPrime()); @@ -38,58 +34,50 @@ public void loadGfpData() throws Exception { @Test public void givenConfiguredUtils_whenGettingParameters_thenReturnExpectedValues() { - assertEquals(PRIME, mpSpdzIntegrationUtils.getPrime()); - assertEquals(R, mpSpdzIntegrationUtils.getR()); - assertEquals(R_INV, mpSpdzIntegrationUtils.getRInv()); + assertThat(mpSpdzIntegrationUtils.getPrime()).isEqualTo(PRIME); + assertThat(mpSpdzIntegrationUtils.getR()).isEqualTo(R); + assertThat(mpSpdzIntegrationUtils.getRInv()).isEqualTo(R_INV); } @Test public void givenTestTriples_whenConvertingFromGfp_thenReturnCorrectOutput() { for (TestTriple testTriple : gfpTestData) { - assertEquals( - "Converted value does not match actual value.", - testTriple.getValue(), - mpSpdzIntegrationUtils.fromGfp(testTriple.getGfp())); + assertThat(mpSpdzIntegrationUtils.fromGfp(testTriple.getGfp())) + .as("Converted value does not match actual value.") + .isEqualTo(testTriple.getValue()); } } @Test public void givenArrayOfWrongLength_whenConvertingFromGfp_thenTrow() { - IllegalArgumentException iae = - assertThrows( - IllegalArgumentException.class, - () -> mpSpdzIntegrationUtils.fromGfp(new byte[] {4, 2})); - assertThat(iae.getMessage(), CoreMatchers.containsString("must have a length of")); + assertThatThrownBy(() -> mpSpdzIntegrationUtils.fromGfp(new byte[] {4, 2})) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("must have a length of"); } @Test public void givenTestTriples_whenConvertingToGfp_thenReturnCorrectOutput() { for (TestTriple testTriple : gfpTestData) { - assertArrayEquals( - "Converted byte data does not match actual spdz representation.", - testTriple.getGfp(), - mpSpdzIntegrationUtils.toGfp(testTriple.getValue())); + assertThat(mpSpdzIntegrationUtils.toGfp(testTriple.getValue())) + .as("Converted byte data does not match actual spdz representation.") + .isEqualTo(testTriple.getGfp()); } } @Test public void givenNegativeValue_whenConvertingToGfp_thenThrow() { - IllegalArgumentException iae = - assertThrows( - IllegalArgumentException.class, - () -> mpSpdzIntegrationUtils.toGfp(BigInteger.ONE.negate())); - assertThat(iae.getMessage(), CoreMatchers.containsString("must not be negative")); + assertThatThrownBy(() -> mpSpdzIntegrationUtils.toGfp(BigInteger.ONE.negate())) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("must not be negative"); } @Test public void givenValueGreaterThanPrime_whenConvertingToGfp_thenThrow() { - IllegalArgumentException iae = - assertThrows( - IllegalArgumentException.class, + assertThatThrownBy( () -> - mpSpdzIntegrationUtils.toGfp( - mpSpdzIntegrationUtils.getPrime().add(BigInteger.ONE))); - assertThat(iae.getMessage(), CoreMatchers.containsString("must not be larger")); + mpSpdzIntegrationUtils.toGfp(mpSpdzIntegrationUtils.getPrime().add(BigInteger.ONE))) + .isExactlyInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("must not be larger"); } @Test @@ -97,8 +85,9 @@ public void givenRandomInput_whenPerformingConversionRoundtrip_thenReturnInput() for (int i = 0; i < REPETITIONS; i++) { long v = Math.abs(RANDOM.nextLong()); byte[] gfp = mpSpdzIntegrationUtils.toGfp(BigInteger.valueOf(v)); - assertEquals( - "Roundtrip does not preserve value.", v, mpSpdzIntegrationUtils.fromGfp(gfp).longValue()); + assertThat(mpSpdzIntegrationUtils.fromGfp(gfp).longValue()) + .as("Roundtrip does not preserve value.") + .isEqualTo(v); } } } diff --git a/src/test/java/io/carbynestack/mpspdz/integration/TestTriple.java b/src/test/java/io/carbynestack/mpspdz/integration/TestTriple.java index 4709b34..4510601 100644 --- a/src/test/java/io/carbynestack/mpspdz/integration/TestTriple.java +++ b/src/test/java/io/carbynestack/mpspdz/integration/TestTriple.java @@ -6,6 +6,8 @@ */ package io.carbynestack.mpspdz.integration; +import static org.assertj.core.api.Assertions.assertThat; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -16,10 +18,8 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; -import org.junit.Assert; public class TestTriple { - private final byte[] gfp; private final BigInteger value; private final BigInteger prime; @@ -30,14 +30,6 @@ public TestTriple(byte[] gfp, BigInteger value, BigInteger prime) { this.prime = prime; } - public byte[] getGfp() { - return gfp; - } - - public BigInteger getValue() { - return value.mod(prime); - } - private static byte[] readGfp(InputStream is, int length) throws IOException { byte[] arr = new byte[length]; if (is.read(arr, 0, length) == -1) { @@ -61,9 +53,12 @@ public static List loadFromResources( try (InputStream gfpTripleStream = TestTriple.class.getResourceAsStream(gfpTriplesName); InputStream humanTripleStream = TestTriple.class.getResourceAsStream(humanReadableTriplesName)) { - Assert.assertNotNull("Resource containing GFp triples can not be opened", gfpTripleStream); - Assert.assertNotNull( - "Resource containing human readable triples can not be opened", humanTripleStream); + assertThat(gfpTripleStream) + .as("Resource containing GFp triples can not be opened") + .isNotNull(); + assertThat(humanTripleStream) + .as("Resource containing human readable triples can not be opened") + .isNotNull(); byte[] gfp; while ((gfp = readGfp(gfpTripleStream, MpSpdzIntegrationUtils.WORD_WIDTH)) != null) { gfps.add(gfp); @@ -74,14 +69,21 @@ public static List loadFromResources( while ((line = br.readLine()) != null) { humanReadableValues.add(new BigInteger(line.trim())); } - Assert.assertEquals( - "Number of read GFp values does not match number of human readable values", - gfps.size(), - humanReadableValues.size()); + assertThat(humanReadableValues.size()) + .as("Number of read GFp values does not match number of human readable values") + .isEqualTo(gfps.size()); } return IntStream.range(0, gfps.size()) .boxed() .map(i -> new TestTriple(gfps.get(i), humanReadableValues.get(i), prime)) .collect(Collectors.toList()); } + + public byte[] getGfp() { + return gfp; + } + + public BigInteger getValue() { + return value.mod(prime); + } }