Skip to content

Commit

Permalink
Upgrade to JUnit 5 and switch from Hamcrest to AssertJ (#10)
Browse files Browse the repository at this point in the history
* Upgrade to JUnit 5 and switch from Hamcrest to AssertJ

Signed-off-by: David Greven <[email protected]>
Signed-off-by: Sebastian Becker <[email protected]>
  • Loading branch information
grevend-bosch authored Nov 4, 2021
1 parent 397e80d commit 409a7a6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
21 changes: 17 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,30 @@
<maven-jacoco-plugin.version>0.8.7</maven-jacoco-plugin.version>
<maven-license-plugin.version>2.0.0</maven-license-plugin.version>

<!-- External dependency versions -->
<junit.version>4.13.2</junit.version>
<!-- Test dependency versions -->
<junit.version>5.8.1</junit.version>
<assertj.version>3.21.0</assertj.version>
</properties>
<dependencies>
<!--Test dependencies-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,87 @@
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<TestTriple> gfpTestData;

private final MpSpdzIntegrationUtils mpSpdzIntegrationUtils =
MpSpdzIntegrationUtils.of(PRIME, R, R_INV);
private List<TestTriple> gfpTestData;

@Before
@BeforeEach
public void loadGfpData() throws Exception {
gfpTestData =
loadFromResources("/GfpTestData", "/BigIntTestData", mpSpdzIntegrationUtils.getPrime());
}

@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
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);
}
}
}
36 changes: 19 additions & 17 deletions src/test/java/io/carbynestack/mpspdz/integration/TestTriple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -61,9 +53,12 @@ public static List<TestTriple> 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);
Expand All @@ -74,14 +69,21 @@ public static List<TestTriple> 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);
}
}

0 comments on commit 409a7a6

Please sign in to comment.