tests: extend readlink and readlinkat coverage
* 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.
diff --git a/tests/readlink.c b/tests/readlink.c
index 7bbec22..85777e4 100644
--- a/tests/readlink.c
+++ b/tests/readlink.c
@@ -34,25 +34,41 @@
# 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);
- rc = syscall(__NR_readlink, fname, buf, sizeof(buf));
- if (rc < 0)
- perror_msg_and_skip("readlink");
+ const unsigned int size = sizeof(TARGET) - 1;
+ char * const buf = tail_alloc(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);
+ (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, 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);
+
+ if (unlink(fname))
+ perror_msg_and_fail("unlink");
puts("+++ exited with 0 +++");
return 0;
diff --git a/tests/readlink.test b/tests/readlink.test
index 48ab7e1..11f2569 100755
--- a/tests/readlink.test
+++ b/tests/readlink.test
@@ -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"
diff --git a/tests/readlinkat.c b/tests/readlinkat.c
index 67cefe9..5ca9e72 100644
--- a/tests/readlinkat.c
+++ b/tests/readlinkat.c
@@ -34,25 +34,41 @@
# 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);
- rc = syscall(__NR_readlinkat, -100, fname, buf, sizeof(buf));
- if (rc < 0)
- perror_msg_and_skip("readlinkat");
+ const unsigned int size = sizeof(TARGET) - 1;
+ char * const buf = tail_alloc(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);
+ (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, 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);
+
+ if (unlink(fname))
+ perror_msg_and_fail("unlink");
puts("+++ exited with 0 +++");
return 0;
@@ -60,6 +76,6 @@
#else
-SKIP_MAIN_UNDEFINED("__NR_readlinkat")
+SKIP_MAIN_UNDEFINED("__NR_readlink")
#endif