diff options
author | Tim Rühsen <tim.ruehsen@gmx.de> | 2019-10-13 12:04:20 +0200 |
---|---|---|
committer | Tim Rühsen <tim.ruehsen@gmx.de> | 2019-10-15 14:51:37 +0200 |
commit | d6ee45fe1cf6715bb728e21bd7ac6c0e9d33d2a8 (patch) | |
tree | 033a70c15916dace1c6bf085ae7497de97a31f3f /tests | |
parent | 21883fcb90dd35a2c4d8e8a005802667615fd345 (diff) | |
download | gnutls-d6ee45fe1cf6715bb728e21bd7ac6c0e9d33d2a8.tar.gz |
tests/buffer.c: Add unit test for _gnutls_buffer_unescape()
Signed-off-by: Tim Rühsen <tim.ruehsen@gmx.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 6 | ||||
-rw-r--r-- | tests/buffer.c | 78 | ||||
-rw-r--r-- | tests/utils.h | 3 |
3 files changed, 86 insertions, 1 deletions
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 <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); + } +} + 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 |