]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: unit test for xextendf()
authordjm@openbsd.org <djm@openbsd.org>
Tue, 2 Sep 2025 11:04:58 +0000 (11:04 +0000)
committerDamien Miller <djm@mindrot.org>
Tue, 2 Sep 2025 11:06:39 +0000 (21:06 +1000)
OpenBSD-Regress-ID: ddb3b4db1a52dda23696b967470882fe2b9c3af7

Makefile.in
regress/unittests/misc/Makefile
regress/unittests/misc/test_xextendf.c [new file with mode: 0644]
regress/unittests/misc/tests.c

index 19a9e4dcf1df3d7cc6058ad6030ebe4c8712fa0e..9bfdc3fd3faf5bc9ca10e32ebfed3a47353e24fd 100644 (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} \
index 7282be13a66777cbe59349fd698c45a27410ce38..a897b4b458b6d3d2eac0c1a1ed9bdeaba62d1988 100644 (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
diff --git a/regress/unittests/misc/test_xextendf.c b/regress/unittests/misc/test_xextendf.c
new file mode 100644 (file)
index 0000000..f9562ad
--- /dev/null
@@ -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();
+}
index 7611a0d3b64575e2ed27c80266e916d3f614b49f..13588edaae1859da1ed7ac449afe704591ee3c13 100644 (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