]> granicus.if.org Git - strace/blob - tests/tests.h
tests: add skip_if_unavailable function to libtests
[strace] / tests / tests.h
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef STRACE_TESTS_H
29 #define STRACE_TESTS_H
30
31 # ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 # endif
34
35 # include <sys/types.h>
36 # include "kernel_types.h"
37 # include "gcc_compat.h"
38
39 /* Tests of "strace -v" are expected to define VERBOSE to 1. */
40 #ifndef VERBOSE
41 # define VERBOSE 0
42 #endif
43
44 /* Cached sysconf(_SC_PAGESIZE). */
45 size_t get_page_size(void);
46
47 /* The size of kernel's sigset_t. */
48 unsigned int get_sigset_size(void);
49
50 /* Print message and strerror(errno) to stderr, then exit(1). */
51 void perror_msg_and_fail(const char *, ...)
52         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
53 /* Print message to stderr, then exit(1). */
54 void error_msg_and_fail(const char *, ...)
55         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
56 /* Print message to stderr, then exit(77). */
57 void error_msg_and_skip(const char *, ...)
58         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
59 /* Print message and strerror(errno) to stderr, then exit(77). */
60 void perror_msg_and_skip(const char *, ...)
61         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
62
63 /* Stat the specified file and skip the test if the stat call failed. */
64 void skip_if_unavailable(const char *);
65
66 /*
67  * Allocate memory that ends on the page boundary.
68  * Pages allocated by this call are preceeded by an unmapped page
69  * and followed also by an unmapped page.
70  */
71 void *tail_alloc(const size_t)
72         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
73 /* Allocate memory using tail_alloc, then memcpy. */
74 void *tail_memdup(const void *, const size_t)
75         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((2));
76
77 /*
78  * Allocate an object of the specified type at the end
79  * of a mapped memory region.
80  * Assign its address to the specified constant pointer.
81  */
82 #define TAIL_ALLOC_OBJECT_CONST_PTR(type_name, type_ptr)        \
83         type_name *const type_ptr = tail_alloc(sizeof(*type_ptr))
84
85 /*
86  * Allocate an object of the specified type at the end
87  * of a mapped memory region.
88  * Assign its address to the specified variable pointer.
89  */
90 #define TAIL_ALLOC_OBJECT_VAR_PTR(type_name, type_ptr)          \
91         type_name *type_ptr = tail_alloc(sizeof(*type_ptr))
92
93 /*
94  * Fill memory (pointed by ptr, having size bytes) with different bytes (with
95  * values starting with start and resetting every period) in order to catch
96  * sign, byte order and/or alignment errors.
97  */
98 void fill_memory_ex(void *ptr, size_t size, unsigned char start,
99                     unsigned char period);
100 /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */
101 void fill_memory(void *ptr, size_t size);
102
103 /* Close stdin, move stdout to a non-standard descriptor, and print. */
104 void tprintf(const char *, ...)
105         ATTRIBUTE_FORMAT((printf, 1, 2));
106
107 /* Make a hexdump copy of C string */
108 const char *hexdump_strdup(const char *);
109
110 /* Make a hexdump copy of memory */
111 const char *hexdump_memdup(const char *, size_t);
112
113 /* Make a hexquoted copy of a string */
114 const char *hexquote_strndup(const char *, size_t);
115
116 /* Return inode number of socket descriptor. */
117 unsigned long inode_of_sockfd(int);
118
119 /* Print string in a quoted form. */
120 void print_quoted_string(const char *);
121
122 /* Print memory in a quoted form. */
123 void print_quoted_memory(const char *, size_t);
124
125 /* Print time_t and nanoseconds in symbolic format. */
126 void print_time_t_nsec(time_t, unsigned long long);
127
128 /* Read an int from the file. */
129 int read_int_from_file(const char *, int *);
130
131 /* Check whether given uid matches kernel overflowuid. */
132 void check_overflowuid(const int);
133
134 /* Check whether given gid matches kernel overflowgid. */
135 void check_overflowgid(const int);
136
137 /* Translate errno to its name. */
138 const char *errno2name(void);
139
140 /* Translate signal number to its name. */
141 const char *signal2name(int);
142
143 /* Print return code and, in case return code is -1, errno information. */
144 const char *sprintrc(long rc);
145 /* sprintrc variant suitable for usage as part of grep pattern. */
146 const char *sprintrc_grep(long rc);
147
148 struct xlat;
149
150 /* Print flags in symbolic form according to xlat table. */
151 int printflags(const struct xlat *, const unsigned long long, const char *);
152
153 /* Print constant in symbolic form according to xlat table. */
154 int printxval(const struct xlat *, const unsigned long long, const char *);
155
156 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
157 int socketcall(const int nr, const int call,
158                long a1, long a2, long a3, long a4, long a5);
159
160 /* Wrappers for recvmmsg and sendmmsg syscalls. */
161 struct mmsghdr;
162 struct timespec;
163 int recv_mmsg(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
164 int send_mmsg(int, struct mmsghdr *, unsigned int, unsigned int);
165
166 /* Create a pipe with maximized descriptor numbers. */
167 void pipe_maxfd(int pipefd[2]);
168
169 #define F8ILL_KULONG_SUPPORTED  (sizeof(void *) < sizeof(kernel_ulong_t))
170 #define F8ILL_KULONG_MASK       ((kernel_ulong_t) 0xffffffff00000000ULL)
171
172 /*
173  * For 64-bit kernel_ulong_t and 32-bit pointer,
174  * return a kernel_ulong_t value by filling higher bits.
175  * For other architertures, return the original pointer.
176  */
177 static inline kernel_ulong_t
178 f8ill_ptr_to_kulong(const void *const ptr)
179 {
180         const unsigned long uptr = (unsigned long) ptr;
181         return F8ILL_KULONG_SUPPORTED
182                ? F8ILL_KULONG_MASK | uptr : (kernel_ulong_t) uptr;
183 }
184
185 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
186 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
187
188 /* Zero-extend a signed integer type to unsigned long long. */
189 #define zero_extend_signed_to_ull(v) \
190         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
191          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
192          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
193          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
194          (unsigned long long) (v))
195
196 /* Sign-extend an unsigned integer type to long long. */
197 #define sign_extend_unsigned_to_ll(v) \
198         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
199          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
200          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
201          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
202          (long long) (v))
203
204 # define SKIP_MAIN_UNDEFINED(arg) \
205         int main(void) { error_msg_and_skip("undefined: %s", arg); }
206
207 /*
208  * The kernel used to define 64-bit types on 64-bit systems on a per-arch
209  * basis.  Some architectures would use unsigned long and others would use
210  * unsigned long long.  These types were exported as part of the
211  * kernel-userspace ABI and now must be maintained forever.  This matches
212  * what the kernel exports for each architecture so we don't need to cast
213  * every printing of __u64 or __s64 to stdint types.
214  */
215 # if SIZEOF_LONG == 4
216 #  define PRI__64 "ll"
217 # elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
218 #  define PRI__64 "l"
219 # else
220 #  define PRI__64 "ll"
221 # endif
222
223 # define PRI__d64 PRI__64"d"
224 # define PRI__u64 PRI__64"u"
225 # define PRI__x64 PRI__64"x"
226
227 # if WORDS_BIGENDIAN
228 #  define LL_PAIR(HI, LO) (HI), (LO)
229 # else
230 #  define LL_PAIR(HI, LO) (LO), (HI)
231 # endif
232 # define LL_VAL_TO_PAIR(llval) LL_PAIR((long) ((llval) >> 32), (long) (llval))
233
234 # define _STR(_arg) #_arg
235 # define ARG_STR(_arg) (_arg), #_arg
236 # define ARG_ULL_STR(_arg) _arg##ULL, #_arg
237
238 #endif /* !STRACE_TESTS_H */