Skip to content

Commit d8a81df

Browse files
committed
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.
1 parent 7637b1d commit d8a81df

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

tests/readlink.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,41 @@
3434
# include <stdio.h>
3535
# include <unistd.h>
3636

37+
# define PREFIX "test.readlink"
38+
# define TARGET (PREFIX ".target")
39+
# define LINKPATH (PREFIX ".link")
40+
3741
int
3842
main(void)
3943
{
40-
static const char fname[] = "readlink.link";
41-
unsigned char buf[31];
42-
long rc;
43-
unsigned int i;
44+
const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
45+
const char * const hex_fname =
46+
hexquote_strndup(fname, sizeof(LINKPATH) - 1);
47+
48+
const unsigned int size = sizeof(TARGET) - 1;
49+
char * const buf = tail_alloc(size);
50+
51+
(void) unlink(fname);
52+
53+
long rc = syscall(__NR_readlink, fname, buf, size);
54+
printf("readlink(\"%s\", %p, %u) = -1 ENOENT (%m)\n",
55+
hex_fname, buf, size);
56+
57+
if (symlink(TARGET, fname))
58+
perror_msg_and_fail("symlink");
4459

45-
rc = syscall(__NR_readlink, fname, buf, sizeof(buf));
46-
if (rc < 0)
47-
perror_msg_and_skip("readlink");
60+
rc = syscall(__NR_readlink, fname, buf, size);
61+
if (rc < 0) {
62+
perror("readlink");
63+
(void) unlink(fname);
64+
return 77;
65+
}
66+
const char * const hex_buf = hexquote_strndup(buf, size);
67+
printf("readlink(\"%s\", \"%s\", %u) = %u\n",
68+
hex_fname, hex_buf, size, size);
4869

49-
printf("readlink(\"");
50-
for (i = 0; fname[i]; ++i)
51-
printf("\\x%02x", (int) (unsigned char) fname[i]);
52-
printf("\", \"");
53-
for (i = 0; i < 3; ++i)
54-
printf("\\x%02x", (int) buf[i]);
55-
printf("\"..., %zu) = %ld\n", sizeof(buf), rc);
70+
if (unlink(fname))
71+
perror_msg_and_fail("unlink");
5672

5773
puts("+++ exited with 0 +++");
5874
return 0;

tests/readlink.test

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@
55
. "${srcdir=.}/init.sh"
66

77
syscall=${ME_%.test}
8-
target=$syscall.c
9-
link=$syscall.link
108
OUT="$LOG.out"
119

12-
ln -snf $target $link ||
13-
framework_skip_ 'failed to create a symlink'
14-
1510
run_prog > /dev/null
16-
run_strace -e $syscall -xx -s3 $args > "$OUT"
11+
run_strace -xx -e $syscall $args > "$OUT"
1712
match_diff "$LOG" "$OUT"
1813

19-
rm -f -- "$OUT" $link
20-
21-
exit 0
14+
rm -f -- "$OUT"

tests/readlinkat.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,48 @@
3434
# include <stdio.h>
3535
# include <unistd.h>
3636

37+
# define PREFIX "test.readlinkat"
38+
# define TARGET (PREFIX ".target")
39+
# define LINKPATH (PREFIX ".link")
40+
3741
int
3842
main(void)
3943
{
40-
static const char fname[] = "readlinkat.link";
41-
unsigned char buf[31];
42-
long rc;
43-
unsigned int i;
44+
const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
45+
const char * const hex_fname =
46+
hexquote_strndup(fname, sizeof(LINKPATH) - 1);
47+
48+
const unsigned int size = sizeof(TARGET) - 1;
49+
char * const buf = tail_alloc(size);
50+
51+
(void) unlink(fname);
52+
53+
long rc = syscall(__NR_readlinkat, -100, fname, buf, size);
54+
printf("readlinkat(AT_FDCWD, \"%s\", %p, %u) = -1 ENOENT (%m)\n",
55+
hex_fname, buf, size);
56+
57+
if (symlink(TARGET, fname))
58+
perror_msg_and_fail("symlink");
4459

45-
rc = syscall(__NR_readlinkat, -100, fname, buf, sizeof(buf));
46-
if (rc < 0)
47-
perror_msg_and_skip("readlinkat");
60+
rc = syscall(__NR_readlinkat, -100, fname, buf, size);
61+
if (rc < 0) {
62+
perror("readlinkat");
63+
(void) unlink(fname);
64+
return 77;
65+
}
66+
const char * const hex_buf = hexquote_strndup(buf, size);
67+
printf("readlinkat(AT_FDCWD, \"%s\", \"%s\", %u) = %u\n",
68+
hex_fname, hex_buf, size, size);
4869

49-
printf("readlinkat(AT_FDCWD, \"");
50-
for (i = 0; fname[i]; ++i)
51-
printf("\\x%02x", (int) (unsigned char) fname[i]);
52-
printf("\", \"");
53-
for (i = 0; i < 3; ++i)
54-
printf("\\x%02x", (int) buf[i]);
55-
printf("\"..., %zu) = %ld\n", sizeof(buf), rc);
70+
if (unlink(fname))
71+
perror_msg_and_fail("unlink");
5672

5773
puts("+++ exited with 0 +++");
5874
return 0;
5975
}
6076

6177
#else
6278

63-
SKIP_MAIN_UNDEFINED("__NR_readlinkat")
79+
SKIP_MAIN_UNDEFINED("__NR_readlink")
6480

6581
#endif

0 commit comments

Comments
 (0)