[PATCH v2] unit: Skip tests that rely on PKCS#8 when parser is missing
Bastien Nocera <[email protected]> Thu, 19 Feb 2026 11:06:48 +0100
| Newsgroups | dev.linux.lists.ell |
|---|---|
| Message-ID | <[email protected]> |
Add pre-check to tests that need the pkcs8_key_parser module loaded
(whether built-in or external), and allow tests that rely on it to fail.
This makes it possible to run the test suite successfully even on
systems where we might not be able to load modules if they're missing
such as CIs, or other systems where we lack permissions.
---
Changes since v1:
- Use precheck test mechanism instead of flags
unit/test-pem.c | 154 +++++++++++++++++++++++++++++++++---------------
1 file changed, 105 insertions(+), 49 deletions(-)
diff --git a/unit/test-pem.c b/unit/test-pem.c
index 7ee0597ed172..306e3a4f8656 100644
--- a/unit/test-pem.c
+++ b/unit/test-pem.c
@@ -10,6 +10,7 @@
#endif
#include <assert.h>
+#include <sys/stat.h>
#include <ell/ell.h>
@@ -136,6 +137,18 @@ static const struct pem_from_data_test single_line_cert_chain = {
"-----END CERTIFICATE-----\n",
};
+static bool pkcs8_key_parser_precheck(const void *data)
+{
+ struct stat s;
+
+ /* Despite the path, this directory exists whether the module
+ * is external or built-in. */
+ if (stat ("/sys/module/pkcs8_key_parser", &s) != 0)
+ return false;
+
+ return S_ISDIR (s.st_mode);
+}
+
static void destroy_cert(void *cert)
{
l_cert_free(cert);
@@ -394,7 +407,8 @@ int main(int argc, char *argv[])
l_test_add("pem/empty label", test_pem, &empty_label);
l_test_add("pem/cert chain from data", test_chain_from_data,
&single_line_cert_chain);
- l_test_add("pem/private key from data", test_priv_key_from_data, NULL);
+ l_test_add_func_precheck("pem/private key from data",
+ test_priv_key_from_data, pkcs8_key_parser_precheck, 0);
if (!l_checksum_is_supported(L_CHECKSUM_MD5, false) ||
!l_checksum_is_supported(L_CHECKSUM_SHA1, false) ||
@@ -402,74 +416,105 @@ int main(int argc, char *argv[])
!l_key_is_supported(L_KEY_FEATURE_CRYPTO))
goto done;
- l_test_add("pem/PKCS#1 vs. PKCS#8 unenecrypted Private Key",
- test_unencrypted_pkey, NULL);
+ l_test_add_func_precheck("pem/PKCS#1 vs. PKCS#8 unenecrypted Private Key",
+ test_unencrypted_pkey, pkcs8_key_parser_precheck, 0);
- l_test_add("pem/v1 MD5AndDES encrypted Private Key",
+ l_test_add_data_func_precheck("pem/v1 MD5AndDES encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-md5-des.pem",
+ test_encrypted_pkey,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pem/v1 SHA1AndDES encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-sha1-des.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs8-md5-des.pem");
- l_test_add("pem/v1 SHA1AndDES encrypted Private Key",
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pem/v2 DES encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-v2-des.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs8-sha1-des.pem");
- l_test_add("pem/v2 DES encrypted Private Key", test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs8-v2-des.pem");
+ pkcs8_key_parser_precheck,
+ 0);
if (l_cipher_is_supported(L_CIPHER_DES3_EDE_CBC) &&
l_checksum_is_supported(L_CHECKSUM_SHA224, false)) {
- l_test_add("pem/v2 DES EDE3 encrypted Private Key",
- test_encrypted_pkey, CERTDIR
- "cert-client-key-pkcs8-v2-des-ede3.pem");
+ l_test_add_data_func_precheck("pem/v2 DES EDE3 encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-v2-des-ede3.pem",
+ test_encrypted_pkey,
+ pkcs8_key_parser_precheck,
+ 0);
}
if (l_cipher_is_supported(L_CIPHER_AES)) {
if (l_checksum_is_supported(L_CHECKSUM_SHA256, false))
- l_test_add("pem/v2 AES128-encrypted Private Key",
+ l_test_add_data_func_precheck("pem/v2 AES128-encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-v2-aes128.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs8-v2-aes128.pem");
+ pkcs8_key_parser_precheck,
+ 0);
if (l_checksum_is_supported(L_CHECKSUM_SHA512, false))
- l_test_add("pem/v2 AES256-encrypted Private Key",
+ l_test_add_data_func_precheck("pem/v2 AES256-encrypted Private Key",
+ CERTDIR "cert-client-key-pkcs8-v2-aes256.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs8-v2-aes256.pem");
+ pkcs8_key_parser_precheck,
+ 0);
}
- l_test_add("pem/PKCS#1 DES-encrypted RSA Private Key",
+ l_test_add_data_func_precheck("pem/PKCS#1 DES-encrypted RSA Private Key",
+ CERTDIR "cert-client-key-pkcs1-des.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs1-des.pem");
+ pkcs8_key_parser_precheck,
+ 0);
if (l_cipher_is_supported(L_CIPHER_DES3_EDE_CBC))
- l_test_add("pem/PKCS#1 DES-EDE3-encrypted RSA Private Key",
+ l_test_add_data_func_precheck("pem/PKCS#1 DES-EDE3-encrypted RSA Private Key",
+ CERTDIR "cert-client-key-pkcs1-des3.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs1-des3.pem");
+ pkcs8_key_parser_precheck,
+ 0);
if (l_cipher_is_supported(L_CIPHER_AES_CBC)) {
- l_test_add("pem/PKCS#1 AES128-encrypted RSA Private Key",
+ l_test_add_data_func_precheck("pem/PKCS#1 AES128-encrypted RSA Private Key",
+ CERTDIR "cert-client-key-pkcs1-aes128.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs1-aes128.pem");
- l_test_add("pem/PKCS#1 AES192-encrypted RSA Private Key",
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pem/PKCS#1 AES192-encrypted RSA Private Key",
+ CERTDIR "cert-client-key-pkcs1-aes192.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs1-aes192.pem");
- l_test_add("pem/PKCS#1 AES256-encrypted RSA Private Key",
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pem/PKCS#1 AES256-encrypted RSA Private Key",
+ CERTDIR "cert-client-key-pkcs1-aes256.pem",
test_encrypted_pkey,
- CERTDIR "cert-client-key-pkcs1-aes256.pem");
+ pkcs8_key_parser_precheck,
+ 0);
}
- l_test_add("detect-format/PEM PKCS#1 unencrypted private key",
- test_load_file,
+ l_test_add_data_func_precheck("detect-format/PEM PKCS#1 unencrypted private key",
TEST_LOAD_PARAMS("cert-client-key-pkcs1.pem",
- false, false, true, false));
- l_test_add("detect-format/PEM PKCS#1 encrypted private key",
+ false, false, true, false),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("detect-format/PEM PKCS#1 encrypted private key",
TEST_LOAD_PARAMS("cert-client-key-pkcs1-des.pem",
- false, false, true, true));
- l_test_add("detect-format/PEM PKCS#8 unencrypted private key",
+ false, false, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("detect-format/PEM PKCS#8 unencrypted private key",
TEST_LOAD_PARAMS("cert-client-key-pkcs8.pem",
- false, false, true, false));
- l_test_add("detect-format/PEM PKCS#8 encrypted private key",
+ false, false, true, false),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("detect-format/PEM PKCS#8 encrypted private key",
TEST_LOAD_PARAMS("cert-client-key-pkcs8-sha1-des.pem",
- false, false, true, true));
+ false, false, true, true),
+ test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
l_test_add("detect-format/PEM X.509 certificate",
test_load_file,
TEST_LOAD_PARAMS("cert-client.pem",
@@ -478,31 +523,42 @@ int main(int argc, char *argv[])
test_load_file,
TEST_LOAD_PARAMS("cert-client.crt",
true, false, false, false));
- l_test_add("detect-format/PEM combined",
- test_load_file,
+ l_test_add_data_func_precheck("detect-format/PEM combined",
TEST_LOAD_PARAMS("cert-entity-combined.pem",
- true, true, true, true));
- l_test_add("detect-format/DER PKCS#12 combined",
+ true, true, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("detect-format/DER PKCS#12 combined",
TEST_LOAD_PARAMS("cert-entity-pkcs12-nomac.p12",
- true, false, true, true));
-
- l_test_add("pkcs#12/Combined RC2-based ciphers + SHA1",
+ true, false, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pkcs#12/Combined RC2-based ciphers + SHA1",
TEST_LOAD_PARAMS("cert-entity-pkcs12-rc2-sha1.p12",
- true, true, true, true));
- l_test_add("pkcs#12/Combined DES-based ciphers + SHA256",
+ true, true, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pkcs#12/Combined DES-based ciphers + SHA256",
TEST_LOAD_PARAMS("cert-entity-pkcs12-des-sha256.p12",
- true, true, true, true));
- l_test_add("pkcs#12/Combined RC4-based ciphers + SHA384",
+ true, true, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pkcs#12/Combined RC4-based ciphers + SHA384",
TEST_LOAD_PARAMS("cert-entity-pkcs12-rc4-sha384.p12",
- true, true, true, true));
- l_test_add("pkcs#12/Combined PKCS#5 ciphers + SHA512",
+ true, true, true, true),
test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
+ l_test_add_data_func_precheck("pkcs#12/Combined PKCS#5 ciphers + SHA512",
TEST_LOAD_PARAMS("cert-entity-pkcs12-pkcs5-sha512.p12",
- true, true, true, true));
+ true, true, true, true),
+ test_load_file,
+ pkcs8_key_parser_precheck,
+ 0);
done:
return l_test_run();
--
2.53.0