1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15
16 /*
17 * Test Description :
18 * Testcase to check the basic functionality of the readlink(2),
19 * readlink() will succeed to read the contents of the symbolic link.
20 */
21
22 #include <pwd.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include "tst_test.h"
27
28 #define TESTFILE "test_file"
29 #define SYMFILE "slink_file"
30
31 static uid_t nobody_uid;
32
test_readlink(void)33 static void test_readlink(void)
34 {
35 char buffer[256];
36 int exp_val = strlen(TESTFILE);
37
38 TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
39 if (TST_RET == -1) {
40 tst_res(TFAIL | TTERRNO, "readlink() on %s failed", SYMFILE);
41 return;
42 }
43
44 if (TST_RET != exp_val) {
45 tst_res(TFAIL, "readlink() returned value %ld "
46 "did't match, Expected %d", TST_RET, exp_val);
47 return;
48 }
49
50 if (memcmp(buffer, TESTFILE, exp_val) != 0) {
51 tst_res(TFAIL, "Pathname %s and buffer contents %s differ",
52 TESTFILE, buffer);
53 } else {
54 tst_res(TPASS, "readlink() functionality on '%s' was correct",
55 SYMFILE);
56 }
57 }
58
verify_readlink(unsigned int n)59 static void verify_readlink(unsigned int n)
60 {
61 pid_t pid;
62
63 if (n) {
64 tst_res(TINFO, "Running test as nobody");
65 pid = SAFE_FORK();
66
67 if (!pid) {
68 SAFE_SETUID(nobody_uid);
69 test_readlink();
70 return;
71 }
72 } else {
73 tst_res(TINFO, "Running test as root");
74 test_readlink();
75 }
76 }
77
setup(void)78 static void setup(void)
79 {
80 int fd;
81 struct passwd *pw;
82
83 pw = SAFE_GETPWNAM("nobody");
84
85 nobody_uid = pw->pw_uid;
86
87 fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, 0444);
88 SAFE_CLOSE(fd);
89 SAFE_SYMLINK(TESTFILE, SYMFILE);
90 }
91
92 static struct tst_test test = {
93 .test = verify_readlink,\
94 .tcnt = 2,
95 .setup = setup,
96 .forks_child = 1,
97 .needs_root = 1,
98 .needs_tmpdir = 1,
99 };
100