From 1bbb18c3992fff7f37bec2ca4685cbacfe5dc080 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 7 May 2024 18:56:50 +0900 Subject: [PATCH] Add function to check run-time version of the library This adds p11_kit_check_version function, which can be used to check the run-time version of p11-kit. Signed-off-by: Daiki Ueno --- Makefile.am | 1 + p11-kit/test-version.c | 16 ++++++++++++++-- p11-kit/util.c | 20 ++++++++++++++++++++ p11-kit/version.h.in | 4 ++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index dc14913c6..a6491af26 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,6 +4,7 @@ NULL = AM_CPPFLAGS = \ -I$(top_srcdir) \ -I$(top_srcdir)/common \ + -I$(top_builddir) \ -I$(top_builddir)/common \ -DBINDIR=\"$(bindir)\" \ -DBUILDDIR=\"$(abs_builddir)\" \ diff --git a/p11-kit/test-version.c b/p11-kit/test-version.c index 010793276..0488f54ff 100644 --- a/p11-kit/test-version.c +++ b/p11-kit/test-version.c @@ -39,7 +39,7 @@ #include "test.h" static void -test_check (void) +test_check_compile_time (void) { #if !P11_KIT_CHECK_VERSION (P11_KIT_VERSION_MAJOR, P11_KIT_VERSION_MINOR, P11_KIT_VERSION_MICRO) assert_not_reached (); @@ -66,13 +66,25 @@ test_check (void) #endif } +static void +test_check_run_time (void) +{ + assert_num_eq (CKR_OK, p11_kit_check_version (P11_KIT_VERSION_MAJOR, P11_KIT_VERSION_MINOR, P11_KIT_VERSION_MICRO)); + assert_num_eq (CKR_OK, p11_kit_check_version (P11_KIT_VERSION_MAJOR, P11_KIT_VERSION_MINOR, 0)); + assert_num_eq (CKR_OK, p11_kit_check_version (P11_KIT_VERSION_MAJOR, 0, 0)); + assert_num_eq (CKR_ARGUMENTS_BAD, p11_kit_check_version (P11_KIT_VERSION_MAJOR + 1, P11_KIT_VERSION_MINOR, P11_KIT_VERSION_MICRO)); + assert_num_eq (CKR_ARGUMENTS_BAD, p11_kit_check_version (P11_KIT_VERSION_MAJOR, P11_KIT_VERSION_MINOR + 1, P11_KIT_VERSION_MICRO)); + assert_num_eq (CKR_ARGUMENTS_BAD, p11_kit_check_version (P11_KIT_VERSION_MAJOR, P11_KIT_VERSION_MINOR, P11_KIT_VERSION_MICRO + 1)); +} + int main (int argc, char *argv[]) { p11_library_init (); - p11_test (test_check, "/version/test_check"); + p11_test (test_check_compile_time, "/version/test_check_compile_time"); + p11_test (test_check_run_time, "/version/test_check_run_time"); return p11_test_run (argc, argv); } diff --git a/p11-kit/util.c b/p11-kit/util.c index 1e21f80a5..1e9a3bac3 100644 --- a/p11-kit/util.c +++ b/p11-kit/util.c @@ -44,6 +44,7 @@ #include "message.h" #include "p11-kit.h" #include "private.h" +#include #include #include @@ -236,3 +237,22 @@ _p11_get_progname_unlocked (void) return NULL; return p11_my_progname; } + +/** + * p11_kit_check_version: + * @major: major version + * @minor: minor version + * @micro: micro version + * + * Check if the run-time version of p11-kit meets the requirement + * given by a triple: @major, @minor, and @micro. + * + * Returns: %CKR_OK if the version requirement meets; %CKR_ARGUMENTS_BAD otherwise. + * Since: 0.25.4 + */ +CK_RV +p11_kit_check_version (int major, int minor, int micro) +{ + return P11_KIT_CHECK_VERSION (major, minor, micro) ? + CKR_OK : CKR_ARGUMENTS_BAD; +} diff --git a/p11-kit/version.h.in b/p11-kit/version.h.in index ee25f8bcc..c134f1101 100644 --- a/p11-kit/version.h.in +++ b/p11-kit/version.h.in @@ -35,6 +35,8 @@ #ifndef P11_KIT_VERSION_H #define P11_KIT_VERSION_H +#include "p11-kit/pkcs11.h" + #ifdef __cplusplus extern "C" { #endif @@ -51,6 +53,8 @@ extern "C" { P11_KIT_VERSION_MINOR == (minor) && \ P11_KIT_VERSION_MICRO >= (micro))) +CK_RV p11_kit_check_version (int major, int minor, int micro); + #ifdef __cplusplus } /* extern "C" */ #endif