mirror of
https://github.com/openssh/openssh-portable.git
synced 2026-01-12 00:04:08 +08:00
upstream: unit test for xextendf()
OpenBSD-Regress-ID: ddb3b4db1a52dda23696b967470882fe2b9c3af7
This commit is contained in:
committed by
Damien Miller
parent
2f369d3fd0
commit
8866d24cdd
@@ -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} \
|
||||
|
||||
@@ -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
|
||||
|
||||
89
regress/unittests/misc/test_xextendf.c
Normal file
89
regress/unittests/misc/test_xextendf.c
Normal 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();
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user