1 /******************************************************************************
2 *
3 * Copyright (c) International Business Machines Corp., 2006
4 * Author: Yi Yang <yyangcdl@cn.ibm.com>
5 * Copyright (c) Cyril Hrubis 2014 <chrubis@suse.cz>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * This test case will verify basic function of readlinkat
22 * added by kernel 2.6.16 or up.
23 *
24 *****************************************************************************/
25
26 #define _GNU_SOURCE
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <signal.h>
36 #include "test.h"
37 #include "safe_macros.h"
38 #include "lapi/readlinkat.h"
39
40 static void setup(void);
41 static void cleanup(void);
42
43 char *TCID = "readlinkat01";
44
45 static int dir_fd, fd;
46 static int fd_invalid = 100;
47 static int fd_atcwd = AT_FDCWD;
48
49 #define TEST_SYMLINK "readlink_symlink"
50 #define TEST_FILE "readlink_file"
51
52 static char abspath[1024];
53
54 static struct test_case {
55 int *dir_fd;
56 const char *path;
57 const char *exp_buf;
58 int exp_ret;
59 int exp_errno;
60 } test_cases[] = {
61 {&dir_fd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
62 {&dir_fd, abspath, TEST_FILE, sizeof(TEST_FILE)-1, 0},
63 {&fd, TEST_SYMLINK, NULL, -1, ENOTDIR},
64 {&fd_invalid, TEST_SYMLINK, NULL, -1, EBADF},
65 {&fd_atcwd, TEST_SYMLINK, TEST_FILE, sizeof(TEST_FILE)-1, 0},
66 };
67
68 int TST_TOTAL = ARRAY_SIZE(test_cases);
69
verify_readlinkat(struct test_case * test)70 static void verify_readlinkat(struct test_case *test)
71 {
72 char buf[1024];
73
74 memset(buf, 0, sizeof(buf));
75
76 TEST(readlinkat(*test->dir_fd, test->path, buf, sizeof(buf)));
77
78 if (TEST_RETURN != test->exp_ret) {
79 tst_resm(TFAIL | TTERRNO,
80 "readlinkat() returned %ld, expected %d",
81 TEST_RETURN, test->exp_ret);
82 return;
83 }
84
85 if (TEST_ERRNO != test->exp_errno) {
86 tst_resm(TFAIL | TTERRNO,
87 "readlinkat() returned %ld, expected %d",
88 TEST_RETURN, test->exp_ret);
89 return;
90 }
91
92 if (test->exp_ret > 0 && strcmp(test->exp_buf, buf)) {
93 tst_resm(TFAIL, "Unexpected buffer have '%s', expected '%s'",
94 buf, test->exp_buf);
95 return;
96 }
97
98 tst_resm(TPASS | TTERRNO, "readlinkat() returned %ld", TEST_RETURN);
99 }
100
main(int ac,char ** av)101 int main(int ac, char **av)
102 {
103 int lc;
104 int i;
105
106 tst_parse_opts(ac, av, NULL, NULL);
107
108 setup();
109
110 for (lc = 0; TEST_LOOPING(lc); lc++) {
111 for (i = 0; i < TST_TOTAL; i++)
112 verify_readlinkat(&test_cases[i]);
113 }
114
115 cleanup();
116 tst_exit();
117 }
118
setup(void)119 static void setup(void)
120 {
121 tst_tmpdir();
122 char *tmpdir = tst_get_tmpdir();
123
124 snprintf(abspath, sizeof(abspath), "%s/" TEST_SYMLINK, tmpdir);
125 free(tmpdir);
126
127 fd = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT, 0600);
128 SAFE_SYMLINK(cleanup, TEST_FILE, TEST_SYMLINK);
129 dir_fd = SAFE_OPEN(cleanup, ".", O_DIRECTORY);
130
131 TEST_PAUSE;
132 }
133
cleanup(void)134 static void cleanup(void)
135 {
136 if (fd > 0 && close(fd))
137 tst_resm(TWARN | TERRNO, "Failed to close fd");
138
139 if (dir_fd > 0 && close(dir_fd))
140 tst_resm(TWARN | TERRNO, "Failed to close dir_fd");
141
142 tst_rmdir();
143 }
144