]> git.ipfire.org Git - pakfire.git/commitdiff
tests: Add tests for JWT
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 07:40:17 +0000 (07:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 07:40:17 +0000 (07:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/libpakfire/jwt.c [new file with mode: 0644]

index 0c25b35fddaf8cf2c52af73b9b5ff3bf4e4009fa..2901844708c81cab3ad31d388016335b3bc83068 100644 (file)
@@ -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
index ceea71da8a452ccffd65931a10b6925c9d3cf502..af3aa3965d0dc76c1182efd82881c0b9a86a1130 100644 (file)
@@ -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 (file)
index 0000000..7cd8d88
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+// json-c
+#include <json.h>
+
+#include <pakfire/jwt.h>
+#include <pakfire/util.h>
+
+#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);
+}