diff options
| author | StevenWdV <stevenwdv@gmail.com> | 2022-01-24 14:59:25 +0100 |
|---|---|---|
| committer | StevenWdV <stevenwdv@gmail.com> | 2022-01-24 16:24:57 +0100 |
| commit | e544c6fa9e15e7277da79e2464243e90b2706b8c (patch) | |
| tree | de6613747e0e34a799089d4677f9833a85748712 /wrappers/swift | |
| parent | aab2e4b966c82b67eb0e204060e5ea6cd4ea15cf (diff) | |
Cleanup
Added variables to Makefiles to specify custom exports/ directory;
Split exception classes in Java & C#;
Added more comments;
Renamed library and Go package;
Removed real (pure) tests;
Added generate_lib.ps1 to generate import .lib for Windows (Swift);
Moved built Go libraries to exports/lib/;
Switch to hopefully faster Swift GitHub Action.
Diffstat (limited to 'wrappers/swift')
| -rw-r--r-- | wrappers/swift/CEduVpnCommon/Sources/CEduVpnCommon/module.modulemap | 4 | ||||
| -rw-r--r-- | wrappers/swift/Makefile | 33 | ||||
| -rw-r--r-- | wrappers/swift/README.md | 20 | ||||
| -rw-r--r-- | wrappers/swift/Sources/EduVpnCommon/EduVpnCommon.swift | 8 | ||||
| -rw-r--r-- | wrappers/swift/Tests/EduVpnCommonTests/EduVpnCommonTests.swift | 22 | ||||
| -rwxr-xr-x | wrappers/swift/swift.cmd | 2 |
6 files changed, 57 insertions, 32 deletions
diff --git a/wrappers/swift/CEduVpnCommon/Sources/CEduVpnCommon/module.modulemap b/wrappers/swift/CEduVpnCommon/Sources/CEduVpnCommon/module.modulemap index c85e48f..2c50cfd 100644 --- a/wrappers/swift/CEduVpnCommon/Sources/CEduVpnCommon/module.modulemap +++ b/wrappers/swift/CEduVpnCommon/Sources/CEduVpnCommon/module.modulemap @@ -1,5 +1,5 @@ module CEduVpnCommon { - header "Headers/eduvpn_verify.h" - link "eduvpn_verify" + header "Headers/eduvpn_common.h" + link "eduvpn_common" export * } diff --git a/wrappers/swift/Makefile b/wrappers/swift/Makefile index 84b3cc1..b46a177 100644 --- a/wrappers/swift/Makefile +++ b/wrappers/swift/Makefile @@ -1,24 +1,33 @@ -.PHONY: build test clean +.PHONY: build test install-header clean -ifneq (clean,$(MAKECMDGOALS)) -include ../../exports/platform.mk +EXPORTS_PATH ?= ../../exports +EXPORTS_LIB_PATH ?= $(EXPORTS_PATH)/lib -ifeq (Windows_NT,$(OS)) +ifneq ($(MAKECMDGOALS),clean) +include $(EXPORTS_PATH)/platform.mk + +LIB_DIR = $(EXPORTS_LIB_PATH)/$(GOOS)/$(GOARCH) + +ifeq ($(OS),Windows_NT) SWIFT = ./swift.cmd else SWIFT = swift endif endif -build: .build_lib - $(SWIFT) build --configuration release -Xlinker -L"../../exports/$(GOOS)/$(GOARCH)" +build: install-header + $(SWIFT) build --configuration release -Xlinker -L"$(LIB_DIR)" + +test: install-header + $(SWIFT) test --parallel -Xlinker -L"$(LIB_DIR)" -test: .build_lib - $(SWIFT) test --parallel -Xlinker -L"../../exports/$(GOOS)/$(GOARCH)" +install-header: +ifneq ($(EXPORTS_PATH),) +ifneq ($(wildcard $(EXPORTS_PATH)/Makefile),) + $(MAKE) -C "$(EXPORTS_PATH)" +endif +endif + install "$(LIB_DIR)/$(LIB_NAME).h" -Dt CEduVpnCommon/Sources/CEduVpnCommon/Headers # Copy header for modulemap clean: rm -rf .build/ CEduVpnCommon/Sources/CEduVpnCommon/Headers/*.h - -.build_lib: - $(MAKE) -C ../../exports - install "../../exports/$(GOOS)/$(GOARCH)/eduvpn_verify.h" -Dt CEduVpnCommon/Sources/CEduVpnCommon/Headers diff --git a/wrappers/swift/README.md b/wrappers/swift/README.md index 8259592..f28b028 100644 --- a/wrappers/swift/README.md +++ b/wrappers/swift/README.md @@ -2,7 +2,8 @@ ## Requirements -You will need to install the [Swift SDK](https://www.swift.org/getting-started), which includes the `swift` tool. +You will need to install the [Swift SDK](https://www.swift.org/getting-started), which includes the `swift` tool. This +project does not require Xcode as it uses the Swift Package Manager. ## Build & test @@ -18,7 +19,22 @@ Build `EduVpnCommon` using shared Go library for specified platform, e.g.: make GOOS=linux GOARCH=amd64 ``` -On Windows, you will also need to generate a .lib for the .dll. +When using this library, you will need to make sure that the linker can find the shared Go library. + +<small>On Windows, you will also need to generate a .lib import library for the .dll. You can +use `exports/generate_lib.ps1` +for this, passing in the path to the DLL file. Execute this from a Visual Studio Developer shell before building the +Swift project. Alternatively, you could use `objdump` and `llvm-dlltool`. You only need to update this if the list of +exported symbols changes.</small> + +If you just want to copy over the C header file to the right directory for the modulemap in `CEduVpnCommon`, run: + +```shell +make install-header +``` + +If you do not build this as part of the full repository, specify `EXPORTS_PATH="path/to/exports-folder"` when calling +make. This folder must contain `platform.mk` and the `lib/` folder with built libraries and headers. Test: diff --git a/wrappers/swift/Sources/EduVpnCommon/EduVpnCommon.swift b/wrappers/swift/Sources/EduVpnCommon/EduVpnCommon.swift index 340e94a..b849626 100644 --- a/wrappers/swift/Sources/EduVpnCommon/EduVpnCommon.swift +++ b/wrappers/swift/Sources/EduVpnCommon/EduVpnCommon.swift @@ -9,7 +9,7 @@ private extension Data { // This closure method guarantees this try withUnsafeBytes { (pointer: UnsafeRawBufferPointer) -> ResultType in // Note: UnsafeRawBufferPointer.startIndex will always be 0, see docs - // Cast to UnsafeMutableRawPointer, assumes it will not be written to + // Cast to UnsafeMutableRawPointer, assumes it will not actually be written to try body(GoSlice(data: UnsafeMutableRawPointer(mutating: pointer.baseAddress), len: GoInt(pointer.count), cap: GoInt(pointer.count))) } @@ -33,9 +33,9 @@ public enum VerifyErr: Error, Equatable { /// Signature has a timestamp lower than the specified minimum signing time. case ErrTooOld /// Other unknown error - case Unknown(code: GoInt) + case Unknown(code: GoInt8) - static func fromCode(_ code: GoInt) -> VerifyErr { + static func fromCode(_ code: GoInt8) -> VerifyErr { precondition(code != 0) switch code { case 1: return ErrUnknownExpectedFileName @@ -54,7 +54,7 @@ public enum VerifyErr: Error, Equatable { /// - signature: .minisig signature file contents. /// - signedJson: Signed .json file contents. /// - expectedFileName: The file type to be verified, one of "server_list.json" or "organization_list.json". -/// - minSignTime: Minimum time for signature. Should be set to at least the time in a previously retrieved file. +/// - minSignTime: Minimum time for signature. Should be set to at least the time of the previous signature. /// - Throws: VerifyErr: If signature verification fails or `expectedFileName` is not one of the allowed values. public func Verify(signature: Data, signedJson: Data, expectedFileName: String, minSignTime: Date) throws { let result = signature.withSlice { signatureData in diff --git a/wrappers/swift/Tests/EduVpnCommonTests/EduVpnCommonTests.swift b/wrappers/swift/Tests/EduVpnCommonTests/EduVpnCommonTests.swift index a508023..21186a9 100644 --- a/wrappers/swift/Tests/EduVpnCommonTests/EduVpnCommonTests.swift +++ b/wrappers/swift/Tests/EduVpnCommonTests/EduVpnCommonTests.swift @@ -6,14 +6,14 @@ final class EduVpnCommonTests: XCTestCase { override class func setUp() { // Swift is confused by CRLF, so on some systems we cannot just take the second-to-last element - InsecureTestingSetExtraKey(keyString: try! String(contentsOfFile: "\(testDataDir)/dummy/public.key") + InsecureTestingSetExtraKey(keyString: try! String(contentsOfFile: "\(testDataDir)/public.key") .components(separatedBy: .newlines).last(where: { !$0.isEmpty })!) } func testValid() throws { try Verify( - signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json.minisig")), - signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json")), + signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json.minisig")), + signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json")), expectedFileName: "server_list.json", minSignTime: Date(timeIntervalSince1970: 0)) } @@ -21,8 +21,8 @@ final class EduVpnCommonTests: XCTestCase { func testInvalidSignature() throws { XCTAssertThrowsError( try Verify( - signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/random.txt")), - signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json")), + signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/random.txt")), + signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json")), expectedFileName: "server_list.json", minSignTime: Date(timeIntervalSince1970: 0)), "", {err in XCTAssertEqual(err as? VerifyErr, VerifyErr.ErrInvalidSignature)}); @@ -31,8 +31,8 @@ final class EduVpnCommonTests: XCTestCase { func testWrongKey() throws { XCTAssertThrowsError( try Verify( - signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json.wrong_key.minisig")), - signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json")), + signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json.wrong_key.minisig")), + signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json")), expectedFileName: "server_list.json", minSignTime: Date(timeIntervalSince1970: 0)), "", {err in XCTAssertEqual(err as? VerifyErr, VerifyErr.ErrInvalidSignatureUnknownKey)}); @@ -41,8 +41,8 @@ final class EduVpnCommonTests: XCTestCase { func testOldSignature() throws { XCTAssertThrowsError( try Verify( - signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json.minisig")), - signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/server_list.json")), + signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json.minisig")), + signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/server_list.json")), expectedFileName: "server_list.json", minSignTime: Date(timeIntervalSince1970: TimeInterval(1 << 31))), "", {err in XCTAssertEqual(err as? VerifyErr, VerifyErr.ErrTooOld)}); @@ -51,8 +51,8 @@ final class EduVpnCommonTests: XCTestCase { func testUnknownExpectedFile() throws { XCTAssertThrowsError( try Verify( - signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/other_list.json.minisig")), - signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/dummy/other_list.json")), + signature: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/other_list.json.minisig")), + signedJson: try! Data(contentsOf: URL(fileURLWithPath: "\(EduVpnCommonTests.testDataDir)/other_list.json")), expectedFileName: "other_list.json", minSignTime: Date(timeIntervalSince1970: 0)), "", {err in XCTAssertEqual(err as? VerifyErr, VerifyErr.ErrUnknownExpectedFileName)}); diff --git a/wrappers/swift/swift.cmd b/wrappers/swift/swift.cmd index 87fba4b..57040bd 100755 --- a/wrappers/swift/swift.cmd +++ b/wrappers/swift/swift.cmd @@ -1,6 +1,6 @@ @echo off -:: Rename PATH -> Path +:: Rename PATH -> Path because of swift issue https://github.com/compnerd/swift-build/issues/413 set _p=%PATH% set PATH= set Path=%_p% |
