blob: 1575ecd229a355e6a2b846764c8de9b172599b2d [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001From 87e6b230fff800eb768b68b2e5173ebbe83fd3ef Mon Sep 17 00:00:00 2001
2From: "Dmitry V. Levin" <ldv@altlinux.org>
3Date: Sat, 2 Jan 2016 12:05:14 +0000
4Subject: [PATCH] tests: introduce libtests
5
6Introduce tests/libtests.a with common functions for use in tests.
7
8* tests/tests.h: New file.
9* tests/error_msg.c: Likewise.
10* tests/tail_alloc.c: Likewise.
11* tests/get_page_size.c: Likewise.
12* tests/Makefile.am (libtests_a_SOURCES, libtests_a_CPPFLAGS,
13check_LIBRARIES, LDADD): New variables.
14(clock_xettime_LDADD, filter_unavailable_LDADD, mq_LDADD,
15pc_LDADD, times_LDADD): Add $(LDADD).
16* tests/.gitignore: Add libtests.a.
17---
18Signed-off-by: Khem Raj <raj.khem@gmail.com>
19Upstream-Status: Backport
20
21 tests/.gitignore | 1 +
22 tests/Makefile.am | 20 ++++++++++----
23 tests/error_msg.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++
24 tests/get_page_size.c | 13 +++++++++
25 tests/tail_alloc.c | 52 ++++++++++++++++++++++++++++++++++++
26 tests/tests.h | 62 ++++++++++++++++++++++++++++++++++++++++++
27 6 files changed, 217 insertions(+), 5 deletions(-)
28 create mode 100644 tests/error_msg.c
29 create mode 100644 tests/get_page_size.c
30 create mode 100644 tests/tail_alloc.c
31 create mode 100644 tests/tests.h
32
33diff --git a/tests/Makefile.am b/tests/Makefile.am
34index 386a2c2..62d0e56 100644
35--- a/tests/Makefile.am
36+++ b/tests/Makefile.am
37@@ -40,6 +40,16 @@ AM_CPPFLAGS = $(ARCH_MFLAGS) \
38 -I$(top_srcdir)
39 AM_LDFLAGS = $(ARCH_MFLAGS)
40
41+libtests_a_SOURCES = \
42+ get_page_size.c \
43+ error_msg.c \
44+ tail_alloc.c \
45+ tests.h \
46+ # end of libtests_a_SOURCES
47+libtests_a_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
48+check_LIBRARIES = libtests.a
49+LDADD = libtests.a
50+
51 check_PROGRAMS = \
52 _newselect \
53 adjtimex \
54@@ -146,19 +156,19 @@ check_PROGRAMS = \
55 xettimeofday \
56 # end of check_PROGRAMS
57
58-clock_xettime_LDADD = -lrt
59-filter_unavailable_LDADD = -lpthread
60+clock_xettime_LDADD = -lrt $(LDADD)
61+filter_unavailable_LDADD = -lpthread $(LDADD)
62 fstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
63 fstatat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
64 ftruncate64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
65 lstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
66 mmap64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
67-mq_LDADD = -lrt
68+mq_LDADD = -lrt $(LDADD)
69 newfstatat_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
70-pc_LDADD = $(dl_LIBS)
71+pc_LDADD = $(dl_LIBS) $(LDADD)
72 stat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
73 statfs_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
74-times_LDADD = -lrt
75+times_LDADD = -lrt $(LDADD)
76 truncate64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
77 uio_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
78 stack_fcall_SOURCES = stack-fcall.c \
79diff --git a/tests/error_msg.c b/tests/error_msg.c
80new file mode 100644
81index 0000000..3fd3411
82--- /dev/null
83+++ b/tests/error_msg.c
84@@ -0,0 +1,74 @@
85+/*
86+ * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
87+ * All rights reserved.
88+ *
89+ * Redistribution and use in source and binary forms, with or without
90+ * modification, are permitted provided that the following conditions
91+ * are met:
92+ * 1. Redistributions of source code must retain the above copyright
93+ * notice, this list of conditions and the following disclaimer.
94+ * 2. Redistributions in binary form must reproduce the above copyright
95+ * notice, this list of conditions and the following disclaimer in the
96+ * documentation and/or other materials provided with the distribution.
97+ * 3. The name of the author may not be used to endorse or promote products
98+ * derived from this software without specific prior written permission.
99+ *
100+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
101+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
102+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
103+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
104+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
105+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
106+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
107+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
108+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
109+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110+ */
111+
112+#include "tests.h"
113+#include <errno.h>
114+#include <stdarg.h>
115+#include <stdio.h>
116+#include <stdlib.h>
117+#include <string.h>
118+
119+void
120+perror_msg_and_fail(const char *fmt, ...)
121+{
122+ int err_no = errno;
123+ va_list p;
124+
125+ va_start(p, fmt);
126+ vfprintf(stderr, fmt, p);
127+ if (err_no)
128+ fprintf(stderr, ": %s\n", strerror(err_no));
129+ else
130+ putc('\n', stderr);
131+ exit(1);
132+}
133+
134+void
135+error_msg_and_skip(const char *fmt, ...)
136+{
137+ va_list p;
138+
139+ va_start(p, fmt);
140+ vfprintf(stderr, fmt, p);
141+ putc('\n', stderr);
142+ exit(77);
143+}
144+
145+void
146+perror_msg_and_skip(const char *fmt, ...)
147+{
148+ int err_no = errno;
149+ va_list p;
150+
151+ va_start(p, fmt);
152+ vfprintf(stderr, fmt, p);
153+ if (err_no)
154+ fprintf(stderr, ": %s\n", strerror(err_no));
155+ else
156+ putc('\n', stderr);
157+ exit(77);
158+}
159diff --git a/tests/get_page_size.c b/tests/get_page_size.c
160new file mode 100644
161index 0000000..aeea861
162--- /dev/null
163+++ b/tests/get_page_size.c
164@@ -0,0 +1,13 @@
165+#include "tests.h"
166+#include <unistd.h>
167+
168+size_t
169+get_page_size(void)
170+{
171+ static size_t page_size;
172+
173+ if (!page_size)
174+ page_size = sysconf(_SC_PAGESIZE);
175+
176+ return page_size;
177+}
178diff --git a/tests/tail_alloc.c b/tests/tail_alloc.c
179new file mode 100644
180index 0000000..2b8b14e
181--- /dev/null
182+++ b/tests/tail_alloc.c
183@@ -0,0 +1,52 @@
184+/*
185+ * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
186+ * All rights reserved.
187+ *
188+ * Redistribution and use in source and binary forms, with or without
189+ * modification, are permitted provided that the following conditions
190+ * are met:
191+ * 1. Redistributions of source code must retain the above copyright
192+ * notice, this list of conditions and the following disclaimer.
193+ * 2. Redistributions in binary form must reproduce the above copyright
194+ * notice, this list of conditions and the following disclaimer in the
195+ * documentation and/or other materials provided with the distribution.
196+ * 3. The name of the author may not be used to endorse or promote products
197+ * derived from this software without specific prior written permission.
198+ *
199+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
204+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
206+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
207+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209+ */
210+
211+#include "tests.h"
212+#include <string.h>
213+#include <sys/mman.h>
214+
215+void *
216+tail_alloc(const size_t size)
217+{
218+ const size_t page_size = get_page_size();
219+ const size_t len = (size + page_size - 1) & -page_size;
220+ const size_t alloc_size = len + 2 * page_size;
221+
222+ void *p = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE,
223+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
224+ if (MAP_FAILED == p)
225+ perror_msg_and_fail("mmap(%zu)", alloc_size);
226+
227+ void *start_work = p + page_size;
228+ void *tail_guard = start_work + len;
229+
230+ if (munmap(p, page_size) || munmap(tail_guard, page_size))
231+ perror_msg_and_fail("munmap");
232+
233+ memset(start_work, 0xff, len);
234+ return tail_guard - size;
235+}
236diff --git a/tests/tests.h b/tests/tests.h
237new file mode 100644
238index 0000000..91fa24e
239--- /dev/null
240+++ b/tests/tests.h
241@@ -0,0 +1,62 @@
242+/*
243+ * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
244+ * All rights reserved.
245+ *
246+ * Redistribution and use in source and binary forms, with or without
247+ * modification, are permitted provided that the following conditions
248+ * are met:
249+ * 1. Redistributions of source code must retain the above copyright
250+ * notice, this list of conditions and the following disclaimer.
251+ * 2. Redistributions in binary form must reproduce the above copyright
252+ * notice, this list of conditions and the following disclaimer in the
253+ * documentation and/or other materials provided with the distribution.
254+ * 3. The name of the author may not be used to endorse or promote products
255+ * derived from this software without specific prior written permission.
256+ *
257+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
258+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
259+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
260+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
261+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
262+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
263+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
265+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267+ */
268+
269+#ifndef TESTS_H_
270+# define TESTS_H_
271+
272+# ifdef HAVE_CONFIG_H
273+# include "config.h"
274+# endif
275+
276+# include <sys/types.h>
277+# include "gcc_compat.h"
278+
279+/* Cached sysconf(_SC_PAGESIZE). */
280+size_t get_page_size(void);
281+
282+/* Print message and strerror(errno) to stderr, then exit(1). */
283+void perror_msg_and_fail(const char *, ...)
284+ ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
285+/* Print message to stderr, then exit(77). */
286+void error_msg_and_skip(const char *, ...)
287+ ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
288+/* Print message and strerror(errno) to stderr, then exit(77). */
289+void perror_msg_and_skip(const char *, ...)
290+ ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
291+
292+/*
293+ * Allocate memory that ends on the page boundary.
294+ * Pages allocated by this call are preceeded by an unmapped page
295+ * and followed also by an unmapped page.
296+ */
297+void *tail_alloc(const size_t)
298+ ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
299+
300+# define SKIP_MAIN_UNDEFINED(arg) \
301+ int main(void) { error_msg_and_skip("undefined: %s", arg); }
302+
303+#endif
304--
3051.9.1
306