]> granicus.if.org Git - strace/commitdiff
tests: extend readlink and readlinkat coverage
authorDmitry V. Levin <ldv@altlinux.org>
Thu, 4 Feb 2016 01:32:45 +0000 (01:32 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Thu, 4 Feb 2016 03:36:25 +0000 (03:36 +0000)
* tests/readlink.c (PREFIX, TARGET, LINKPATH): New macros.
(main): Allocate memory for string and buffer passed to the syscall
being tested using tail_alloc mechanism.  Create and cleanup test
symlink.  Test syscall error path output.  Print hexquoted strings
using hexquote_strndup.
* tests/readlinkat.c: Likewise.
* tests/readlink.test: Remove creation and cleanup of test symlinks.

tests/readlink.c
tests/readlink.test
tests/readlinkat.c

index 7bbec22011fb4417ad22c8336f23c821844cce7b..85777e42fb1283045582ba832b99b19e78f6000e 100644 (file)
 # include <stdio.h>
 # include <unistd.h>
 
+# define PREFIX "test.readlink"
+# define TARGET (PREFIX ".target")
+# define LINKPATH (PREFIX ".link")
+
 int
 main(void)
 {
-       static const char fname[] = "readlink.link";
-       unsigned char buf[31];
-       long rc;
-       unsigned int i;
+       const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
+       const char * const hex_fname =
+               hexquote_strndup(fname, sizeof(LINKPATH) - 1);
+
+       const unsigned int size = sizeof(TARGET) - 1;
+       char * const buf = tail_alloc(size);
+
+       (void) unlink(fname);
+
+       long rc = syscall(__NR_readlink, fname, buf, size);
+       printf("readlink(\"%s\", %p, %u) = -1 ENOENT (%m)\n",
+              hex_fname, buf, size);
+
+       if (symlink(TARGET, fname))
+               perror_msg_and_fail("symlink");
 
-       rc = syscall(__NR_readlink, fname, buf, sizeof(buf));
-       if (rc < 0)
-               perror_msg_and_skip("readlink");
+       rc = syscall(__NR_readlink, fname, buf, size);
+       if (rc < 0) {
+               perror("readlink");
+               (void) unlink(fname);
+               return 77;
+       }
+       const char * const hex_buf = hexquote_strndup(buf, size);
+       printf("readlink(\"%s\", \"%s\", %u) = %u\n",
+              hex_fname, hex_buf, size, size);
 
-       printf("readlink(\"");
-       for (i = 0; fname[i]; ++i)
-               printf("\\x%02x", (int) (unsigned char) fname[i]);
-       printf("\", \"");
-       for (i = 0; i < 3; ++i)
-               printf("\\x%02x", (int) buf[i]);
-       printf("\"..., %zu) = %ld\n", sizeof(buf), rc);
+       if (unlink(fname))
+               perror_msg_and_fail("unlink");
 
        puts("+++ exited with 0 +++");
        return 0;
index 48ab7e12ffde7c512406df1b9c3a9de185a5f065..11f2569fb4429344f71e37781b36e1c2c35ed316 100755 (executable)
@@ -5,17 +5,10 @@
 . "${srcdir=.}/init.sh"
 
 syscall=${ME_%.test}
-target=$syscall.c
-link=$syscall.link
 OUT="$LOG.out"
 
-ln -snf $target $link ||
-       framework_skip_ 'failed to create a symlink'
-
 run_prog > /dev/null
-run_strace -e $syscall -xx -s3 $args > "$OUT"
+run_strace -xx -e $syscall $args > "$OUT"
 match_diff "$LOG" "$OUT"
 
-rm -f -- "$OUT" $link
-
-exit 0
+rm -f -- "$OUT"
index 67cefe99d1ebd70c439a5f35296397142470180e..5ca9e72e0fc619869a045cbfdb26f0aa59a771a4 100644 (file)
 # include <stdio.h>
 # include <unistd.h>
 
+# define PREFIX "test.readlinkat"
+# define TARGET (PREFIX ".target")
+# define LINKPATH (PREFIX ".link")
+
 int
 main(void)
 {
-       static const char fname[] = "readlinkat.link";
-       unsigned char buf[31];
-       long rc;
-       unsigned int i;
+       const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
+       const char * const hex_fname =
+               hexquote_strndup(fname, sizeof(LINKPATH) - 1);
+
+       const unsigned int size = sizeof(TARGET) - 1;
+       char * const buf = tail_alloc(size);
+
+       (void) unlink(fname);
+
+       long rc = syscall(__NR_readlinkat, -100, fname, buf, size);
+       printf("readlinkat(AT_FDCWD, \"%s\", %p, %u) = -1 ENOENT (%m)\n",
+              hex_fname, buf, size);
+
+       if (symlink(TARGET, fname))
+               perror_msg_and_fail("symlink");
 
-       rc = syscall(__NR_readlinkat, -100, fname, buf, sizeof(buf));
-       if (rc < 0)
-               perror_msg_and_skip("readlinkat");
+       rc = syscall(__NR_readlinkat, -100, fname, buf, size);
+       if (rc < 0) {
+               perror("readlinkat");
+               (void) unlink(fname);
+               return 77;
+       }
+       const char * const hex_buf = hexquote_strndup(buf, size);
+       printf("readlinkat(AT_FDCWD, \"%s\", \"%s\", %u) = %u\n",
+              hex_fname, hex_buf, size, size);
 
-       printf("readlinkat(AT_FDCWD, \"");
-       for (i = 0; fname[i]; ++i)
-               printf("\\x%02x", (int) (unsigned char) fname[i]);
-       printf("\", \"");
-       for (i = 0; i < 3; ++i)
-               printf("\\x%02x", (int) buf[i]);
-       printf("\"..., %zu) = %ld\n", sizeof(buf), rc);
+       if (unlink(fname))
+               perror_msg_and_fail("unlink");
 
        puts("+++ exited with 0 +++");
        return 0;
@@ -60,6 +76,6 @@ main(void)
 
 #else
 
-SKIP_MAIN_UNDEFINED("__NR_readlinkat")
+SKIP_MAIN_UNDEFINED("__NR_readlink")
 
 #endif