aboutsummaryrefslogtreecommitdiff
path: root/contrib/netbsd-tests/lib/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/netbsd-tests/lib/libutil')
-rw-r--r--contrib/netbsd-tests/lib/libutil/t_efun.c135
-rw-r--r--contrib/netbsd-tests/lib/libutil/t_parsedate.c583
-rw-r--r--contrib/netbsd-tests/lib/libutil/t_pidfile.c363
-rw-r--r--contrib/netbsd-tests/lib/libutil/t_snprintb.c117
-rw-r--r--contrib/netbsd-tests/lib/libutil/t_sockaddr_snprintf.c185
5 files changed, 1383 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/lib/libutil/t_efun.c b/contrib/netbsd-tests/lib/libutil/t_efun.c
new file mode 100644
index 000000000000..f5187f25ab56
--- /dev/null
+++ b/contrib/netbsd-tests/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/contrib/netbsd-tests/lib/libutil/t_parsedate.c b/contrib/netbsd-tests/lib/libutil/t_parsedate.c
new file mode 100644
index 000000000000..c78df58b6b1c
--- /dev/null
+++ b/contrib/netbsd-tests/lib/libutil/t_parsedate.c
@@ -0,0 +1,583 @@
+/* $NetBSD: t_parsedate.c,v 1.25 2016/06/22 15:01:38 kre Exp $ */
+/*-
+ * Copyright (c) 2010, 2015 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.25 2016/06/22 15:01:38 kre Exp $");
+
+#include <atf-c.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <util.h>
+
+/*
+ * ANY is used as a placeholder for values that do not need to be
+ * checked. The actual value is arbitrary. We don't use -1
+ * because some tests might want to use -1 as a literal value.
+ */
+#define ANY -30215
+
+/* parsecheck --
+ * call parsedate(), then call time_to_tm() on the result,
+ * and check that year/month/day/hour/minute/second are as expected.
+ *
+ * time_to_tm should usually be localtime_r or gmtime_r.
+ *
+ * Don't check values specified as ANY.
+ */
+static void
+parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
+ struct tm * time_to_tm(const time_t *, struct tm *),
+ int year, int month, int day, int hour, int minute, int second)
+{
+ time_t t;
+ struct tm tm;
+ char argstr[128];
+
+ /*
+ * printable version of the args.
+ *
+ * Note that printf("%.*d", 0, 0)) prints nothing at all,
+ * while printf("%.*d", 1, val) prints the value as usual.
+ */
+ snprintf(argstr, sizeof(argstr), "%s%s%s, %s%.*jd, %s%.*d",
+ /* NULL or \"<datestr>\" */
+ (datestr ? "\"" : ""),
+ (datestr ? datestr : "NULL"),
+ (datestr ? "\"" : ""),
+ /* NULL or *reftime */
+ (reftime ? "" : "NULL"),
+ (reftime ? 1 : 0),
+ (reftime ? (intmax_t)*reftime : (intmax_t)0),
+ /* NULL or *zoff */
+ (zoff ? "" : "NULL"),
+ (zoff ? 1 : 0),
+ (zoff ? *zoff : 0));
+
+ ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
+ "parsedate(%s) returned -1\n", argstr);
+ ATF_CHECK(time_to_tm(&t, &tm) != NULL);
+ if (year != ANY)
+ ATF_CHECK_MSG(tm.tm_year + 1900 == year,
+ "parsedate(%s) expected year %d got %d (+1900)\n",
+ argstr, year, (int)tm.tm_year);
+ if (month != ANY)
+ ATF_CHECK_MSG(tm.tm_mon + 1 == month,
+ "parsedate(%s) expected month %d got %d (+1)\n",
+ argstr, month, (int)tm.tm_mon);
+ if (day != ANY)
+ ATF_CHECK_MSG(tm.tm_mday == day,
+ "parsedate(%s) expected day %d got %d\n",
+ argstr, day, (int)tm.tm_mday);
+ if (hour != ANY)
+ ATF_CHECK_MSG(tm.tm_hour == hour,
+ "parsedate(%s) expected hour %d got %d\n",
+ argstr, hour, (int)tm.tm_hour);
+ if (minute != ANY)
+ ATF_CHECK_MSG(tm.tm_min == minute,
+ "parsedate(%s) expected minute %d got %d\n",
+ argstr, minute, (int)tm.tm_min);
+ if (second != ANY)
+ ATF_CHECK_MSG(tm.tm_sec == second,
+ "parsedate(%s) expected second %d got %d\n",
+ argstr, second, (int)tm.tm_sec);
+}
+
+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)
+{
+
+ parsecheck("9/10/69", NULL, NULL, localtime_r,
+ 2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
+ parsecheck("9/10/70", NULL, NULL, localtime_r,
+ 1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
+ parsecheck("69-09-10", NULL, NULL, localtime_r,
+ 69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
+ parsecheck("70-09-10", NULL, NULL, localtime_r,
+ 70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
+ parsecheck("2006-11-17", NULL, NULL, localtime_r,
+ 2006, 11, 17, 0, 0, 0);
+ parsecheck("10/1/2000", NULL, NULL, localtime_r,
+ 2000, 10, 1, 0, 0, 0); /* month/day/year */
+ parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
+ 1994, 6, 20, 0, 0, 0);
+ parsecheck("97 September 2", NULL, NULL, localtime_r,
+ 1997, 9, 2, 0, 0, 0);
+ parsecheck("23jun2001", NULL, NULL, localtime_r,
+ 2001, 6, 23, 0, 0, 0);
+ parsecheck("1-sep-06", NULL, NULL, localtime_r,
+ 2006, 9, 1, 0, 0, 0);
+ parsecheck("1/11", NULL, NULL, localtime_r,
+ ANY, 1, 11, 0, 0, 0); /* month/day */
+ parsecheck("1500-01-02", NULL, NULL, localtime_r,
+ 1500, 1, 2, 0, 0, 0);
+ parsecheck("9999-12-21", NULL, NULL, localtime_r,
+ 9999, 12, 21, 0, 0, 0);
+ parsecheck("2015.12.07.08.07.35", NULL, NULL, localtime_r,
+ 2015, 12, 7, 8, 7, 35);
+}
+
+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)
+{
+
+ parsecheck("10:01", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 10, 1, 0);
+ parsecheck("10:12pm", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 22, 12, 0);
+ parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 12, 11, 1);
+ parsecheck("12:21-0500", NULL, NULL, gmtime_r,
+ ANY, ANY, ANY, 12+5, 21, 0);
+ /* numeric zones not permitted with am/pm ... */
+ parsecheck("7 a.m. ICT", NULL, NULL, gmtime_r,
+ ANY, ANY, ANY, 7-7, 0, 0);
+ parsecheck("midnight", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 0, 0, 0);
+ parsecheck("mn", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 0, 0, 0);
+ parsecheck("noon", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 12, 0, 0);
+}
+
+ATF_TC(dsttimes);
+
+ATF_TC_HEAD(dsttimes, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test DST transition times"
+ " (PR lib/47916)");
+}
+
+ATF_TC_BODY(dsttimes, tc)
+{
+ struct tm tm;
+ time_t t;
+ int tzoff;
+
+ putenv(__UNCONST("TZ=EST"));
+ tzset();
+ parsecheck("12:0", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 12, 0, 0);
+
+ putenv(__UNCONST("TZ=Asia/Tokyo"));
+ tzset();
+ parsecheck("12:0", NULL, NULL, localtime_r,
+ ANY, ANY, ANY, 12, 0, 0);
+
+ /*
+ * When the effective local time is Tue Jul 9 13:21:53 BST 2013,
+ * check mktime("14:00")
+ */
+ putenv(__UNCONST("TZ=Europe/London"));
+ tzset();
+ tm = (struct tm){
+ .tm_year = 2013-1900, .tm_mon = 7-1, .tm_mday = 9,
+ .tm_hour = 13, .tm_min = 21, .tm_sec = 53,
+ .tm_isdst = 0 };
+ t = mktime(&tm);
+ ATF_CHECK(t != (time_t)-1);
+ parsecheck("14:00", &t, NULL, localtime_r,
+ 2013, 7, 9, 14, 0, 0);
+ tzoff = -60; /* British Summer Time */
+ parsecheck("14:00", &t, &tzoff, localtime_r,
+ 2013, 7, 9, 14, 0, 0);
+}
+
+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)
+{
+ struct tm tm;
+ time_t now;
+
+#define REL_CHECK(s, now, tm) do { \
+ time_t p, q; \
+ char nb[30], pb[30], qb[30]; \
+ p = parsedate(s, &now, NULL); \
+ q = mktime(&tm); \
+ ATF_CHECK_EQ_MSG(p, q, \
+ "From %jd (%24.24s) using \"%s\", obtained %jd (%24.24s); expected %jd (%24.24s)", \
+ (uintmax_t)now, ctime_r(&now, nb), \
+ s, (uintmax_t)p, ctime_r(&p, pb), (uintmax_t)q, \
+ ctime_r(&q, qb)); \
+ } while (/*CONSTCOND*/0)
+
+#define isleap(yr) (((yr) & 3) == 0 && (((yr) % 100) != 0 || \
+ ((1900+(yr)) % 400) == 0))
+
+ 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);
+
+ /*
+ * Test relative to a number of fixed dates. Avoid the
+ * edges of the time_t range to avert under- or overflow
+ * of the relative date, and use a prime step for maximum
+ * coverage of different times of day/week/month/year.
+ */
+ for (now = 0x00FFFFFF; now < 0xFF000000; now += 3777779) {
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mday--;
+ /* "yesterday" leaves time untouched */
+ tm.tm_isdst = -1;
+ REL_CHECK("yesterday", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mday++;
+ /* as does "tomorrow" */
+ tm.tm_isdst = -1;
+ REL_CHECK("tomorrow", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ if (tm.tm_wday > 4)
+ tm.tm_mday += 7;
+ tm.tm_mday += 4 - tm.tm_wday;
+ /* if a day name is mentioned, it means midnight (by default) */
+ tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
+ tm.tm_isdst = -1;
+ REL_CHECK("this thursday", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mday += 14 - (tm.tm_wday ? tm.tm_wday : 7);
+ tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
+ tm.tm_isdst = -1;
+ REL_CHECK("next sunday", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ if (tm.tm_wday <= 5)
+ tm.tm_mday -= 7;
+ tm.tm_mday += 5 - tm.tm_wday;
+ tm.tm_sec = tm.tm_min = 0;
+ tm.tm_hour = 16;
+ tm.tm_isdst = -1;
+ REL_CHECK("last friday 4 p.m.", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mday += 14;
+ if (tm.tm_wday > 3)
+ tm.tm_mday += 7;
+ tm.tm_mday += 3 - tm.tm_wday;
+ tm.tm_sec = tm.tm_min = 0;
+ tm.tm_hour = 3;
+ tm.tm_isdst = -1;
+ REL_CHECK("we fortnight 3 a.m.", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_min -= 5;
+ tm.tm_isdst = -1;
+ REL_CHECK("5 minutes ago", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_hour++;
+ tm.tm_min += 37;
+ tm.tm_isdst = -1;
+ REL_CHECK("97 minutes", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mon++;
+ if (tm.tm_mon == 1 &&
+ tm.tm_mday > 28 + isleap(tm.tm_year))
+ tm.tm_mday = 28 + isleap(tm.tm_year);
+ else if ((tm.tm_mon == 3 || tm.tm_mon == 5 ||
+ tm.tm_mon == 8 || tm.tm_mon == 10) && tm.tm_mday == 31)
+ tm.tm_mday = 30;
+ tm.tm_isdst = -1;
+ REL_CHECK("month", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mon += 2; /* "next" means add 2 ... */
+ if (tm.tm_mon == 13 &&
+ tm.tm_mday > 28 + isleap(tm.tm_year + 1))
+ tm.tm_mday = 28 + isleap(tm.tm_year + 1);
+ else if (tm.tm_mon == 8 && tm.tm_mday == 31)
+ tm.tm_mday = 30;
+ tm.tm_isdst = -1;
+ REL_CHECK("next month", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mon--;
+ if (tm.tm_mon == 1 &&
+ tm.tm_mday > 28 + isleap(tm.tm_year))
+ tm.tm_mday = 28 + isleap(tm.tm_year);
+ else if ((tm.tm_mon == 3 || tm.tm_mon == 5 ||
+ tm.tm_mon == 8 || tm.tm_mon == 10) && tm.tm_mday == 31)
+ tm.tm_mday = 30;
+ tm.tm_isdst = -1;
+ REL_CHECK("last month", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mon += 6;
+ if (tm.tm_mon == 13 &&
+ tm.tm_mday > 28 + isleap(tm.tm_year + 1))
+ tm.tm_mday = 28 + isleap(tm.tm_year + 1);
+ else if ((tm.tm_mon == 15 || tm.tm_mon == 17 ||
+ tm.tm_mon == 8 || tm.tm_mon == 10) && tm.tm_mday == 31)
+ tm.tm_mday = 30;
+ tm.tm_mday += 2;
+ tm.tm_isdst = -1;
+ REL_CHECK("+6 months 2 days", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_mon -= 9;
+ if (tm.tm_mon == 1 && tm.tm_mday > 28 + isleap(tm.tm_year))
+ tm.tm_mday = 28 + isleap(tm.tm_year);
+ else if ((tm.tm_mon == -9 || tm.tm_mon == -7 ||
+ tm.tm_mon == -2) && tm.tm_mday == 31)
+ tm.tm_mday = 30;
+ tm.tm_isdst = -1;
+ REL_CHECK("9 months ago", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ if (tm.tm_wday <= 2)
+ tm.tm_mday -= 7;
+ tm.tm_mday += 2 - tm.tm_wday;
+ tm.tm_isdst = -1;
+ tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+ REL_CHECK("1 week ago Tu", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_isdst = -1;
+ tm.tm_mday++;
+ tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+ REL_CHECK("midnight tomorrow", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_isdst = -1;
+ tm.tm_mday++;
+ tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+ REL_CHECK("tomorrow midnight", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ tm.tm_isdst = -1;
+ tm.tm_mday++;
+ tm.tm_hour = 12;
+ tm.tm_min = tm.tm_sec = 0;
+ REL_CHECK("noon tomorrow", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ if (tm.tm_wday > 2)
+ tm.tm_mday += 7;
+ tm.tm_mday += 2 - tm.tm_wday;
+ tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
+ tm.tm_isdst = -1;
+ REL_CHECK("midnight Tuesday", now, tm);
+
+ ATF_CHECK(localtime_r(&now, &tm) != NULL);
+ if (tm.tm_wday > 2 + 1)
+ tm.tm_mday += 7;
+ tm.tm_mday += 2 - tm.tm_wday;
+ tm.tm_mday++; /* xxx midnight --> the next day */
+ tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
+ tm.tm_isdst = -1;
+ REL_CHECK("Tuesday midnight", now, tm);
+ }
+}
+
+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_TC(zones);
+
+ATF_TC_HEAD(zones, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test parsing dates with zones");
+}
+
+ATF_TC_BODY(zones, tc)
+{
+ parsecheck("2015-12-06 16:11:48 UTC", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 16, 11, 48);
+ parsecheck("2015-12-06 16:11:48 UT", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 16, 11, 48);
+ parsecheck("2015-12-06 16:11:48 GMT", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 16, 11, 48);
+ parsecheck("2015-12-06 16:11:48 +0000", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 16, 11, 48);
+
+ parsecheck("2015-12-06 16:11:48 -0500", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 21, 11, 48);
+ parsecheck("2015-12-06 16:11:48 EST", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 21, 11, 48);
+ parsecheck("2015-12-06 16:11:48 EDT", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 20, 11, 48);
+ parsecheck("2015-12-06 16:11:48 +0500", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 11, 11, 48);
+
+ parsecheck("2015-12-06 16:11:48 +1000", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 6, 11, 48);
+ parsecheck("2015-12-06 16:11:48 AEST", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 6, 11, 48);
+ parsecheck("2015-12-06 16:11:48 -1000", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 2, 11, 48);
+ parsecheck("2015-12-06 16:11:48 HST", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 2, 11, 48);
+
+ parsecheck("2015-12-06 16:11:48 AWST", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 8, 11, 48);
+ parsecheck("2015-12-06 16:11:48 NZDT", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 3, 11, 48);
+
+ parsecheck("Sun, 6 Dec 2015 09:43:16 -0500", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 14, 43, 16);
+ parsecheck("Mon Dec 7 03:13:31 ICT 2015", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 20, 13, 31);
+ /* the day name is ignored when a day of month (etc) is given... */
+ parsecheck("Sat Dec 7 03:13:31 ICT 2015", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 20, 13, 31);
+
+
+ parsecheck("2015-12-06 12:00:00 IDLW", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 0, 0, 0);
+ parsecheck("2015-12-06 12:00:00 IDLE", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 0, 0, 0);
+
+ parsecheck("2015-12-06 21:17:33 NFT", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 0, 47, 33);
+ parsecheck("2015-12-06 21:17:33 ACST", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 11, 47, 33);
+ parsecheck("2015-12-06 21:17:33 +0717", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 14, 0, 33);
+
+ parsecheck("2015-12-06 21:21:21 Z", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 21, 21, 21);
+ parsecheck("2015-12-06 21:21:21 A", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 22, 21, 21);
+ parsecheck("2015-12-06 21:21:21 G", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 4, 21, 21);
+ parsecheck("2015-12-06 21:21:21 M", NULL, NULL, gmtime_r,
+ 2015, 12, 7, 9, 21, 21);
+ parsecheck("2015-12-06 21:21:21 N", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 20, 21, 21);
+ parsecheck("2015-12-06 21:21:21 T", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 14, 21, 21);
+ parsecheck("2015-12-06 21:21:21 Y", NULL, NULL, gmtime_r,
+ 2015, 12, 6, 9, 21, 21);
+
+}
+
+ATF_TC(gibberish);
+
+ATF_TC_HEAD(gibberish, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test (not) parsing nonsense");
+}
+
+ATF_TC_BODY(gibberish, tc)
+{
+ errno = 0;
+ ATF_CHECK(parsedate("invalid nonsense", NULL, NULL) == (time_t)-1
+ && errno != 0);
+ errno = 0;
+ ATF_CHECK(parsedate("12th day of Christmas", NULL, NULL) == (time_t)-1
+ && errno != 0);
+ errno = 0;
+ ATF_CHECK(parsedate("2015-31-07 15:00", NULL, NULL) == (time_t)-1
+ && errno != 0);
+ errno = 0;
+ ATF_CHECK(parsedate("2015-02-29 10:01", NULL, NULL) == (time_t)-1
+ && errno != 0);
+ errno = 0;
+ ATF_CHECK(parsedate("2015-12-06 24:01", NULL, NULL) == (time_t)-1
+ && errno != 0);
+ errno = 0;
+ ATF_CHECK(parsedate("2015-12-06 14:61", 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, dsttimes);
+ ATF_TP_ADD_TC(tp, relative);
+ ATF_TP_ADD_TC(tp, atsecs);
+ ATF_TP_ADD_TC(tp, zones);
+ ATF_TP_ADD_TC(tp, gibberish);
+
+ return atf_no_error();
+}
+
diff --git a/contrib/netbsd-tests/lib/libutil/t_pidfile.c b/contrib/netbsd-tests/lib/libutil/t_pidfile.c
new file mode 100644
index 000000000000..a2aff833df38
--- /dev/null
+++ b/contrib/netbsd-tests/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/contrib/netbsd-tests/lib/libutil/t_snprintb.c b/contrib/netbsd-tests/lib/libutil/t_snprintb.c
new file mode 100644
index 000000000000..8863e8b13657
--- /dev/null
+++ b/contrib/netbsd-tests/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/contrib/netbsd-tests/lib/libutil/t_sockaddr_snprintf.c b/contrib/netbsd-tests/lib/libutil/t_sockaddr_snprintf.c
new file mode 100644
index 000000000000..b4e8cb37011b
--- /dev/null
+++ b/contrib/netbsd-tests/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();
+}