diff options
Diffstat (limited to 'lib/libc/tests')
35 files changed, 4761 insertions, 11 deletions
diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile index f9a0bd4..91097ef 100644 --- a/lib/libc/tests/gen/Makefile +++ b/lib/libc/tests/gen/Makefile @@ -4,8 +4,15 @@ TESTSDIR= ${TESTSBASE}/lib/libc/gen -ATF_TESTS_C= arc4random_test +ATF_TESTS_C+= arc4random_test +ATF_TESTS_C+= fmtcheck2_test +ATF_TESTS_C+= fmtmsg_test +ATF_TESTS_C+= fnmatch2_test ATF_TESTS_C+= fpclassify2_test +ATF_TESTS_C+= ftw_test +ATF_TESTS_C+= popen_test +ATF_TESTS_C+= posix_spawn_test +ATF_TESTS_C+= wordexp_test # TODO: t_closefrom, t_cpuset, t_fmtcheck, t_randomid, t_sleep # TODO: t_siginfo (fixes require further inspection) @@ -55,7 +62,25 @@ DPADD.nice_test+= ${LIBPTHREAD} LDADD.syslog_test+= -lpthread DPADD.syslog_test+= ${LIBPTHREAD} +CFLAGS+= -I${.CURDIR} + +SRCS.fmtcheck2_test= fmtcheck_test.c +SRCS.fnmatch2_test= fnmatch_test.c + TESTS_SUBDIRS= execve TESTS_SUBDIRS+= posix_spawn +# The old testcase name +TEST_FNMATCH= test-fnmatch +CLEANFILES+= ${GEN_SH_CASE_TESTCASES} +sh-tests: .PHONY +.for target in clean obj depend all + @cd ${.CURDIR} && ${MAKE} PROG=${TEST_FNMATCH} \ + -DNO_SUBDIR ${target} +.endfor + @cd ${.OBJDIR} && ./${TEST_FNMATCH} -s 1 > \ + ${SRCTOP}/bin/sh/tests/builtins/case2.0 + @cd ${.OBJDIR} && ./${TEST_FNMATCH} -s 2 > \ + ${SRCTOP}/bin/sh/tests/builtins/case3.0 + .include <bsd.test.mk> diff --git a/lib/libc/tests/gen/fmtcheck_test.c b/lib/libc/tests/gen/fmtcheck_test.c new file mode 100644 index 0000000..3e180c9 --- /dev/null +++ b/lib/libc/tests/gen/fmtcheck_test.c @@ -0,0 +1,105 @@ +/* $NetBSD: tfmtcheck.c,v 1.3 2008/04/28 20:23:04 martin Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code was contributed to The NetBSD Foundation by Allen Briggs. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +struct test_fmt { + char *fmt1; + char *fmt2; + int correct; +} test_fmts[] = { + { "%d", "%d", 1 }, + { "%2d", "%2.2d", 1 }, + { "%x", "%d", 1 }, + { "%u", "%d", 1 }, + { "%03d", "%d", 1 }, + { "%-2d", "%d", 1 }, + { "%d", "%-12.1d", 1 }, + { "%d", "%-01.3d", 1 }, + { "%X", "%-01.3d", 1 }, + { "%D", "%ld", 1 }, + { "%s", "%s", 1 }, + { "%s", "This is a %s test", 1 }, + { "Hi, there. This is a %s test", "%s", 1 }, + { "%d", "%s", 2 }, + { "%e", "%s", 2 }, + { "%r", "%d", 2 }, + { "%*.2d", "%*d", 1 }, + { "%2.*d", "%*d", 2 }, + { "%*d", "%*d", 1 }, + { "%-3", "%d", 2 }, + { "%d %s", "%d", 2 }, + { "%*.*.*d", "%*.*.*d", 2 }, + { "%d", "%d %s", 1 }, + { "%40s", "%20s", 1 }, + { "%x %x %x", "%o %u %d", 1 }, + { "%o %u %d", "%x %x %X", 1 }, + { "%#o %u %#-d", "%x %#x %X", 1 }, + { "%qd", "%llx", 1 }, + { "%%", "%llx", 1 }, + { "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 }, +}; + +ATF_TC_WITHOUT_HEAD(fmtcheck_test); +ATF_TC_BODY(fmtcheck_test, tc) +{ + int i; + const char *f, *cf, *f1, *f2; + + for (i = 0; i < nitems(test_fmts); i++) { + f1 = test_fmts[i].fmt1; + f2 = test_fmts[i].fmt2; + f = fmtcheck(f1, f2); + if (test_fmts[i].correct == 1) + cf = f1; + else + cf = f2; + ATF_CHECK_MSG(f == cf, + "Test %d: (%s) vs. (%s) failed " + "(should have returned %s)", i + 1, f1, f2, + (test_fmts[i].correct == 1) ? "1st" : "2nd"); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fmtcheck_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fmtmsg_test.c b/lib/libc/tests/gen/fmtmsg_test.c new file mode 100644 index 0000000..aa3ca19 --- /dev/null +++ b/lib/libc/tests/gen/fmtmsg_test.c @@ -0,0 +1,252 @@ +/*- + * Copyright (c) 2012 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/wait.h> +#include <err.h> +#include <errno.h> +#include <fmtmsg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +static char *run_test(long classification, const char *label, int severity, + const char *text, const char *action, const char *tag); + +struct testcase { + long classification; + const char *label; + int severity; + const char *text; + const char *action; + const char *tag; + const char *msgverb; + const char *result; +} testcases[] = { + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "text:severity:action:tag", + "illegal option -- z: ERROR\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "text", + "illegal option -- z\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "severity:text", + "ERROR: illegal option -- z\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "ignore me", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "tag:severity:text:nothing:action", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + "", + "BSD:ls: ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, MM_NULLLBL, MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "ERROR: illegal option -- z\n" + "TO FIX: refer to manual BSD:ls:001\n" + }, + { + MM_UTIL | MM_PRINT, "BSD:ls", MM_ERROR, + "illegal option -- z", MM_NULLACT, MM_NULLTAG, + NULL, + "BSD:ls: ERROR: illegal option -- z\n" + }, + { + MM_UTIL | MM_NULLMC, "BSD:ls", MM_ERROR, + "illegal option -- z", "refer to manual", "BSD:ls:001", + NULL, + "" + }, + { + MM_APPL | MM_PRINT, "ABCDEFGHIJ:abcdefghijklmn", MM_INFO, + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "refer to manual", "ABCDEFGHIJ:abcdefghijklmn:001", + NULL, + "ABCDEFGHIJ:abcdefghijklmn: INFO: " + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" + "TO FIX: refer to manual ABCDEFGHIJ:abcdefghijklmn:001\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_HALT, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: HALT: failed\n" + "TO FIX: nothing can help me NOTHING\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_WARNING, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: WARNING: failed\n" + "TO FIX: nothing can help me NOTHING\n" + }, + { + MM_OPSYS | MM_PRINT, "TEST:test", MM_NOSEV, + "failed", "nothing can help me", "NOTHING", + NULL, + "TEST:test: failed\n" + "TO FIX: nothing can help me NOTHING\n" + } +}; + +static char * +run_test(long classification, const char *label, int severity, + const char *text, const char *action, const char *tag) +{ + int pip[2]; + pid_t pid, wpid; + char *result, *p; + size_t resultsize; + ssize_t n; + int status; + + if (pipe(pip) == -1) + err(2, "pipe"); + pid = fork(); + if (pid == -1) + err(2, "fork"); + if (pid == 0) { + close(pip[0]); + if (pip[1] != STDERR_FILENO && + dup2(pip[1], STDERR_FILENO) == -1) + _exit(2); + if (fmtmsg(classification, label, severity, text, action, tag) + != MM_OK) + _exit(1); + else + _exit(0); + } + close(pip[1]); + resultsize = 1024; + result = malloc(resultsize); + p = result; + while ((n = read(pip[0], p, result + resultsize - p - 1)) != 0) { + if (n == -1) { + if (errno == EINTR) + continue; + else + err(2, "read"); + } + p += n; + if (result + resultsize == p - 1) { + resultsize *= 2; + result = realloc(result, resultsize); + if (result == NULL) + err(2, "realloc"); + } + } + if (memchr(result, '\0', p - result) != NULL) { + free(result); + return (NULL); + } + *p = '\0'; + close(pip[0]); + while ((wpid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) + ; + if (wpid == -1) + err(2, "waitpid"); + if (status != 0) { + free(result); + return (NULL); + } + return (result); +} + +ATF_TC_WITHOUT_HEAD(fmtmsg_test); +ATF_TC_BODY(fmtmsg_test, tc) +{ + char *result; + struct testcase *t; + int i; + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + if (t->msgverb != NULL) + setenv("MSGVERB", t->msgverb, 1); + else + unsetenv("MSGVERB"); + result = run_test(t->classification, t->label, t->severity, + t->text, t->action, t->tag); + ATF_CHECK_MSG(result != NULL, "testcase %d failed", i + 1); + if (result != NULL) + ATF_CHECK_MSG(strcmp(result, t->result) == 0, + "results for testcase %d didn't match; " + "`%s` != `%s`", i + 1, result, t->result); + free(result); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fmtmsg_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fnmatch_test.c b/lib/libc/tests/gen/fnmatch_test.c new file mode 100644 index 0000000..8d9ead2 --- /dev/null +++ b/lib/libc/tests/gen/fnmatch_test.c @@ -0,0 +1,188 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "fnmatch_testcases.h" + +static const char * +flags_to_string(int flags) +{ + static const int flagvalues[] = { FNM_NOESCAPE, FNM_PATHNAME, + FNM_PERIOD, FNM_LEADING_DIR, FNM_CASEFOLD, 0 }; + static const char flagnames[] = "FNM_NOESCAPE\0FNM_PATHNAME\0FNM_PERIOD\0FNM_LEADING_DIR\0FNM_CASEFOLD\0"; + static char result[sizeof(flagnames) + 3 * sizeof(int) + 2]; + char *p; + size_t i, len; + const char *fp; + + p = result; + fp = flagnames; + for (i = 0; flagvalues[i] != 0; i++) { + len = strlen(fp); + if (flags & flagvalues[i]) { + if (p != result) + *p++ = '|'; + memcpy(p, fp, len); + p += len; + flags &= ~flagvalues[i]; + } + fp += len + 1; + } + if (p == result) + memcpy(p, "0", 2); + else if (flags != 0) + sprintf(p, "%d", flags); + else + *p = '\0'; + return result; +} + +ATF_TC_WITHOUT_HEAD(fnmatch_test); +ATF_TC_BODY(fnmatch_test, tc) +{ + size_t i; + int flags, result; + struct testcase *t; + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + flags = t->flags; + do { + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + if (strchr(t->pattern, '\\') == NULL && + !(flags & FNM_NOESCAPE)) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') != NULL && + strchr(t->string, '\\') == NULL && + t->result == FNM_NOMATCH && + !(flags & (FNM_NOESCAPE | FNM_LEADING_DIR))) { + flags |= FNM_NOESCAPE; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((t->string[0] != '.' || t->pattern[0] == '.' || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || + t->result == FNM_NOMATCH) && + !(flags & FNM_PATHNAME)) { + flags |= FNM_PATHNAME; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strstr(t->string, "/.") == NULL) || + t->result == FNM_NOMATCH) && + flags & FNM_PATHNAME && !(flags & FNM_PERIOD)) { + flags |= FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((((t->string[0] != '.' || t->pattern[0] == '.') && + strchr(t->string, '/') == NULL) || + t->result == FNM_NOMATCH) && + !(flags & (FNM_PATHNAME | FNM_PERIOD))) { + flags |= FNM_PATHNAME | FNM_PERIOD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if ((strchr(t->string, '/') == NULL || t->result == 0) + && !(flags & FNM_LEADING_DIR)) { + flags |= FNM_LEADING_DIR; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (t->result == 0 && !(flags & FNM_CASEFOLD)) { + flags |= FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + if (strchr(t->pattern, '\\') == NULL && + t->result == 0 && + !(flags & (FNM_NOESCAPE | FNM_CASEFOLD))) { + flags |= FNM_NOESCAPE | FNM_CASEFOLD; + result = fnmatch(t->pattern, t->string, flags); + if (result != t->result) + break; + flags = t->flags; + } + } while (0); + + ATF_CHECK(result == t->result); + if (result == t->result) + printf("fnmatch(\"%s\", \"%s\", %s) == %d\n", + t->pattern, t->string, flags_to_string(flags), result); + else + printf("fnmatch(\"%s\", \"%s\", %s) != %d (was %d)\n", + t->pattern, t->string, flags_to_string(flags), + t->result, result); + } + +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, fnmatch_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/fnmatch_testcases.h b/lib/libc/tests/gen/fnmatch_testcases.h new file mode 100644 index 0000000..537011f --- /dev/null +++ b/lib/libc/tests/gen/fnmatch_testcases.h @@ -0,0 +1,176 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <fnmatch.h> + +struct testcase { + const char *pattern; + const char *string; + int flags; + int result; +} testcases[] = { + { "", "", 0, 0 }, + { "a", "a", 0, 0 }, + { "a", "b", 0, FNM_NOMATCH }, + { "a", "A", 0, FNM_NOMATCH }, + { "*", "a", 0, 0 }, + { "*", "aa", 0, 0 }, + { "*a", "a", 0, 0 }, + { "*a", "b", 0, FNM_NOMATCH }, + { "*a*", "b", 0, FNM_NOMATCH }, + { "*a*b*", "ab", 0, 0 }, + { "*a*b*", "qaqbq", 0, 0 }, + { "*a*bb*", "qaqbqbbq", 0, 0 }, + { "*a*bc*", "qaqbqbcq", 0, 0 }, + { "*a*bb*", "qaqbqbb", 0, 0 }, + { "*a*bc*", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbb", 0, 0 }, + { "*a*bc", "qaqbqbc", 0, 0 }, + { "*a*bb", "qaqbqbbq", 0, FNM_NOMATCH }, + { "*a*bc", "qaqbqbcq", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaa", 0, FNM_NOMATCH }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaa", 0, 0 }, + { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaaa", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", ".........", 0, FNM_NOMATCH }, + { ".*.*.*.*.*.*.*.*.*.*", "..........", 0, 0 }, + { ".*.*.*.*.*.*.*.*.*.*", "...........", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "123456789", 0, FNM_NOMATCH }, + { "??????????*", "123456789", 0, FNM_NOMATCH }, + { "*??????????", "123456789", 0, FNM_NOMATCH }, + { "*?*?*?*?*?*?*?*?*?*?*", "1234567890", 0, 0 }, + { "??????????*", "1234567890", 0, 0 }, + { "*??????????", "1234567890", 0, 0 }, + { "*?*?*?*?*?*?*?*?*?*?*", "12345678901", 0, 0 }, + { "??????????*", "12345678901", 0, 0 }, + { "*??????????", "12345678901", 0, 0 }, + { "[x]", "x", 0, 0 }, + { "[*]", "*", 0, 0 }, + { "[?]", "?", 0, 0 }, + { "[", "[", 0, 0 }, + { "[[]", "[", 0, 0 }, + { "[[]", "x", 0, FNM_NOMATCH }, + { "[*]", "", 0, FNM_NOMATCH }, + { "[*]", "x", 0, FNM_NOMATCH }, + { "[?]", "x", 0, FNM_NOMATCH }, + { "*[*]*", "foo*foo", 0, 0 }, + { "*[*]*", "foo", 0, FNM_NOMATCH }, + { "[0-9]", "0", 0, 0 }, + { "[0-9]", "5", 0, 0 }, + { "[0-9]", "9", 0, 0 }, + { "[0-9]", "/", 0, FNM_NOMATCH }, + { "[0-9]", ":", 0, FNM_NOMATCH }, + { "[0-9]", "*", 0, FNM_NOMATCH }, + { "[!0-9]", "0", 0, FNM_NOMATCH }, + { "[!0-9]", "5", 0, FNM_NOMATCH }, + { "[!0-9]", "9", 0, FNM_NOMATCH }, + { "[!0-9]", "/", 0, 0 }, + { "[!0-9]", ":", 0, 0 }, + { "[!0-9]", "*", 0, 0 }, + { "*[0-9]", "a0", 0, 0 }, + { "*[0-9]", "a5", 0, 0 }, + { "*[0-9]", "a9", 0, 0 }, + { "*[0-9]", "a/", 0, FNM_NOMATCH }, + { "*[0-9]", "a:", 0, FNM_NOMATCH }, + { "*[0-9]", "a*", 0, FNM_NOMATCH }, + { "*[!0-9]", "a0", 0, FNM_NOMATCH }, + { "*[!0-9]", "a5", 0, FNM_NOMATCH }, + { "*[!0-9]", "a9", 0, FNM_NOMATCH }, + { "*[!0-9]", "a/", 0, 0 }, + { "*[!0-9]", "a:", 0, 0 }, + { "*[!0-9]", "a*", 0, 0 }, + { "*[0-9]", "a00", 0, 0 }, + { "*[0-9]", "a55", 0, 0 }, + { "*[0-9]", "a99", 0, 0 }, + { "*[0-9]", "a0a0", 0, 0 }, + { "*[0-9]", "a5a5", 0, 0 }, + { "*[0-9]", "a9a9", 0, 0 }, + { "\\*", "*", 0, 0 }, + { "\\?", "?", 0, 0 }, + { "\\[x]", "[x]", 0, 0 }, + { "\\[", "[", 0, 0 }, + { "\\\\", "\\", 0, 0 }, + { "*\\**", "foo*foo", 0, 0 }, + { "*\\**", "foo", 0, FNM_NOMATCH }, + { "*\\\\*", "foo\\foo", 0, 0 }, + { "*\\\\*", "foo", 0, FNM_NOMATCH }, + { "\\(", "(", 0, 0 }, + { "\\a", "a", 0, 0 }, + { "\\*", "a", 0, FNM_NOMATCH }, + { "\\?", "a", 0, FNM_NOMATCH }, + { "\\*", "\\*", 0, FNM_NOMATCH }, + { "\\?", "\\?", 0, FNM_NOMATCH }, + { "\\[x]", "\\[x]", 0, FNM_NOMATCH }, + { "\\[x]", "\\x", 0, FNM_NOMATCH }, + { "\\[", "\\[", 0, FNM_NOMATCH }, + { "\\(", "\\(", 0, FNM_NOMATCH }, + { "\\a", "\\a", 0, FNM_NOMATCH }, + { "\\", "\\", 0, FNM_NOMATCH }, + { "\\", "", 0, 0 }, + { "\\*", "\\*", FNM_NOESCAPE, 0 }, + { "\\?", "\\?", FNM_NOESCAPE, 0 }, + { "\\", "\\", FNM_NOESCAPE, 0 }, + { "\\\\", "\\", FNM_NOESCAPE, FNM_NOMATCH }, + { "\\\\", "\\\\", FNM_NOESCAPE, 0 }, + { "*\\*", "foo\\foo", FNM_NOESCAPE, 0 }, + { "*\\*", "foo", FNM_NOESCAPE, FNM_NOMATCH }, + { "*", ".", FNM_PERIOD, FNM_NOMATCH }, + { "?", ".", FNM_PERIOD, FNM_NOMATCH }, + { ".*", ".", 0, 0 }, + { ".*", "..", 0, 0 }, + { ".*", ".a", 0, 0 }, + { "[0-9]", ".", FNM_PERIOD, FNM_NOMATCH }, + { "a*", "a.", 0, 0 }, + { "a/a", "a/a", FNM_PATHNAME, 0 }, + { "a/*", "a/a", FNM_PATHNAME, 0 }, + { "*/a", "a/a", FNM_PATHNAME, 0 }, + { "*/*", "a/a", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/x", FNM_PATHNAME, 0 }, + { "a*b/*", "abbb/.x", FNM_PATHNAME, 0 }, + { "*", "a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "*/*", "a/a/a", FNM_PATHNAME, FNM_NOMATCH }, + { "b/*", "b/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b*/*", "a/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, + { "b/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "b*/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, + { "a", "A", FNM_CASEFOLD, 0 }, + { "A", "a", FNM_CASEFOLD, 0 }, + { "[a]", "A", FNM_CASEFOLD, 0 }, + { "[A]", "a", FNM_CASEFOLD, 0 }, + { "a", "b", FNM_CASEFOLD, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "*b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, + { "a", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*a", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, + { "*", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, + { "*a", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, + { "a*b/*", "abbb/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, +}; diff --git a/lib/libc/tests/gen/ftw_test.c b/lib/libc/tests/gen/ftw_test.c new file mode 100644 index 0000000..b120f01 --- /dev/null +++ b/lib/libc/tests/gen/ftw_test.c @@ -0,0 +1,127 @@ +/*- + * Copyright (c) 2012 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/wait.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <ftw.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <unistd.h> + +#include <atf-c.h> + +extern char **environ; + +static char template[] = "testftw.XXXXXXXXXX"; +static char dir[PATH_MAX]; +static int ftwflags; + +static int +cb(const char *path, const struct stat *st, int type, struct FTW *f) +{ + + switch (type) { + case FTW_D: + if ((ftwflags & FTW_DEPTH) == 0) + return (0); + break; + case FTW_DP: + if ((ftwflags & FTW_DEPTH) != 0) + return (0); + break; + case FTW_SL: + if ((ftwflags & FTW_PHYS) != 0) + return (0); + break; + } + ATF_CHECK_MSG(false, + "unexpected path=%s type=%d f.level=%d\n", + path, type, f->level); + return (0); +} + +ATF_TC_WITHOUT_HEAD(ftw_test); +ATF_TC_BODY(ftw_test, tc) +{ + int fd; + + ATF_REQUIRE_MSG(mkdtemp(template) != NULL, "mkdtemp failed"); + + /* XXX: the path needs to be absolute for the 0/FTW_DEPTH testcases */ + ATF_REQUIRE_MSG(realpath(template, dir) != NULL, + "realpath failed; errno=%d", errno); + + fd = open(dir, O_DIRECTORY|O_RDONLY); + ATF_REQUIRE_MSG(fd != -1, "open failed; errno=%d", errno); + + ATF_REQUIRE_MSG(mkdirat(fd, "d1", 0777) == 0, + "mkdirat failed; errno=%d", errno); + + ATF_REQUIRE_MSG(symlinkat(dir, fd, "d1/looper") == 0, + "symlinkat failed; errno=%d", errno); + + printf("ftwflags=FTW_PHYS\n"); + ftwflags = FTW_PHYS; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_PHYS failed; errno=%d", errno); + + printf("ftwflags=FTW_PHYS|FTW_DEPTH\n"); + ftwflags = FTW_PHYS|FTW_DEPTH; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_PHYS|FTW_DEPTH failed; errno=%d", errno); + + printf("ftwflags=0\n"); + ftwflags = 0; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw 0 failed; errno=%d", errno); + + printf("ftwflags=FTW_DEPTH\n"); + ftwflags = FTW_DEPTH; + ATF_REQUIRE_MSG(nftw(dir, cb, 10, ftwflags) != -1, + "nftw FTW_DEPTH failed; errno=%d", errno); + + close(fd); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, ftw_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/popen_test.c b/lib/libc/tests/gen/popen_test.c new file mode 100644 index 0000000..5c615b7 --- /dev/null +++ b/lib/libc/tests/gen/popen_test.c @@ -0,0 +1,251 @@ +/*- + * Copyright (c) 2013 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Limited test program for popen() as specified by IEEE Std. 1003.1-2008, + * with BSD extensions. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/wait.h> +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +static volatile sig_atomic_t got_sigpipe; + +static void +sigpipe_handler(int sig __unused) +{ + got_sigpipe = 1; +} + +static void +check_cloexec(FILE *fp, const char *mode) +{ + int exp_flags, flags; + + flags = fcntl(fileno(fp), F_GETFD); + ATF_CHECK_MSG(flags != -1, "fcntl(F_GETFD) failed; errno=%d", errno); + if (flags == -1) + return; + if (strchr(mode, 'e') != NULL) + exp_flags = FD_CLOEXEC; + else + exp_flags = 0; + ATF_CHECK_MSG((flags & FD_CLOEXEC) == exp_flags, + "bad cloexec flag; %d != %d", flags, exp_flags); +} + +ATF_TC_WITHOUT_HEAD(popen_all_modes_test); +ATF_TC_BODY(popen_all_modes_test, tc) +{ + FILE *fp; + int i, status; + const char *mode; + const char *allmodes[] = { "r", "w", "r+", "re", "we", "r+e", "re+" }; + + for (i = 0; i < nitems(allmodes); i++) { + mode = allmodes[i]; + fp = popen("exit 7", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 7, + "bad exit status (no I/O)"); + } +} + +ATF_TC_WITHOUT_HEAD(popen_rmodes_test); +ATF_TC_BODY(popen_rmodes_test, tc) +{ + FILE *fp; + const char *rmodes[] = { "r", "r+", "re", "r+e", "re+" }; + const char *mode; + char buf[80]; + int i, status; + + for (i = 0; i < nitems(rmodes); i++) { + mode = rmodes[i]; + fp = popen("exit 9", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + bool input_error_1 = !(fgetc(fp) != EOF || !feof(fp) || !ferror(fp)); + ATF_CHECK_MSG(!input_error_1, "input error 1"); + if (input_error_1) + continue; + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 9, + "bad exit status (input)"); + } + + for (i = 0; i < nitems(rmodes); i++) { + char *sres; + mode = rmodes[i]; + fp = popen("echo hi there", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG((sres = fgets(buf, sizeof(buf), fp)) != NULL, + "Input error 2"); + if (sres != NULL) + ATF_CHECK_MSG(strcmp(buf, "hi there\n") == 0, + "Bad input 1"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "Bad exit status (input)"); + } +} + +ATF_TC_WITHOUT_HEAD(popen_wmodes_test); +ATF_TC_BODY(popen_wmodes_test, tc) +{ + FILE *fp, *fp2; + const char *wmodes[] = { "w", "r+", "we", "r+e", "re+" }; + const char *mode; + struct sigaction act, oact; + int i, j, status; + + for (i = 0; i < nitems(wmodes); i++) { + mode = wmodes[i]; + fp = popen("read x && [ \"$x\" = abcd ]", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG(fputs("abcd\n", fp) != EOF, + "Output error 1"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "Bad exit status (output)"); + } + + act.sa_handler = sigpipe_handler; + act.sa_flags = SA_RESTART; + sigemptyset(&act.sa_mask); + ATF_CHECK_MSG(sigaction(SIGPIPE, &act, &oact) != -1, + "sigaction() failed"); + for (i = 0; i < nitems(wmodes); i++) { + mode = wmodes[i]; + fp = popen("exit 88", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + got_sigpipe = 0; + while (fputs("abcd\n", fp) != EOF) + ; + ATF_CHECK_MSG(ferror(fp) && errno == EPIPE, "Expected EPIPE"); + ATF_CHECK_MSG(got_sigpipe, "Expected SIGPIPE"); + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 88, + "Bad exit status (EPIPE)"); + } + ATF_CHECK_MSG(sigaction(SIGPIPE, &oact, NULL) != -1, + "sigaction() failed"); + + for (i = 0; i < nitems(wmodes); i++) { + for (j = 0; j < nitems(wmodes); j++) { + mode = wmodes[i]; + fp = popen("read x", mode); + ATF_CHECK_MSG(fp != NULL, + "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + mode = wmodes[j]; + fp2 = popen("read x", mode); + ATF_CHECK_MSG(fp2 != NULL, + "popen(, \"%s\") failed", mode); + if (fp2 == NULL) { + pclose(fp); + continue; + } + /* If fp2 inherits fp's pipe, we will deadlock here. */ + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1, + "bad exit status (2 pipes)"); + status = pclose(fp2); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 1, + "bad exit status (2 pipes)"); + } + } +} + +ATF_TC_WITHOUT_HEAD(popen_rwmodes_test); +ATF_TC_BODY(popen_rwmodes_test, tc) +{ + const char *rwmodes[] = { "r+", "r+e", "re+" }; + FILE *fp; + const char *mode; + char *sres; + char buf[80]; + int i, ires, status; + + for (i = 0; i < nitems(rwmodes); i++) { + mode = rwmodes[i]; + fp = popen("read x && printf '%s\\n' \"Q${x#a}\"", mode); + ATF_CHECK_MSG(fp != NULL, "popen(, \"%s\") failed", mode); + if (fp == NULL) + continue; + check_cloexec(fp, mode); + ATF_CHECK_MSG((ires = fputs("abcd\n", fp)) != EOF, + "Output error 2"); + if (ires != EOF) { + sres = fgets(buf, sizeof(buf), fp); + ATF_CHECK_MSG(sres != NULL, "Input error 3"); + if (sres != NULL) + ATF_CHECK_MSG(strcmp(buf, "Qbcd\n") == 0, + "Bad input 2"); + } + status = pclose(fp); + ATF_CHECK_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "bad exit status (I/O)"); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, popen_all_modes_test); + ATF_TP_ADD_TC(tp, popen_rmodes_test); + ATF_TP_ADD_TC(tp, popen_wmodes_test); + ATF_TP_ADD_TC(tp, popen_rwmodes_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/posix_spawn_test.c b/lib/libc/tests/gen/posix_spawn_test.c new file mode 100644 index 0000000..e04d103 --- /dev/null +++ b/lib/libc/tests/gen/posix_spawn_test.c @@ -0,0 +1,103 @@ +/*- + * Copyright (c) 2011 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for posix_spawn() and posix_spawnp() as specified by + * IEEE Std. 1003.1-2008. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/wait.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <spawn.h> + +#include <atf-c.h> + +char *myenv[2] = { "answer=42", NULL }; + +ATF_TC_WITHOUT_HEAD(posix_spawn_simple_test); +ATF_TC_BODY(posix_spawn_simple_test, tc) +{ + char *myargs[4]; + int error, status; + pid_t pid, waitres; + + /* Make sure we have no child processes. */ + while (waitpid(-1, NULL, 0) != -1) + ; + ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno); + + /* Simple test. */ + myargs[0] = "sh"; + myargs[1] = "-c"; + myargs[2] = "exit $answer"; + myargs[3] = NULL; + error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv); + ATF_REQUIRE(error == 0); + waitres = waitpid(pid, &status, 0); + ATF_REQUIRE(waitres == pid); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42); +} + +ATF_TC_WITHOUT_HEAD(posix_spawn_no_such_command_negative_test); +ATF_TC_BODY(posix_spawn_no_such_command_negative_test, tc) +{ + char *myargs[4]; + int error, status; + pid_t pid, waitres; + + /* + * If the executable does not exist, the function shall either fail + * and not create a child process or succeed and create a child + * process that exits with status 127. + */ + myargs[0] = "/var/empty/nonexistent"; + myargs[1] = NULL; + error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv); + if (error == 0) { + waitres = waitpid(pid, &status, 0); + ATF_REQUIRE(waitres == pid); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127); + } else { + ATF_REQUIRE(error == ENOENT); + waitres = waitpid(-1, NULL, 0); + ATF_REQUIRE(waitres == -1 && errno == ECHILD); + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, posix_spawn_simple_test); + ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/test-fnmatch.c b/lib/libc/tests/gen/test-fnmatch.c new file mode 100644 index 0000000..7771ab3 --- /dev/null +++ b/lib/libc/tests/gen/test-fnmatch.c @@ -0,0 +1,102 @@ +/*- + * Copyright (c) 2010 Jilles Tjoelker + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "fnmatch_testcases.h" + +static int +write_sh_tests(const char *progname, int num) +{ + size_t i; + struct testcase *t; + + printf("# Generated by %s -s %d, do not edit.\n", progname, num); + printf("# $" "FreeBSD$\n"); + printf("failures=\n"); + printf("failed() { printf '%%s\\n' \"Failed: $1 '$2' '$3'\"; failures=x$failures; }\n"); + if (num == 1) { + printf("testmatch() { eval \"case \\$2 in ''$1) ;; *) failed testmatch \\\"\\$@\\\";; esac\"; }\n"); + printf("testnomatch() { eval \"case \\$2 in ''$1) failed testnomatch \\\"\\$@\\\";; esac\"; }\n"); + } else if (num == 2) { + printf("# We do not treat a backslash specially in this case,\n"); + printf("# but this is not the case in all shells.\n"); + printf("netestmatch() { case $2 in $1) ;; *) failed netestmatch \"$@\";; esac; }\n"); + printf("netestnomatch() { case $2 in $1) failed netestnomatch \"$@\";; esac; }\n"); + } + + for (i = 0; i < nitems(testcases); i++) { + t = &testcases[i]; + if (strchr(t->pattern, '\'') != NULL || + strchr(t->string, '\'') != NULL) + continue; + if (t->flags == 0 && strcmp(t->pattern, "\\") == 0) + continue; + if (num == 1 && t->flags == 0) + printf("test%smatch '%s' '%s'\n", + t->result == FNM_NOMATCH ? "no" : "", + t->pattern, t->string); + if (num == 2 && (t->flags == FNM_NOESCAPE || + (t->flags == 0 && strchr(t->pattern, '\\') == NULL))) + printf("netest%smatch '%s' '%s'\n", + t->result == FNM_NOMATCH ? "no" : "", + t->pattern, t->string); + } + printf("[ -z \"$failures\" ]\n"); + return 0; +} + +static void +usage(char *progname) +{ + fprintf(stderr, "usage: %s [-s num]\n", progname); + fprintf(stderr, "-s option writes tests for sh(1), num is 1 or 2\n"); +} + +int +main(int argc, char *argv[]) +{ + int opt; + + while ((opt = getopt(argc, argv, "s:")) != -1) { + switch (opt) { + case 's': + return (write_sh_tests(argv[0], atoi(optarg))); + default: + usage(argv[0]); + exit(1); + } + } + usage(argv[0]); + exit(1); +} diff --git a/lib/libc/tests/gen/wordexp_test.c b/lib/libc/tests/gen/wordexp_test.c new file mode 100644 index 0000000..3ccc677 --- /dev/null +++ b/lib/libc/tests/gen/wordexp_test.c @@ -0,0 +1,360 @@ +/*- + * Copyright (c) 2003 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wordexp() and wordfree() as specified by + * IEEE Std. 1003.1-2001. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/wait.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wordexp.h> + +#include <atf-c.h> + +static void +chld_handler(int x) +{ + int status, serrno; + + (void)x; + serrno = errno; + while (waitpid(-1, &status, WNOHANG) > 0) + ; + errno = serrno; +} + +ATF_TC_WITHOUT_HEAD(simple_test); +ATF_TC_BODY(simple_test, tc) +{ + wordexp_t we; + int r; + + /* Test that the macros are there. */ + (void)(WRDE_APPEND + WRDE_DOOFFS + WRDE_NOCMD + WRDE_REUSE + + WRDE_SHOWERR + WRDE_UNDEF); + (void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE + + WRDE_SYNTAX); + + /* Simple test. */ + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(long_output_test); +ATF_TC_BODY(long_output_test, tc) +{ + char longdata[6 * 10000 + 1]; + wordexp_t we; + int i, r; + + /* Long output. */ + for (i = 0; i < 10000; i++) + snprintf(longdata + 6 * i, 7, "%05d ", i); + r = wordexp(longdata, &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 10000); + ATF_REQUIRE(we.we_wordv[10000] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS_test); +ATF_TC_BODY(WRDE_DOOFFS_test, tc) +{ + wordexp_t we; + int r; + + we.we_offs = 3; + r = wordexp("hello world", &we, WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(we.we_wordv[0] == NULL); + ATF_REQUIRE(we.we_wordv[1] == NULL); + ATF_REQUIRE(we.we_wordv[2] == NULL); + ATF_REQUIRE(strcmp(we.we_wordv[3], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[4], "world") == 0); + ATF_REQUIRE(we.we_wordv[5] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_REUSE_test); +ATF_TC_BODY(WRDE_REUSE_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("hello world", &we, 0); + r = wordexp("hello world", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_APPEND_test); +ATF_TC_BODY(WRDE_APPEND_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("this is", &we, 0); + ATF_REQUIRE(r == 0); + r = wordexp("a test", &we, WRDE_APPEND); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 4); + ATF_REQUIRE(strcmp(we.we_wordv[0], "this") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "is") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[2], "a") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[3], "test") == 0); + ATF_REQUIRE(we.we_wordv[4] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS__WRDE_APPEND_test); +ATF_TC_BODY(WRDE_DOOFFS__WRDE_APPEND_test, tc) +{ + wordexp_t we; + int r; + + we.we_offs = 2; + r = wordexp("this is", &we, WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFFS); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 6); + ATF_REQUIRE(we.we_wordv[0] == NULL); + ATF_REQUIRE(we.we_wordv[1] == NULL); + ATF_REQUIRE(strcmp(we.we_wordv[2], "this") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[3], "is") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[4], "a") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[5], "test") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[6], "of") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[7], "wordexp") == 0); + ATF_REQUIRE(we.we_wordv[8] == NULL); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_UNDEF_test); +ATF_TC_BODY(WRDE_UNDEF_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("${dont_set_me}", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_BADVAL); +} + +ATF_TC_WITHOUT_HEAD(WRDE_NOCMD_test); +ATF_TC_BODY(WRDE_NOCMD_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("`date`", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("\"`date`\"", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("$(date)", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("\"$(date)\"", &we, WRDE_NOCMD); + ATF_REQUIRE(r == WRDE_CMDSUB); + r = wordexp("$((3+5))", &we, WRDE_NOCMD); + ATF_REQUIRE(r == 0); + r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE); + ATF_REQUIRE(r == 0); + wordfree(&we); +} + +ATF_TC_WITHOUT_HEAD(WRDE_BADCHAR_test); +ATF_TC_BODY(WRDE_BADCHAR_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("'\n|&;<>(){}'", &we, 0); + ATF_REQUIRE(r == 0); + r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE); + ATF_REQUIRE(r == 0); + wordfree(&we); + r = wordexp("test \n test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test | test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test & test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ; test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test > test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test < test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ( test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test ) test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test { test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); + r = wordexp("test } test", &we, 0); + ATF_REQUIRE(r == WRDE_BADCHAR); +} + +ATF_TC_WITHOUT_HEAD(WRDE_SYNTAX_test); +ATF_TC_BODY(WRDE_SYNTAX_test, tc) +{ + wordexp_t we; + int r; + + r = wordexp("'", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("'", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("'\\'", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 1); + ATF_REQUIRE(strcmp(we.we_wordv[0], "\\") == 0); + ATF_REQUIRE(we.we_wordv[1] == NULL); + wordfree(&we); + /* Two syntax errors that are not detected by the current we_check(). */ + r = wordexp("${IFS:+'}", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("${IFS:+'}", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, 0); + ATF_REQUIRE(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, WRDE_UNDEF); + ATF_REQUIRE(r == WRDE_SYNTAX); +} + +ATF_TC_WITHOUT_HEAD(with_SIGCHILD_handler_test); +ATF_TC_BODY(with_SIGCHILD_handler_test, tc) +{ + struct sigaction sa; + wordexp_t we; + int r; + + /* With a SIGCHLD handler that reaps all zombies. */ + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sa.sa_handler = chld_handler; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + sa.sa_handler = SIG_DFL; + r = sigaction(SIGCHLD, &sa, NULL); + ATF_REQUIRE(r == 0); +} + +ATF_TC_WITHOUT_HEAD(with_unused_non_default_IFS_test); +ATF_TC_BODY(with_unused_non_default_IFS_test, tc) +{ + wordexp_t we; + int r; + + /* + * With IFS set to a non-default value (without depending on whether + * IFS is inherited or not). + */ + r = setenv("IFS", ":", 1); + ATF_REQUIRE(r == 0); + r = wordexp("hello world", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + r = unsetenv("IFS"); + ATF_REQUIRE(r == 0); +} + +ATF_TC_WITHOUT_HEAD(with_used_non_default_IFS_test); +ATF_TC_BODY(with_used_non_default_IFS_test, tc) +{ + wordexp_t we; + int r; + + /* + * With IFS set to a non-default value, and using it. + */ + r = setenv("IFS", ":", 1); + ATF_REQUIRE(r == 0); + r = wordexp("${IFS+hello:world}", &we, 0); + ATF_REQUIRE(r == 0); + ATF_REQUIRE(we.we_wordc == 2); + ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0); + ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0); + ATF_REQUIRE(we.we_wordv[2] == NULL); + wordfree(&we); + r = unsetenv("IFS"); + ATF_REQUIRE(r == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, simple_test); + ATF_TP_ADD_TC(tp, long_output_test); + ATF_TP_ADD_TC(tp, WRDE_DOOFFS_test); + ATF_TP_ADD_TC(tp, WRDE_REUSE_test); + ATF_TP_ADD_TC(tp, WRDE_APPEND_test); + ATF_TP_ADD_TC(tp, WRDE_DOOFFS__WRDE_APPEND_test); + ATF_TP_ADD_TC(tp, WRDE_UNDEF_test); + ATF_TP_ADD_TC(tp, WRDE_NOCMD_test); + ATF_TP_ADD_TC(tp, WRDE_BADCHAR_test); + ATF_TP_ADD_TC(tp, WRDE_SYNTAX_test); + ATF_TP_ADD_TC(tp, with_SIGCHILD_handler_test); + ATF_TP_ADD_TC(tp, with_unused_non_default_IFS_test); + ATF_TP_ADD_TC(tp, with_used_non_default_IFS_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/Makefile b/lib/libc/tests/locale/Makefile index e4dc553..d15fd5e 100644 --- a/lib/libc/tests/locale/Makefile +++ b/lib/libc/tests/locale/Makefile @@ -4,6 +4,24 @@ TESTSDIR= ${TESTSBASE}/lib/libc/locale +ATF_TESTS_C+= btowc_test +ATF_TESTS_C+= c16rtomb_test +ATF_TESTS_C+= iswctype_test +ATF_TESTS_C+= mblen_test +ATF_TESTS_C+= mbrlen_test +ATF_TESTS_C+= mbrtoc16_test +ATF_TESTS_C+= mbrtowc_2_test +ATF_TESTS_C+= mbsnrtowcs_2_test +ATF_TESTS_C+= mbsrtowcs_test +ATF_TESTS_C+= mbstowcs_2_test +ATF_TESTS_C+= mbtowc_2_test +ATF_TESTS_C+= towctrans_test +ATF_TESTS_C+= wcrtomb_test +ATF_TESTS_C+= wcsnrtombs_test +ATF_TESTS_C+= wcsrtombs_test +ATF_TESTS_C+= wcstombs_test +ATF_TESTS_C+= wctomb_2_test + NETBSD_ATF_TESTS_C= io_test NETBSD_ATF_TESTS_C+= mbrtowc_test NETBSD_ATF_TESTS_C+= mbstowcs_test @@ -15,7 +33,13 @@ NETBSD_ATF_TESTS_C+= wcsspn_test NETBSD_ATF_TESTS_C+= wcstod_test NETBSD_ATF_TESTS_C+= wctomb_test -CFLAGS.t_wctomb.c+= -Wno-stack-protector +SRCS.mbrtowc_2_test= mbrtowc_test.c +SRCS.mbsnrtowcs_2_test= mbsnrtowcs_test.c +SRCS.mbstowcs_2_test= mbstowcs_test.c +SRCS.mbtowc_2_test= mbtowc_test.c +SRCS.wctomb_2_test= wctomb_test.c + +CFLAGS.t_wctomb.c+= -Wno-stack-protector .include "../Makefile.netbsd-tests" diff --git a/lib/libc/tests/locale/btowc_test.c b/lib/libc/tests/locale/btowc_test.c new file mode 100644 index 0000000..01a1133 --- /dev/null +++ b/lib/libc/tests/locale/btowc_test.c @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for btowc() and wctob() as specified by IEEE Std. 1003.1-2001 + * and ISO/IEC 9899:1999. + * + * The function is tested in the "C" and "ja_JP.eucJP" locales. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(btowc_test); +ATF_TC_BODY(btowc_test, tc) +{ + int i; + + /* C/POSIX locale. */ + ATF_REQUIRE(btowc(EOF) == WEOF); + ATF_REQUIRE(wctob(WEOF) == EOF); + for (i = 0; i < UCHAR_MAX; i++) + ATF_REQUIRE(btowc(i) == (wchar_t)i && i == (int)wctob(i)); + + /* Japanese (EUC) locale. */ + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + ATF_REQUIRE(btowc('A') == L'A' && wctob(L'A') == 'A'); + ATF_REQUIRE(btowc(0xa3) == WEOF && wctob(0xa3c1) == EOF); + +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, btowc_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/c16rtomb_test.c b/lib/libc/tests/locale/c16rtomb_test.c new file mode 100644 index 0000000..ea1f64f --- /dev/null +++ b/lib/libc/tests/locale/c16rtomb_test.c @@ -0,0 +1,166 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * Test program for c16rtomb() as specified by ISO/IEC 9899:2011. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <string.h> +#include <uchar.h> + +#include <atf-c.h> + +static void +require_lc_ctype(const char *locale_name) +{ + char *lc_ctype_set; + + lc_ctype_set = setlocale(LC_CTYPE, locale_name); + if (lc_ctype_set == NULL) + atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d", + locale_name, errno); + + ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0); +} + +static mbstate_t s; +static char buf[MB_LEN_MAX + 1]; + +ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test); +ATF_TC_BODY(c16rtomb_c_locale_test, tc) +{ + + require_lc_ctype("C"); + + /* + * If the buffer argument is NULL, c16 is implicitly 0, + * c16rtomb() resets its internal state. + */ + ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1); + ATF_REQUIRE(c16rtomb(NULL, 0xdc00, NULL) == 1); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0, &s) == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1); + ATF_REQUIRE(c16rtomb(NULL, L'A', NULL) == 1); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, L'A', &s) == 1); + ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); + + /* Unicode character 'Pile of poo'. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); + ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); + ATF_REQUIRE(errno == EILSEQ); + ATF_REQUIRE((unsigned char)buf[0] == 0xcc); +} + +ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test); +ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc) +{ + + require_lc_ctype("en_US.ISO8859-1"); + + /* Unicode character 'Euro sign'. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == (size_t)-1); + ATF_REQUIRE(errno == EILSEQ); + ATF_REQUIRE((unsigned char)buf[0] == 0xcc); +} + +ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test); +ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc) +{ + + require_lc_ctype("en_US.ISO8859-15"); + + /* Unicode character 'Euro sign'. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0xa4 && (unsigned char)buf[1] == 0xcc); +} + +ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test); +ATF_TC_BODY(c16rtomb_utf_8_test, tc) +{ + + require_lc_ctype("en_US.UTF-8"); + + /* Unicode character 'Pile of poo'. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); + ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == 4); + ATF_REQUIRE((unsigned char)buf[0] == 0xf0 && (unsigned char)buf[1] == 0x9f && + (unsigned char)buf[2] == 0x92 && (unsigned char)buf[3] == 0xa9 && + (unsigned char)buf[4] == 0xcc); + + /* Invalid code; 'Pile of poo' without the trail surrogate. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); + ATF_REQUIRE(c16rtomb(buf, L'A', &s) == (size_t)-1); + ATF_REQUIRE(errno == EILSEQ); + ATF_REQUIRE((unsigned char)buf[0] == 0xcc); + + /* Invalid code; 'Pile of poo' without the lead surrogate. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); + ATF_REQUIRE(errno == EILSEQ); + ATF_REQUIRE((unsigned char)buf[0] == 0xcc); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test); + ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test); + ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test); + ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/iswctype_test.c b/lib/libc/tests/locale/iswctype_test.c new file mode 100644 index 0000000..14e57b4 --- /dev/null +++ b/lib/libc/tests/locale/iswctype_test.c @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2003 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wctype() and iswctype() as specified by + * IEEE Std. 1003.1-2001 and ISO/IEC 9899:1999. + * + * The functions are tested in the "C" and "ja_JP.eucJP" locales. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <errno.h> +#include <locale.h> +#include <stdio.h> +#include <string.h> +#include <wchar.h> +#include <wctype.h> + +#include <atf-c.h> + +static void +require_lc_ctype(const char *locale_name) +{ + char *lc_ctype_set; + + lc_ctype_set = setlocale(LC_CTYPE, locale_name); + if (lc_ctype_set == NULL) + atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d", + locale_name, errno); + + ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0); +} + +static wctype_t t; +static int i, j; +static struct { + const char *name; + int (*func)(wint_t); +} cls[] = { + { "alnum", iswalnum }, + { "alpha", iswalpha }, + { "blank", iswblank }, + { "cntrl", iswcntrl }, + { "digit", iswdigit }, + { "graph", iswgraph }, + { "lower", iswlower }, + { "print", iswprint }, + { "punct", iswpunct }, + { "space", iswspace }, + { "upper", iswupper }, + { "xdigit", iswxdigit } +}; + +ATF_TC_WITHOUT_HEAD(iswctype_c_locale_test); +ATF_TC_BODY(iswctype_c_locale_test, tc) +{ + + require_lc_ctype("C"); + for (i = 0; i < nitems(cls); i++) { + t = wctype(cls[i].name); + ATF_REQUIRE(t != 0); + for (j = 0; j < 256; j++) + ATF_REQUIRE(cls[i].func(j) == iswctype(j, t)); + } + t = wctype("elephant"); + ATF_REQUIRE(t == 0); + for (i = 0; i < 256; i++) + ATF_REQUIRE(iswctype(i, t) == 0); +} + +ATF_TC_WITHOUT_HEAD(iswctype_euc_jp_test); +ATF_TC_BODY(iswctype_euc_jp_test, tc) +{ + + require_lc_ctype("ja_JP.eucJP"); + + for (i = 0; i < nitems(cls); i++) { + t = wctype(cls[i].name); + ATF_REQUIRE(t != 0); + for (j = 0; j < 65536; j++) + ATF_REQUIRE(cls[i].func(j) == iswctype(j, t)); + } + t = wctype("elephant"); + ATF_REQUIRE(t == 0); + for (i = 0; i < 65536; i++) + ATF_REQUIRE(iswctype(i, t) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, iswctype_c_locale_test); + ATF_TP_ADD_TC(tp, iswctype_euc_jp_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mblen_test.c b/lib/libc/tests/locale/mblen_test.c new file mode 100644 index 0000000..fbb938d --- /dev/null +++ b/lib/libc/tests/locale/mblen_test.c @@ -0,0 +1,116 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mblen(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1990. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mblen_test); +ATF_TC_BODY(mblen_test, tc) +{ + char buf[MB_LEN_MAX + 1]; + + /* + * C/POSIX locale. + */ + + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* No shift states in C locale. */ + ATF_REQUIRE(mblen(NULL, 0) == 0); + + /* Null wide character. */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = '\0'; + ATF_REQUIRE(mblen(buf, 1) == 0); + + /* Latin letter A. */ + buf[0] = 'A'; + ATF_REQUIRE(mblen(buf, 1) == 1); + + /* Incomplete character sequence. */ + buf[0] = '\0'; + ATF_REQUIRE(mblen(buf, 0) == -1); + ATF_REQUIRE(mblen(NULL, 0) == 0); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + /* No shift states in EUC. */ + ATF_REQUIRE(mblen(NULL, 0) == 0); + + /* Null wide character. */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = '\0'; + ATF_REQUIRE(mblen(buf, 1) == 0); + + /* Latin letter A. */ + buf[0] = 'A'; + ATF_REQUIRE(mblen(buf, 1) == 1); + + /* Incomplete character sequence. */ + buf[0] = '\0'; + ATF_REQUIRE(mblen(buf, 0) == -1); + ATF_REQUIRE(mblen(NULL, 0) == 0); + + /* Incomplete character sequence (truncated double-byte). */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0xa3; + buf[1] = 0x00; + ATF_REQUIRE(mblen(buf, 1) == -1); + ATF_REQUIRE(mblen(NULL, 0) == 0); + + /* Same as above, but complete. */ + buf[1] = 0xc1; + ATF_REQUIRE(mblen(buf, 2) == 2); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mblen_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbrlen_test.c b/lib/libc/tests/locale/mbrlen_test.c new file mode 100644 index 0000000..c2bc548 --- /dev/null +++ b/lib/libc/tests/locale/mbrlen_test.c @@ -0,0 +1,126 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbrlen(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbrlen_test); +ATF_TC_BODY(mbrlen_test, tc) +{ + mbstate_t s; + char buf[MB_LEN_MAX + 1]; + + /* C/POSIX locale. */ + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* Null wide character, internal state. */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0; + ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 1, &s) == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0); + buf[0] = 'A'; + ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 1, &s) == 1); + + /* Incomplete character sequence. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2); + + /* Japanese (EUC) locale. */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + /* Null wide character, internal state. */ + ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0); + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0; + ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 1, &s) == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0); + buf[0] = 'A'; + ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 1, &s) == 1); + + /* Incomplete character sequence (zero length). */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2); + + /* Incomplete character sequence (truncated double-byte). */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0xa3; + buf[1] = 0x00; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 1, &s) == (size_t)-2); + + /* Same as above, but complete. */ + buf[1] = 0xc1; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrlen(buf, 2, &s) == 2); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbrlen_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbrtoc16_test.c b/lib/libc/tests/locale/mbrtoc16_test.c new file mode 100644 index 0000000..a6289e1 --- /dev/null +++ b/lib/libc/tests/locale/mbrtoc16_test.c @@ -0,0 +1,214 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * Test program for mbrtoc16() as specified by ISO/IEC 9899:2011. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <string.h> +#include <uchar.h> + +#include <atf-c.h> + +static void +require_lc_ctype(const char *locale_name) +{ + char *lc_ctype_set; + + lc_ctype_set = setlocale(LC_CTYPE, locale_name); + if (lc_ctype_set == NULL) + atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d", + locale_name, errno); + + ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0); +} + +static mbstate_t s; +static char16_t c16; + +ATF_TC_WITHOUT_HEAD(mbrtoc16_c_locale_test); +ATF_TC_BODY(mbrtoc16_c_locale_test, tc) +{ + + require_lc_ctype("C"); + + /* Null wide character, internal state. */ + ATF_REQUIRE(mbrtoc16(&c16, "", 1, NULL) == 0); + ATF_REQUIRE(c16 == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "", 1, &s) == 0); + ATF_REQUIRE(c16 == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrtoc16(NULL, 0, 0, NULL) == 0); + ATF_REQUIRE(mbrtoc16(&c16, "A", 1, NULL) == 1); + ATF_REQUIRE(c16 == L'A'); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "A", 1, &s) == 1); + ATF_REQUIRE(c16 == L'A'); + + /* Incomplete character sequence. */ + c16 = L'z'; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "", 0, &s) == (size_t)-2); + ATF_REQUIRE(c16 == L'z'); + + /* Check that mbrtoc16() doesn't access the buffer when n == 0. */ + c16 = L'z'; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "", 0, &s) == (size_t)-2); + ATF_REQUIRE(c16 == L'z'); + + /* Check that mbrtoc16() doesn't read ahead too aggressively. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "AB", 2, &s) == 1); + ATF_REQUIRE(c16 == L'A'); + ATF_REQUIRE(mbrtoc16(&c16, "C", 1, &s) == 1); + ATF_REQUIRE(c16 == L'C'); + +} + +ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_1_test); +ATF_TC_BODY(mbrtoc16_iso_8859_1_test, tc) +{ + + require_lc_ctype("en_US.ISO8859-1"); + + /* Currency sign. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "\xa4", 1, &s) == 1); + ATF_REQUIRE(c16 == 0xa4); +} + +ATF_TC_WITHOUT_HEAD(mbrtoc16_iso_8859_15_test); +ATF_TC_BODY(mbrtoc16_iso_8859_15_test, tc) +{ + + require_lc_ctype("en_US.ISO8859-15"); + + /* Euro sign. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "\xa4", 1, &s) == 1); + ATF_REQUIRE(c16 == 0x20ac); +} + +ATF_TC_WITHOUT_HEAD(mbrtoc16_utf_8_test); +ATF_TC_BODY(mbrtoc16_utf_8_test, tc) +{ + + require_lc_ctype("en_US.UTF-8"); + + /* Null wide character, internal state. */ + ATF_REQUIRE(mbrtoc16(NULL, 0, 0, NULL) == 0); + ATF_REQUIRE(mbrtoc16(&c16, "", 1, NULL) == 0); + ATF_REQUIRE(c16 == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "", 1, &s) == 0); + ATF_REQUIRE(c16 == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrtoc16(NULL, 0, 0, NULL) == 0); + ATF_REQUIRE(mbrtoc16(&c16, "A", 1, NULL) == 1); + ATF_REQUIRE(c16 == L'A'); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "A", 1, &s) == 1); + ATF_REQUIRE(c16 == L'A'); + + /* Incomplete character sequence (zero length). */ + c16 = L'z'; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtoc16(&c16, "", 0, &s) == (size_t)-2); + ATF_REQUIRE(c16 == L'z'); + + /* Incomplete character sequence (truncated double-byte). */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\xc3", 1, &s) == (size_t)-2); + + /* Same as above, but complete. */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\xc3\x84", 2, &s) == 2); + ATF_REQUIRE(c16 == 0xc4); + + /* Test restarting behaviour. */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\xc3", 1, &s) == (size_t)-2); + ATF_REQUIRE(c16 == 0); + ATF_REQUIRE(mbrtoc16(&c16, "\xb7", 1, &s) == 1); + ATF_REQUIRE(c16 == 0xf7); + + /* Surrogate pair. */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\xf0\x9f\x92\xa9", 4, &s) == 4); + ATF_REQUIRE(c16 == 0xd83d); + ATF_REQUIRE(mbrtoc16(&c16, "", 0, &s) == (size_t)-3); + ATF_REQUIRE(c16 == 0xdca9); + + /* Letter e with acute, precomposed. */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\xc3\xa9", 2, &s) == 2); + ATF_REQUIRE(c16 == 0xe9); + + /* Letter e with acute, combined. */ + memset(&s, 0, sizeof(s)); + c16 = 0; + ATF_REQUIRE(mbrtoc16(&c16, "\x65\xcc\x81", 3, &s) == 1); + ATF_REQUIRE(c16 == 0x65); + ATF_REQUIRE(mbrtoc16(&c16, "\xcc\x81", 2, &s) == 2); + ATF_REQUIRE(c16 == 0x301); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbrtoc16_c_locale_test); + ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_1_test); + ATF_TP_ADD_TC(tp, mbrtoc16_iso_8859_15_test); + ATF_TP_ADD_TC(tp, mbrtoc16_utf_8_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbrtowc_test.c b/lib/libc/tests/locale/mbrtowc_test.c new file mode 100644 index 0000000..37e0bfd --- /dev/null +++ b/lib/libc/tests/locale/mbrtowc_test.c @@ -0,0 +1,165 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbrtowc(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbrtowc_test); +ATF_TC_BODY(mbrtowc_test, tc) +{ + mbstate_t s; + wchar_t wc; + char buf[MB_LEN_MAX + 1]; + + /* + * C/POSIX locale. + */ + + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* Null wide character, internal state. */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 0); + ATF_REQUIRE(wc == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 0); + ATF_REQUIRE(wc == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0); + buf[0] = 'A'; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Incomplete character sequence. */ + wc = L'z'; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2); + ATF_REQUIRE(wc == L'z'); + + /* Check that mbrtowc() doesn't access the buffer when n == 0. */ + wc = L'z'; + memset(&s, 0, sizeof(s)); + buf[0] = '\0'; + ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2); + ATF_REQUIRE(wc == L'z'); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + /* Null wide character, internal state. */ + ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0); + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 0); + ATF_REQUIRE(wc == 0); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 0); + ATF_REQUIRE(wc == 0); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(mbrtowc(NULL, 0, 0, NULL) == 0); + buf[0] = 'A'; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, NULL) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Incomplete character sequence (zero length). */ + wc = L'z'; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbrtowc(&wc, buf, 0, &s) == (size_t)-2); + ATF_REQUIRE(wc == L'z'); + + /* Incomplete character sequence (truncated double-byte). */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0xa3; + buf[1] = 0x00; + memset(&s, 0, sizeof(s)); + wc = 0; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == (size_t)-2); + + /* Same as above, but complete. */ + buf[1] = 0xc1; + memset(&s, 0, sizeof(s)); + wc = 0; + ATF_REQUIRE(mbrtowc(&wc, buf, 2, &s) == 2); + ATF_REQUIRE(wc == 0xa3c1); + + /* Test restarting behaviour. */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0xa3; + memset(&s, 0, sizeof(s)); + wc = 0; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == (size_t)-2); + ATF_REQUIRE(wc == 0); + buf[0] = 0xc1; + ATF_REQUIRE(mbrtowc(&wc, buf, 1, &s) == 1); + ATF_REQUIRE(wc == 0xa3c1); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbrtowc_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbsnrtowcs_test.c b/lib/libc/tests/locale/mbsnrtowcs_test.c new file mode 100644 index 0000000..4012b7e --- /dev/null +++ b/lib/libc/tests/locale/mbsnrtowcs_test.c @@ -0,0 +1,195 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbsnrtowcs(). + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbsnrtowcs_test); +ATF_TC_BODY(mbsnrtowcs_test, tc) +{ + char srcbuf[128]; + wchar_t dstbuf[128]; + char *src; + mbstate_t s; + + /* C/POSIX locale. */ + + /* Simple null terminated string. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 5); + ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0); + ATF_REQUIRE(dstbuf[6] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Simple null terminated string, stopping early. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 4, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 4); + ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0); + ATF_REQUIRE(dstbuf[5] == 0xcccc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Not enough space in destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, 4, &s) == 4); + ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0); + ATF_REQUIRE(dstbuf[5] == 0xcccc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Null terminated string, internal dest. buffer */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 6, 0, &s) == 5); + + /* Null terminated string, internal dest. buffer, stopping early */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 4, 0, &s) == 4); + + /* Null terminated string, internal state. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) / + sizeof(*dstbuf), NULL) == 5); + ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0); + ATF_REQUIRE(dstbuf[6] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Null terminated string, internal state, internal dest. buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 6, 0, NULL) == 5); + + /* Empty source buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + srcbuf[0] = '\0'; + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, 1, &s) == 0); + ATF_REQUIRE(dstbuf[0] == 0); + ATF_REQUIRE(dstbuf[1] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Zero length destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, 0, &s) == 0); + ATF_REQUIRE(dstbuf[0] == 0xcccc); + ATF_REQUIRE(src == srcbuf); + + /* Zero length source buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 0, 1, &s) == 0); + ATF_REQUIRE(dstbuf[0] == 0xcccc); + ATF_REQUIRE(src == srcbuf); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 8, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 5); + ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 && + dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0); + ATF_REQUIRE(src == NULL); + + /* Partial character. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 4); + ATF_REQUIRE(src == srcbuf + 6); + ATF_REQUIRE(!mbsinit(&s)); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 1); + ATF_REQUIRE(src == srcbuf + 7); + ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 0); + ATF_REQUIRE(src == NULL); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbsnrtowcs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbsrtowcs_test.c b/lib/libc/tests/locale/mbsrtowcs_test.c new file mode 100644 index 0000000..1b9d5c9 --- /dev/null +++ b/lib/libc/tests/locale/mbsrtowcs_test.c @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbsrtowcs(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbsrtowcs_test); +ATF_TC_BODY(mbsrtowcs_test, tc) +{ + char srcbuf[128]; + wchar_t dstbuf[128]; + char *src; + mbstate_t s; + + /* + * C/POSIX locale. + */ + + /* Simple null terminated string. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 5); + ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0); + ATF_REQUIRE(dstbuf[6] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Not enough space in destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 4, &s) == 4); + ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0); + ATF_REQUIRE(dstbuf[5] == 0xcccc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Null terminated string, internal dest. buffer */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(mbsrtowcs(NULL, (const char **)&src, 0, &s) == 5); + + /* Null terminated string, internal state. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + src = srcbuf; + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) / + sizeof(*dstbuf), NULL) == 5); + ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0); + ATF_REQUIRE(dstbuf[6] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Null terminated string, internal state, internal dest. buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + ATF_REQUIRE(mbsrtowcs(NULL, (const char **)&src, 0, NULL) == 5); + + /* Empty source buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + srcbuf[0] = '\0'; + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 1, &s) == 0); + ATF_REQUIRE(dstbuf[0] == 0); + ATF_REQUIRE(dstbuf[1] == 0xcccc); + ATF_REQUIRE(src == NULL); + + /* Zero length destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, 0, &s) == 0); + ATF_REQUIRE(dstbuf[0] == 0xcccc); + ATF_REQUIRE(src == srcbuf); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbsrtowcs(dstbuf, (const char **)&src, sizeof(dstbuf) / + sizeof(*dstbuf), &s) == 5); + ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 && + dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0); + ATF_REQUIRE(src == NULL); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbsrtowcs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbstowcs_test.c b/lib/libc/tests/locale/mbstowcs_test.c new file mode 100644 index 0000000..77b0e9e --- /dev/null +++ b/lib/libc/tests/locale/mbstowcs_test.c @@ -0,0 +1,111 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbstowcs(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbstowcs_test); +ATF_TC_BODY(mbstowcs_test, tc) +{ + char srcbuf[128]; + wchar_t dstbuf[128]; + + /* C/POSIX locale. */ + + /* Simple null terminated string. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5); + ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0); + ATF_REQUIRE(dstbuf[6] == 0xcccc); + + /* Not enough space in destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 4) == 4); + ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0); + ATF_REQUIRE(dstbuf[5] == 0xcccc); + + /* Null terminated string, internal dest. buffer (XSI extension) */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + ATF_REQUIRE(mbstowcs(NULL, srcbuf, 0) == 5); + + /* Empty source buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + srcbuf[0] = '\0'; + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 1) == 0); + ATF_REQUIRE(dstbuf[0] == 0); + ATF_REQUIRE(dstbuf[1] == 0xcccc); + + /* Zero length destination buffer. */ + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "hello"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, 0) == 0); + ATF_REQUIRE(dstbuf[0] == 0xcccc); + + /* Japanese (EUC) locale. */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + memset(srcbuf, 0xcc, sizeof(srcbuf)); + strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3"); + wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf)); + ATF_REQUIRE(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5); + ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 && + dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbstowcs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/mbtowc_test.c b/lib/libc/tests/locale/mbtowc_test.c new file mode 100644 index 0000000..7c7f80e --- /dev/null +++ b/lib/libc/tests/locale/mbtowc_test.c @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for mbtowc(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1990. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(mbtowc_test); +ATF_TC_BODY(mbtowc_test, tc) +{ + char buf[MB_LEN_MAX + 1]; + wchar_t wc; + + /* C/POSIX locale. */ + + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* No shift states in C locale. */ + ATF_REQUIRE(mbtowc(NULL, NULL, 0) == 0); + + /* Null wide character. */ + wc = 0xcccc; + memset(buf, 0, sizeof(buf)); + ATF_REQUIRE(mbtowc(&wc, buf, 1) == 0); + ATF_REQUIRE(wc == 0); + + /* Latin letter A. */ + buf[0] = 'A'; + ATF_REQUIRE(mbtowc(&wc, buf, 1) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Incomplete character sequence. */ + wc = L'z'; + buf[0] = '\0'; + ATF_REQUIRE(mbtowc(&wc, buf, 0) == -1); + ATF_REQUIRE(wc == L'z'); + ATF_REQUIRE(mbtowc(NULL, NULL, 0) == 0); + + /* Japanese (EUC) locale. */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + /* Null wide character */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0; + wc = 0xcccc; + ATF_REQUIRE(mbtowc(&wc, buf, 1) == 0); + ATF_REQUIRE(wc == 0); + + /* Latin letter A. */ + buf[0] = 'A'; + ATF_REQUIRE(mbtowc(&wc, buf, 1) == 1); + ATF_REQUIRE(wc == L'A'); + + /* Incomplete character sequence (zero length). */ + wc = L'z'; + buf[0] = '\0'; + ATF_REQUIRE(mbtowc(&wc, buf, 0) == -1); + ATF_REQUIRE(wc == L'z'); + ATF_REQUIRE(mbtowc(NULL, NULL, 0) == 0); + + /* Incomplete character sequence (truncated double-byte). */ + memset(buf, 0xcc, sizeof(buf)); + buf[0] = 0xa3; + buf[1] = 0x00; + wc = L'z'; + ATF_REQUIRE(mbtowc(&wc, buf, 1) == -1); + ATF_REQUIRE(wc == L'z'); + ATF_REQUIRE(mbtowc(NULL, NULL, 0) == 0); + + /* Same as above, but complete. */ + buf[1] = 0xc1; + ATF_REQUIRE(mbtowc(&wc, buf, 2) == 2); + ATF_REQUIRE(wc == 0xa3c1); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, mbtowc_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/towctrans_test.c b/lib/libc/tests/locale/towctrans_test.c new file mode 100644 index 0000000..bb9d97c --- /dev/null +++ b/lib/libc/tests/locale/towctrans_test.c @@ -0,0 +1,90 @@ +/*- + * Copyright (c) 2003 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wctrans() and towctrans() as specified by + * IEEE Std. 1003.1-2001 and ISO/IEC 9899:1999. + * + * The functions are tested in the "C" and "ja_JP.eucJP" locales. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <locale.h> +#include <stdio.h> +#include <string.h> +#include <wchar.h> +#include <wctype.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(towctrans_test); +ATF_TC_BODY(towctrans_test, tc) +{ + wctype_t t; + int i, j; + struct { + const char *name; + wint_t (*func)(wint_t); + } tran[] = { + { "tolower", towlower }, + { "toupper", towupper }, + }; + + /* C/POSIX locale. */ + for (i = 0; i < sizeof(tran) / sizeof(*tran); i++) { + t = wctrans(tran[i].name); + ATF_REQUIRE(t != 0); + for (j = 0; j < 256; j++) + ATF_REQUIRE(tran[i].func(j) == towctrans(j, t)); + } + t = wctrans("elephant"); + ATF_REQUIRE(t == 0); + for (i = 0; i < 256; i++) + ATF_REQUIRE(towctrans(i, t) == i); + + /* Japanese (EUC) locale. */ + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + for (i = 0; i < sizeof(tran) / sizeof(*tran); i++) { + t = wctrans(tran[i].name); + ATF_REQUIRE(t != 0); + for (j = 0; j < 65536; j++) + ATF_REQUIRE(tran[i].func(j) == towctrans(j, t)); + } + t = wctrans("elephant"); + ATF_REQUIRE(t == 0); + for (i = 0; i < 65536; i++) + ATF_REQUIRE(towctrans(i, t) == i); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, towctrans_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/wcrtomb_test.c b/lib/libc/tests/locale/wcrtomb_test.c new file mode 100644 index 0000000..09199ba --- /dev/null +++ b/lib/libc/tests/locale/wcrtomb_test.c @@ -0,0 +1,135 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wcrtomb(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(wcrtomb_test); +ATF_TC_BODY(wcrtomb_test, tc) +{ + mbstate_t s; + size_t len; + char buf[MB_LEN_MAX + 1]; + + /* C/POSIX locale. */ + + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* + * If the buffer argument is NULL, wc is implicitly L'\0', + * wcrtomb() resets its internal state. + */ + ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1); + ATF_REQUIRE(wcrtomb(NULL, UCHAR_MAX + 1, NULL) == 1); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + len = wcrtomb(buf, L'\0', &s); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1); + ATF_REQUIRE(wcrtomb(NULL, L'A', NULL) == 1); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + len = wcrtomb(buf, L'A', &s); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); + + /* Invalid code. */ + ATF_REQUIRE(wcrtomb(buf, UCHAR_MAX + 1, NULL) == (size_t)-1); + ATF_REQUIRE(errno == EILSEQ); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX == 3); + + /* + * If the buffer argument is NULL, wc is implicitly L'\0', + * wcrtomb() resets its internal state. + */ + ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1); + + /* Null wide character. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + len = wcrtomb(buf, L'\0', &s); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); + + /* Latin letter A, internal state. */ + ATF_REQUIRE(wcrtomb(NULL, L'\0', NULL) == 1); + ATF_REQUIRE(wcrtomb(NULL, L'A', NULL) == 1); + + /* Latin letter A. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + len = wcrtomb(buf, L'A', &s); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); + + /* Full width letter A. */ + memset(&s, 0, sizeof(s)); + memset(buf, 0xcc, sizeof(buf)); + len = wcrtomb(buf, 0xa3c1, &s); + ATF_REQUIRE(len == 2); + ATF_REQUIRE((unsigned char)buf[0] == 0xa3 && + (unsigned char)buf[1] == 0xc1 && + (unsigned char)buf[2] == 0xcc); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, wcrtomb_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/wcsnrtombs_test.c b/lib/libc/tests/locale/wcsnrtombs_test.c new file mode 100644 index 0000000..2e330a1 --- /dev/null +++ b/lib/libc/tests/locale/wcsnrtombs_test.c @@ -0,0 +1,195 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wcsnrtombs(). + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(wcsnrtombs_test); +ATF_TC_BODY(wcsnrtombs_test, tc) +{ + wchar_t srcbuf[128]; + char dstbuf[128]; + wchar_t *src; + mbstate_t s; + + /* C/POSIX locale. */ + + /* Simple null terminated string. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf), + &s) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + ATF_REQUIRE(src == NULL); + + /* Simple null terminated string, stopping early. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 4, sizeof(dstbuf), + &s) == 4); + ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0); + ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Not enough space in destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 4, + &s) == 4); + ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0); + ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Null terminated string, internal dest. buffer */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 6, sizeof(dstbuf), + &s) == 5); + + /* Null terminated string, internal dest. buffer, stopping early. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 4, sizeof(dstbuf), + &s) == 4); + + /* Null terminated string, internal state. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf), + NULL) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + ATF_REQUIRE(src == NULL); + + /* Null terminated string, internal state, internal dest. buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + src = srcbuf; + ATF_REQUIRE(wcsnrtombs(NULL, (const wchar_t **)&src, 6, 0, NULL) == 5); + + /* Empty source buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = L'\0'; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 1, sizeof(dstbuf), + &s) == 0); + ATF_REQUIRE(dstbuf[0] == L'\0'); + + /* Zero length destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 0, &s) == 0); + ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc); + + /* Zero length source buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 0, sizeof(dstbuf), + &s) == 0); + ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc); + ATF_REQUIRE(src == srcbuf); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = 0xA3C1; + srcbuf[1] = 0x0020; + srcbuf[2] = 0x0042; + srcbuf[3] = 0x0020; + srcbuf[4] = 0xA3C3; + srcbuf[5] = 0x0000; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, sizeof(dstbuf), + &s) == 7); + ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0); + ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc); + ATF_REQUIRE(src == NULL); + + /* Stopping early. */ + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsnrtombs(dstbuf, (const wchar_t **)&src, 6, 6, + &s) == 5); + ATF_REQUIRE(memcmp(dstbuf, "\xA3\xC1 B ", 5) == 0); + ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc); + ATF_REQUIRE(src == srcbuf + 4); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, wcsnrtombs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/wcsrtombs_test.c b/lib/libc/tests/locale/wcsrtombs_test.c new file mode 100644 index 0000000..4c3455a8 --- /dev/null +++ b/lib/libc/tests/locale/wcsrtombs_test.c @@ -0,0 +1,156 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wcsrtombs(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(wcsrtombs_test); +ATF_TC_BODY(wcsrtombs_test, tc) +{ + wchar_t srcbuf[128]; + char dstbuf[128]; + wchar_t *src; + mbstate_t s; + + /* C/POSIX locale. */ + + /* Simple null terminated string. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf), + &s) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + ATF_REQUIRE(src == NULL); + + /* Not enough space in destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, 4, + &s) == 4); + ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0); + ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc); + ATF_REQUIRE(src == srcbuf + 4); + + /* Null terminated string, internal dest. buffer */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(NULL, (const wchar_t **)&src, sizeof(dstbuf), + &s) == 5); + + /* Null terminated string, internal state. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf), + NULL) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + ATF_REQUIRE(src == NULL); + + /* Null terminated string, internal state, internal dest. buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + src = srcbuf; + ATF_REQUIRE(wcsrtombs(NULL, (const wchar_t **)&src, 0, NULL) == 5); + + /* Empty source buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = L'\0'; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf), + &s) == 0); + ATF_REQUIRE(dstbuf[0] == L'\0'); + + /* Zero length destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, 0, &s) == 0); + ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = 0xA3C1; + srcbuf[1] = 0x0020; + srcbuf[2] = 0x0042; + srcbuf[3] = 0x0020; + srcbuf[4] = 0xA3C3; + srcbuf[5] = 0x0000; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + src = srcbuf; + memset(&s, 0, sizeof(s)); + ATF_REQUIRE(wcsrtombs(dstbuf, (const wchar_t **)&src, sizeof(dstbuf), + &s) == 7); + ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0); + ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc); + ATF_REQUIRE(src == NULL); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, wcsrtombs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/wcstombs_test.c b/lib/libc/tests/locale/wcstombs_test.c new file mode 100644 index 0000000..f3fcd81 --- /dev/null +++ b/lib/libc/tests/locale/wcstombs_test.c @@ -0,0 +1,130 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wcstombs(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(wcstombs_test); +ATF_TC_BODY(wcstombs_test, tc) +{ + wchar_t srcbuf[128]; + char dstbuf[128]; + + /* C/POSIX locale. */ + + /* Simple null terminated string. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + + /* Not enough space in destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, 4) == 4); + ATF_REQUIRE(memcmp(dstbuf, "hell", 4) == 0); + ATF_REQUIRE((unsigned char)dstbuf[5] == 0xcc); + + /* Null terminated string, internal dest. buffer */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + ATF_REQUIRE(wcstombs(NULL, srcbuf, sizeof(dstbuf)) == 5); + + /* Null terminated string, internal state. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5); + ATF_REQUIRE(strcmp(dstbuf, "hello") == 0); + ATF_REQUIRE((unsigned char)dstbuf[6] == 0xcc); + + /* Null terminated string, internal state, internal dest. buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + ATF_REQUIRE(wcstombs(NULL, srcbuf, 0) == 5); + + /* Empty source buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = L'\0'; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 0); + ATF_REQUIRE(dstbuf[0] == L'\0'); + + /* Zero length destination buffer. */ + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + wcscpy(srcbuf, L"hello"); + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, 0) == 0); + ATF_REQUIRE((unsigned char)dstbuf[0] == 0xcc); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX > 1); + + wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); + srcbuf[0] = 0xA3C1; + srcbuf[1] = 0x0020; + srcbuf[2] = 0x0042; + srcbuf[3] = 0x0020; + srcbuf[4] = 0xA3C3; + srcbuf[5] = 0x0000; + memset(dstbuf, 0xcc, sizeof(dstbuf)); + ATF_REQUIRE(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 7); + ATF_REQUIRE(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0); + ATF_REQUIRE((unsigned char)dstbuf[8] == 0xcc); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, wcstombs_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/locale/wctomb_test.c b/lib/libc/tests/locale/wctomb_test.c new file mode 100644 index 0000000..255cda4 --- /dev/null +++ b/lib/libc/tests/locale/wctomb_test.c @@ -0,0 +1,113 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Test program for wctomb(), as specified by IEEE Std. 1003.1-2001 and + * ISO/IEC 9899:1999. + * + * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and + * "ja_JP.eucJP". Other encodings are not tested. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <errno.h> +#include <limits.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +ATF_TC_WITHOUT_HEAD(wctomb_test); +ATF_TC_BODY(wctomb_test, tc) +{ + size_t len; + char buf[MB_LEN_MAX + 1]; + + /* C/POSIX locale. */ + + ATF_REQUIRE(MB_CUR_MAX == 1); + + /* No shift states in C locale. */ + ATF_REQUIRE(wctomb(NULL, L'\0') == 0); + + /* Null wide character. */ + memset(buf, 0xcc, sizeof(buf)); + len = wctomb(buf, L'\0'); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); + + /* Latin letter A. */ + memset(buf, 0xcc, sizeof(buf)); + len = wctomb(buf, L'A'); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); + + /* Invalid code. */ + ATF_REQUIRE(wctomb(buf, UCHAR_MAX + 1) == -1); + ATF_REQUIRE(wctomb(NULL, 0) == 0); + + /* + * Japanese (EUC) locale. + */ + + ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); + ATF_REQUIRE(MB_CUR_MAX == 3); + + /* No shift states in EUC encoding. */ + ATF_REQUIRE(wctomb(NULL, L'\0') == 0); + + /* Null wide character. */ + memset(buf, 0xcc, sizeof(buf)); + len = wctomb(buf, L'\0'); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); + + /* Latin letter A. */ + memset(buf, 0xcc, sizeof(buf)); + len = wctomb(buf, L'A'); + ATF_REQUIRE(len == 1); + ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); + + /* Full width letter A. */ + memset(buf, 0xcc, sizeof(buf)); + len = wctomb(buf, 0xa3c1); + ATF_REQUIRE(len == 2); + ATF_REQUIRE((unsigned char)buf[0] == 0xa3 && + (unsigned char)buf[1] == 0xc1 && + (unsigned char)buf[2] == 0xcc); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, wctomb_test); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/net/Makefile b/lib/libc/tests/net/Makefile index eb199f0..b8f6d97 100644 --- a/lib/libc/tests/net/Makefile +++ b/lib/libc/tests/net/Makefile @@ -1,24 +1,25 @@ # $FreeBSD$ -.include <bsd.own.mk> - TESTSDIR= ${TESTSBASE}/lib/libc/net -BINDIR= ${TESTSDIR} +ATF_TESTS_C+= ether_test +ATF_TESTS_C+= eui64_aton_test +ATF_TESTS_C+= eui64_ntoa_test -NETBSD_ATF_TESTS_C= getprotoent_test -NETBSD_ATF_TESTS_C+= ether_aton_test +CFLAGS+= -I${.CURDIR} -SRCS.t_ether_aton= aton_ether_subr.c t_ether_aton.c +NETBSD_ATF_TESTS_C+= getprotoent_test +NETBSD_ATF_TESTS_C+= ether_aton_test -aton_ether_subr.c: gen_ether_subr ${.CURDIR:H:H:H:H}/sys/net/if_ethersubr.c - ${HOST_SH} ${.ALLSRC} ${.TARGET} +SRCS.ether_aton_test= aton_ether_subr.c t_ether_aton.c # TODO: hostent_test NETBSD_ATF_TESTS_SH= nsdispatch_test NETBSD_ATF_TESTS_SH+= protoent_test NETBSD_ATF_TESTS_SH+= servent_test +BINDIR= ${TESTSDIR} + PROGS= h_nsd_recurse PROGS+= h_protoent PROGS+= h_servent @@ -28,12 +29,14 @@ DPADD.h_nsd_recurse+= ${LIBPTHREAD} LDADD.h_nsd_recurse+= -lpthread CLEANFILES+= aton_ether_subr.c +aton_ether_subr.c: gen_ether_subr ${SRCTOP}/sys/net/if_ethersubr.c + ${HOST_SH} ${.ALLSRC} ${.TARGET} .include "../Makefile.netbsd-tests" # TODO: the testcases needs to be ported to FreeBSD #TESTS_SUBDIRS= getaddrinfo -FILES= hosts +FILES+= hosts FILES+= resolv.conf .include <bsd.test.mk> diff --git a/lib/libc/tests/net/ether_test.c b/lib/libc/tests/net/ether_test.c new file mode 100644 index 0000000..5b596f0 --- /dev/null +++ b/lib/libc/tests/net/ether_test.c @@ -0,0 +1,192 @@ +/*- + * Copyright (c) 2007 Robert N. M. Watson + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> + +#include <net/ethernet.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> + +#include <atf-c.h> + +static const char *ether_line_string = "01:23:45:67:89:ab ether_line_hostname"; +static const char *ether_line_hostname = "ether_line_hostname"; +static const struct ether_addr ether_line_addr = { + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } +}; + +ATF_TC_WITHOUT_HEAD(ether_line); +ATF_TC_BODY(ether_line, tc) +{ + struct ether_addr e; + char hostname[256]; + + ATF_REQUIRE_MSG(ether_line(ether_line_string, &e, hostname) == 0, + "ether_line failed; errno=%d", errno); + ATF_REQUIRE_MSG(bcmp(&e, ðer_line_addr, ETHER_ADDR_LEN) == 0, + "bad address"); + ATF_REQUIRE_MSG(strcmp(hostname, ether_line_hostname) == 0, + "bad hostname"); +} + +static const char *ether_line_bad_1_string = "x"; + +ATF_TC_WITHOUT_HEAD(ether_line_bad_1); +ATF_TC_BODY(ether_line_bad_1, tc) +{ + struct ether_addr e; + char hostname[256]; + + ATF_REQUIRE_MSG(ether_line(ether_line_bad_1_string, &e, hostname) != 0, + "ether_line succeeded unexpectedly"); +} + +static const char *ether_line_bad_2_string = "x x"; + +ATF_TC_WITHOUT_HEAD(ether_line_bad_2); +ATF_TC_BODY(ether_line_bad_2, tc) +{ + struct ether_addr e; + char hostname[256]; + + ATF_REQUIRE_MSG(ether_line(ether_line_bad_2_string, &e, hostname) != 0, + "ether_line succeeded unexpectedly"); +} + +static const char *ether_aton_string = "01:23:45:67:89:ab"; +static const struct ether_addr ether_aton_addr = { + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } +}; + +ATF_TC_WITHOUT_HEAD(ether_aton_r); +ATF_TC_BODY(ether_aton_r, tc) +{ + struct ether_addr e, *ep; + + ep = ether_aton_r(ether_aton_string, &e); + + ATF_REQUIRE_MSG(ep != NULL, "ether_aton_r failed; errno=%d", errno); + ATF_REQUIRE_MSG(ep == &e, + "ether_aton_r returned different pointers; %p != %p", ep, &e); +} + +static const char *ether_aton_bad_string = "x"; + +ATF_TC_WITHOUT_HEAD(ether_aton_r_bad); +ATF_TC_BODY(ether_aton_r_bad, tc) +{ + struct ether_addr e, *ep; + + ep = ether_aton_r(ether_aton_bad_string, &e); + ATF_REQUIRE_MSG(ep == NULL, "ether_aton_r succeeded unexpectedly"); +} + +ATF_TC_WITHOUT_HEAD(ether_aton); +ATF_TC_BODY(ether_aton, tc) +{ + struct ether_addr *ep; + + ep = ether_aton(ether_aton_string); + ATF_REQUIRE_MSG(ep != NULL, "ether_aton failed"); + ATF_REQUIRE_MSG(bcmp(ep, ðer_aton_addr, ETHER_ADDR_LEN) == 0, + "bad address"); +} + +ATF_TC_WITHOUT_HEAD(ether_aton_bad); +ATF_TC_BODY(ether_aton_bad, tc) +{ + struct ether_addr *ep; + + ep = ether_aton(ether_aton_bad_string); + ATF_REQUIRE_MSG(ep == NULL, "ether_aton succeeded unexpectedly"); +} + +static const char *ether_ntoa_string = "01:23:45:67:89:ab"; +static const struct ether_addr ether_ntoa_addr = { + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } +}; + +ATF_TC_WITHOUT_HEAD(ether_ntoa_r); +ATF_TC_BODY(ether_ntoa_r, tc) +{ + char buf[256], *cp; + + cp = ether_ntoa_r(ðer_ntoa_addr, buf); + ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa_r failed"); + ATF_REQUIRE_MSG(cp == buf, + "ether_ntoa_r returned a different pointer; %p != %p", cp, buf); + ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0, + "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string); +} + +ATF_TC_WITHOUT_HEAD(ether_ntoa); +ATF_TC_BODY(ether_ntoa, tc) +{ + char *cp; + + cp = ether_ntoa(ðer_ntoa_addr); + ATF_REQUIRE_MSG(cp != NULL, "ether_ntoa failed"); + ATF_REQUIRE_MSG(strcmp(cp, ether_ntoa_string) == 0, + "strings did not match (`%s` != `%s`)", cp, ether_ntoa_string); +} + +#if 0 +ATF_TC_WITHOUT_HEAD(ether_ntohost); +ATF_TC_BODY(ether_ntohost, tc) +{ + +} + +ATF_TC_WITHOUT_HEAD(ether_hostton); +ATF_TC_BODY(ether_hostton, tc) +{ + +} +#endif + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, ether_line); + ATF_TP_ADD_TC(tp, ether_line_bad_1); + ATF_TP_ADD_TC(tp, ether_line_bad_2); + ATF_TP_ADD_TC(tp, ether_aton_r); + ATF_TP_ADD_TC(tp, ether_aton_r_bad); + ATF_TP_ADD_TC(tp, ether_aton); + ATF_TP_ADD_TC(tp, ether_aton_bad); + ATF_TP_ADD_TC(tp, ether_ntoa_r); + ATF_TP_ADD_TC(tp, ether_ntoa); +#if 0 + ATF_TP_ADD_TC(tp, ether_ntohost); + ATF_TP_ADD_TC(tp, ether_hostton); +#endif + + return (atf_no_error()); +} diff --git a/lib/libc/tests/net/eui64_aton_test.c b/lib/libc/tests/net/eui64_aton_test.c new file mode 100644 index 0000000..7b97d7f --- /dev/null +++ b/lib/libc/tests/net/eui64_aton_test.c @@ -0,0 +1,104 @@ +/* + * Copyright 2004 The Aerospace Corporation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions, and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of The Aerospace Corporation may not be used to endorse or + * promote products derived from this software. + * + * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/eui64.h> +#include <locale.h> +#include <stdio.h> +#include <string.h> + +#include <atf-c.h> + +#include "test-eui64.h" + +static void +test_str(const char *str, const struct eui64 *eui) +{ + struct eui64 e; + char buf[EUI64_SIZ]; + int rc; + + ATF_REQUIRE_MSG(eui64_aton(str, &e) == 0, "eui64_aton failed"); + rc = memcmp(&e, eui, sizeof(e)); + if (rc != 0) { + eui64_ntoa(&e, buf, sizeof(buf)); + atf_tc_fail( + "eui64_aton(\"%s\", ..) failed; memcmp returned %d. " + "String obtained form eui64_ntoa was: `%s`", + str, rc, buf); + } +} + +ATF_TC_WITHOUT_HEAD(id_ascii); +ATF_TC_BODY(id_ascii, tc) +{ + + test_str(test_eui64_id_ascii, &test_eui64_id); +} + +ATF_TC_WITHOUT_HEAD(id_colon_ascii); +ATF_TC_BODY(id_colon_ascii, tc) +{ + + test_str(test_eui64_id_colon_ascii, &test_eui64_id); +} + +ATF_TC_WITHOUT_HEAD(mac_ascii); +ATF_TC_BODY(mac_ascii, tc) +{ + + test_str(test_eui64_mac_ascii, &test_eui64_eui48); +} + +ATF_TC_WITHOUT_HEAD(mac_colon_ascii); +ATF_TC_BODY(mac_colon_ascii, tc) +{ + + test_str(test_eui64_mac_colon_ascii, &test_eui64_eui48); +} + +ATF_TC_WITHOUT_HEAD(hex_ascii); +ATF_TC_BODY(hex_ascii, tc) +{ + + test_str(test_eui64_hex_ascii, &test_eui64_id); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, id_ascii); + ATF_TP_ADD_TC(tp, id_colon_ascii); + ATF_TP_ADD_TC(tp, mac_ascii); + ATF_TP_ADD_TC(tp, mac_colon_ascii); + ATF_TP_ADD_TC(tp, hex_ascii); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/net/eui64_ntoa_test.c b/lib/libc/tests/net/eui64_ntoa_test.c new file mode 100644 index 0000000..4158e7a --- /dev/null +++ b/lib/libc/tests/net/eui64_ntoa_test.c @@ -0,0 +1,64 @@ +/* + * Copyright 2004 The Aerospace Corporation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions, and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of The Aerospace Corporation may not be used to endorse or + * promote products derived from this software. + * + * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/eui64.h> +#include <stdio.h> +#include <string.h> + +#include <atf-c.h> + +#include "test-eui64.h" + +static void +test_str(const char *str, const struct eui64 *eui) +{ + char a[EUI64_SIZ]; + + ATF_REQUIRE_MSG(eui64_ntoa(&test_eui64_id, a, sizeof(a)) == 0, + "eui64_ntoa failed"); + ATF_REQUIRE_MSG(strcmp(a, test_eui64_id_ascii) == 0, + "the strings mismatched: `%s` != `%s`", a, test_eui64_id_ascii); +} + +ATF_TC_WITHOUT_HEAD(id_ascii); +ATF_TC_BODY(id_ascii, tc) +{ + + test_str(test_eui64_id_ascii, &test_eui64_id); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, id_ascii); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/net/test-eui64.h b/lib/libc/tests/net/test-eui64.h new file mode 100644 index 0000000..ea82a10 --- /dev/null +++ b/lib/libc/tests/net/test-eui64.h @@ -0,0 +1,55 @@ +/* + * Copyright 2004 The Aerospace Corporation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions, and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of The Aerospace Corporation may not be used to endorse or + * promote products derived from this software. + * + * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AEROSPACE CORPORATION BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ +#ifndef _TEST_EUI64_H +#define _TEST_EUI64_H + +struct eui64 test_eui64_id = {{0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77}}; +struct eui64 test_eui64_eui48 = {{0x00,0x11,0x22,0xFF,0xFE,0x33,0x44,0x55}}; +struct eui64 test_eui64_mac48 = {{0x00,0x11,0x22,0xFF,0xFF,0x33,0x44,0x55}}; + +#define test_eui64_id_ascii "00-11-22-33-44-55-66-77" +#define test_eui64_id_colon_ascii "00:11:22:33:44:55:66:77" +#define test_eui64_hex_ascii "0x0011223344556677" +#define test_eui64_eui48_ascii "00-11-22-ff-fe-33-44-55" +#define test_eui64_mac48_ascii "00-11-22-ff-fe-33-44-55" +#define test_eui64_mac_ascii "00-11-22-33-44-55" +#define test_eui64_mac_colon_ascii "00:11:22:33:44:55" +#define test_eui64_id_host "id" +#define test_eui64_eui48_host "eui-48" +#define test_eui64_mac48_host "mac-48" + +#define test_eui64_line_id "00-11-22-33-44-55-66-77 id" +#define test_eui64_line_id_colon "00:11:22:33:44:55:66:77 id" +#define test_eui64_line_eui48 "00-11-22-FF-fe-33-44-55 eui-48" +#define test_eui64_line_mac48 "00-11-22-FF-ff-33-44-55 mac-48" +#define test_eui64_line_eui48_6byte "00-11-22-33-44-55 eui-48" +#define test_eui64_line_eui48_6byte_c "00:11:22:33:44:55 eui-48" + +#endif /* !_TEST_EUI64_H */ diff --git a/lib/libc/tests/sys/Makefile b/lib/libc/tests/sys/Makefile index 89431bc..39aec1f 100644 --- a/lib/libc/tests/sys/Makefile +++ b/lib/libc/tests/sys/Makefile @@ -4,6 +4,8 @@ TESTSDIR= ${TESTSBASE}/lib/libc/sys +ATF_TESTS_C+= queue_test + # TODO: clone, lwp_create, lwp_ctl, posix_fadvise, recvmmsg, # swapcontext NETBSD_ATF_TESTS_C+= access_test diff --git a/lib/libc/tests/sys/queue_test.c b/lib/libc/tests/sys/queue_test.c new file mode 100644 index 0000000..2405e3a --- /dev/null +++ b/lib/libc/tests/sys/queue_test.c @@ -0,0 +1,237 @@ +/*- + * Copyright (c) 2015 EMC Corp. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/queue.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +ATF_TC(slist_test); +ATF_TC_HEAD(slist_test, tc) +{ + + atf_tc_set_md_var(tc, "descr", "SLIST macro feature tests"); +} + +ATF_TC_BODY(slist_test, tc) +{ + SLIST_HEAD(stailhead, entry) head = SLIST_HEAD_INITIALIZER(head); + struct entry { + SLIST_ENTRY(entry) entries; + int i; + } *n1, *n2, *n3, *np; + int i, j, length; + + SLIST_INIT(&head); + + printf("Ensuring SLIST_EMPTY works\n"); + + ATF_REQUIRE(SLIST_EMPTY(&head)); + + i = length = 0; + + SLIST_FOREACH(np, &head, entries) { + length++; + } + ATF_REQUIRE_EQ(length, 0); + + printf("Ensuring SLIST_INSERT_HEAD works\n"); + + n1 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n1 != NULL); + n1->i = i++; + + SLIST_INSERT_HEAD(&head, n1, entries); + + printf("Ensuring SLIST_FIRST returns element 1\n"); + ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1); + + j = length = 0; + SLIST_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 1); + + printf("Ensuring SLIST_INSERT_AFTER works\n"); + + n2 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n2 != NULL); + n2->i = i++; + + SLIST_INSERT_AFTER(n1, n2, entries); + + n3 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n3 != NULL); + n3->i = i++; + + SLIST_INSERT_AFTER(n2, n3, entries); + + j = length = 0; + SLIST_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 3); + + printf("Ensuring SLIST_REMOVE_HEAD works\n"); + + printf("Ensuring SLIST_FIRST returns element 1\n"); + ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1); + + SLIST_REMOVE_HEAD(&head, entries); + + printf("Ensuring SLIST_FIRST now returns element 2\n"); + ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2); + + j = 1; /* Starting point's 1 this time */ + length = 0; + SLIST_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 2); + + printf("Ensuring SLIST_REMOVE_AFTER works by removing the tail\n"); + + SLIST_REMOVE_AFTER(n2, entries); + + j = 1; /* Starting point's 1 this time */ + length = 0; + SLIST_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 1); + + printf("Ensuring SLIST_FIRST returns element 2\n"); + ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2); + +} + +ATF_TC(stailq_test); +ATF_TC_HEAD(stailq_test, tc) +{ + + atf_tc_set_md_var(tc, "descr", "STAILQ macro feature tests"); +} + +ATF_TC_BODY(stailq_test, tc) +{ + STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head); + struct entry { + STAILQ_ENTRY(entry) entries; + int i; + } *n1, *n2, *n3, *np; + int i, j, length; + + printf("Ensuring empty STAILQs are treated properly\n"); + STAILQ_INIT(&head); + ATF_REQUIRE(STAILQ_EMPTY(&head)); + + i = length = 0; + + STAILQ_FOREACH(np, &head, entries) { + length++; + } + ATF_REQUIRE_EQ(length, 0); + + printf("Ensuring STAILQ_INSERT_HEAD works\n"); + + n1 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n1 != NULL); + n1->i = i++; + + STAILQ_INSERT_HEAD(&head, n1, entries); + + j = length = 0; + STAILQ_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 1); + + printf("Ensuring STAILQ_INSERT_TAIL works\n"); + + n2 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n2 != NULL); + n2->i = i++; + + STAILQ_INSERT_TAIL(&head, n2, entries); + + n3 = malloc(sizeof(struct entry)); + ATF_REQUIRE(n3 != NULL); + n3->i = i++; + + STAILQ_INSERT_TAIL(&head, n3, entries); + + j = length = 0; + STAILQ_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 3); + + printf("Ensuring STAILQ_REMOVE_HEAD works\n"); + + STAILQ_REMOVE_HEAD(&head, entries); + + j = 1; /* Starting point's 1 this time */ + length = 0; + STAILQ_FOREACH(np, &head, entries) { + ATF_REQUIRE_EQ_MSG(np->i, j, + "%d (entry counter) != %d (counter)", np->i, j); + j++; + length++; + } + ATF_REQUIRE_EQ(length, 2); + +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, slist_test); + ATF_TP_ADD_TC(tp, stailq_test); + + return (atf_no_error()); +} |