Skip to contentSkip to conversation

unit: Skip tests that rely on PKCS#8 when parser is missing

PATCH Superseded Bastien Nocera 2 participants 4 messages Feb 19

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 unit: Skip tests that rely on PKCS#8 when parser is missing

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(-)
unit/test-pem.c +105 -49
index 7ee0597ed172..306e3a4f8656 100644
@@ -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();
Signature · 1 line
2.53.0
#1 lore raw

Hi Bastien,

Quoted 32 lines

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(-)
unit/test-pem.c +3 -0
index 7ee0597ed172..306e3a4f8656 100644
@@ -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)
+{

unit/test-pem.c static const struct pem_from_data_test single_line_cert_chain = { · line 151

Quoted 10 lines
unit/test-pem.c +10 -0
+ 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);
+}
+

you don’t think doing it like this is better:

+static bool test_load_precheck(const void *data)
+{
+ return true;
+}
+
+#define add_file_test(name, data) l_test_add_data_func_precheck(name, data, \
+ test_load_file, test_load_precheck, 0);
+
int main(int argc, char *argv[])

 {
        l_test_init(&argc, &argv);
@@ -499,8 +507,7 @@ int main(int argc, char *argv[])
                        test_load_file,
                        TEST_LOAD_PARAMS("cert-entity-pkcs12-rc4-sha384.p12",
                                                true, true, true, true));
-       l_test_add("pkcs#12/Combined PKCS#5 ciphers + SHA512",
-                       test_load_file,
+       add_file_test("pkcs#12/Combined PKCS#5 ciphers + SHA512",
                        TEST_LOAD_PARAMS("cert-entity-pkcs12-pkcs5-sha512.p12",
                                                true, true, true, true));

I would most likely also push the TEST_LOAD_PARAMS into the add_file_test() macro, but seems a bit more work.

Regards

Marcel

#2 lore raw
Quoted 57 lines · Marcel Holtmann

Hi Bastien,

Quoted 36 lines

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(-)
unit/test-pem.c +3 -0
index 7ee0597ed172..306e3a4f8656 100644
@@ -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)
+{

unit/test-pem.c static const struct pem_from_data_test · line 152

Quoted 10 lines
unit/test-pem.c +10 -0
+ 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);
+}
+

you don’t think doing it like this is better:

unit/test-pem.c +6 -0
+static bool test_load_precheck(const void *data)
+{
+       return true;
+}
+
+#define add_file_test(name, data)
Quoted 3 lines

l_test_add_data_func_precheck(name, data, \
+                                       test_load_file, test_load_precheck, 0);

It's a lot more work to undo something I already did. The types of tests that are being run are also too heterogeneous to have one catch- all macro, and we'll end up with a level of indirection where readers try to figure out what macros do.

I don't find it more readable, but if you need this to land the patch, I can make a v3.

Quoted 24 lines

+
 int main(int argc, char *argv[])
 {
        l_test_init(&argc, &argv);

(quoted diff) +0 -0
@@ -499,8 +507,7 @@ int main(int argc, char *argv[])
                         test_load_file,

                        TEST_LOAD_PARAMS("cert-entity-pkcs12-rc4- sha384.p12",
                                                true, true, true, true));
-       l_test_add("pkcs#12/Combined PKCS#5 ciphers + SHA512", -                       test_load_file,
+       add_file_test("pkcs#12/Combined PKCS#5 ciphers + SHA512", TEST_LOAD_PARAMS("cert-entity-pkcs12-pkcs5- sha512.p12",
                                                true, true, true, true));

I would most likely also push the TEST_LOAD_PARAMS into the add_file_test() macro, but seems a bit more work.

Regards

Marcel

#3 lore raw
In reply to Marcel Holtmann
Quoted 78 lines · Marcel Holtmann

Hi Bastien,

Quoted 36 lines

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(-)
unit/test-pem.c +3 -0
index 7ee0597ed172..306e3a4f8656 100644
@@ -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)
+{

unit/test-pem.c static const struct pem_from_data_test · line 152

Quoted 10 lines
unit/test-pem.c +10 -0
+ 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);
+}
+

you don’t think doing it like this is better:

unit/test-pem.c +6 -0
+static bool test_load_precheck(const void *data)
+{
+       return true;
+}
+
+#define add_file_test(name, data)

l_test_add_data_func_precheck(name, data, \
+                                       test_load_file, test_load_precheck, 0);
+
 int main(int argc, char *argv[])
 {
        l_test_init(&argc, &argv);

(quoted diff) +0 -0
@@ -499,8 +507,7 @@ int main(int argc, char *argv[])
                         test_load_file,

                        TEST_LOAD_PARAMS("cert-entity-pkcs12-rc4- sha384.p12",
                                                true, true, true, true));
-       l_test_add("pkcs#12/Combined PKCS#5 ciphers + SHA512", -                       test_load_file,
+       add_file_test("pkcs#12/Combined PKCS#5 ciphers + SHA512", TEST_LOAD_PARAMS("cert-entity-pkcs12-pkcs5- sha512.p12",
                                                true, true, true, true));

Quoted 2 lines

I would most likely also push the TEST_LOAD_PARAMS into the add_file_test() macro, but seems a bit more work.

Did something similar in v3.

Cheers