[glib/glib-2-68: 1/4] tests/tls-certificate: Add a unit test




commit 955d376b85c96b353dd28f63e0fc2661f4ecf2f3
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu Jun 3 16:24:47 2021 -0400

    tests/tls-certificate: Add a unit test
    
    Test whether g_tls_certificate_new_from_pem() can handle a
    non-nul-terminated PEM.
    
    (Fixed minor conflict when cherry-picking.)
    
    Helps https://gitlab.gnome.org/GNOME/glib/-/issues/2416

 gio/tests/tls-certificate.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
---
diff --git a/gio/tests/tls-certificate.c b/gio/tests/tls-certificate.c
index c0fc80c4b..ac046d15d 100644
--- a/gio/tests/tls-certificate.c
+++ b/gio/tests/tls-certificate.c
@@ -200,6 +200,34 @@ pem_parser_handles_chain (const Reference *ref)
   g_object_unref (original_cert);
 }
 
+static void
+pem_parser_no_sentinel (void)
+{
+  GTlsCertificate *cert;
+  gchar *pem;
+  gsize pem_len = 0;
+  gchar *pem_copy;
+  GError *error = NULL;
+
+  /* Check certificate from not-nul-terminated PEM */
+  g_file_get_contents (g_test_get_filename (G_TEST_DIST, "cert-tests", "cert1.pem", NULL), &pem, &pem_len, 
&error);
+  g_assert_no_error (error);
+  g_assert_nonnull (pem);
+  g_assert_cmpuint (pem_len, >=, 10);
+
+  pem_copy = g_new (char, pem_len);
+  /* Do not copy the terminating nul: */
+  memmove (pem_copy, pem, pem_len);
+  g_free (pem);
+
+  cert = g_tls_certificate_new_from_pem (pem_copy, pem_len, &error);
+  g_assert_no_error (error);
+  g_assert_nonnull (cert);
+
+  g_free (pem_copy);
+  g_object_unref (cert);
+}
+
 static void
 from_file (const Reference *ref)
 {
@@ -500,6 +528,8 @@ main (int   argc,
                    from_pkcs11_uri);
   g_test_add_func ("/tls-certificate/pkcs11-uri-unsupported",
                    from_unsupported_pkcs11_uri);
+  g_test_add_func ("/tls-certificate/pem-parser-no-sentinel",
+                   pem_parser_no_sentinel);
 
   rtv = g_test_run();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]