From e544c6fa9e15e7277da79e2464243e90b2706b8c Mon Sep 17 00:00:00 2001 From: StevenWdV Date: Mon, 24 Jan 2022 14:59:25 +0100 Subject: 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. --- wrappers/php/.gitignore | 1 + wrappers/php/Makefile | 38 ++++++++++++++++++++++----- wrappers/php/README.md | 6 +++++ wrappers/php/src/Discovery.php | 10 ++++--- wrappers/php/src/SignatureTooOldException.php | 2 +- wrappers/php/tests/DiscoveryTest.php | 22 ++++++++-------- 6 files changed, 57 insertions(+), 22 deletions(-) (limited to 'wrappers/php') diff --git a/wrappers/php/.gitignore b/wrappers/php/.gitignore index 4a56e6c..bdaa6e3 100644 --- a/wrappers/php/.gitignore +++ b/wrappers/php/.gitignore @@ -1,4 +1,5 @@ /vendor/ +/lib/* composer.phar .phpunit* *.h diff --git a/wrappers/php/Makefile b/wrappers/php/Makefile index 06c49f1..e0262e9 100644 --- a/wrappers/php/Makefile +++ b/wrappers/php/Makefile @@ -1,25 +1,51 @@ .PHONY: install-header test install-dev-dependencies clean -ifneq (clean,$(MAKECMDGOALS)) -include ../../exports/platform.mk +EXPORTS_PATH ?= ../../exports +EXPORTS_LIB_PATH ?= $(EXPORTS_PATH)/lib +ifneq ($(MAKECMDGOALS),clean) +include $(EXPORTS_PATH)/platform.mk + +# Add phpunit to PATH export PATH := $(abspath vendor/bin):$(PATH) endif +ifeq ($(COPY_LIB),1) +COPY_LIB_DIR = lib +endif + +ifneq ($(COPY_LIB_DIR),) +COPY_LIB_DIR := $(COPY_LIB_DIR)/ +endif + +# Strip / replace elements confusing PHP's limited C parser: __SIZE_TYPE__, _Complex, extern "C" +# Also add FFI_LIB library name, see https://www.php.net/manual/en/ffi.load install-header: - $(MAKE) -C ../../exports +ifneq ($(EXPORTS_PATH),) +ifneq ($(wildcard $(EXPORTS_PATH)/Makefile),) + $(MAKE) -C "$(EXPORTS_PATH)" +endif +endif mkdir -p src/headers sed --null-data \ -e 's/DO NOT EDIT/Modified for PHP/' \ + \ -e 's/__SIZE_TYPE__/size_t/g' \ -e 's/[^\n]*_Complex[^\n]*//g' \ -e 's/#ifdef __cplusplus[^#]*#endif//g' \ - -e 's/^/#define FFI_LIB "$(LIB_PREFIX)eduvpn_verify$(LIB_SUFFIX)"\n\n/' \ - "../../exports/$(GOOS)/$(GOARCH)/eduvpn_verify.h" > src/headers/eduvpn_verify_php.h + \ + -e 's/^/#define FFI_LIB "$(subst /,\/,$(COPY_LIB_DIR))$(LIB_FILE)"\n\n/' \ + \ + "$(EXPORTS_LIB_PATH)/$(GOOS)/$(GOARCH)/$(LIB_NAME).h" > src/headers/$(LIB_NAME)_php.h +ifeq ($(COPY_LIB),1) + install "$(EXPORTS_LIB_PATH)/$(GOOS)/$(GOARCH)/$(LIB_FILE)" -Dt "$(COPY_LIB_DIR)" +endif test: install-header install-dev-dependencies phpunit +# Try: composer, composer.phar, ./composer.phar, ./composer +# check-platform-reqs is needed because of config.platform in composer.json, see https://getcomposer.org/doc/06-config.md#platform install-dev-dependencies: if command -v composer; then \ composer install && composer check-platform-reqs; \ @@ -31,4 +57,4 @@ install-dev-dependencies: fi clean: - rm -rf vendor/ .phpunit* src/headers/*.h + rm -rf vendor/ .phpunit* src/headers/*.h lib/* diff --git a/wrappers/php/README.md b/wrappers/php/README.md index 776c0ac..b5cafa2 100644 --- a/wrappers/php/README.md +++ b/wrappers/php/README.md @@ -27,3 +27,9 @@ Or for the specified platform: ```shell make install-header GOOS=windows GOARCH=amd64 ``` + +When using this library, you will need to make sure that the linker can find the shared Go library. Alternatively, +pass `COPY_LIB=1` to `make install-header` to copy the library over to this folder and load it via this relative path. + +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. diff --git a/wrappers/php/src/Discovery.php b/wrappers/php/src/Discovery.php index 3ae7010..322d621 100644 --- a/wrappers/php/src/Discovery.php +++ b/wrappers/php/src/Discovery.php @@ -11,13 +11,15 @@ final class Discovery { public function __construct() { } + const LIB_NAME = "eduvpn_common"; + private static ?FFI $ffi = null; private static function ffi(): FFI { if (!self::$ffi) { - if (!(self::$ffi = FFI::load(__DIR__ . '/headers/eduvpn_verify_php.h'))) - throw new Error('failed to load eduvpn_verify'); + if (!(self::$ffi = FFI::load(__DIR__ . '/headers/' . self::LIB_NAME . '_php.h'))) + throw new Error('failed to load ' . self::LIB_NAME); } return self::$ffi; } @@ -30,8 +32,8 @@ final class Discovery * @param string $signedJson Signed .json file contents. * @param string $expectedFileName The file type to be verified, one of "server_list.json" or * "organization_list.json". - * @param int $minSignTime Minimum time for signature. Should be set to at least the time in a previously - * retrieved file. + * @param int $minSignTime Minimum time for signature. Should be set to at least the time of the previous + * signature. * @return void * @throws InvalidArgumentException If expectedFileName is not one of the allowed values. * @throws VerifyException If signature verification fails. diff --git a/wrappers/php/src/SignatureTooOldException.php b/wrappers/php/src/SignatureTooOldException.php index bbae949..4b7e341 100644 --- a/wrappers/php/src/SignatureTooOldException.php +++ b/wrappers/php/src/SignatureTooOldException.php @@ -2,7 +2,7 @@ namespace EduVpn\Common; -/** Signature has a timestamp lower than the specified minimum signing time. */ +/** Signature timestamp smaller than specified minimum signing time (rollback). */ final class SignatureTooOldException extends VerifyException { public function __construct() diff --git a/wrappers/php/tests/DiscoveryTest.php b/wrappers/php/tests/DiscoveryTest.php index 25ac187..fdce505 100644 --- a/wrappers/php/tests/DiscoveryTest.php +++ b/wrappers/php/tests/DiscoveryTest.php @@ -13,31 +13,31 @@ class DiscoveryTest extends TestCase public static function setUpBeforeClass(): void { - preg_match('/[\r\n](\S+)\s*/', file_get_contents(self::TEST_DATA_DIR . '/dummy/public.key'), $matches); + preg_match('/[\r\n](\S+)\s*/', file_get_contents(self::TEST_DATA_DIR . '/public.key'), $matches); Discovery::insecureTestingSetExtraKey($matches[1]); } public function testValid(): void { $this->expectNotToPerformAssertions(); - Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json.minisig'), - file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json'), + Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.minisig'), + file_get_contents(self::TEST_DATA_DIR . '/server_list.json'), 'server_list.json', 0); } public function testInvalidSignature(): void { $this->expectException(InvalidSignatureException::class); - Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/dummy/random.txt'), - file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json'), + Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/random.txt'), + file_get_contents(self::TEST_DATA_DIR . '/server_list.json'), 'server_list.json', 0); } public function testWrongKey(): void { $this->expectException(InvalidSignatureUnknownKeyException::class); - Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json.wrong_key.minisig'), - file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json'), + Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.wrong_key.minisig'), + file_get_contents(self::TEST_DATA_DIR . '/server_list.json'), 'server_list.json', 0); } @@ -45,16 +45,16 @@ class DiscoveryTest extends TestCase public function testOldSignature(): void { $this->expectException(SignatureTooOldException::class); - Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json.minisig'), - file_get_contents(self::TEST_DATA_DIR . '/dummy/server_list.json'), + Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/server_list.json.minisig'), + file_get_contents(self::TEST_DATA_DIR . '/server_list.json'), 'server_list.json', 1 << 31); } public function testUnknownExpectedFileName(): void { $this->expectException(InvalidArgumentException::class); - Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/dummy/other_list.json.minisig'), - file_get_contents(self::TEST_DATA_DIR . '/dummy/other_list.json'), + Discovery::verify(file_get_contents(self::TEST_DATA_DIR . '/other_list.json.minisig'), + file_get_contents(self::TEST_DATA_DIR . '/other_list.json'), 'other_list.json', 0); } } -- cgit v1.2.3