From 6713740f7da69f5c07ce30fc94ad53b3c4337821 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 27 Jun 2025 07:40:17 +0000 Subject: [PATCH] tests: Add tests for JWT Signed-off-by: Michael Tremer --- .gitignore | 1 + Makefile.am | 16 +++++++++ tests/libpakfire/jwt.c | 75 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 tests/libpakfire/jwt.c diff --git a/.gitignore b/.gitignore index 0c25b35f..29018447 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ /tests/libpakfire/file /tests/libpakfire/httpclient /tests/libpakfire/jail +/tests/libpakfire/jwt /tests/libpakfire/key /tests/libpakfire/log_buffer /tests/libpakfire/log_file diff --git a/Makefile.am b/Makefile.am index ceea71da..af3aa396 100644 --- a/Makefile.am +++ b/Makefile.am @@ -606,6 +606,7 @@ check_PROGRAMS += \ tests/libpakfire/file \ tests/libpakfire/httpclient \ tests/libpakfire/jail \ + tests/libpakfire/jwt \ tests/libpakfire/key \ tests/libpakfire/log_buffer \ tests/libpakfire/log_file \ @@ -832,6 +833,21 @@ tests_libpakfire_jail_LDFLAGS = \ tests_libpakfire_jail_LDADD = \ $(TESTSUITE_LDADD) +dist_tests_libpakfire_jwt_SOURCES = \ + tests/libpakfire/jwt.c + +tests_libpakfire_jwt_CPPFLAGS = \ + $(TESTSUITE_CPPFLAGS) + +tests_libpakfire_jwt_CFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_jwt_LDFLAGS = \ + $(TESTSUITE_CFLAGS) + +tests_libpakfire_jwt_LDADD = \ + $(TESTSUITE_LDADD) + tests_libpakfire_key_SOURCES = \ tests/libpakfire/key.c diff --git a/tests/libpakfire/jwt.c b/tests/libpakfire/jwt.c new file mode 100644 index 00000000..7cd8d88b --- /dev/null +++ b/tests/libpakfire/jwt.c @@ -0,0 +1,75 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +// json-c +#include + +#include +#include + +#include "../testsuite.h" + +// This is a sample access token +const char* ACCESS_TOKEN = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + "eyJzdWIiOiJtc0BJUEZJUkUuT1JHIiwidHlwIjoiYWNjZXNzIiwiaWF0IjoxNzUxMDA5NTU3LCJleHAiOjE3NTEwMTMxNTd9." + "FaqgeZBdnYjgQZqV3mtu2oC5agJmkUPuSVFFGBG-yAY"; + +const char* INVALID_TOKEN = "THISISANINVALIDTOKEN"; + +static int test_payload(const struct test* t) { + struct json_object* payload = NULL; + int r = EXIT_FAILURE; + + // Try to decode an invalid token + ASSERT_ERROR(pakfire_jwt_payload(INVALID_TOKEN, &payload), EINVAL); + + // Decode the token + ASSERT_SUCCESS(pakfire_jwt_payload(ACCESS_TOKEN, &payload)); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (payload) + json_object_put(payload); + + return r; +} + +static int test_expiry(const struct test* t) { + int r = EXIT_FAILURE; + + // Extract the expiry time + ASSERT_EQUALS(pakfire_jwt_expires_at(ACCESS_TOKEN), 1751013157); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + return r; +} + +int main(int argc, const char* argv[]) { + testsuite_add_test(test_payload, 0); + testsuite_add_test(test_expiry, 0); + + return testsuite_run(argc, argv); +} -- 2.47.3