public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
Subject: [PATCH common 2/3] tests: introduce tests for pem_to_der
Date: Mon,  9 Nov 2026 11:37:32 +0100	[thread overview]
Message-ID: <20261109103733.128704-3-t.ellmenreich@proxmox.com> (raw)
In-Reply-To: <20261109103733.128704-1-t.ellmenreich@proxmox.com>

To pin down the functionality of the pem_to_der subroutine, introduce a
small testsuite. Also reduces the risk to future changes in subroutines
used by the pem_to_der subroutine.

Signed-off-by: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
---
 test/Makefile           |   1 +
 test/pem_to_der_test.pl | 161 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 162 insertions(+)
 create mode 100755 test/pem_to_der_test.pl

diff --git a/test/Makefile b/test/Makefile
index 9caba10..f111511 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -19,6 +19,7 @@ TESTS = lock_file.test			\
 	ticket_test.test \
 	check_pem_test.test \
 	split_pem_test.test \
+	pem_to_der_test.test \
 
 all:
 
diff --git a/test/pem_to_der_test.pl b/test/pem_to_der_test.pl
new file mode 100755
index 0000000..c35e1f4
--- /dev/null
+++ b/test/pem_to_der_test.pl
@@ -0,0 +1,161 @@
+#!/usr/bin/perl
+# Tests the PVE::Certificate::pem_to_der function for
+# correctness and coverage of edgecases.
+use strict;
+use warnings;
+
+use lib '../src';
+
+use Test::More;
+
+use PVE::Certificate;
+
+# Arrange
+my $setup = [
+    {
+        name => "single pem",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Np
+bmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFi
+b3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVu
+aWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBu
+aXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0
+ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxp
+dCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBF
+eGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBz
+dW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlk
+IGVzdCBsYWJvcnVtLg==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result =>
+            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
+    },
+    {
+        name => "single pem no terminating newline",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0=
+-----END CERTIFICATE-----},
+        expected_decoded_result => "Lorem ipsum",
+    },
+    {
+        name => "multiple pem",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem leading newline",
+        pem => q{
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem leading text",
+        pem => q{
+some leading
+text that is expected
+to be stripped
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem interleaved text",
+        pem => q{
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+some end text
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem no newline after footer",
+        pem => q{
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE----------BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "all in one line",
+        pem => "-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=-----END CERTIFICATE-----",
+        expected_error => "header",
+    },
+    {
+        name => "only newline after begin",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0=-----END CERTIFICATE-----},
+        expected_error => "footer",
+    },
+    {
+        name => "only newline after content",
+        pem => q{-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=
+-----END CERTIFICATE-----},
+        expected_error => "header",
+    },
+    {
+        name => "only newline after end",
+        pem => q{-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=-----END CERTIFICATE-----
+},
+        expected_error => "header",
+    },
+];
+
+for my $data ($setup->@*) {
+    subtest $data->{name} => sub {
+        my $to_der_result = eval { PVE::Certificate::pem_to_der($data->{pem}) };
+        my $to_der_error = $@;
+        if (defined($data->{expected_decoded_result})) {
+            is($to_der_result, $data->{expected_decoded_result}, "has correct decoded result");
+        } else {
+            like(
+                $to_der_error,
+                qr/Could not convert PEM to DER, no $data->{expected_error} found./,
+                "has died with expected error",
+            );
+        }
+    }
+}
+
+done_testing();
-- 
2.47.3





  parent reply	other threads:[~2026-11-09 10:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 10:37 [PATCH common 0/3] pem parser: handle missing newline edge case Thomas Ellmenreich
2026-11-09 10:37 ` [PATCH common 1/3] pem to der: make header and footer parsing stricter Thomas Ellmenreich
2026-11-09 10:37 ` Thomas Ellmenreich [this message]
2026-11-09 10:37 ` [PATCH common 3/3] check pem: force at least one newline after header and footer Thomas Ellmenreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261109103733.128704-3-t.ellmenreich@proxmox.com \
    --to=t.ellmenreich@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal