NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/57205: [PATCH] Add tests for strchrnul(3)



>Number:         57205
>Category:       lib
>Synopsis:       [PATCH] Add tests for strchrnul(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Jan 30 13:15:00 +0000 2023
>Originator:     Dag-Erling Smørgrav
>Release:        MAIN
>Organization:
>Environment:
N/A
>Description:
NetBSD includes tests for strchr(3) but not strchrnul(3).

In addition, the strchr(3) test was clearly copy-pasted from the strlen(3) test and one instance of "strlen" was left unchanged.
>How-To-Repeat:
Browse http://cvsweb.netbsd.org/bsdweb.cgi/src/tests/lib/libc/string/
>Fix:
See https://github.com/KlaraSystems/netbsd/commit/2ad64de322a9eebf733ac95c0d2d1211eb9c2835

>From 2ad64de322a9eebf733ac95c0d2d1211eb9c2835 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= <des%FreeBSD.org@localhost>
Date: Mon, 30 Jan 2023 13:46:41 +0100
Subject: [PATCH] Add strchrnul(3) tests, derived from the strchr(3) tests.

While there, fix a pasto and some whitespace issues in the latter.
---
 tests/lib/libc/string/Makefile      |   1 +
 tests/lib/libc/string/t_strchr.c    |  10 +-
 tests/lib/libc/string/t_strchrnul.c | 293 ++++++++++++++++++++++++++++
 3 files changed, 299 insertions(+), 5 deletions(-)
 create mode 100644 tests/lib/libc/string/t_strchrnul.c

diff --git a/tests/lib/libc/string/Makefile b/tests/lib/libc/string/Makefile
index 4c7b3ec578b7..f12e399aa392 100644
--- a/tests/lib/libc/string/Makefile
+++ b/tests/lib/libc/string/Makefile
@@ -15,6 +15,7 @@ TESTS_C+=	t_memset
 TESTS_C+=	t_popcount
 TESTS_C+=	t_strcat
 TESTS_C+=	t_strchr
+TESTS_C+=	t_strchrnul
 TESTS_C+=	t_strcmp
 TESTS_C+=	t_strcoll
 TESTS_C+=	t_strcpy
diff --git a/tests/lib/libc/string/t_strchr.c b/tests/lib/libc/string/t_strchr.c
index 5dd9a62213ab..c5fda42e604d 100644
--- a/tests/lib/libc/string/t_strchr.c
+++ b/tests/lib/libc/string/t_strchr.c
@@ -247,7 +247,7 @@ ATF_TC_BODY(strchr_basic, tc)
 	};
 
 	dl_handle = dlopen(NULL, RTLD_LAZY);
-	strchr_fn = dlsym(dl_handle, "test_strlen");
+	strchr_fn = dlsym(dl_handle, "test_strchr");
 	if (!strchr_fn)
 		strchr_fn = strchr;
 
@@ -269,11 +269,11 @@ ATF_TC_BODY(strchr_basic, tc)
 			/* Then for the '/' in the strings */
 			verify_strchr(buf + a, '/', t, a);
 
-		   	/* check zero extension of char arg */
-		   	verify_strchr(buf + a, 0xffffff00 | '/', t, a);
+			/* check zero extension of char arg */
+			verify_strchr(buf + a, 0xffffff00 | '/', t, a);
 
-		   	/* Replace all the '/' with 0xff */
-		   	while ((off = slow_strchr(buf + a, '/')) != NULL)
+			/* Replace all the '/' with 0xff */
+			while ((off = slow_strchr(buf + a, '/')) != NULL)
 				*off = 0xff;
 
 			buf[a + len] = 0xff;
diff --git a/tests/lib/libc/string/t_strchrnul.c b/tests/lib/libc/string/t_strchrnul.c
new file mode 100644
index 000000000000..0a82fb1b607b
--- /dev/null
+++ b/tests/lib/libc/string/t_strchrnul.c
@@ -0,0 +1,293 @@
+/* $NetBSD$ */
+
+/*
+ * Written by J.T. Conklin <jtc%acorntoolworks.com@localhost>
+ * Public domain.
+ */
+
+#include <atf-c.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+static char	*slow_strchrnul(char *, int);
+static void	 verify_strchrnul(char *, int, unsigned int, unsigned int);
+
+char * (*volatile strchrnul_fn)(const char *, int);
+
+static char *
+slow_strchrnul(char *buf, int ch)
+{
+	unsigned char c = 1;
+
+	ch &= 0xff;
+
+	for (; ; buf++) {
+		c = *buf;
+		if (c == ch || c == 0)
+			return buf;
+	}
+}
+
+static void
+verify_strchrnul(char *buf, int ch, unsigned int t, unsigned int a)
+{
+	const char *off, *ok_off;
+
+	off = strchrnul_fn(buf, ch);
+	ok_off = slow_strchrnul(buf, ch);
+	if (off == ok_off)
+		return;
+
+	fprintf(stderr, "test_strchrnul(\"%s\", %#x) gave %zd not %zd (test %d, "
+	    "alignment %d)\n",
+	    buf, ch, off ? off - buf : -1, ok_off ? ok_off - buf : -1, t, a);
+
+	atf_tc_fail("Check stderr for details");
+}
+
+ATF_TC(strchrnul_basic);
+ATF_TC_HEAD(strchrnul_basic, tc)
+{
+
+        atf_tc_set_md_var(tc, "descr", "Test strchrnul(3) results");
+}
+
+ATF_TC_BODY(strchrnul_basic, tc)
+{
+	void *dl_handle;
+	char *off;
+	char buf[32];
+	unsigned int t, a;
+
+	const char *tab[] = {
+		"",
+		"a",
+		"aa",
+		"abc",
+		"abcd",
+		"abcde",
+		"abcdef",
+		"abcdefg",
+		"abcdefgh",
+
+		"/",
+		"//",
+		"/a",
+		"/a/",
+		"/ab",
+		"/ab/",
+		"/abc",
+		"/abc/",
+		"/abcd",
+		"/abcd/",
+		"/abcde",
+		"/abcde/",
+		"/abcdef",
+		"/abcdef/",
+		"/abcdefg",
+		"/abcdefg/",
+		"/abcdefgh",
+		"/abcdefgh/",
+
+		"a/",
+		"a//",
+		"a/a",
+		"a/a/",
+		"a/ab",
+		"a/ab/",
+		"a/abc",
+		"a/abc/",
+		"a/abcd",
+		"a/abcd/",
+		"a/abcde",
+		"a/abcde/",
+		"a/abcdef",
+		"a/abcdef/",
+		"a/abcdefg",
+		"a/abcdefg/",
+		"a/abcdefgh",
+		"a/abcdefgh/",
+
+		"ab/",
+		"ab//",
+		"ab/a",
+		"ab/a/",
+		"ab/ab",
+		"ab/ab/",
+		"ab/abc",
+		"ab/abc/",
+		"ab/abcd",
+		"ab/abcd/",
+		"ab/abcde",
+		"ab/abcde/",
+		"ab/abcdef",
+		"ab/abcdef/",
+		"ab/abcdefg",
+		"ab/abcdefg/",
+		"ab/abcdefgh",
+		"ab/abcdefgh/",
+
+		"abc/",
+		"abc//",
+		"abc/a",
+		"abc/a/",
+		"abc/ab",
+		"abc/ab/",
+		"abc/abc",
+		"abc/abc/",
+		"abc/abcd",
+		"abc/abcd/",
+		"abc/abcde",
+		"abc/abcde/",
+		"abc/abcdef",
+		"abc/abcdef/",
+		"abc/abcdefg",
+		"abc/abcdefg/",
+		"abc/abcdefgh",
+		"abc/abcdefgh/",
+
+		"abcd/",
+		"abcd//",
+		"abcd/a",
+		"abcd/a/",
+		"abcd/ab",
+		"abcd/ab/",
+		"abcd/abc",
+		"abcd/abc/",
+		"abcd/abcd",
+		"abcd/abcd/",
+		"abcd/abcde",
+		"abcd/abcde/",
+		"abcd/abcdef",
+		"abcd/abcdef/",
+		"abcd/abcdefg",
+		"abcd/abcdefg/",
+		"abcd/abcdefgh",
+		"abcd/abcdefgh/",
+
+		"abcde/",
+		"abcde//",
+		"abcde/a",
+		"abcde/a/",
+		"abcde/ab",
+		"abcde/ab/",
+		"abcde/abc",
+		"abcde/abc/",
+		"abcde/abcd",
+		"abcde/abcd/",
+		"abcde/abcde",
+		"abcde/abcde/",
+		"abcde/abcdef",
+		"abcde/abcdef/",
+		"abcde/abcdefg",
+		"abcde/abcdefg/",
+		"abcde/abcdefgh",
+		"abcde/abcdefgh/",
+
+		"abcdef/",
+		"abcdef//",
+		"abcdef/a",
+		"abcdef/a/",
+		"abcdef/ab",
+		"abcdef/ab/",
+		"abcdef/abc",
+		"abcdef/abc/",
+		"abcdef/abcd",
+		"abcdef/abcd/",
+		"abcdef/abcde",
+		"abcdef/abcde/",
+		"abcdef/abcdef",
+		"abcdef/abcdef/",
+		"abcdef/abcdefg",
+		"abcdef/abcdefg/",
+		"abcdef/abcdefgh",
+		"abcdef/abcdefgh/",
+
+		"abcdefg/",
+		"abcdefg//",
+		"abcdefg/a",
+		"abcdefg/a/",
+		"abcdefg/ab",
+		"abcdefg/ab/",
+		"abcdefg/abc",
+		"abcdefg/abc/",
+		"abcdefg/abcd",
+		"abcdefg/abcd/",
+		"abcdefg/abcde",
+		"abcdefg/abcde/",
+		"abcdefg/abcdef",
+		"abcdefg/abcdef/",
+		"abcdefg/abcdefg",
+		"abcdefg/abcdefg/",
+		"abcdefg/abcdefgh",
+		"abcdefg/abcdefgh/",
+
+		"abcdefgh/",
+		"abcdefgh//",
+		"abcdefgh/a",
+		"abcdefgh/a/",
+		"abcdefgh/ab",
+		"abcdefgh/ab/",
+		"abcdefgh/abc",
+		"abcdefgh/abc/",
+		"abcdefgh/abcd",
+		"abcdefgh/abcd/",
+		"abcdefgh/abcde",
+		"abcdefgh/abcde/",
+		"abcdefgh/abcdef",
+		"abcdefgh/abcdef/",
+		"abcdefgh/abcdefg",
+		"abcdefgh/abcdefg/",
+		"abcdefgh/abcdefgh",
+		"abcdefgh/abcdefgh/",
+	};
+
+	dl_handle = dlopen(NULL, RTLD_LAZY);
+	strchrnul_fn = dlsym(dl_handle, "test_strchrnul");
+	if (!strchrnul_fn)
+		strchrnul_fn = strchrnul;
+
+	for (a = 3; a < 3 + sizeof(long); ++a) {
+		/* Put char and a \0 before the buffer */
+		buf[a-1] = '/';
+		buf[a-2] = '0';
+		buf[a-3] = 0xff;
+		for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
+			int len = strlen(tab[t]) + 1;
+			memcpy(&buf[a], tab[t], len);
+
+			/* Put the char we are looking for after the \0 */
+			buf[a + len] = '/';
+
+			/* Check search for NUL at end of string */
+			verify_strchrnul(buf + a, 0, t, a);
+
+			/* Then for the '/' in the strings */
+			verify_strchrnul(buf + a, '/', t, a);
+
+			/* check zero extension of char arg */
+			verify_strchrnul(buf + a, 0xffffff00 | '/', t, a);
+
+			/* Replace all the '/' with 0xff */
+			while (*(off = slow_strchrnul(buf + a, '/')) != '\0')
+				*off = 0xff;
+
+			buf[a + len] = 0xff;
+
+			/* Check we can search for 0xff as well as '/' */
+			verify_strchrnul(buf + a, 0xff, t, a);
+		}
+	}
+	(void)dlclose(dl_handle);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, strchrnul_basic);
+
+	return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index