From d6ee45fe1cf6715bb728e21bd7ac6c0e9d33d2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Sun, 13 Oct 2019 12:04:20 +0200 Subject: tests/buffer.c: Add unit test for _gnutls_buffer_unescape() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Rühsen --- lib/libgnutls.map | 4 +++ tests/Makefile.am | 6 ++++- tests/buffer.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/utils.h | 3 +++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/buffer.c diff --git a/lib/libgnutls.map b/lib/libgnutls.map index d6973f72ef..6e1da857f6 100644 --- a/lib/libgnutls.map +++ b/lib/libgnutls.map @@ -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; diff --git a/tests/Makefile.am b/tests/Makefile.am index c462b54e41..0f488867de 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000000..a6fb476ae0 --- /dev/null +++ b/tests/buffer.c @@ -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 +#endif + +#include +#include +#include + +#include +#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); + } +} + diff --git a/tests/utils.h b/tests/utils.h index e36e4f3b6b..61d6dc9f9e 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -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 -- cgit v1.2.1