upstream: unit test for xextendf()

OpenBSD-Regress-ID: ddb3b4db1a52dda23696b967470882fe2b9c3af7
This commit is contained in:
djm@openbsd.org
2025-09-02 11:04:58 +00:00
committed by Damien Miller
parent 2f369d3fd0
commit 8866d24cdd
4 changed files with 96 additions and 3 deletions

View File

@@ -695,7 +695,8 @@ UNITTESTS_TEST_MISC_OBJS=\
regress/unittests/misc/test_argv.o \
regress/unittests/misc/test_strdelim.o \
regress/unittests/misc/test_hpdelim.o \
regress/unittests/misc/test_ptimeout.o
regress/unittests/misc/test_ptimeout.o \
regress/unittests/misc/test_xextendf.o
regress/unittests/misc/test_misc$(EXEEXT): \
${UNITTESTS_TEST_MISC_OBJS} \

View File

@@ -1,4 +1,4 @@
# $OpenBSD: Makefile,v 1.10 2025/04/15 04:00:42 djm Exp $
# $OpenBSD: Makefile,v 1.11 2025/09/02 11:04:58 djm Exp $
PROG=test_misc
SRCS=tests.c
@@ -9,6 +9,7 @@ SRCS+= test_argv.c
SRCS+= test_strdelim.c
SRCS+= test_hpdelim.c
SRCS+= test_ptimeout.c
SRCS+= test_xextendf.c
# From usr.bin/ssh/Makefile.inc
SRCS+= sshbuf.c

View File

@@ -0,0 +1,89 @@
/* $OpenBSD: test_xextendf.c,v 1.1 2025/09/02 11:04:58 djm Exp $ */
/*
* Regress test for misc xextendf() function.
*
* Placed in the public domain.
*/
#include <sys/types.h>
#include <stdio.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "../test_helper/test_helper.h"
#include "log.h"
#include "misc.h"
#include "xmalloc.h"
void test_xextendf(void);
void
test_xextendf(void)
{
char *s = NULL;
TEST_START("xextendf NULL string");
xextendf(&s, ",", "hello");
ASSERT_STRING_EQ(s, "hello");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf empty string");
s = xstrdup("");
xextendf(&s, ",", "world");
ASSERT_STRING_EQ(s, "world");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf append to string");
s = xstrdup("foo");
xextendf(&s, ",", "bar");
ASSERT_STRING_EQ(s, "foo,bar");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf append with NULL separator");
s = xstrdup("foo");
xextendf(&s, NULL, "bar");
ASSERT_STRING_EQ(s, "foobar");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf append with empty separator");
s = xstrdup("foo");
xextendf(&s, "", "bar");
ASSERT_STRING_EQ(s, "foobar");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf with format string");
s = xstrdup("start");
xextendf(&s, ":", "s=%s,d=%d", "string", 123);
ASSERT_STRING_EQ(s, "start:s=string,d=123");
free(s);
s = NULL;
TEST_DONE();
TEST_START("xextendf multiple appends");
s = NULL;
xextendf(&s, ",", "one");
ASSERT_STRING_EQ(s, "one");
xextendf(&s, ",", "two");
ASSERT_STRING_EQ(s, "one,two");
xextendf(&s, ":", "three=%d", 3);
ASSERT_STRING_EQ(s, "one,two:three=3");
xextendf(&s, NULL, "four");
ASSERT_STRING_EQ(s, "one,two:three=3four");
free(s);
s = NULL;
TEST_DONE();
}

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: tests.c,v 1.11 2025/04/15 04:00:42 djm Exp $ */
/* $OpenBSD: tests.c,v 1.12 2025/09/02 11:04:58 djm Exp $ */
/*
* Regress test for misc helper functions.
*
@@ -27,6 +27,7 @@ void test_argv(void);
void test_strdelim(void);
void test_hpdelim(void);
void test_ptimeout(void);
void test_xextendf(void);
void
tests(void)
@@ -38,6 +39,7 @@ tests(void)
test_strdelim();
test_hpdelim();
test_ptimeout();
test_xextendf();
}
void