2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Add unit test for parse_host_port and make an empty port an error.

This commit is contained in:
Todd C. Miller 2019-11-05 06:04:13 -07:00
parent 2949d2c47b
commit 98ce89ee80
4 changed files with 149 additions and 1 deletions

View File

@ -156,6 +156,7 @@ lib/util/regress/getgrouplist/getgrouplist_test.c
lib/util/regress/glob/files
lib/util/regress/glob/globtest.c
lib/util/regress/glob/globtest.in
lib/util/regress/host_port/host_port_test.c
lib/util/regress/mktemp/mktemp_test.c
lib/util/regress/parse_gids/parse_gids_test.c
lib/util/regress/progname/progname_test.c

View File

@ -100,7 +100,7 @@ PVS_LOG_OPTS = -a 'GA:1,2' -e -t errorfile -d $(PVS_IGNORE)
# Regression tests
TEST_PROGS = conf_test hltq_test parseln_test progname_test strsplit_test \
strtobool_test strtoid_test strtomode_test strtonum_test \
parse_gids_test getgrouplist_test @COMPAT_TEST_PROGS@
parse_gids_test getgrouplist_test host_port_test @COMPAT_TEST_PROGS@
TEST_LIBS = @LIBS@
TEST_LDFLAGS = @LDFLAGS@
@ -157,6 +157,8 @@ PARSE_GIDS_TEST_OBJS = parse_gids_test.lo gidlist.lo
GETGROUPLIST_TEST_OBJS = getgrouplist_test.lo getgrouplist.lo
HOST_PORT_TEST_OBJS = host_port_test.lo host_port.lo
STRSIG_TEST_OBJS = strsig_test.lo sig2str.lo str2sig.lo @SIGNAME@
VSYSLOG_TEST_OBJS = vsyslog_test.lo vsyslog.lo
@ -253,6 +255,9 @@ parse_gids_test: $(PARSE_GIDS_TEST_OBJS) libsudo_util.la
getgrouplist_test: $(GETGROUPLIST_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(GETGROUPLIST_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
host_port_test: $(HOST_PORT_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(HOST_PORT_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
strsplit_test: $(STRSPLIT_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(STRSPLIT_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
@ -342,6 +347,7 @@ check: $(TEST_PROGS)
./strsig_test || rval=`expr $$rval + $$?`; \
fi; \
./getgrouplist_test || rval=`expr $$rval + $$?`; \
./host_port_test || rval=`expr $$rval + $$?`; \
./strtobool_test || rval=`expr $$rval + $$?`; \
./strtoid_test || rval=`expr $$rval + $$?`; \
./strtomode_test || rval=`expr $$rval + $$?`; \
@ -756,6 +762,18 @@ host_port.i: $(srcdir)/host_port.c $(incdir)/compat/stdbool.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
host_port.plog: host_port.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/host_port.c --i-file $< --output-file $@
host_port_test.lo: $(srcdir)/regress/host_port/host_port_test.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/regress/host_port/host_port_test.c
host_port_test.i: $(srcdir)/regress/host_port/host_port_test.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
host_port_test.plog: host_port_test.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/host_port/host_port_test.c --i-file $< --output-file $@
inet_pton.lo: $(srcdir)/inet_pton.c $(incdir)/sudo_compat.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/inet_pton.c

View File

@ -71,6 +71,8 @@ sudo_parse_host_port_v1(char *str, char **hostp, char **portp, char *defport)
if (port == NULL)
port = defport;
else if (*port == '\0')
goto done;
*hostp = host;
*portp = port;

View File

@ -0,0 +1,127 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif
#include "sudo_compat.h"
#include "sudo_fatal.h"
#include "sudo_util.h"
__dso_public int main(int argc, char *argv[]);
/*
* Test that sudo_parse_host_port() works as expected.
*/
struct host_port_test {
const char *str; /* input string */
const char *host; /* parsed host */
const char *port; /* parsed port */
char *defport; /* default port */
bool ret; /* return value */
};
static struct host_port_test test_data[] = {
{ "xerxes", "xerxes", "12345", "12345", true },
{ "xerxes:12345", "xerxes", "12345", "67890", true },
{ "127.0.0.1", "127.0.0.1", "12345", "12345", true },
{ "127.0.0.1:12345", "127.0.0.1", "12345", "67890", true },
{ "[::1]", "::1", "12345", "12345", true },
{ "[::1]:12345", "::1", "12345", "67890", true },
{ "xerxes:", NULL, NULL, "12345", false }, /* missing port */
{ "127.0.0.1:", NULL, NULL, "12345", false }, /* missing port */
{ "[::1:12345", NULL, NULL, "67890", false }, /* missing bracket */
{ "[::1]:", NULL, NULL, "12345", false }, /* missing port */
{ NULL }
};
int
main(int argc, char *argv[])
{
int i, errors = 0, ntests = 0;
char *host, *port, *copy = NULL;
bool ret;
initprogname(argc > 0 ? argv[0] : "host_port_test");
for (i = 0; test_data[i].str != NULL; i++) {
host = port = NULL;
free(copy);
if ((copy = strdup(test_data[i].str)) == NULL)
sudo_fatal_nodebug(NULL);
ntests++;
ret = sudo_parse_host_port(copy, &host, &port, test_data[i].defport);
if (ret != test_data[i].ret) {
sudo_warnx_nodebug("test #%d: %s: returned %s, expected %s",
ntests, test_data[i].str, ret ? "true" : "false",
test_data[i].ret ? "true" : "false");
errors++;
continue;
}
if (!ret)
continue;
if (host == NULL) {
sudo_warnx_nodebug("test #%d: %s: NULL host",
ntests, test_data[i].str);
errors++;
continue;
}
if (strcmp(host, test_data[i].host) != 0) {
sudo_warnx_nodebug("test #%d: %s: bad host, expected %s, got %s",
ntests, test_data[i].str, test_data[i].host, host);
errors++;
continue;
}
if (port == NULL) {
sudo_warnx_nodebug("test #%d: %s: NULL port",
ntests, test_data[i].str);
errors++;
continue;
}
if (strcmp(port, test_data[i].port) != 0) {
sudo_warnx_nodebug("test #%d: %s: bad port, expected %s, got %s",
ntests, test_data[i].str, test_data[i].port, port);
errors++;
continue;
}
}
free(copy);
if (ntests != 0) {
printf("%s: %d tests run, %d errors, %d%% success rate\n",
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
}
exit(errors);
}