xref: /openbsd/regress/lib/libc/sys/h_macros.h (revision 5a38ef86)
1 /*	$OpenBSD: h_macros.h,v 1.2 2021/09/02 12:40:44 mbuhl Exp $	*/
2 /* $NetBSD: h_macros.h,v 1.13 2016/08/20 15:49:08 christos Exp $ */
3 
4 /*-
5  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef SRC_TESTS_H_MACROS_H_
31 #define SRC_TESTS_H_MACROS_H_
32 
33 #include <sys/types.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "atf-c.h"
41 
42 #define REQUIRE_LIBC(x, v) \
43 	ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno))
44 
45 #define CHECK_LIBC(x, v) \
46 	ATF_CHECK_MSG((x) != (v), "%s: %s", #x, strerror(errno))
47 
48 #define RL(x) REQUIRE_LIBC(x, -1)
49 #define RLF(x, fmt, arg) \
50 	ATF_CHECK_MSG((x) != -1, "%s [" fmt "]: %s", #x, arg, strerror(errno))
51 #define RZ(x)								\
52 do {									\
53 	int RZ_rv = x;							\
54 	ATF_REQUIRE_MSG(RZ_rv == 0, "%s: %s", #x, strerror(RZ_rv));	\
55 } while (/*CONSTCOND*/0)
56 
57 __dead static __inline __attribute__((__format__(__printf__,1,2))) void
58 atf_tc_fail_errno(const char *fmt, ...)
59 {
60 	va_list ap;
61 	char buf[1024];
62 	int sverrno = errno;
63 
64 	va_start(ap, fmt);
65 	vsnprintf(buf, sizeof(buf), fmt, ap);
66 	va_end(ap);
67 
68 	strlcat(buf, ": ", sizeof(buf));
69 	strlcat(buf, strerror(sverrno), sizeof(buf));
70 
71 	atf_tc_fail("%s", buf);
72 }
73 
74 static __inline void
75 tests_makegarbage(void *space, size_t len)
76 {
77 	uint16_t *sb = space;
78 	uint16_t randval;
79 
80 	while (len >= sizeof(randval)) {
81 		*sb++ = (uint16_t)random();
82 		len -= sizeof(*sb);
83 	}
84 	randval = (uint16_t)random();
85 	memcpy(sb, &randval, len);
86 }
87 
88 #endif
89