]> granicus.if.org Git - strace/commitdiff
tests: introduce libtests
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 2 Jan 2016 12:05:14 +0000 (12:05 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Tue, 5 Jan 2016 23:17:07 +0000 (23:17 +0000)
Introduce tests/libtests.a with common functions for use in tests.

* tests/tests.h: New file.
* tests/error_msg.c: Likewise.
* tests/tail_alloc.c: Likewise.
* tests/get_page_size.c: Likewise.
* tests/Makefile.am (libtests_a_SOURCES, libtests_a_CPPFLAGS,
check_LIBRARIES, LDADD): New variables.
(clock_xettime_LDADD, filter_unavailable_LDADD, mq_LDADD,
pc_LDADD, times_LDADD): Add $(LDADD).
* tests/.gitignore: Add libtests.a.

tests/.gitignore
tests/Makefile.am
tests/error_msg.c [new file with mode: 0644]
tests/get_page_size.c [new file with mode: 0644]
tests/tail_alloc.c [new file with mode: 0644]
tests/tests.h [new file with mode: 0644]

index 1891eeecef0e6a810c04769e7611fd6b3fe293ba..d0eb98026b4ae9c796f7af49ea1caf8d30f8180d 100644 (file)
@@ -40,6 +40,7 @@ ipc_sem
 ipc_shm
 ksysent
 ksysent.h
+libtests.a
 llseek
 lseek
 lstat
index 386a2c26868ec527d5ef48c7488d7242856c54ec..62d0e560bf2252c7f66c91308a48a6931e4525da 100644 (file)
@@ -40,6 +40,16 @@ AM_CPPFLAGS = $(ARCH_MFLAGS) \
              -I$(top_srcdir)
 AM_LDFLAGS = $(ARCH_MFLAGS)
 
+libtests_a_SOURCES = \
+       get_page_size.c \
+       error_msg.c \
+       tail_alloc.c \
+       tests.h \
+       # end of libtests_a_SOURCES
+libtests_a_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
+check_LIBRARIES = libtests.a
+LDADD = libtests.a
+
 check_PROGRAMS = \
        _newselect \
        adjtimex \
@@ -146,19 +156,19 @@ check_PROGRAMS = \
        xettimeofday \
        # end of check_PROGRAMS
 
-clock_xettime_LDADD = -lrt
-filter_unavailable_LDADD = -lpthread
+clock_xettime_LDADD = -lrt $(LDADD)
+filter_unavailable_LDADD = -lpthread $(LDADD)
 fstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 fstatat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 ftruncate64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 lstat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 mmap64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
-mq_LDADD = -lrt
+mq_LDADD = -lrt $(LDADD)
 newfstatat_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
-pc_LDADD = $(dl_LIBS)
+pc_LDADD = $(dl_LIBS) $(LDADD)
 stat64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 statfs_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
-times_LDADD = -lrt
+times_LDADD = -lrt $(LDADD)
 truncate64_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 uio_CPPFLAGS = $(AM_CPPFLAGS) -D_FILE_OFFSET_BITS=64
 stack_fcall_SOURCES = stack-fcall.c \
diff --git a/tests/error_msg.c b/tests/error_msg.c
new file mode 100644 (file)
index 0000000..3fd3411
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void
+perror_msg_and_fail(const char *fmt, ...)
+{
+       int err_no = errno;
+       va_list p;
+
+       va_start(p, fmt);
+       vfprintf(stderr, fmt, p);
+       if (err_no)
+               fprintf(stderr, ": %s\n", strerror(err_no));
+       else
+               putc('\n', stderr);
+       exit(1);
+}
+
+void
+error_msg_and_skip(const char *fmt, ...)
+{
+       va_list p;
+
+       va_start(p, fmt);
+       vfprintf(stderr, fmt, p);
+       putc('\n', stderr);
+       exit(77);
+}
+
+void
+perror_msg_and_skip(const char *fmt, ...)
+{
+       int err_no = errno;
+       va_list p;
+
+       va_start(p, fmt);
+       vfprintf(stderr, fmt, p);
+       if (err_no)
+               fprintf(stderr, ": %s\n", strerror(err_no));
+       else
+               putc('\n', stderr);
+       exit(77);
+}
diff --git a/tests/get_page_size.c b/tests/get_page_size.c
new file mode 100644 (file)
index 0000000..aeea861
--- /dev/null
@@ -0,0 +1,13 @@
+#include "tests.h"
+#include <unistd.h>
+
+size_t
+get_page_size(void)
+{
+       static size_t page_size;
+
+       if (!page_size)
+               page_size = sysconf(_SC_PAGESIZE);
+
+       return page_size;
+}
diff --git a/tests/tail_alloc.c b/tests/tail_alloc.c
new file mode 100644 (file)
index 0000000..2b8b14e
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <string.h>
+#include <sys/mman.h>
+
+void *
+tail_alloc(const size_t size)
+{
+       const size_t page_size = get_page_size();
+       const size_t len = (size + page_size - 1) & -page_size;
+       const size_t alloc_size = len + 2 * page_size;
+
+       void *p = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE,
+                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+       if (MAP_FAILED == p)
+               perror_msg_and_fail("mmap(%zu)", alloc_size);
+
+       void *start_work = p + page_size;
+       void *tail_guard = start_work + len;
+
+       if (munmap(p, page_size) || munmap(tail_guard, page_size))
+               perror_msg_and_fail("munmap");
+
+       memset(start_work, 0xff, len);
+       return tail_guard - size;
+}
diff --git a/tests/tests.h b/tests/tests.h
new file mode 100644 (file)
index 0000000..91fa24e
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TESTS_H_
+# define TESTS_H_
+
+# ifdef HAVE_CONFIG_H
+#  include "config.h"
+# endif
+
+# include <sys/types.h>
+# include "gcc_compat.h"
+
+/* Cached sysconf(_SC_PAGESIZE). */
+size_t get_page_size(void);
+
+/* Print message and strerror(errno) to stderr, then exit(1). */
+void perror_msg_and_fail(const char *, ...)
+       ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+/* Print message to stderr, then exit(77). */
+void error_msg_and_skip(const char *, ...)
+       ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+/* Print message and strerror(errno) to stderr, then exit(77). */
+void perror_msg_and_skip(const char *, ...)
+       ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
+
+/*
+ * Allocate memory that ends on the page boundary.
+ * Pages allocated by this call are preceeded by an unmapped page
+ * and followed also by an unmapped page.
+ */
+void *tail_alloc(const size_t)
+       ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
+
+# define SKIP_MAIN_UNDEFINED(arg) \
+       int main(void) { error_msg_and_skip("undefined: %s", arg); }
+
+#endif