Skip to content

Commit

Permalink
port to JUnit5
Browse files Browse the repository at this point in the history
stefano81 committed Feb 29, 2024
1 parent 3b534f8 commit 1a1b0d0
Showing 7 changed files with 87 additions and 81 deletions.
16 changes: 10 additions & 6 deletions src/test/java/ProgrammaticAccessTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.*;
import org.junit.jupiter.api.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.AuthorityKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.Ciphertext;
import sg.edu.ntu.sce.sands.crypto.dcpabe.DCPABE;
import sg.edu.ntu.sce.sands.crypto.dcpabe.GlobalParameters;
import sg.edu.ntu.sce.sands.crypto.dcpabe.Message;
import sg.edu.ntu.sce.sands.crypto.dcpabe.PersonalKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.PublicKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.ac.AccessStructure;
import sg.edu.ntu.sce.sands.crypto.utility.Utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class ProgrammaticAccessTest {
@Test
public void testKeyCorrectlyDecrypted() throws IOException {
public void testKeyCorrectlyDecrypted() {
GlobalParameters GP = DCPABE.globalSetup(160);
AccessStructure accessStructure = AccessStructure.buildFromPolicy("A");

17 changes: 10 additions & 7 deletions src/test/java/Testing.java
Original file line number Diff line number Diff line change
@@ -2,20 +2,23 @@
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import it.unisa.dia.gas.plaf.jpbc.pairing.a.TypeACurveGenerator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import sg.edu.ntu.sce.sands.crypto.dcpabe.*;
import org.junit.jupiter.api.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.AuthorityKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.Ciphertext;
import sg.edu.ntu.sce.sands.crypto.dcpabe.DCPABE;
import sg.edu.ntu.sce.sands.crypto.dcpabe.GlobalParameters;
import sg.edu.ntu.sce.sands.crypto.dcpabe.Message;
import sg.edu.ntu.sce.sands.crypto.dcpabe.PersonalKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.PublicKeys;
import sg.edu.ntu.sce.sands.crypto.dcpabe.ac.AccessStructure;
import sg.edu.ntu.sce.sands.crypto.dcpabe.key.PersonalKey;

import java.security.SecureRandom;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;


@RunWith(JUnit4.class)
public class Testing {
@Test
public void testDCPABE2() {
63 changes: 32 additions & 31 deletions src/test/java/sg/edu/ntu/sce/sands/crypto/DCPABEToolTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package sg.edu.ntu.sce.sands.crypto;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import picocli.CommandLine;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DCPABEToolTest {
private static CommandLine cmd;
@@ -38,33 +38,37 @@ public class DCPABEToolTest {

private static final String policy = "and a or d and b c";

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@BeforeClass
@BeforeAll
public static void beforeAll() throws Exception {
gpFile = Files.createTempFile("dcpabe", "gp").toFile();
cmd = new CommandLine(new DCPABETool());
cmd.execute("gsetup", "-f", gpFile.getPath());
resFile = new File(DCPABEToolTest.class.getResource("/testResource.txt").toURI());
}

@Before
@BeforeEach
public void setUp() throws Exception {
fakeOutput = folder.newFile();
fakeOutput = Files.createTempFile("fake", "output").toFile();
fakeOutput.deleteOnExit();
fakeCmdOutput = new PrintWriter(fakeOutput);
cmd.setErr(fakeCmdOutput);
cmd.setOut(fakeCmdOutput);

apFileS = folder.newFile();
apFileP = folder.newFile();
encFile = folder.newFile();
resFile2 = folder.newFile();
key1AFile = folder.newFile();
key1DFile = folder.newFile();
apFileS = Files.createTempFile("fake", "apFileS").toFile();
apFileS.deleteOnExit();
apFileP = Files.createTempFile("fake", "apFileP").toFile();
apFileP.deleteOnExit();
encFile = Files.createTempFile("fake", "encFile").toFile();
encFile.deleteOnExit();
resFile2 = Files.createTempFile("fake", "resFile2").toFile();
resFile2.deleteOnExit();
key1AFile = Files.createTempFile("fake", "key1AFile").toFile();
key1AFile.deleteOnExit();
key1DFile = Files.createTempFile("fake", "key1DFile").toFile();
key1DFile.deleteOnExit();
}

@After
@AfterEach
public void tearDown() {
fakeCmdOutput.close();
}
@@ -215,8 +219,7 @@ public void testGSetupFailsWhenMissingArgs() {
public void testKeyGenFailsWhenMissingArgs() {
assertTrue(key1AFile.delete());

List<String> args = new ArrayList<>();
args.addAll(Arrays.asList("keygen", gpFile.getPath(), "user1", "a", apFileS.getPath(), key1AFile.getPath()));
List<String> args = new ArrayList<>(Arrays.asList("keygen", gpFile.getPath(), "user1", "a", apFileS.getPath(), key1AFile.getPath()));
for (int i = 1; i < args.size(); i++) {
String missingArgs = args.remove(i);
int exitCode = cmd.execute(args.toArray(new String[0]));
@@ -228,15 +231,13 @@ public void testKeyGenFailsWhenMissingArgs() {
}

@Test
public void testPrintsVersion() {
public void testPrintsVersion() throws IOException {
String version_expected = "1.2.0";

int exitCode = cmd.execute("--version");
String version = null;
try (Stream<String> output = Files.lines(fakeOutput.toPath())) {
version = output.iterator().next();
} catch (IOException e) {
fail("failed to retrieve command output");
}

assertEquals(0, exitCode);
@@ -246,11 +247,11 @@ public void testPrintsVersion() {

@Test
public void testCommandFailsWhenInputFileDoesNotExist() throws IOException {
// BUG: CommandLine insists to print to System.err, but only when gpFile is missing
PrintStream systemErr = System.err;
try (PrintStream errorStream = new PrintStream(fakeOutput)) {
System.setErr(errorStream);
File gpFile_ = folder.newFile();
File gpFile_ = Files.createTempFile("fake", "gpFile").toFile();
gpFile.deleteOnExit();
gpFile_.delete();
String[][] commands = {
{"asetup", "-f", gpFile_.getPath(), "authority1", apFileS.getPath(), apFileP.getPath(), "a", "b", "c", "d"},
@@ -266,7 +267,7 @@ public void testCommandFailsWhenInputFileDoesNotExist() throws IOException {
int exitCode = cmd.execute(command);

String msg = String.format("command \"%s\" output %d exitCode. Expected: %d.", command[0], exitCode, exitCode_expected);
assertEquals(msg, exitCode_expected, exitCode);
assertEquals(exitCode_expected, exitCode, msg);
}
}
System.setErr(systemErr);
@@ -289,7 +290,7 @@ public void testCommandFailsWhenInputFilePathIsInvalid() {
int exitCode = cmd.execute(command);

String msg = String.format("command \"%s\" output %d exitCode. Expected: %d.", command[0], exitCode, exitCode_expected);
assertEquals(msg, exitCode_expected, exitCode);
assertEquals(exitCode_expected, exitCode, msg);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package sg.edu.ntu.sce.sands.crypto.dcpabe;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.ac.AccessStructure;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;


public class CiphertextTest {
private static GlobalParameters gp;
private static AuthorityKeys authority;
private AccessStructure arho;
private PublicKeys pks;

@BeforeClass
@BeforeAll
public static void init() {
gp = DCPABE.globalSetup(160);
authority = DCPABE.authoritySetup("authority", gp, "A", "B", "C", "D");
}

@Before
@BeforeEach
public void setUp() {
arho = AccessStructure.buildFromPolicy("and A or D and C B");
pks = new PublicKeys();
@@ -46,17 +47,17 @@ public void testSerialization() throws Exception {

Ciphertext ct1 = (Ciphertext) ois.readObject();

assertArrayEquals("C0", ct.getC0(), ct1.getC0());
assertArrayEquals(ct.getC0(), ct1.getC0());

assertEquals("access structure differ", ct.getAccessStructure(), ct1.getAccessStructure());
assertEquals(ct.getAccessStructure(), ct1.getAccessStructure());

assertEquals("differ on l", ct.getAccessStructure().getL(), ct1.getAccessStructure().getL());
assertEquals("differ on n", ct.getAccessStructure().getN(), ct1.getAccessStructure().getN());
assertEquals(ct.getAccessStructure().getL(), ct1.getAccessStructure().getL());
assertEquals(ct.getAccessStructure().getN(), ct1.getAccessStructure().getN());

for (int i = 0; i < ct.getAccessStructure().getL(); i++) {
assertArrayEquals("differ on C1" + i, ct.getC1(i), ct1.getC1(i));
assertArrayEquals("differ on C2" + i, ct.getC2(i), ct1.getC2(i));
assertArrayEquals("differ on C3" + i, ct.getC3(i), ct1.getC3(i));
assertArrayEquals(ct.getC1(i), ct1.getC1(i), "differ on C1" + i);
assertArrayEquals(ct.getC2(i), ct1.getC2(i), "differ on C2" + i);
assertArrayEquals(ct.getC3(i), ct1.getC3(i), "differ on C3" + i);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package sg.edu.ntu.sce.sands.crypto.dcpabe;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;


public class GlobalParametersTest {

@@ -29,5 +30,4 @@ public void testSerialization() throws Exception {

assertEquals(gp, gp1);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package sg.edu.ntu.sce.sands.crypto.dcpabe;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.ac.AccessStructure;
import sg.edu.ntu.sce.sands.crypto.dcpabe.key.PersonalKey;
import sg.edu.ntu.sce.sands.crypto.dcpabe.key.PublicKey;
import sg.edu.ntu.sce.sands.crypto.dcpabe.key.SecretKey;

import java.util.Collections;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class SerializationTest {
private static final ObjectMapper mapper = new ObjectMapper();


@Test
public void serializeGlobalParameters() throws Exception {
GlobalParameters gp = DCPABE.globalSetup(160);
@@ -28,7 +26,7 @@ public void serializeGlobalParameters() throws Exception {
GlobalParameters deserialized = mapper.readValue(serializedValue, GlobalParameters.class);

assertNotNull(deserialized);
assertThat(gp, equalTo(deserialized));
assertEquals(gp, deserialized);
}

@Test
@@ -42,7 +40,7 @@ public void serializeAuthorityKeys() throws Exception {
AuthorityKeys deserialized = mapper.readValue(serializedValue, AuthorityKeys.class);

assertNotNull(deserialized);
assertThat(authorityKeys, equalTo(deserialized));
assertEquals(authorityKeys, deserialized);
}

@Test
@@ -56,7 +54,7 @@ public void serializeAccessStructure() throws Exception {
AccessStructure deserialized = mapper.readValue(serializedValue, AccessStructure.class);

assertNotNull(deserialized);
assertThat(ac.toString(), equalTo(deserialized.toString()));
assertEquals(ac.toString(), deserialized.toString());
}

@Test
@@ -70,7 +68,7 @@ public void serializeMessage() throws Exception {
Message deserialized = mapper.readValue(serializedValue, Message.class);

assertNotNull(deserialized);
assertThat(message, equalTo(deserialized));
assertEquals(message, deserialized);
}

@Test
@@ -84,7 +82,7 @@ public void serializePersonalKey() throws Exception {
PersonalKey deserialized = mapper.readValue(serializedValue, PersonalKey.class);

assertNotNull(deserialized);
assertThat(personalKey, equalTo(deserialized));
assertEquals(personalKey, deserialized);
}

@Test
@@ -98,7 +96,7 @@ public void serializePublicKey() throws Exception {
PublicKey deserialized = mapper.readValue(serializedValue, PublicKey.class);

assertNotNull(deserialized);
assertThat(publicKey, equalTo(deserialized));
assertEquals(publicKey, deserialized);
}

@Test
@@ -112,7 +110,7 @@ public void serializeSecretKey() throws Exception {
SecretKey deserialized = mapper.readValue(serializedValue, SecretKey.class);

assertNotNull(deserialized);
assertThat(secretKey, equalTo(deserialized));
assertEquals(secretKey, deserialized);
}

@Test
@@ -127,7 +125,7 @@ public void serializePersonalKeys() throws Exception {
PersonalKeys deserialized = mapper.readValue(serializedValue, PersonalKeys.class);

assertNotNull(deserialized);
assertThat(personalKeys, equalTo(deserialized));
assertEquals(personalKeys, deserialized);
}

@Test
@@ -144,6 +142,6 @@ public void serializePublicKeys() throws Exception {
PublicKeys deserialized = mapper.readValue(serializedValue, PublicKeys.class);

assertNotNull(deserialized);
assertThat(publicKeys, equalTo(deserialized));
assertEquals(publicKeys, deserialized);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package sg.edu.ntu.sce.sands.crypto.dcpabe.ac;

import org.junit.Before;
import org.junit.Test;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sg.edu.ntu.sce.sands.crypto.dcpabe.ac.AccessStructure.MatrixElement;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AccessStructureTest {
private AccessStructure arho;
private String policy;

@Before
@BeforeEach
public void setUp() {
policy = "and or D and C B A";
arho = AccessStructure.buildFromPolicy(policy);

0 comments on commit 1a1b0d0

Please sign in to comment.