From d8a81dfa40db3c2656a44bf428e957e63490e5c1 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Thu, 4 Feb 2016 01:32:45 +0000 Subject: [PATCH] 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. --- tests/readlink.c | 44 +++++++++++++++++++++++++++++-------------- tests/readlink.test | 11 ++--------- tests/readlinkat.c | 46 ++++++++++++++++++++++++++++++--------------- 3 files changed, 63 insertions(+), 38 deletions(-) diff --git a/tests/readlink.c b/tests/readlink.c index 7bbec220..85777e42 100644 --- a/tests/readlink.c +++ b/tests/readlink.c @@ -34,25 +34,41 @@ # include # include +# 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; diff --git a/tests/readlink.test b/tests/readlink.test index 48ab7e12..11f2569f 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 67cefe99..5ca9e72e 100644 --- a/tests/readlinkat.c +++ b/tests/readlinkat.c @@ -34,25 +34,41 @@ # include # include +# 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 -- 2.40.0