1124861Sharti/* $NetBSD: h_macros.h,v 1.13 2016/08/20 15:49:08 christos Exp $ */
2124861Sharti
3124861Sharti/*-
4124861Sharti * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5124861Sharti * All rights reserved.
6124861Sharti *
7133211Sharti * Redistribution and use in source and binary forms, with or without
8133211Sharti * modification, are permitted provided that the following conditions
9133211Sharti * are met:
10133211Sharti * 1. Redistributions of source code must retain the above copyright
11133211Sharti *    notice, this list of conditions and the following disclaimer.
12133211Sharti * 2. Redistributions in binary form must reproduce the above copyright
13124861Sharti *    notice, this list of conditions and the following disclaimer in the
14124861Sharti *    documentation and/or other materials provided with the distribution.
15124861Sharti *
16133211Sharti * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17133211Sharti * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18133211Sharti * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19133211Sharti * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20133211Sharti * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21133211Sharti * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22133211Sharti * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23133211Sharti * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24133211Sharti * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25133211Sharti * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26133211Sharti * POSSIBILITY OF SUCH DAMAGE.
27133211Sharti */
28124861Sharti
29150920Sharti#ifndef SRC_TESTS_H_MACROS_H_
30124861Sharti#define SRC_TESTS_H_MACROS_H_
31124861Sharti
32124861Sharti#include <sys/types.h>
33124861Sharti#include <errno.h>
34124861Sharti#include <stdarg.h>
35124861Sharti#include <stdio.h>
36124861Sharti#include <stdlib.h>
37124861Sharti#include <string.h>
38124861Sharti
39124861Sharti#include <atf-c.h>
40124861Sharti
41124861Sharti#define REQUIRE_LIBC(x, v) \
42124861Sharti	ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
43124861Sharti
44124861Sharti#define CHECK_LIBC(x, v) \
45124861Sharti	ATF_CHECK_MSG((x) != (v), "%s: %s", #x, strerror(errno))
46124861Sharti
47124861Sharti#define RL(x) REQUIRE_LIBC(x, -1)
48124861Sharti#define RLF(x, fmt, arg) \
49124861Sharti	ATF_CHECK_MSG((x) != -1, "%s [" fmt "]: %s", #x, arg, strerror(errno))
50124861Sharti#define RZ(x)								\
51124861Shartido {									\
52124861Sharti	int RZ_rv = x;							\
53124861Sharti	ATF_REQUIRE_MSG(RZ_rv == 0, "%s: %s", #x, strerror(RZ_rv));	\
54124861Sharti} while (/*CONSTCOND*/0)
55124861Sharti
56124861Sharti__dead static __inline __printflike(1, 2) void
57124861Shartiatf_tc_fail_errno(const char *fmt, ...)
58124861Sharti{
59124861Sharti	va_list ap;
60124861Sharti	char buf[1024];
61124861Sharti	int sverrno = errno;
62124861Sharti
63124861Sharti	va_start(ap, fmt);
64124861Sharti	vsnprintf(buf, sizeof(buf), fmt, ap);
65124861Sharti	va_end(ap);
66124861Sharti
67124861Sharti	strlcat(buf, ": ", sizeof(buf));
68124861Sharti	strlcat(buf, strerror(sverrno), sizeof(buf));
69124861Sharti
70124861Sharti	atf_tc_fail("%s", buf);
71124861Sharti}
72124861Sharti
73124861Shartistatic __inline void
74124861Shartitests_makegarbage(void *space, size_t len)
75124861Sharti{
76124861Sharti	uint16_t *sb = space;
77124861Sharti	uint16_t randval;
78124861Sharti
79124861Sharti	while (len >= sizeof(randval)) {
80124861Sharti		*sb++ = (uint16_t)random();
81124861Sharti		len -= sizeof(*sb);
82124861Sharti	}
83124861Sharti	randval = (uint16_t)random();
84124861Sharti	memcpy(sb, &randval, len);
85124861Sharti}
86124861Sharti
87124861Sharti#endif
88124861Sharti