]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tests/buffer.c: Add unit test for _gnutls_buffer_unescape()
authorTim Rühsen <tim.ruehsen@gmx.de>
Sun, 13 Oct 2019 10:04:20 +0000 (12:04 +0200)
committerTim Rühsen <tim.ruehsen@gmx.de>
Tue, 15 Oct 2019 12:51:37 +0000 (14:51 +0200)
Signed-off-by: Tim Rühsen <tim.ruehsen@gmx.de>
lib/libgnutls.map
tests/Makefile.am
tests/buffer.c [new file with mode: 0644]
tests/utils.h

index d6973f72ef9e3a8f8afd09c4eb348d7971ebac2d..6e1da857f60b2b46b5c8a0be207c39c4f56f7487 100644 (file)
@@ -1395,4 +1395,8 @@ GNUTLS_PRIVATE_3_4 {
        _gnutls_iov_iter_init;
        _gnutls_iov_iter_next;
        _gnutls_iov_iter_sync;
+       # needed by tests/buffer:
+       _gnutls_buffer_unescape;
+       _gnutls_buffer_pop_datum;
+       _gnutls_buffer_clear;
 } GNUTLS_3_4;
index c462b54e41c252f9f3ebb7d2c06f6f7c49955d67..0f488867de612eb9058e49cb6926b10f70d09156 100644 (file)
@@ -215,7 +215,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei
         resume-with-stek-expiration resume-with-previous-stek rawpk-api \
         tls-record-size-limit-asym dh-compute ecdh-compute sign-verify-data-newapi \
         sign-verify-newapi sign-verify-deterministic iov aead-cipher-vec \
-        tls13-without-timeout-func
+        tls13-without-timeout-func buffer
 
 if HAVE_SECCOMP_TESTS
 ctests += dtls-with-seccomp tls-with-seccomp dtls-client-with-seccomp tls-client-with-seccomp
@@ -465,6 +465,10 @@ iov_CPPFLAGS = $(AM_CPPFLAGS) \
        -I$(top_srcdir)/gl      \
        -I$(top_builddir)/gl
 
+buffer_CPPFLAGS = $(AM_CPPFLAGS) \
+       -I$(top_srcdir)/gl      \
+       -I$(top_builddir)/gl
+
 if ENABLE_PKCS11
 if !WINDOWS
 ctests += tls13/post-handshake-with-cert-pkcs11 pkcs11/tls-neg-pkcs11-no-key
diff --git a/tests/buffer.c b/tests/buffer.c
new file mode 100644 (file)
index 0000000..a6fb476
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2019 Tim Rühsen
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GnuTLS; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gnutls_int.h>
+#include "utils.h"
+
+void doit(void)
+{
+       static const struct test_data {
+               const char *
+                       input;
+               const char *
+                       output;
+       } test_data[] = {
+               { "%20%20", "  ", },
+               { "%20", " ", },
+               { "%2z", "%2z", },
+               { "%2", "%2", },
+               { "%", "%", },
+               { "", "", },
+       };
+
+       for (unsigned it = 0; it < countof(test_data); it++) {
+               const struct test_data *t = &test_data[it];
+               gnutls_buffer_st str;
+               int ret;
+
+               _gnutls_buffer_init(&str);
+
+               ret = _gnutls_buffer_append_data(&str, t->input, strlen(t->input));
+               if (ret < 0)
+                       fail("_gnutls_buffer_append_str: %s\n", gnutls_strerror(ret));
+
+               ret = _gnutls_buffer_unescape(&str);
+               if (ret < 0)
+                       fail("_gnutls_buffer_unescape: %s\n", gnutls_strerror(ret));
+
+               ret = _gnutls_buffer_append_data(&str, "", 1);
+               if (ret < 0)
+                       fail("_gnutls_buffer_append_data: %s\n", gnutls_strerror(ret));
+
+               /* using malloc() instead of stack memory for better buffer overflow detection */
+               gnutls_datum output;
+
+               _gnutls_buffer_pop_datum(&str, &output, strlen(t->output) + 1);
+
+               if (strcmp(t->output, (char *) output.data))
+                       fail("output differs [%d]: expected '%s', seen '%s'\n", it, t->output, (char *) output.data);
+
+               _gnutls_buffer_clear(&str);
+       }
+}
+
index e36e4f3b6b2b6e9b619af2ea0915606b735cb148..61d6dc9f9ed6bc415cdd3bb6f0cf85a295b9a43e 100644 (file)
@@ -49,6 +49,9 @@
 # define FALLTHROUGH
 #endif
 
+/* number of elements within an array */
+#define countof(a) (sizeof(a)/sizeof(*(a)))
+
 inline static int global_init(void)
 {
 #ifdef ENABLE_PKCS11