]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: unit tests for a bunch of misc.c functions; ok deraadt@
authordjm@openbsd.org <djm@openbsd.org>
Thu, 4 Sep 2025 00:34:17 +0000 (00:34 +0000)
committerDamien Miller <djm@mindrot.org>
Thu, 4 Sep 2025 03:06:19 +0000 (13:06 +1000)
OpenBSD-Regress-ID: 886cf142605405e777ee77a96b48694dc2e9235d

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

index 9bfdc3fd3faf5bc9ca10e32ebfed3a47353e24fd..769ec17f426bfc1388929afa6a16fb568cbe56fa 100644 (file)
@@ -696,7 +696,8 @@ UNITTESTS_TEST_MISC_OBJS=\
        regress/unittests/misc/test_strdelim.o \
        regress/unittests/misc/test_hpdelim.o \
        regress/unittests/misc/test_ptimeout.o \
-       regress/unittests/misc/test_xextendf.o
+       regress/unittests/misc/test_xextendf.o \
+       regress/unittests/misc/test_misc.o
 
 regress/unittests/misc/test_misc$(EXEEXT): \
     ${UNITTESTS_TEST_MISC_OBJS} \
index a897b4b458b6d3d2eac0c1a1ed9bdeaba62d1988..77575061d4561334911e397f68c94407511a97af 100644 (file)
@@ -1,7 +1,8 @@
-#      $OpenBSD: Makefile,v 1.11 2025/09/02 11:04:58 djm Exp $
+#      $OpenBSD: Makefile,v 1.12 2025/09/04 00:34:17 djm Exp $
 
 PROG=test_misc
 SRCS=tests.c
+SRCS+= test_misc.c
 SRCS+= test_convtime.c
 SRCS+= test_expand.c
 SRCS+= test_parse.c
diff --git a/regress/unittests/misc/test_misc.c b/regress/unittests/misc/test_misc.c
new file mode 100644 (file)
index 0000000..d175196
--- /dev/null
@@ -0,0 +1,436 @@
+/*
+ * Regress test for misc helper functions.
+ *
+ * Placed in the public domain.
+ */
+
+#include "includes.h"
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../test_helper/test_helper.h"
+
+#include "log.h"
+#include "misc.h"
+#include "xmalloc.h"
+
+void test_misc(void);
+
+static void
+test_chop(void)
+{
+       char *s;
+
+       TEST_START("chop newline");
+       s = xstrdup("hello\n");
+       ASSERT_STRING_EQ(chop(s), "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop carriage return");
+       s = xstrdup("hello\r");
+       ASSERT_STRING_EQ(chop(s), "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop CRLF");
+       s = xstrdup("hello\r\n");
+       ASSERT_STRING_EQ(chop(s), "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop newline in middle");
+       s = xstrdup("he\nllo");
+       ASSERT_STRING_EQ(chop(s), "he");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop no newline");
+       s = xstrdup("hello");
+       ASSERT_STRING_EQ(chop(s), "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop empty string");
+       s = xstrdup("");
+       ASSERT_STRING_EQ(chop(s), "");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop only newline");
+       s = xstrdup("\n");
+       ASSERT_STRING_EQ(chop(s), "");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop only CR");
+       s = xstrdup("\r");
+       ASSERT_STRING_EQ(chop(s), "");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("chop only CRLF");
+       s = xstrdup("\r\n");
+       ASSERT_STRING_EQ(chop(s), "");
+       free(s);
+       TEST_DONE();
+}
+
+static void
+test_rtrim(void)
+{
+       char *s;
+
+       TEST_START("rtrim trailing space");
+       s = xstrdup("hello  ");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim trailing tab");
+       s = xstrdup("hello\t\t");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim trailing mixed whitespace");
+       s = xstrdup("hello \t ");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim no trailing whitespace");
+       s = xstrdup("hello");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim whitespace in middle");
+       s = xstrdup("he llo");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "he llo");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim empty string");
+       s = xstrdup("");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("rtrim only whitespace");
+       s = xstrdup("   \t");
+       rtrim(s);
+       ASSERT_STRING_EQ(s, "");
+       free(s);
+       TEST_DONE();
+}
+
+static void
+test_strprefix(void)
+{
+       const char *s;
+
+       TEST_START("strprefix basic match");
+       s = strprefix("hello world", "hello", 0);
+       ASSERT_PTR_NE(s, NULL);
+       ASSERT_STRING_EQ(s, " world");
+       TEST_DONE();
+
+       TEST_START("strprefix no match");
+       s = strprefix("hello world", "world", 0);
+       ASSERT_PTR_EQ(s, NULL);
+       TEST_DONE();
+
+       TEST_START("strprefix full match");
+       s = strprefix("hello", "hello", 0);
+       ASSERT_PTR_NE(s, NULL);
+       ASSERT_STRING_EQ(s, "");
+       TEST_DONE();
+
+       TEST_START("strprefix empty string");
+       s = strprefix("", "hello", 0);
+       ASSERT_PTR_EQ(s, NULL);
+       TEST_DONE();
+
+       TEST_START("strprefix empty prefix");
+       s = strprefix("hello", "", 0);
+       ASSERT_PTR_NE(s, NULL);
+       ASSERT_STRING_EQ(s, "hello");
+       TEST_DONE();
+
+       TEST_START("strprefix case sensitive no match");
+       s = strprefix("Hello world", "hello", 0);
+       ASSERT_PTR_EQ(s, NULL);
+       TEST_DONE();
+
+       TEST_START("strprefix case insensitive match");
+       s = strprefix("Hello world", "hello", 1);
+       ASSERT_PTR_NE(s, NULL);
+       ASSERT_STRING_EQ(s, " world");
+       TEST_DONE();
+
+       TEST_START("strprefix case insensitive full match");
+       s = strprefix("HELLO", "hello", 1);
+       ASSERT_PTR_NE(s, NULL);
+       ASSERT_STRING_EQ(s, "");
+       TEST_DONE();
+}
+
+static void
+test_fmt_timeframe(void)
+{
+       TEST_START("fmt_timeframe seconds");
+       ASSERT_STRING_EQ(fmt_timeframe(0), "00:00:00");
+       ASSERT_STRING_EQ(fmt_timeframe(59), "00:00:59");
+       ASSERT_STRING_EQ(fmt_timeframe(60), "00:01:00");
+       ASSERT_STRING_EQ(fmt_timeframe(3599), "00:59:59");
+       ASSERT_STRING_EQ(fmt_timeframe(3600), "01:00:00");
+       ASSERT_STRING_EQ(fmt_timeframe(86399), "23:59:59");
+       TEST_DONE();
+
+       TEST_START("fmt_timeframe days");
+       ASSERT_STRING_EQ(fmt_timeframe(86400), "1d00h00m");
+       ASSERT_STRING_EQ(fmt_timeframe(90061), "1d01h01m");
+       ASSERT_STRING_EQ(fmt_timeframe(604799), "6d23h59m");
+       TEST_DONE();
+
+       TEST_START("fmt_timeframe weeks");
+       ASSERT_STRING_EQ(fmt_timeframe(604800), "01w0d00h");
+       ASSERT_STRING_EQ(fmt_timeframe(694861), "01w1d01h");
+       TEST_DONE();
+}
+
+static void
+test_arglist(void)
+{
+       arglist args;
+       u_int i;
+
+       memset(&args, 0, sizeof(args));
+
+       TEST_START("addargs initial");
+       addargs(&args, "one");
+       ASSERT_U_INT_EQ(args.num, 1);
+       ASSERT_U_INT_EQ(args.nalloc, 32);
+       ASSERT_PTR_NE(args.list, NULL);
+       ASSERT_STRING_EQ(args.list[0], "one");
+       ASSERT_PTR_EQ(args.list[1], NULL);
+       TEST_DONE();
+
+       TEST_START("addargs second");
+       addargs(&args, "two");
+       ASSERT_U_INT_EQ(args.num, 2);
+       ASSERT_U_INT_EQ(args.nalloc, 32);
+       ASSERT_PTR_NE(args.list, NULL);
+       ASSERT_STRING_EQ(args.list[0], "one");
+       ASSERT_STRING_EQ(args.list[1], "two");
+       ASSERT_PTR_EQ(args.list[2], NULL);
+       TEST_DONE();
+
+       TEST_START("addargs with format");
+       addargs(&args, "three=%d", 3);
+       ASSERT_U_INT_EQ(args.num, 3);
+       ASSERT_U_INT_EQ(args.nalloc, 32);
+       ASSERT_PTR_NE(args.list, NULL);
+       ASSERT_STRING_EQ(args.list[0], "one");
+       ASSERT_STRING_EQ(args.list[1], "two");
+       ASSERT_STRING_EQ(args.list[2], "three=3");
+       ASSERT_PTR_EQ(args.list[3], NULL);
+       TEST_DONE();
+
+       TEST_START("replacearg middle");
+       replacearg(&args, 1, "TWO!");
+       ASSERT_U_INT_EQ(args.num, 3);
+       ASSERT_STRING_EQ(args.list[0], "one");
+       ASSERT_STRING_EQ(args.list[1], "TWO!");
+       ASSERT_STRING_EQ(args.list[2], "three=3");
+       ASSERT_PTR_EQ(args.list[3], NULL);
+       TEST_DONE();
+
+       TEST_START("replacearg first");
+       replacearg(&args, 0, "ONE!");
+       ASSERT_U_INT_EQ(args.num, 3);
+       ASSERT_STRING_EQ(args.list[0], "ONE!");
+       ASSERT_STRING_EQ(args.list[1], "TWO!");
+       ASSERT_STRING_EQ(args.list[2], "three=3");
+       ASSERT_PTR_EQ(args.list[3], NULL);
+       TEST_DONE();
+
+       TEST_START("replacearg last");
+       replacearg(&args, 2, "THREE=3!");
+       ASSERT_U_INT_EQ(args.num, 3);
+       ASSERT_STRING_EQ(args.list[0], "ONE!");
+       ASSERT_STRING_EQ(args.list[1], "TWO!");
+       ASSERT_STRING_EQ(args.list[2], "THREE=3!");
+       ASSERT_PTR_EQ(args.list[3], NULL);
+       TEST_DONE();
+
+       TEST_START("replacearg with format");
+       replacearg(&args, 1, "two=%d", 2);
+       ASSERT_U_INT_EQ(args.num, 3);
+       ASSERT_STRING_EQ(args.list[0], "ONE!");
+       ASSERT_STRING_EQ(args.list[1], "two=2");
+       ASSERT_STRING_EQ(args.list[2], "THREE=3!");
+       ASSERT_PTR_EQ(args.list[3], NULL);
+       TEST_DONE();
+
+       TEST_START("addargs reallocation");
+       for (i = args.num; i < 33; i++)
+               addargs(&args, "pad-%d", i);
+       ASSERT_U_INT_EQ(args.num, 33);
+       ASSERT_U_INT_GE(args.nalloc, 33);
+       ASSERT_STRING_EQ(args.list[32], "pad-32");
+       ASSERT_PTR_EQ(args.list[33], NULL);
+       TEST_DONE();
+
+       TEST_START("freeargs");
+       freeargs(&args);
+       ASSERT_U_INT_EQ(args.num, 0);
+       ASSERT_U_INT_EQ(args.nalloc, 0);
+       ASSERT_PTR_EQ(args.list, NULL);
+       TEST_DONE();
+
+       TEST_START("freeargs on NULL");
+       freeargs(NULL);
+       TEST_DONE();
+
+       TEST_START("freeargs on empty");
+       memset(&args, 0, sizeof(args));
+       freeargs(&args);
+       ASSERT_U_INT_EQ(args.num, 0);
+       ASSERT_U_INT_EQ(args.nalloc, 0);
+       ASSERT_PTR_EQ(args.list, NULL);
+       TEST_DONE();
+}
+
+static void
+test_tohex(void)
+{
+       char *hex;
+
+       TEST_START("tohex simple");
+       hex = tohex("foo", 3);
+       ASSERT_STRING_EQ(hex, "666f6f");
+       free(hex);
+       TEST_DONE();
+
+       TEST_START("tohex with null");
+       hex = tohex("a\0b", 3);
+       ASSERT_STRING_EQ(hex, "610062");
+       free(hex);
+       TEST_DONE();
+
+       TEST_START("tohex empty");
+       hex = tohex("", 0);
+       ASSERT_STRING_EQ(hex, "");
+       free(hex);
+       TEST_DONE();
+}
+
+static void
+test_lowercase(void)
+{
+       char *s;
+
+       TEST_START("lowercase mixed");
+       s = xstrdup("HeLlO WoRlD 123");
+       lowercase(s);
+       ASSERT_STRING_EQ(s, "hello world 123");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("lowercase empty");
+       s = xstrdup("");
+       lowercase(s);
+       ASSERT_STRING_EQ(s, "");
+       free(s);
+       TEST_DONE();
+}
+
+static void
+test_path_absolute(void)
+{
+       TEST_START("path_absolute absolute");
+       ASSERT_INT_EQ(path_absolute("/foo/bar"), 1);
+       TEST_DONE();
+
+       TEST_START("path_absolute relative");
+       ASSERT_INT_EQ(path_absolute("foo/bar"), 0);
+       TEST_DONE();
+
+       TEST_START("path_absolute empty");
+       ASSERT_INT_EQ(path_absolute(""), 0);
+       TEST_DONE();
+}
+
+static void
+test_skip_space(void)
+{
+       char *s, *p;
+
+       TEST_START("skip_space leading spaces");
+       s = p = xstrdup("  hello");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("skip_space leading tabs");
+       s = p = xstrdup("\t\thello");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("skip_space leading mixed whitespace");
+       s = p = xstrdup(" \t hello");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("skip_space no leading whitespace");
+       s = p = xstrdup("hello");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "hello");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("skip_space empty string");
+       s = p = xstrdup("");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "");
+       free(s);
+       TEST_DONE();
+
+       TEST_START("skip_space only whitespace");
+       s = p = xstrdup(" \t ");
+       skip_space(&p);
+       ASSERT_STRING_EQ(p, "");
+       free(s);
+       TEST_DONE();
+}
+
+void
+test_misc(void)
+{
+       test_chop();
+       test_rtrim();
+       test_strprefix();
+       test_fmt_timeframe();
+       test_arglist();
+       test_tohex();
+       test_lowercase();
+       test_path_absolute();
+       test_skip_space();
+}
index 13588edaae1859da1ed7ac449afe704591ee3c13..578bc1d90fa59880428c4bd1ca56b037f70c717e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: tests.c,v 1.12 2025/09/02 11:04:58 djm Exp $ */
+/*     $OpenBSD: tests.c,v 1.13 2025/09/04 00:34:17 djm Exp $ */
 /*
  * Regress test for misc helper functions.
  *
@@ -28,6 +28,7 @@ void test_strdelim(void);
 void test_hpdelim(void);
 void test_ptimeout(void);
 void test_xextendf(void);
+void test_misc(void);
 
 void
 tests(void)
@@ -40,6 +41,7 @@ tests(void)
        test_hpdelim();
        test_ptimeout();
        test_xextendf();
+       test_misc();
 }
 
 void