From f463d4c1a550c4b3dfc0be362f0b0a723a88122d Mon Sep 17 00:00:00 2001 From: StevenWdV Date: Mon, 29 Nov 2021 00:12:42 +0100 Subject: Improve Java wrapper: add all tests, link correct libraries, use Maven, add Makefile & GitHub workflow. Fix make clean targets. --- .../EduVpnCommon/src/eduvpncommon/Discovery.java | 68 -------------- .../src/eduvpncommon/VerifyException.java | 5 - .../tests/eduvpncommon/VerifyTests.java | 31 ------- wrappers/java/.gitignore | 1 + wrappers/java/Makefile | 14 +++ wrappers/java/README.md | 31 +++++++ wrappers/java/pom.xml | 102 +++++++++++++++++++++ .../src/main/java/nl/eduvpn/common/Discovery.java | 66 +++++++++++++ .../java/nl/eduvpn/common/VerifyException.java | 9 ++ .../test/java/nl/eduvpn/common/VerifyTests.java | 78 ++++++++++++++++ wrappers/python/Makefile | 3 + 11 files changed, 304 insertions(+), 104 deletions(-) delete mode 100644 wrappers/EduVpnCommon/src/eduvpncommon/Discovery.java delete mode 100644 wrappers/EduVpnCommon/src/eduvpncommon/VerifyException.java delete mode 100644 wrappers/EduVpnCommon/tests/eduvpncommon/VerifyTests.java create mode 100644 wrappers/java/.gitignore create mode 100644 wrappers/java/Makefile create mode 100644 wrappers/java/README.md create mode 100644 wrappers/java/pom.xml create mode 100644 wrappers/java/src/main/java/nl/eduvpn/common/Discovery.java create mode 100644 wrappers/java/src/main/java/nl/eduvpn/common/VerifyException.java create mode 100644 wrappers/java/src/test/java/nl/eduvpn/common/VerifyTests.java (limited to 'wrappers') diff --git a/wrappers/EduVpnCommon/src/eduvpncommon/Discovery.java b/wrappers/EduVpnCommon/src/eduvpncommon/Discovery.java deleted file mode 100644 index 6c27eee..0000000 --- a/wrappers/EduVpnCommon/src/eduvpncommon/Discovery.java +++ /dev/null @@ -1,68 +0,0 @@ -package eduvpncommon; - -import com.sun.jna.*; - -import java.nio.charset.StandardCharsets; -import java.time.Instant; -import java.util.*; - -public class Discovery { - private static final String libName = "eduvpn_verify"; - private static final NativeApi discovery = Native.load(libName, NativeApi.class); - - /** - * Verifies the signature on the JSON server_list.json/organization_list.json file. - * If the function returns the signature is valid for the given file type. - * - * @param signature .minisig signature file contents. - * @param signedJson Signed .json file contents. - * @param expectedFileName The file type to be verified, one of {@code "server_list.json"} or {@code "organization_list.json"}. - * @param minSignTime Minimum time for signature. Should be set to at least the time in a previously retrieved file. - * @throws VerifyException If signature verification fails. - */ - public static void verify(byte[] signature, byte[] signedJson, String expectedFileName, Instant minSignTime) throws VerifyException { - long err = discovery.Verify(NativeApi.GoSlice.make(signature), NativeApi.GoSlice.make(signedJson), - NativeApi.GoSlice.make(expectedFileName.getBytes(StandardCharsets.UTF_8)), - minSignTime.getEpochSecond()); - if (err != 0) throw new VerifyException(); - //TODO throw new IllegalArgumentException() - } - - /** - * Use for testing only, see Go documentation. - */ - // package-private - static void insecureTestingSetExtraKey(String keyString) { - discovery.InsecureTestingSetExtraKey(NativeApi.GoSlice.make(keyString.getBytes(StandardCharsets.UTF_8))); - } - - private interface NativeApi extends Library { - class GoSlice extends Structure implements Structure.ByValue { - public Pointer data; - public long len, cap; - - public GoSlice(Pointer data, long len, long cap) { - this.data = data; - this.len = len; - this.cap = cap; - } - - public static GoSlice make(byte[] bytes) { - Memory memory = new Memory(bytes.length); - memory.write(0, bytes, 0, bytes.length); - return new GoSlice(memory, bytes.length, bytes.length); - } - - protected List getFieldOrder() { - return Arrays.asList("data", "len", "cap"); - } - } - - long Verify(GoSlice signatureFileContent, GoSlice signedJson, GoSlice expectedFileName, long minSignTime); - - void InsecureTestingSetExtraKey(GoSlice keyString); - } - - private Discovery() { - } -} diff --git a/wrappers/EduVpnCommon/src/eduvpncommon/VerifyException.java b/wrappers/EduVpnCommon/src/eduvpncommon/VerifyException.java deleted file mode 100644 index a81aceb..0000000 --- a/wrappers/EduVpnCommon/src/eduvpncommon/VerifyException.java +++ /dev/null @@ -1,5 +0,0 @@ -package eduvpncommon; - -public class VerifyException extends Exception { - //TODO -} \ No newline at end of file diff --git a/wrappers/EduVpnCommon/tests/eduvpncommon/VerifyTests.java b/wrappers/EduVpnCommon/tests/eduvpncommon/VerifyTests.java deleted file mode 100644 index 117f5ef..0000000 --- a/wrappers/EduVpnCommon/tests/eduvpncommon/VerifyTests.java +++ /dev/null @@ -1,31 +0,0 @@ -package eduvpncommon; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.time.Instant; - -class VerifyTests { - private static final Path testDataDir = Paths.get("../../test_data"); - - @BeforeAll - static void oneTimeSetup() throws IOException { - Discovery.insecureTestingSetExtraKey(Files.lines(testDataDir.resolve("dummy/public.key")).reduce((a, b) -> b).get()); - } - - @Test - void testValid() throws IOException, VerifyException { - Discovery.verify( - Files.readAllBytes(Paths.get("../../test_data/dummy/server_list.json.minisig")), - Files.readAllBytes(Paths.get("../../test_data/dummy/server_list.json")), - "server_list.json", - Instant.EPOCH - ); - } - - //TODO -} \ No newline at end of file diff --git a/wrappers/java/.gitignore b/wrappers/java/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/wrappers/java/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/wrappers/java/Makefile b/wrappers/java/Makefile new file mode 100644 index 0000000..03e7d88 --- /dev/null +++ b/wrappers/java/Makefile @@ -0,0 +1,14 @@ +.PHONY: build pack test clean + +build: + mvn compile + +pack: + mvn package + +test: + $(MAKE) -C ../../exports + mvn test + +clean: + mvn clean diff --git a/wrappers/java/README.md b/wrappers/java/README.md new file mode 100644 index 0000000..87cdd49 --- /dev/null +++ b/wrappers/java/README.md @@ -0,0 +1,31 @@ +# Java wrapper + +## Requirements + +You will need to install JDK 8 or later ([Adoptium](https://adoptium.net/) +or [Oracle](https://www.oracle.com/java/technologies/downloads/)). To easily compile the project, you should +download [Maven](https://maven.apache.org/). + +## Build & test + +First build the shared Go library. Next: + +Build `EduVpnCommon`: + +```shell +make +``` + +Build as JAR, including eduvpn_verify library: + +```shell +make pack +``` + +The JAR will include all versions of the library that are built in the `exports` folder. + +Test: + +```shell +make test +``` diff --git a/wrappers/java/pom.xml b/wrappers/java/pom.xml new file mode 100644 index 0000000..bd9f721 --- /dev/null +++ b/wrappers/java/pom.xml @@ -0,0 +1,102 @@ + + 4.0.0 + + nl.eduvpn.common + 0.1.0 + jar + + eduvpncommon + eduVPN common library + + + UTF-8 + 1.8 + 1.8 + + + + + net.java.dev.jna + jna + 5.10.0 + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.8.1 + test + + + + + + + + + ../../exports/linux/amd64 + + *.so + + linux-x86-64 + + + ../../exports/linux/arm + + *.so + + linux-arm + + + ../../exports/linux/arm64 + + *.so + + linux-arm64 + + + + ../../exports/windows/amd64 + + *.dll + + win32-x86-64 + + + ../../exports/windows/386 + + *.dll + + win32-x86 + + + ../../exports/windows/arm + + *.dll + + win32-arm + + + ../../exports/windows/arm64 + + *.dll + + win32-arm64 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + + + diff --git a/wrappers/java/src/main/java/nl/eduvpn/common/Discovery.java b/wrappers/java/src/main/java/nl/eduvpn/common/Discovery.java new file mode 100644 index 0000000..d73a3d2 --- /dev/null +++ b/wrappers/java/src/main/java/nl/eduvpn/common/Discovery.java @@ -0,0 +1,66 @@ +package nl.eduvpn.common; + +import com.sun.jna.*; + +import java.nio.charset.StandardCharsets; +import java.time.Instant; + +public class Discovery { + private static final NativeApi discovery = Native.load("eduvpn_verify", NativeApi.class); + + /** + * Verifies the signature on the JSON server_list.json/organization_list.json file. + * If the function returns the signature is valid for the given file type. + * + * @param signature .minisig signature file contents. + * @param signedJson Signed .json file contents. + * @param expectedFileName The file type to be verified, one of {@code "server_list.json"} or {@code "organization_list.json"}. + * @param minSignTime Minimum time for signature. Should be set to at least the time in a previously retrieved file. + * @throws IllegalArgumentException If {@code expectedFileName} is not one of the allowed values or one of the parameters is empty. + * @throws VerifyException If signature verification fails. + */ + public static void verify(byte[] signature, byte[] signedJson, String expectedFileName, Instant minSignTime) throws VerifyException { + long err = discovery.Verify(NativeApi.GoSlice.make(signature), NativeApi.GoSlice.make(signedJson), + NativeApi.GoSlice.make(expectedFileName.getBytes(StandardCharsets.UTF_8)), + minSignTime.getEpochSecond()); + if (err != 0) { + if (err == 1) throw new IllegalArgumentException("Unknown excpectedFileName"); + throw new VerifyException(err); + } + } + + /** + * Use for testing only, see Go documentation. + */ + // package-private + static void insecureTestingSetExtraKey(String keyString) { + discovery.InsecureTestingSetExtraKey(NativeApi.GoSlice.make(keyString.getBytes(StandardCharsets.UTF_8))); + } + + private interface NativeApi extends Library { + @Structure.FieldOrder({"data", "len", "cap"}) + class GoSlice extends Structure implements Structure.ByValue { + public Pointer data; + public long len, cap; + + public GoSlice(Pointer data, long len, long cap) { + this.data = data; + this.len = len; + this.cap = cap; + } + + public static GoSlice make(byte[] bytes) { + Memory memory = new Memory(bytes.length); + memory.write(0, bytes, 0, bytes.length); + return new GoSlice(memory, bytes.length, bytes.length); + } + } + + long Verify(GoSlice signatureFileContent, GoSlice signedJson, GoSlice expectedFileName, long minSignTime); + + void InsecureTestingSetExtraKey(GoSlice keyString); + } + + private Discovery() { + } +} diff --git a/wrappers/java/src/main/java/nl/eduvpn/common/VerifyException.java b/wrappers/java/src/main/java/nl/eduvpn/common/VerifyException.java new file mode 100644 index 0000000..83dffb1 --- /dev/null +++ b/wrappers/java/src/main/java/nl/eduvpn/common/VerifyException.java @@ -0,0 +1,9 @@ +package nl.eduvpn.common; + +public class VerifyException extends Exception { + public final long code; //TODO not use plain long + + public VerifyException(long code) { + this.code = code; + } +} \ No newline at end of file diff --git a/wrappers/java/src/test/java/nl/eduvpn/common/VerifyTests.java b/wrappers/java/src/test/java/nl/eduvpn/common/VerifyTests.java new file mode 100644 index 0000000..b4767a5 --- /dev/null +++ b/wrappers/java/src/test/java/nl/eduvpn/common/VerifyTests.java @@ -0,0 +1,78 @@ +package nl.eduvpn.common; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Instant; + +import static org.junit.jupiter.api.Assertions.*; + +class VerifyTests { + private static final Path testDataDir = Paths.get("../../test_data"); + + @SuppressWarnings("OptionalGetWithoutIsPresent") + @BeforeAll + static void oneTimeSetup() throws IOException { + Discovery.insecureTestingSetExtraKey(Files.lines(testDataDir.resolve("dummy/public.key")).reduce((a, b) -> b).get()); + } + + @Test + void testValid() { + assertDoesNotThrow(() -> + Discovery.verify( + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json.minisig")), + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json")), + "server_list.json", + Instant.EPOCH + )); + } + + @Test + void testInvalidSignature() { + Assertions.assertEquals(2, assertThrows(VerifyException.class, () -> + Discovery.verify( + Files.readAllBytes(testDataDir.resolve("dummy/random.txt")), + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json")), + "server_list.json", + Instant.EPOCH + )).code); + } + + @Test + void testWrongKey() { + assertEquals(3, assertThrows(VerifyException.class, () -> + Discovery.verify( + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json.wrong_key.minisig")), + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json")), + "server_list.json", + Instant.EPOCH + )).code); + } + + @Test + void testOldSignature() { + assertEquals(4, assertThrows(VerifyException.class, () -> + Discovery.verify( + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json.minisig")), + Files.readAllBytes(testDataDir.resolve("dummy/server_list.json")), + "server_list.json", + Instant.MAX + )).code); + } + + @Test + void testUnknownExpectedFile() { + assertThrows(IllegalArgumentException.class, () -> + Discovery.verify( + Files.readAllBytes(testDataDir.resolve("dummy/other_list.json.minisig")), + Files.readAllBytes(testDataDir.resolve("dummy/other_list.json")), + "other_list.json", + Instant.EPOCH + )); + } +} \ No newline at end of file diff --git a/wrappers/python/Makefile b/wrappers/python/Makefile index 77c620e..690901f 100644 --- a/wrappers/python/Makefile +++ b/wrappers/python/Makefile @@ -6,3 +6,6 @@ compile: test: $(MAKE) -C ../../exports python3 -m unittest test_discovery + +clean: + # Nothing to do -- cgit v1.2.3