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




commit b9d07813d8cede0cb5dca091d49d6d4363c01f38
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.
    
    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 cfc349ea1..a705a4645 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)
 {
@@ -594,6 +622,8 @@ main (int   argc,
                    subject_name);
   g_test_add_func ("/tls-certificate/issuer-name",
                    issuer_name);
+  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]