aboutsummaryrefslogtreecommitdiff
path: root/lib/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libutil')
-rw-r--r--lib/libutil/Makefile22
-rw-r--r--lib/libutil/t_efun.c135
-rw-r--r--lib/libutil/t_parsedate.c142
-rw-r--r--lib/libutil/t_pidfile.c363
-rw-r--r--lib/libutil/t_snprintb.c117
-rw-r--r--lib/libutil/t_sockaddr_snprintf.c185
6 files changed, 964 insertions, 0 deletions
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile
new file mode 100644
index 000000000000..eaef6279a0e2
--- /dev/null
+++ b/lib/libutil/Makefile
@@ -0,0 +1,22 @@
+# $NetBSD: Makefile,v 1.4 2011/03/30 17:02:17 jruoho Exp $
+
+NOMAN= # defined
+
+.include <bsd.own.mk>
+
+TESTSDIR= ${TESTSBASE}/lib/libutil
+
+DPADD+= ${LIBUTIL}
+LDADD+= -lutil
+
+TESTS_C= t_efun
+TESTS_C+= t_parsedate
+TESTS_C+= t_pidfile
+TESTS_C+= t_snprintb
+TESTS_C+= t_sockaddr_snprintf
+
+.if (${USE_INET6} != "no")
+CFLAGS+= -DINET6
+.endif
+
+.include <bsd.test.mk>
diff --git a/lib/libutil/t_efun.c b/lib/libutil/t_efun.c
new file mode 100644
index 000000000000..f5187f25ab56
--- /dev/null
+++ b/lib/libutil/t_efun.c
@@ -0,0 +1,135 @@
+/* $NetBSD: t_efun.c,v 1.3 2012/11/04 23:37:02 christos Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * 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>
+__RCSID("$NetBSD: t_efun.c,v 1.3 2012/11/04 23:37:02 christos Exp $");
+
+#include <atf-c.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <util.h>
+
+static bool fail;
+static void handler(int, const char *, ...);
+
+static void
+handler(int ef, const char *fmt, ...)
+{
+ fail = false;
+}
+
+ATF_TC(ecalloc);
+ATF_TC_HEAD(ecalloc, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of ecalloc(3)");
+}
+
+ATF_TC_BODY(ecalloc, tc)
+{
+ char *x;
+
+ fail = true;
+ x = ecalloc(-1, 1);
+
+ ATF_REQUIRE(x == NULL);
+ ATF_REQUIRE(fail != true);
+
+ fail = true;
+ x = ecalloc(SIZE_MAX, 2);
+
+ ATF_REQUIRE(x == NULL);
+ ATF_REQUIRE(fail != true);
+}
+
+ATF_TC(efopen);
+ATF_TC_HEAD(efopen, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of efopen(3)");
+}
+
+ATF_TC_BODY(efopen, tc)
+{
+ FILE *f;
+
+ fail = true;
+ f = efopen("XXX", "XXX");
+
+ ATF_REQUIRE(f == NULL);
+ ATF_REQUIRE(fail != true);
+}
+
+ATF_TC(emalloc);
+ATF_TC_HEAD(emalloc, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of emalloc(3)");
+}
+
+ATF_TC_BODY(emalloc, tc)
+{
+ char *x;
+
+ fail = true;
+ x = emalloc(-1);
+
+ ATF_REQUIRE(x == NULL);
+ ATF_REQUIRE(fail != true);
+}
+
+ATF_TC(erealloc);
+ATF_TC_HEAD(erealloc, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of erealloc(3)");
+}
+
+ATF_TC_BODY(erealloc, tc)
+{
+ char *x;
+
+ fail = true;
+ x = erealloc(NULL, -1);
+
+ ATF_REQUIRE(x == NULL);
+ ATF_REQUIRE(fail != true);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_REQUIRE(esetfunc(handler) != NULL);
+
+ ATF_TP_ADD_TC(tp, ecalloc);
+ ATF_TP_ADD_TC(tp, efopen);
+ ATF_TP_ADD_TC(tp, emalloc);
+ ATF_TP_ADD_TC(tp, erealloc);
+
+ return atf_no_error();
+}
diff --git a/lib/libutil/t_parsedate.c b/lib/libutil/t_parsedate.c
new file mode 100644
index 000000000000..ec035a6dfaf6
--- /dev/null
+++ b/lib/libutil/t_parsedate.c
@@ -0,0 +1,142 @@
+/* $NetBSD: t_parsedate.c,v 1.7 2013/01/19 15:21:43 apb Exp $ */
+/*-
+ * Copyright (c) 2010 The NetBSD Foundation, Inc.
+ * 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 COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDERS 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>
+__RCSID("$NetBSD: t_parsedate.c,v 1.7 2013/01/19 15:21:43 apb Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <util.h>
+
+ATF_TC(dates);
+
+ATF_TC_HEAD(dates, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
+ " (PR lib/44255)");
+}
+
+ATF_TC_BODY(dates, tc)
+{
+
+ ATF_CHECK(parsedate("69-09-10", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("2006-11-17", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("10/1/2000", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("20 Jun 1994", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("23jun2001", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("1-sep-06", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("1/11", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("1500-01-02", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("9999-12-21", NULL, NULL) != -1);
+}
+
+ATF_TC(times);
+
+ATF_TC_HEAD(times, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test times"
+ " (PR lib/44255)");
+}
+
+ATF_TC_BODY(times, tc)
+{
+
+ ATF_CHECK(parsedate("10:01", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("10:12pm", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("12:11:01.000012", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("12:21-0500", NULL, NULL) != -1);
+}
+
+ATF_TC(relative);
+
+ATF_TC_HEAD(relative, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test relative items"
+ " (PR lib/44255)");
+}
+
+ATF_TC_BODY(relative, tc)
+{
+
+ ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
+ ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
+}
+
+ATF_TC(atsecs);
+
+ATF_TC_HEAD(atsecs, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
+}
+
+ATF_TC_BODY(atsecs, tc)
+{
+ int tzoff;
+
+ /* "@0" -> (time_t)0, regardless of timezone */
+ ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
+ putenv(__UNCONST("TZ=Europe/Berlin"));
+ tzset();
+ ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
+ putenv(__UNCONST("TZ=America/New_York"));
+ tzset();
+ ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
+ tzoff = 0;
+ ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
+ tzoff = 3600;
+ ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
+ tzoff = -3600;
+ ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
+
+ /* -1 or other negative numbers are not errors */
+ errno = 0;
+ ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
+ ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
+
+ /* junk is an error */
+ errno = 0;
+ ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, dates);
+ ATF_TP_ADD_TC(tp, times);
+ ATF_TP_ADD_TC(tp, relative);
+ ATF_TP_ADD_TC(tp, atsecs);
+
+ return atf_no_error();
+}
diff --git a/lib/libutil/t_pidfile.c b/lib/libutil/t_pidfile.c
new file mode 100644
index 000000000000..a2aff833df38
--- /dev/null
+++ b/lib/libutil/t_pidfile.c
@@ -0,0 +1,363 @@
+/* $NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $ */
+
+/*
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * 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 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.
+ */
+
+/*
+ * This file implements tests for the pidfile(3) functions.
+ *
+ * The tests here are tricky because we need to validate that the atexit(3)
+ * handler registered by pidfile(3) correctly deletes the generated pidfile.
+ * To do so:
+ * 1) We spawn a subprocess in every test case,
+ * 2) Run our test code in such subprocesses. We cannot call any of the ATF
+ * primitives from inside these.
+ * 3) Wait for the subprocess to terminate and ensure it exited successfully.
+ * 4) Check that the pidfile(s) created in the subprocess are gone.
+ *
+ * Additionally, pidfile(3) hardcodes a path to a directory writable only by
+ * root (/var/run). This makes us require root privileges to execute these
+ * tests.
+ */
+
+#include <sys/cdefs.h>
+__COPYRIGHT("@(#) Copyright (c) 2011\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_pidfile.c,v 1.3 2011/03/29 13:55:37 jmmv Exp $");
+
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <assert.h>
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <paths.h>
+#include <unistd.h>
+#include <util.h>
+
+#include <atf-c.h>
+
+/* Used by routines that can be called both from the parent and the child
+ * code to implement proper error reporting. */
+static bool in_child = false;
+
+/* Callable from the test case child code. */
+static void
+check_pidfile(const char *path)
+{
+ FILE *file;
+ int pid;
+
+ printf("Validating contents of pidfile '%s'\n", path);
+
+ if ((file = fopen(path, "r")) == NULL)
+ errx(EXIT_FAILURE, "Cannot open expected pidfile '%s'", path);
+
+ if (fscanf(file, "%d", &pid) == -1)
+ errx(EXIT_FAILURE, "Failed to read pid from pidfile '%s'",
+ path);
+
+ printf("Read pid %d, current pid %d\n", pid, getpid());
+ if (pid != getpid())
+ errx(EXIT_FAILURE, "Pid in pidfile (%d) does not match "
+ "current pid (%d)", pid, getpid());
+}
+
+/* Callable from the test case parent/child code. */
+static void
+ensure_deleted(const char *path)
+{
+ printf("Ensuring pidfile %s does not exist any more\n", path);
+ if (access(path, R_OK) != -1) {
+ unlink(path);
+ if (in_child)
+ errx(EXIT_FAILURE, "The pidfile %s was not deleted",
+ path);
+ else
+ atf_tc_fail("The pidfile %s was not deleted", path);
+ }
+}
+
+/* Callable from the test case parent code. */
+static void
+run_child(void (*child)(const char *), const char *cookie)
+{
+ pid_t pid;
+
+ pid = fork();
+ ATF_REQUIRE(pid != -1);
+ if (pid == 0) {
+ in_child = true;
+ child(cookie);
+ assert(false);
+ /* UNREACHABLE */
+ } else {
+ int status;
+
+ ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
+ atf_tc_fail("See stderr for details");
+ }
+}
+
+/* Callable from the test case parent/child code. */
+static char *
+generate_varrun_pidfile(const char *basename)
+{
+ char *path;
+
+ if (asprintf(&path, "%s%s.pid", _PATH_VARRUN,
+ basename == NULL ? getprogname() : basename) == -1) {
+ if (in_child)
+ errx(EXIT_FAILURE, "Cannot allocate memory for path");
+ else
+ atf_tc_fail("Cannot allocate memory for path");
+ }
+
+ return path;
+}
+
+static void
+helper_default_path(const char *path)
+{
+
+ if (pidfile(NULL) == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile with default "
+ "basename");
+
+ check_pidfile(path);
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC(default_path);
+ATF_TC_HEAD(default_path, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(default_path, tc)
+{
+ char *path;
+
+ path = generate_varrun_pidfile(NULL);
+ run_child(helper_default_path, path);
+ ensure_deleted(path);
+ free(path);
+}
+
+static void
+helper_custom_basename(const char *path)
+{
+
+ if (pidfile("custom-basename") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile with custom "
+ "basename");
+
+ check_pidfile(path);
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC(custom_basename);
+ATF_TC_HEAD(custom_basename, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(custom_basename, tc)
+{
+ char *path;
+
+ path = generate_varrun_pidfile("custom-basename");
+ run_child(helper_custom_basename, path);
+ ensure_deleted(path);
+ free(path);
+}
+
+static void
+helper_custom_path(const char *path)
+{
+
+ if (pidfile(path) == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile '%s'", path);
+ check_pidfile(path);
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC_WITHOUT_HEAD(custom_path);
+ATF_TC_BODY(custom_path, tc)
+{
+
+ ATF_REQUIRE(mkdir("var", 0777) != -1);
+ ATF_REQUIRE(mkdir("var/run", 0777) != -1);
+
+ run_child(helper_custom_path, "./var/run/my-pidfile.pid");
+
+ ensure_deleted("./var/run/my-pidfile.pid");
+}
+
+static void
+helper_change_basenames(const char *unused_cookie)
+{
+ char *default_path;
+ char *custom_path;
+
+ default_path = generate_varrun_pidfile(NULL);
+ if (pidfile(NULL) == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile with default "
+ "basename");
+ check_pidfile(default_path);
+ if (pidfile(NULL) == -1)
+ errx(EXIT_FAILURE, "Failed to recreate pidfile with default "
+ "basename");
+ check_pidfile(default_path);
+
+ custom_path = generate_varrun_pidfile("custom-basename");
+ if (pidfile("custom-basename") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile with custom "
+ "basename");
+ ensure_deleted(default_path);
+ check_pidfile(custom_path);
+ if (pidfile("custom-basename") == -1)
+ errx(EXIT_FAILURE, "Failed to recreate pidfile with custom "
+ "basename");
+ check_pidfile(custom_path);
+
+ free(custom_path);
+ free(default_path);
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC(change_basenames);
+ATF_TC_HEAD(change_basenames, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(change_basenames, tc)
+{
+ char *default_path;
+ char *custom_path;
+
+ run_child(helper_change_basenames, NULL);
+
+ default_path = generate_varrun_pidfile(NULL);
+ custom_path = generate_varrun_pidfile("custom-basename");
+
+ ensure_deleted(default_path);
+ ensure_deleted(custom_path);
+
+ free(custom_path);
+ free(default_path);
+}
+
+static void
+helper_change_paths(const char *unused_cookie)
+{
+
+ if (pidfile("./var/run/first.pid") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile "
+ "'./var/run/first.pid'");
+ check_pidfile("./var/run/first.pid");
+
+ if (pidfile("./second.pid") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
+ ensure_deleted("./var/run/first.pid");
+ check_pidfile("./second.pid");
+
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC_WITHOUT_HEAD(change_paths);
+ATF_TC_BODY(change_paths, tc)
+{
+
+ ATF_REQUIRE(mkdir("var", 0777) != -1);
+ ATF_REQUIRE(mkdir("var/run", 0777) != -1);
+
+ run_child(helper_change_paths, NULL);
+
+ ensure_deleted("./var/run/my-pidfile.pid");
+ ensure_deleted("second.pid");
+}
+
+static void
+helper_mix(const char *unused_cookie)
+{
+ char *default_path;
+ char *custom_path;
+
+ default_path = generate_varrun_pidfile(NULL);
+ custom_path = generate_varrun_pidfile("custom-basename");
+
+ if (pidfile(NULL) == -1)
+ errx(EXIT_FAILURE, "Failed to create default pidfile");
+ check_pidfile(default_path);
+
+ if (pidfile("./second.pid") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
+ ensure_deleted(default_path);
+ check_pidfile("./second.pid");
+
+ if (pidfile("custom-basename") == -1)
+ errx(EXIT_FAILURE, "Failed to create pidfile 'second.pid'");
+ ensure_deleted(default_path);
+ ensure_deleted("./second.pid");
+ ensure_deleted("./custom-basename");
+ check_pidfile(custom_path);
+
+ free(custom_path);
+ free(default_path);
+ exit(EXIT_SUCCESS);
+}
+
+ATF_TC(change_mix);
+ATF_TC_HEAD(change_mix, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(change_mix, tc)
+{
+ char *default_path;
+
+ run_child(helper_mix, NULL);
+
+ default_path = generate_varrun_pidfile(NULL);
+ ensure_deleted(default_path);
+ ensure_deleted("second.pid");
+ free(default_path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, default_path);
+ ATF_TP_ADD_TC(tp, custom_basename);
+ ATF_TP_ADD_TC(tp, custom_path);
+ ATF_TP_ADD_TC(tp, change_basenames);
+ ATF_TP_ADD_TC(tp, change_paths);
+ ATF_TP_ADD_TC(tp, change_mix);
+
+ return atf_no_error();
+}
diff --git a/lib/libutil/t_snprintb.c b/lib/libutil/t_snprintb.c
new file mode 100644
index 000000000000..8863e8b13657
--- /dev/null
+++ b/lib/libutil/t_snprintb.c
@@ -0,0 +1,117 @@
+/* $NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $ */
+
+/*
+ * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code was contributed to The NetBSD Foundation by Christos Zoulas.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008, 2010\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_snprintb.c,v 1.4 2014/06/06 06:59:21 shm Exp $");
+
+#include <string.h>
+#include <util.h>
+
+#include <atf-c.h>
+
+static void
+h_snprintb(const char *fmt, uint64_t val, const char *res)
+{
+ char buf[1024];
+ int len, slen;
+
+ len = snprintb(buf, sizeof(buf), fmt, val);
+ slen = (int) strlen(res);
+
+ ATF_REQUIRE_STREQ(res, buf);
+ ATF_REQUIRE_EQ(len, slen);
+}
+
+ATF_TC(snprintb);
+ATF_TC_HEAD(snprintb, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks snprintb(3)");
+}
+ATF_TC_BODY(snprintb, tc)
+{
+ h_snprintb("\10\2BITTWO\1BITONE", 3, "03<BITTWO,BITONE>");
+ h_snprintb("\177\20b\0A\0\0", 0, "0x0");
+
+ h_snprintb("\177\20b\05NOTBOOT\0b\06FPP\0b\013SDVMA\0b\015VIDEO\0"
+ "b\020LORES\0b\021FPA\0b\022DIAG\0b\016CACHE\0"
+ "b\017IOCACHE\0b\022LOOPBACK\0b\04DBGCACHE\0",
+ 0xe860, "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>");
+}
+
+static void
+h_snprintb_m(const char *fmt, uint64_t val, int line_max, const char *res,
+ int res_len)
+{
+ char buf[1024];
+ int len;
+
+ len = snprintb_m(buf, sizeof(buf), fmt, val, line_max);
+
+ ATF_REQUIRE_EQ(len, res_len);
+ ATF_REQUIRE_EQ(0, memcmp(res, buf, res_len + 1));
+}
+
+ATF_TC(snprintb_m);
+ATF_TC_HEAD(snprintb_m, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Checks snprintb_m(3)");
+}
+ATF_TC_BODY(snprintb_m, tc)
+{
+ h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
+ "f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
+ "b\x1fMSB\0\0",
+ 0x800f0701,
+ 33,
+ "0x800f0701<LSB,NIBBLE2=0x0>\0"
+ "0x800f0701<BURST=0xf=SIXTEEN,MSB>\0\0",
+ 62);
+
+ h_snprintb_m("\177\020b\0LSB\0b\1_BITONE\0f\4\4NIBBLE2\0"
+ "f\x10\4BURST\0=\4FOUR\0=\xfSIXTEEN\0"
+ "b\x1fMSB\0\0",
+ 0x800f0701,
+ 32,
+ "0x800f0701<LSB,NIBBLE2=0x0>\0"
+ "0x800f0701<BURST=0xf=SIXTEEN>\0"
+ "0x800f0701<MSB>\0\0",
+ 74);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, snprintb);
+ ATF_TP_ADD_TC(tp, snprintb_m);
+
+ return atf_no_error();
+}
diff --git a/lib/libutil/t_sockaddr_snprintf.c b/lib/libutil/t_sockaddr_snprintf.c
new file mode 100644
index 000000000000..b4e8cb37011b
--- /dev/null
+++ b/lib/libutil/t_sockaddr_snprintf.c
@@ -0,0 +1,185 @@
+/* $NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $ */
+
+/*
+ * Copyright (c) 2002, 2004, 2008, 2010 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code was contributed to The NetBSD Foundation by Christos Zoulas.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2008, 2010\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_sockaddr_snprintf.c,v 1.1 2010/07/16 13:56:32 jmmv Exp $");
+
+#include <sys/socket.h> /* AF_ */
+#include <sys/un.h> /* sun */
+
+#include <net/if_dl.h> /* sdl */
+#include <netatalk/at.h> /* sat */
+#include <netinet/in.h> /* sin/sin6 */
+
+#include <string.h>
+#include <util.h>
+
+#include <atf-c.h>
+
+ATF_TC(sockaddr_snprintf_in);
+ATF_TC_HEAD(sockaddr_snprintf_in, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks sockaddr_snprintf(3) with sockaddr_in");
+}
+ATF_TC_BODY(sockaddr_snprintf_in, tc)
+{
+ char buf[1024];
+ struct sockaddr_in sin4;
+ int i;
+
+ memset(&sin4, 0, sizeof(sin4));
+ sin4.sin_len = sizeof(sin4);
+ sin4.sin_family = AF_INET;
+ sin4.sin_port = ntohs(80);
+ sin4.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
+ i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
+ (struct sockaddr *)&sin4);
+
+ ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sin4");
+ ATF_REQUIRE_STREQ(buf, "2 16 80 127.0.0.1");
+}
+
+ATF_TC(sockaddr_snprintf_in6);
+ATF_TC_HEAD(sockaddr_snprintf_in6, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks sockaddr_snprintf(3) with sockaddr_in6");
+}
+ATF_TC_BODY(sockaddr_snprintf_in6, tc)
+{
+#ifdef INET6
+ char buf[1024];
+ struct sockaddr_in6 sin6;
+ int i;
+
+ memset(&sin6, 0, sizeof(sin6));
+ sin6.sin6_len = sizeof(sin6);
+ sin6.sin6_family = AF_INET6;
+ sin6.sin6_port = ntohs(80);
+ sin6.sin6_addr = in6addr_nodelocal_allnodes;
+ i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %p %a",
+ (struct sockaddr *)&sin6);
+
+ ATF_REQUIRE_EQ_MSG(i, 16, "bad length for sin6");
+ ATF_REQUIRE_STREQ(buf, "24 28 80 ff01::1");
+#else
+ atf_tc_skip("Tests built with USE_INET6=no");
+#endif /* INET6 */
+}
+
+ATF_TC(sockaddr_snprintf_un);
+ATF_TC_HEAD(sockaddr_snprintf_un, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks sockaddr_snprintf(3) with sockaddr_un");
+}
+ATF_TC_BODY(sockaddr_snprintf_un, tc)
+{
+ char buf[1024];
+ struct sockaddr_un sun;
+ int i;
+
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_len = sizeof(sun);
+ sun.sun_family = AF_UNIX;
+ strncpy(sun.sun_path, "/tmp/sock", sizeof(sun.sun_path));
+ i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
+ (struct sockaddr *)&sun);
+
+ ATF_REQUIRE_EQ_MSG(i, 15, "bad length for sun");
+ ATF_REQUIRE_STREQ(buf, "1 106 /tmp/sock");
+}
+
+ATF_TC(sockaddr_snprintf_at);
+ATF_TC_HEAD(sockaddr_snprintf_at, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks sockaddr_snprintf(3) with sockaddr_at");
+}
+ATF_TC_BODY(sockaddr_snprintf_at, tc)
+{
+ char buf[1024];
+ struct sockaddr_at sat;
+ int i;
+
+ memset(&sat, 0, sizeof(sat));
+ sat.sat_len = sizeof(sat);
+ sat.sat_family = AF_APPLETALK;
+ sat.sat_addr.s_net = ntohs(101);
+ sat.sat_addr.s_node = 3;
+ i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
+ (struct sockaddr *)&sat);
+
+ ATF_REQUIRE_EQ_MSG(i, 11, "bad length for sat");
+ ATF_REQUIRE_STREQ(buf, "16 16 101.3");
+}
+
+ATF_TC(sockaddr_snprintf_dl);
+ATF_TC_HEAD(sockaddr_snprintf_dl, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks sockaddr_snprintf(3) with sockaddr_dl");
+}
+ATF_TC_BODY(sockaddr_snprintf_dl, tc)
+{
+ char buf[1024];
+ struct sockaddr_dl sdl;
+ int i;
+
+ memset(&sdl, 0, sizeof(sdl));
+ sdl.sdl_len = sizeof(sdl);
+ sdl.sdl_family = AF_LINK;
+ sdl.sdl_index = 0;
+ sdl.sdl_type = 0;
+ sdl.sdl_nlen = 0;
+ sdl.sdl_alen = 6;
+ sdl.sdl_slen = 0;
+ memcpy(sdl.sdl_data, "\01\02\03\04\05\06", 6);
+ i = sockaddr_snprintf(buf, sizeof(buf), "%f %l %a",
+ (struct sockaddr *)&sdl);
+
+ ATF_REQUIRE_EQ_MSG(i, 17, "bad length for sdl");
+ ATF_REQUIRE_STREQ(buf, "18 20 1.2.3.4.5.6");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, sockaddr_snprintf_in);
+ ATF_TP_ADD_TC(tp, sockaddr_snprintf_in6);
+ ATF_TP_ADD_TC(tp, sockaddr_snprintf_un);
+ ATF_TP_ADD_TC(tp, sockaddr_snprintf_at);
+ ATF_TP_ADD_TC(tp, sockaddr_snprintf_dl);
+
+ return atf_no_error();
+}