1*0a6a1f1dSLionel Sambuc /* $NetBSD: h_macros.h,v 1.11 2015/01/14 22:57:27 christos Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #ifndef SRC_TESTS_H_MACROS_H_
3011be35a1SLionel Sambuc #define SRC_TESTS_H_MACROS_H_
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambuc #include <sys/types.h>
3311be35a1SLionel Sambuc #include <errno.h>
3411be35a1SLionel Sambuc #include <stdarg.h>
3511be35a1SLionel Sambuc #include <stdio.h>
3611be35a1SLionel Sambuc #include <stdlib.h>
3711be35a1SLionel Sambuc #include <string.h>
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc #include <atf-c.h>
4011be35a1SLionel Sambuc
4111be35a1SLionel Sambuc #define REQUIRE_LIBC(x, v) \
4211be35a1SLionel Sambuc ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc #define CHECK_LIBC(x, v) \
4511be35a1SLionel Sambuc ATF_CHECK_MSG((x) != (v), "%s: %s", #x, strerror(errno))
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc #define RL(x) REQUIRE_LIBC(x, -1)
4811be35a1SLionel Sambuc #define RZ(x) \
4911be35a1SLionel Sambuc do { \
5011be35a1SLionel Sambuc int RZ_rv = x; \
5111be35a1SLionel Sambuc ATF_REQUIRE_MSG(RZ_rv == 0, "%s: %s", #x, strerror(RZ_rv)); \
5211be35a1SLionel Sambuc } while (/*CONSTCOND*/0)
5311be35a1SLionel Sambuc
5411be35a1SLionel Sambuc static __inline __printflike(1, 2) void
atf_tc_fail_errno(const char * fmt,...)5511be35a1SLionel Sambuc atf_tc_fail_errno(const char *fmt, ...)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc va_list ap;
5811be35a1SLionel Sambuc char buf[1024];
5911be35a1SLionel Sambuc int sverrno = errno;
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc va_start(ap, fmt);
6211be35a1SLionel Sambuc vsnprintf(buf, sizeof(buf), fmt, ap);
6311be35a1SLionel Sambuc va_end(ap);
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc strlcat(buf, ": ", sizeof(buf));
6611be35a1SLionel Sambuc strlcat(buf, strerror(sverrno), sizeof(buf));
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc atf_tc_fail("%s", buf);
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc static __inline void
tests_makegarbage(void * space,size_t len)7211be35a1SLionel Sambuc tests_makegarbage(void *space, size_t len)
7311be35a1SLionel Sambuc {
7411be35a1SLionel Sambuc uint16_t *sb = space;
7511be35a1SLionel Sambuc uint16_t randval;
7611be35a1SLionel Sambuc
7711be35a1SLionel Sambuc while (len >= sizeof(randval)) {
78*0a6a1f1dSLionel Sambuc *sb++ = (uint16_t)random();
7911be35a1SLionel Sambuc len -= sizeof(*sb);
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc randval = (uint16_t)random();
8211be35a1SLionel Sambuc memcpy(sb, &randval, len);
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc
8511be35a1SLionel Sambuc #endif
86