Skip to content

Commit 41f3c4c

Browse files
fixes #12
1 parent 07204f9 commit 41f3c4c

File tree

8 files changed

+8976
-285416
lines changed

8 files changed

+8976
-285416
lines changed

pom.xml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848

4949
<!-- Tests -->
5050
<dependency>
51-
<groupId>junit</groupId>
52-
<artifactId>junit</artifactId>
53-
<version>4.12</version>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter</artifactId>
53+
<version>5.4.2</version>
5454
<scope>test</scope>
5555
</dependency>
5656
<dependency>
@@ -59,6 +59,12 @@
5959
<version>2.27.0</version>
6060
<scope>test</scope>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.hamcrest</groupId>
64+
<artifactId>hamcrest</artifactId>
65+
<version>2.1</version>
66+
<scope>test</scope>
67+
</dependency>
6268
<dependency>
6369
<groupId>com.google.guava</groupId>
6470
<artifactId>guava</artifactId>
@@ -109,6 +115,7 @@
109115
<release>7</release>
110116
<source>1.7</source> <!-- redundant to <release> but needed until https://bugs.openjdk.java.net/browse/JDK-8219474 is fixed (see javadoc config) -->
111117
<target>1.7</target>
118+
<testRelease>8</testRelease>
112119
<encoding>UTF-8</encoding>
113120
<showWarnings>true</showWarnings>
114121
</configuration>
@@ -195,6 +202,31 @@
195202
</build>
196203

197204
<profiles>
205+
<profile>
206+
<id>intellij</id>
207+
<!-- workaround for intellij bug: https://youtrack.jetbrains.com/issue/IDEA-85478 -->
208+
<activation>
209+
<activeByDefault>false</activeByDefault>
210+
<property>
211+
<name>idea.maven.embedder.version</name>
212+
</property>
213+
</activation>
214+
<build>
215+
<plugins>
216+
<plugin>
217+
<groupId>org.apache.maven.plugins</groupId>
218+
<artifactId>maven-compiler-plugin</artifactId>
219+
<version>3.8.1</version>
220+
<configuration>
221+
<release>8</release>
222+
<source>1.8</source>
223+
<target>1.8</target>
224+
</configuration>
225+
</plugin>
226+
</plugins>
227+
</build>
228+
</profile>
229+
198230
<profile>
199231
<id>dependency-check</id>
200232
<build>

src/test/go/siv-test-vectors.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ func printTestCases() {
7676
// Print a single test case to STDOUT.
7777
func printTestCase(key []byte, plaintext []byte, associatedData [][]byte) {
7878
// CTR mode encryption key
79-
fmt.Println(hex.EncodeToString(key[len(key)/2:]))
79+
fmt.Printf("%s;", hex.EncodeToString(key[len(key)/2:]))
8080

8181
// MAC (authentication) key
82-
fmt.Println(hex.EncodeToString(key[:len(key)/2]))
82+
fmt.Printf("%s;", hex.EncodeToString(key[:len(key)/2]))
8383

8484
// Plaintext
85-
fmt.Println(hex.EncodeToString(plaintext))
85+
fmt.Printf("%s;", hex.EncodeToString(plaintext))
8686

8787
// Additional associated data
88-
fmt.Printf("%v\n", len(associatedData))
88+
fmt.Printf("%v;", len(associatedData))
8989
for _, adElem := range associatedData {
90-
fmt.Println(hex.EncodeToString(adElem))
90+
fmt.Printf("%s;", hex.EncodeToString(adElem))
9191
}
9292

9393
// Ciphertext
@@ -97,7 +97,4 @@ func printTestCase(key []byte, plaintext []byte, associatedData [][]byte) {
9797
os.Exit(7)
9898
}
9999
fmt.Println(hex.EncodeToString(ciphertext));
100-
101-
// End of test case
102-
fmt.Println("---")
103100
}

src/test/java/org/cryptomator/siv/BenchmarkTest.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
******************************************************************************/
99
package org.cryptomator.siv;
1010

11-
import org.junit.Test;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.condition.EnabledOnJre;
1213
import org.openjdk.jmh.runner.Runner;
1314
import org.openjdk.jmh.runner.RunnerException;
1415
import org.openjdk.jmh.runner.options.Options;
@@ -18,22 +19,17 @@ public class BenchmarkTest {
1819

1920
@Test
2021
public void runBenchmarks() throws RunnerException {
21-
if (System.getProperty("java.version").startsWith("1.8")) {
22-
// Taken from http://stackoverflow.com/a/30486197/4014509:
23-
Options opt = new OptionsBuilder()
24-
// Specify which benchmarks to run
25-
.include(getClass().getPackage().getName() + ".*Benchmark.*")
26-
// Set the following options as needed
27-
.threads(2).forks(2) //
28-
.shouldFailOnError(true).shouldDoGC(true)
29-
// .jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining")
30-
// .addProfiler(WinPerfAsmProfiler.class)
31-
.build();
32-
33-
new Runner(opt).run();
34-
} else {
35-
System.out.println("Skipping Benchmarks");
36-
}
22+
// Taken from http://stackoverflow.com/a/30486197/4014509:
23+
Options opt = new OptionsBuilder()
24+
// Specify which benchmarks to run
25+
.include(getClass().getPackage().getName() + ".*Benchmark.*")
26+
// Set the following options as needed
27+
.threads(2).forks(2) //
28+
.shouldFailOnError(true).shouldDoGC(true)
29+
// .jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining")
30+
// .addProfiler(WinPerfAsmProfiler.class)
31+
.build();
32+
new Runner(opt).run();
3733
}
3834

3935
}

src/test/java/org/cryptomator/siv/EncryptionTestCase.java

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,53 @@
11
package org.cryptomator.siv;
22

3+
import com.google.common.base.Splitter;
4+
import com.google.common.io.BaseEncoding;
5+
36
import java.io.BufferedReader;
47
import java.io.IOException;
58
import java.io.InputStreamReader;
69
import java.nio.charset.StandardCharsets;
710
import java.util.ArrayList;
811
import java.util.Arrays;
912
import java.util.List;
10-
11-
import com.google.common.io.BaseEncoding;
13+
import java.util.Optional;
1214

1315
public class EncryptionTestCase {
16+
17+
private static int TESTCASE_CTR = 0;
18+
19+
private final int testCaseNumber;
1420
private final byte[] ctrKey;
1521
private final byte[] macKey;
1622
private final byte[] plaintext;
1723
private final byte[][] additionalData;
1824
private final byte[] ciphertext;
1925

20-
public EncryptionTestCase(byte[] ctrKey, byte[] macKey, byte[] plaintext, byte[][] additionalData, byte[] ciphertext) {
26+
public EncryptionTestCase(int testCaseNumber, byte[] ctrKey, byte[] macKey, byte[] plaintext, byte[][] additionalData, byte[] ciphertext) {
27+
this.testCaseNumber = testCaseNumber;
2128
this.ctrKey = ctrKey;
2229
this.macKey = macKey;
2330
this.plaintext = plaintext;
2431
this.additionalData = additionalData;
2532
this.ciphertext = ciphertext;
2633
}
2734

28-
// Read and parse the test cases generated with `siv-test-vectors.go`
29-
public static EncryptionTestCase[] readTestCases() throws IOException {
30-
// testcases.txt should contain an output from `siv-test-vectors.go`
31-
BufferedReader reader = new BufferedReader(new InputStreamReader(EncryptionTestCase.class.getResourceAsStream("/testcases.txt"), StandardCharsets.US_ASCII));
32-
33-
try {
34-
List<EncryptionTestCase> result = new ArrayList<EncryptionTestCase>();
35-
36-
for (;;) {
37-
String ctrKeyStr = reader.readLine();
38-
if (ctrKeyStr == null) {
39-
// No more test cases
40-
break;
41-
}
42-
byte[] ctrKey = BaseEncoding.base16().decode(ctrKeyStr.toUpperCase());
43-
byte[] macKey = BaseEncoding.base16().decode(reader.readLine().toUpperCase());
44-
byte[] plaintext = BaseEncoding.base16().decode(reader.readLine().toUpperCase());
45-
int adCount = Integer.parseInt(reader.readLine());
46-
byte[][] ad = new byte[adCount][];
47-
for (int adIdx = 0; adIdx < adCount; adIdx++) {
48-
ad[adIdx] = BaseEncoding.base16().decode(reader.readLine().toUpperCase());
49-
}
50-
byte[] ciphertext = BaseEncoding.base16().decode(reader.readLine().toUpperCase());
51-
52-
String divider = reader.readLine();
53-
if (!divider.equals("---")) {
54-
throw new IllegalStateException("expected test case divider but found: " + divider);
55-
}
56-
57-
result.add(new EncryptionTestCase(ctrKey, macKey, plaintext, ad, ciphertext));
58-
}
59-
60-
return result.toArray(new EncryptionTestCase[result.size()]);
61-
} finally {
62-
reader.close();
35+
public static EncryptionTestCase fromLine(String line) {
36+
List<String> fields = Splitter.on(';').splitToList(line);
37+
byte[] ctrKey = BaseEncoding.base16().decode(fields.get(0).toUpperCase());
38+
byte[] macKey = BaseEncoding.base16().decode(fields.get(1).toUpperCase());
39+
byte[] plaintext = BaseEncoding.base16().decode(fields.get(2).toUpperCase());
40+
int adCount = Integer.parseInt(fields.get(3));
41+
byte[][] ad = new byte[adCount][];
42+
for (int adIdx = 0; adIdx < adCount; adIdx++) {
43+
ad[adIdx] = BaseEncoding.base16().decode(fields.get(4+adIdx).toUpperCase());
6344
}
45+
byte[] ciphertext = BaseEncoding.base16().decode(fields.get(4+adCount).toUpperCase());
46+
return new EncryptionTestCase(TESTCASE_CTR++, ctrKey, macKey, plaintext, ad, ciphertext);
47+
}
48+
49+
public int getTestCaseNumber() {
50+
return testCaseNumber;
6451
}
6552

6653
public byte[] getCtrKey() {

src/test/java/org/cryptomator/siv/JceAesBlockCipherTest.java

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@
1111
import org.bouncycastle.crypto.DataLengthException;
1212
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
1313
import org.bouncycastle.crypto.params.KeyParameter;
14-
import org.junit.Assert;
15-
import org.junit.Rule;
16-
import org.junit.Test;
17-
import org.junit.rules.ExpectedException;
14+
import org.hamcrest.CoreMatchers;
15+
import org.hamcrest.MatcherAssert;
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.Test;
1818

1919
import java.security.Provider;
2020
import java.security.Security;
2121

2222
public class JceAesBlockCipherTest {
2323

24-
@Rule
25-
public final ExpectedException thrown = ExpectedException.none();
26-
2724
@Test
2825
public void testInitWithNullParam() {
2926
JceAesBlockCipher cipher = new JceAesBlockCipher();
30-
thrown.expect(IllegalArgumentException.class);
31-
thrown.expectMessage("missing parameter of type KeyParameter");
32-
cipher.init(true, null);
27+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, () -> {
28+
cipher.init(true, null);
29+
});
30+
MatcherAssert.assertThat(e.getMessage(), CoreMatchers.containsString("missing parameter of type KeyParameter"));
3331
}
3432

3533
@Test
3634
public void testInitWithMissingKey() {
3735
JceAesBlockCipher cipher = new JceAesBlockCipher();
38-
thrown.expect(IllegalArgumentException.class);
39-
thrown.expectMessage("missing parameter of type KeyParameter");
40-
cipher.init(true, new AsymmetricKeyParameter(true));
36+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, () -> {
37+
cipher.init(true, new AsymmetricKeyParameter(true));
38+
});
39+
MatcherAssert.assertThat(e.getMessage(), CoreMatchers.containsString("missing parameter of type KeyParameter"));
4140
}
4241

4342
@Test
4443
public void testInitWithInvalidKey() {
4544
JceAesBlockCipher cipher = new JceAesBlockCipher();
46-
thrown.expect(IllegalArgumentException.class);
47-
thrown.expectMessage("Invalid key");
48-
cipher.init(true, new KeyParameter(new byte[7]));
45+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, () -> {
46+
cipher.init(true, new KeyParameter(new byte[7]));
47+
});
48+
MatcherAssert.assertThat(e.getMessage(), CoreMatchers.containsString("Invalid key"));
4949
}
5050

5151
@Test
@@ -74,45 +74,48 @@ public void testInitForDecryptionWithProvider() {
7474

7575
private Provider getSunJceProvider() {
7676
Provider provider = Security.getProvider("SunJCE");
77-
Assert.assertNotNull(provider);
77+
Assertions.assertNotNull(provider);
7878
return provider;
7979
}
8080

8181
@Test
8282
public void testGetAlgorithmName() {
8383
JceAesBlockCipher cipher = new JceAesBlockCipher();
84-
Assert.assertEquals("AES", cipher.getAlgorithmName());
84+
Assertions.assertEquals("AES", cipher.getAlgorithmName());
8585
}
8686

8787
@Test
8888
public void testGetBlockSize() {
8989
JceAesBlockCipher cipher = new JceAesBlockCipher();
90-
Assert.assertEquals(16, cipher.getBlockSize());
90+
Assertions.assertEquals(16, cipher.getBlockSize());
9191
}
9292

9393
@Test
9494
public void testProcessBlockWithUninitializedCipher() {
9595
JceAesBlockCipher cipher = new JceAesBlockCipher();
96-
thrown.expect(IllegalStateException.class);
97-
cipher.processBlock(new byte[16], 0, new byte[16], 0);
96+
Assertions.assertThrows(IllegalStateException.class, () -> {
97+
cipher.processBlock(new byte[16], 0, new byte[16], 0);
98+
});
9899
}
99100

100101
@Test
101102
public void testProcessBlockWithInsufficientInput() {
102103
JceAesBlockCipher cipher = new JceAesBlockCipher();
103104
cipher.init(true, new KeyParameter(new byte[16]));
104-
thrown.expect(DataLengthException.class);
105-
thrown.expectMessage("Insufficient data in 'in'");
106-
cipher.processBlock(new byte[16], 1, new byte[16], 0);
105+
DataLengthException e = Assertions.assertThrows(DataLengthException.class, () -> {
106+
cipher.processBlock(new byte[16], 1, new byte[16], 0);
107+
});
108+
MatcherAssert.assertThat(e.getMessage(), CoreMatchers.containsString("Insufficient data in 'in'"));
107109
}
108110

109111
@Test
110112
public void testProcessBlockWithInsufficientOutput() {
111113
JceAesBlockCipher cipher = new JceAesBlockCipher();
112114
cipher.init(true, new KeyParameter(new byte[16]));
113-
thrown.expect(DataLengthException.class);
114-
thrown.expectMessage("Insufficient space in 'out'");
115-
cipher.processBlock(new byte[16], 0, new byte[16], 1);
115+
DataLengthException e = Assertions.assertThrows(DataLengthException.class, () -> {
116+
cipher.processBlock(new byte[16], 0, new byte[16], 1);
117+
});
118+
MatcherAssert.assertThat(e.getMessage(), CoreMatchers.containsString("Insufficient space in 'out'"));
116119
}
117120

118121
@Test
@@ -129,13 +132,13 @@ private void testProcessBlock(JceAesBlockCipher cipher) {
129132
cipher.init(true, new KeyParameter(new byte[16]));
130133
byte[] ciphertext = new byte[16];
131134
int encrypted = cipher.processBlock(new byte[20], 0, ciphertext, 0);
132-
Assert.assertEquals(16, encrypted);
135+
Assertions.assertEquals(16, encrypted);
133136

134137
cipher.init(false, new KeyParameter(new byte[16]));
135138
byte[] cleartext = new byte[16];
136139
int decrypted = cipher.processBlock(ciphertext, 0, cleartext, 0);
137-
Assert.assertEquals(16, decrypted);
138-
Assert.assertArrayEquals(new byte[16], cleartext);
140+
Assertions.assertEquals(16, decrypted);
141+
Assertions.assertArrayEquals(new byte[16], cleartext);
139142
}
140143

141144
@Test

0 commit comments

Comments
 (0)