]> granicus.if.org Git - strace/commitdiff
tests: rewrite getdents test in C for better coverage
authorDmitry V. Levin <ldv@altlinux.org>
Thu, 19 Nov 2015 16:39:32 +0000 (16:39 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Fri, 20 Nov 2015 00:08:48 +0000 (00:08 +0000)
* tests/getdents.awk: Remove.
* tests/getdents.out: Remove.
* tests/getdents.c: New file.
* tests/getdents.test: Rewrite.
* tests/getdents64.c: New file.
* tests/getdents64.test: New test.
* tests/Makefile.am (check_PROGRAMS): Add getdents and getdents64.
(TESTS): Add getdents64.test.
(EXTRA_DIST): Remove getdents.awk and getdents.out.
* tests/.gitignore: Add getdents and getdents64.

tests/.gitignore
tests/Makefile.am
tests/getdents.awk [deleted file]
tests/getdents.c [new file with mode: 0644]
tests/getdents.out [deleted file]
tests/getdents.test
tests/getdents64.c [new file with mode: 0644]
tests/getdents64.test [new file with mode: 0755]

index f877783369a312a4cc88445220a8bec54a1e058f..8542f53f3432f79b9e6aaa1b272dec89c7292755 100644 (file)
@@ -10,6 +10,8 @@ execve
 execveat
 fanotify_mark
 filter-unavailable
+getdents
+getdents64
 getrandom
 inet-accept-connect-send-recv
 ioctl
index 0ca46dd6b545a752999810ee66de7ba991e45785..86a9a507a04d8c45bbb6760e875c1e08efa53f8c 100644 (file)
@@ -23,6 +23,8 @@ check_PROGRAMS = \
        execveat \
        fanotify_mark \
        filter-unavailable \
+       getdents \
+       getdents64 \
        getrandom \
        inet-accept-connect-send-recv \
        ioctl \
@@ -121,6 +123,7 @@ TESTS = \
        fanotify_mark.test \
        filter-unavailable.test \
        getdents.test \
+       getdents64.test \
        getrandom.test \
        ioctl.test \
        ip_mreq.test \
@@ -208,8 +211,6 @@ EXTRA_DIST = init.sh run.sh match.awk \
             execveat-v.expected \
             fanotify_mark.expected \
             filter-unavailable.expected \
-            getdents.awk \
-            getdents.out \
             ip_mreq.expected \
             ipc.sh \
             ipc_msgbuf.expected \
diff --git a/tests/getdents.awk b/tests/getdents.awk
deleted file mode 100644 (file)
index e68999c..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-BEGIN {
-       i = "[0-9]+"
-       len = "[1-9]" i
-
-       d_ino = "d_ino=" i
-       d_off = "d_off=" i
-       d_reclen = "d_reclen=" len
-       d_name_1 = "d_name=\"\\.\""
-       d_name_2 = "d_name=\"\\.\\.\""
-       d_name_3 = "d_name=\"(A\\\\n){127}Z\""
-       # Some older systems might not pass back d_type at all like Alpha.
-       d_type_dir = "d_type=DT_(DIR|UNKNOWN)"
-       d_type_reg = "d_type=DT_(REG|UNKNOWN)"
-
-       dirent_1   = "\\{" d_ino ", " d_off ", " d_reclen ", " d_name_1 ", " d_type_dir "\\}"
-       dirent_2   = "\\{" d_ino ", " d_off ", " d_reclen ", " d_name_2 ", " d_type_dir "\\}"
-       dirent_3   = "\\{" d_ino ", " d_off ", " d_reclen ", " d_name_3 ", " d_type_reg "\\}"
-
-       dirent64_1 = "\\{" d_ino ", " d_off ", " d_reclen ", " d_type_dir ", " d_name_1 "\\}"
-       dirent64_2 = "\\{" d_ino ", " d_off ", " d_reclen ", " d_type_dir ", " d_name_2 "\\}"
-       dirent64_3 = "\\{" d_ino ", " d_off ", " d_reclen ", " d_type_reg ", " d_name_3 "\\}"
-
-       d_123 = dirent_1 ", " dirent_2 ", " dirent_3
-       d_213 = dirent_2 ", " dirent_1 ", " dirent_3
-       d_132 = dirent_1 ", " dirent_3 ", " dirent_2
-       d_321 = dirent_3 ", " dirent_2 ", " dirent_1
-       d_231 = dirent_2 ", " dirent_3 ", " dirent_1
-       d_312 = dirent_3 ", " dirent_1 ", " dirent_2
-
-       d64_123 = dirent64_1 ", " dirent64_2 ", " dirent64_3
-       d64_213 = dirent64_2 ", " dirent64_1 ", " dirent64_3
-       d64_132 = dirent64_1 ", " dirent64_3 ", " dirent64_2
-       d64_321 = dirent64_3 ", " dirent64_2 ", " dirent64_1
-       d64_231 = dirent64_2 ", " dirent64_3 ", " dirent64_1
-       d64_312 = dirent64_3 ", " dirent64_1 ", " dirent64_2
-
-       dents = "\\[(" d_123 "|" d_213 "|" d_132 "|" d_321 "|" d_231 "|" d_312 ")\\]"
-       dents64 = "\\[(" d64_123 "|" d64_213 "|" d64_132 "|" d64_321 "|" d64_231 "|" d64_312 ")\\]"
-
-       getdents   =   "getdents\\(" i ", " dents   ", " len "\\)"
-       getdents64 = "getdents64\\(" i ", " dents64 ", " len "\\)"
-
-       r[1] = "^(" getdents "|" getdents64 ") += " len "$"
-       r[2] = "^getdents(64)?\\([0-9]+, \\[\\], [1-9][0-9]+\\) += 0$"
-       s[3] = "+++ exited with 0 +++"
-
-       lines = 3
-       fail = 0
-}
-
-@include "match.awk"
diff --git a/tests/getdents.c b/tests/getdents.c
new file mode 100644 (file)
index 0000000..f0aea23
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/syscall.h>
+
+#ifdef __NR_getdents
+
+#include <assert.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static const char fname[] =
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ";
+static const char qname[] =
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
+
+typedef struct {
+               unsigned long d_ino;
+               unsigned long d_off;
+               unsigned short d_reclen;
+               char d_name[256];
+} kernel_dirent;
+
+static char buf[8192];
+
+static const char *
+str_d_type(const unsigned char d_type)
+{
+       switch (d_type) {
+               case DT_DIR:
+                       return "DT_DIR";
+               case DT_REG:
+                       return "DT_REG";
+               default:
+                       return "DT_UNKNOWN";
+       }
+}
+static void
+print_dirent(const kernel_dirent *d)
+{
+       const unsigned int d_name_offset = offsetof(kernel_dirent, d_name);
+       int d_name_len = d->d_reclen - d_name_offset - 1;
+       assert(d_name_len > 0);
+
+       printf("{d_ino=%lu, d_off=%lu, d_reclen=%u, d_name=",
+              d->d_ino, d->d_off, d->d_reclen);
+
+       if (d->d_name[0] == '.')
+               printf("\"%.*s\"", d_name_len, d->d_name);
+       else
+               printf("\"%s\"", qname);
+
+       printf(", d_type=%s}",
+              str_d_type(*((const char *) d + d->d_reclen - 1)));
+}
+
+int
+main(int ac, const char **av)
+{
+       char *dname;
+       int rc;
+
+       assert(ac == 1);
+       assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0);
+       assert(!mkdir(dname, 0700));
+       assert(!chdir(dname));
+       (void) close(0);
+       assert(!creat(fname, 0600));
+       assert(!close(0));
+       assert(!open(".", O_RDONLY | O_DIRECTORY));
+       while ((rc = syscall(__NR_getdents, 0, buf, sizeof(buf)))) {
+               kernel_dirent *d;
+               int i;
+
+               if (rc < 0)
+                       return 77;
+               printf("getdents(0, [");
+               for (i = 0; i < rc; i += d->d_reclen) {
+                       d = (kernel_dirent *) &buf[i];
+                       if (i)
+                               printf(", ");
+                       print_dirent(d);
+               }
+               printf("], %zu) = %d\n", sizeof(buf), rc);
+       }
+       printf("getdents(0, [], %zu) = 0\n", sizeof(buf));
+       puts("+++ exited with 0 +++");
+       assert(!unlink(fname));
+       assert(!chdir(".."));
+       assert(!rmdir(dname));
+
+       return 0;
+}
+
+#else
+
+int
+main(void)
+{
+       return 77;
+}
+
+#endif
diff --git a/tests/getdents.out b/tests/getdents.out
deleted file mode 100644 (file)
index c5cd37c..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-getdents(3, [{d_ino=123456789, d_off=1, d_reclen=24, d_name=".", d_type=DT_DIR}, {d_ino=234567890, d_off=2, d_reclen=24, d_name="..", d_type=DT_DIR}, {d_ino=345678901, d_off=3, d_reclen=280, d_name="A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ", d_type=DT_REG}], 4096) = 328
-getdents(3, [], 4096)                   = 0
-+++ exited with 0 +++
index 3fa1c8db50d7ec11377817b7d61890f2e6289f19..62911b8b5618663a6212bc4d0936806e18fa0671 100755 (executable)
@@ -1,33 +1,16 @@
 #!/bin/sh
 
-# Check that getdents/getdents64 syscalls are traced properly.
+# Check getdents syscall decoding.
 
 . "${srcdir=.}/init.sh"
 
-check_prog gawk
-AWKPATH="$srcdir" gawk -f "$srcdir"/getdents.awk "$srcdir"/getdents.out ||
-       framework_skip_ 'gawk does not work properly'
+rm -rf -- "$LOG".dir
+run_prog > /dev/null
 
-check_prog ls
-check_prog mkdir
-check_prog rm
-check_prog seq
-check_prog touch
+OUT="$LOG.out"
+run_strace -a22 -vegetdents $args > "$OUT"
 
-dir="$LOG.dir"
-rm -rf -- "$dir"
-mkdir -- "$dir" ||
-       framework_skip_ 'failed to create a directory'
-
-touch -- "$dir/$(for i in $(seq 1 127); do echo A; done; echo Z)" ||
-       framework_skip_ 'failed to create a file'
-
-ls -- "$dir" > /dev/null ||
-       framework_skip_ 'failed to list a directory'
-
-run_strace -vegetdents,getdents64 ls -- "$dir" > /dev/null
-match_awk
-
-rm -rf -- "$dir"
+match_diff "$OUT" "$LOG"
+rm -f "$OUT"
 
 exit 0
diff --git a/tests/getdents64.c b/tests/getdents64.c
new file mode 100644 (file)
index 0000000..0f09c13
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/syscall.h>
+
+#ifdef __NR_getdents64
+
+#include <assert.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static const char fname[] =
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
+       "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ";
+static const char qname[] =
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
+       "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
+
+typedef struct {
+               uint64_t d_ino;
+               uint64_t d_off;
+               unsigned short d_reclen;
+               unsigned char d_type;
+               char d_name[256];
+} kernel_dirent64;
+
+static char buf[8192];
+
+static const char *
+str_d_type(const unsigned char d_type)
+{
+       switch (d_type) {
+               case DT_DIR:
+                       return "DT_DIR";
+               case DT_REG:
+                       return "DT_REG";
+               default:
+                       return "DT_UNKNOWN";
+       }
+}
+static void
+print_dirent(const kernel_dirent64 *d)
+{
+       const unsigned int d_name_offset = offsetof(kernel_dirent64, d_name);
+       int d_name_len = d->d_reclen - d_name_offset;
+       assert(d_name_len > 0);
+
+       printf("{d_ino=%" PRIu64 ", d_off=%" PRId64
+              ", d_reclen=%u, d_type=%s, d_name=",
+              d->d_ino, d->d_off, d->d_reclen, str_d_type(d->d_type));
+
+       if (d->d_name[0] == '.')
+               printf("\"%.*s\"}", d_name_len, d->d_name);
+       else
+               printf("\"%s\"}", qname);
+}
+
+int
+main(int ac, const char **av)
+{
+       char *dname;
+       int rc;
+
+       assert(ac == 1);
+       assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0);
+       assert(!mkdir(dname, 0700));
+       assert(!chdir(dname));
+       (void) close(0);
+       assert(!creat(fname, 0600));
+       assert(!close(0));
+       assert(!open(".", O_RDONLY | O_DIRECTORY));
+       while ((rc = syscall(__NR_getdents64, 0, buf, sizeof(buf)))) {
+               kernel_dirent64 *d;
+               int i;
+
+               if (rc < 0)
+                       return 77;
+               printf("getdents64(0, [");
+               for (i = 0; i < rc; i += d->d_reclen) {
+                       d = (kernel_dirent64 *) &buf[i];
+                       if (i)
+                               printf(", ");
+                       print_dirent(d);
+               }
+               printf("], %zu) = %d\n", sizeof(buf), rc);
+       }
+       printf("getdents64(0, [], %zu) = 0\n", sizeof(buf));
+       puts("+++ exited with 0 +++");
+       assert(!unlink(fname));
+       assert(!chdir(".."));
+       assert(!rmdir(dname));
+
+       return 0;
+}
+
+#else
+
+int
+main(void)
+{
+       return 77;
+}
+
+#endif
diff --git a/tests/getdents64.test b/tests/getdents64.test
new file mode 100755 (executable)
index 0000000..a033508
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+# Check getdents64 syscall decoding.
+
+. "${srcdir=.}/init.sh"
+
+rm -rf -- "$LOG".dir
+run_prog > /dev/null
+
+OUT="$LOG.out"
+run_strace -a24 -vegetdents64 $args > "$OUT"
+
+match_diff "$OUT" "$LOG"
+rm -f "$OUT"
+
+exit 0