diff --git a/src/test/java/ProgrammaticAccessTest.java b/src/test/java/ProgrammaticAccessTest.java index ee2cfc0..d635e09 100644 --- a/src/test/java/ProgrammaticAccessTest.java +++ b/src/test/java/ProgrammaticAccessTest.java @@ -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"); diff --git a/src/test/java/Testing.java b/src/test/java/Testing.java index cb43898..7245db1 100644 --- a/src/test/java/Testing.java +++ b/src/test/java/Testing.java @@ -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() { diff --git a/src/test/java/sg/edu/ntu/sce/sands/crypto/DCPABEToolTest.java b/src/test/java/sg/edu/ntu/sce/sands/crypto/DCPABEToolTest.java index 3e4895d..a057898 100644 --- a/src/test/java/sg/edu/ntu/sce/sands/crypto/DCPABEToolTest.java +++ b/src/test/java/sg/edu/ntu/sce/sands/crypto/DCPABEToolTest.java @@ -1,12 +1,9 @@ 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; @@ -14,13 +11,16 @@ 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,10 +38,7 @@ 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()); @@ -49,22 +46,29 @@ public static void beforeAll() throws Exception { 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 args = new ArrayList<>(); - args.addAll(Arrays.asList("keygen", gpFile.getPath(), "user1", "a", apFileS.getPath(), key1AFile.getPath())); + List 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 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); } } } diff --git a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/CiphertextTest.java b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/CiphertextTest.java index f64545b..f30f36f 100644 --- a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/CiphertextTest.java +++ b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/CiphertextTest.java @@ -1,8 +1,8 @@ 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; @@ -10,8 +10,9 @@ 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; @@ -19,13 +20,13 @@ public class CiphertextTest { 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); } } } diff --git a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/GlobalParametersTest.java b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/GlobalParametersTest.java index ff9a4ba..61c386d 100644 --- a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/GlobalParametersTest.java +++ b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/GlobalParametersTest.java @@ -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); } - } diff --git a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/SerializationTest.java b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/SerializationTest.java index 008f8f8..5a31dfd 100644 --- a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/SerializationTest.java +++ b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/SerializationTest.java @@ -1,7 +1,7 @@ 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; @@ -9,14 +9,12 @@ 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); } } diff --git a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/ac/AccessStructureTest.java b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/ac/AccessStructureTest.java index 94b1cd1..bedfc0f 100644 --- a/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/ac/AccessStructureTest.java +++ b/src/test/java/sg/edu/ntu/sce/sands/crypto/dcpabe/ac/AccessStructureTest.java @@ -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);