1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
4 * Email: code@zilogic.com
5 */
6
7 /*\
8 * [Description]
9 *
10 * This code tests the following flags with statx syscall:
11 *
12 * - AT_EMPTY_PATH
13 * - AT_SYMLINK_NOFOLLOW
14 *
15 * A test file and a link for it is created.
16 *
17 * To check empty path flag, test file fd alone is passed.
18 * Predefined size of testfile is checked against obtained value.
19 *
20 * To check symlink no follow flag, the linkname is statxed.
21 * To ensure that link is not dereferenced, obtained inode is compared
22 * with test file inode.
23 */
24
25 #define _GNU_SOURCE
26 #include <stdio.h>
27 #include <inttypes.h>
28 #include "tst_test.h"
29 #include "tst_safe_macros.h"
30 #include "lapi/stat.h"
31 #include "lapi/fcntl.h"
32
33 #define TESTFILE "test_temp"
34 #define LINK_FILE "test_temp_ln"
35 #define MODE 0644
36 #define SIZE 14
37
38 static int file_fd;
39
test_empty_path(void)40 static void test_empty_path(void)
41 {
42 struct statx buf;
43
44 TEST(statx(file_fd, "", AT_EMPTY_PATH, 0, &buf));
45 if (TST_RET == 0)
46 tst_res(TPASS,
47 "statx(file_fd, \"\", AT_EMPTY_PATH, 0, &buf)");
48 else
49 tst_brk(TFAIL | TTERRNO,
50 "statx(file_fd, \"\", AT_EMPTY_PATH, 0, &buff)");
51
52 if (buf.stx_size == SIZE)
53 tst_res(TPASS,
54 "stx_size(%"PRIu64") is correct", (uint64_t)buf.stx_size);
55 else
56 tst_res(TFAIL,
57 "stx_size(%"PRIu64") is not same as expected(%u)",
58 (uint64_t)buf.stx_size, SIZE);
59
60 }
61
test_sym_link(void)62 static void test_sym_link(void)
63 {
64 struct statx fbuf;
65 struct statx lbuf;
66
67 TEST(statx(AT_FDCWD, TESTFILE, 0, 0, &fbuf));
68
69 if (TST_RET == 0)
70 tst_res(TPASS,
71 "statx(AT_FDCWD, %s, 0, 0, &fbuf)", TESTFILE);
72 else
73 tst_brk(TFAIL | TTERRNO,
74 "statx(AT_FDCWD, %s, 0, 0, &fbuf)", TESTFILE);
75
76 TEST(statx(AT_FDCWD, LINK_FILE, AT_SYMLINK_NOFOLLOW, 0, &lbuf));
77
78 if (TST_RET == 0)
79 tst_res(TPASS,
80 "statx(AT_FDCWD, %s, AT_SYMLINK_NOFOLLOW, 0,&lbuf)",
81 LINK_FILE);
82 else
83 tst_brk(TFAIL | TTERRNO,
84 "statx(AT_FDCWD, %s, AT_SYMLINK_NOFOLLOW, 0,&lbuf)",
85 LINK_FILE);
86
87 if (fbuf.stx_ino != lbuf.stx_ino)
88 tst_res(TPASS, "Statx symlink flag worked as expected");
89 else
90 tst_res(TFAIL,
91 "Statx symlink flag failed to work as expected");
92 }
93
94 static struct tcase {
95 void (*tfunc)(void);
96 } tcases[] = {
97 {&test_empty_path},
98 {&test_sym_link}
99 };
100
run(unsigned int i)101 static void run(unsigned int i)
102 {
103 tcases[i].tfunc();
104 }
105
setup(void)106 static void setup(void)
107 {
108 char data_buf[SIZE] = "LinusTorvalds";
109
110 file_fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, MODE);
111 SAFE_WRITE(SAFE_WRITE_ANY, file_fd, data_buf, sizeof(data_buf));
112
113 SAFE_SYMLINK(TESTFILE, LINK_FILE);
114 }
115
cleanup(void)116 static void cleanup(void)
117 {
118 if (file_fd > 0)
119 SAFE_CLOSE(file_fd);
120 }
121
122 static struct tst_test test = {
123 .test = run,
124 .tcnt = ARRAY_SIZE(tcases),
125 .setup = setup,
126 .cleanup = cleanup,
127 .min_kver = "4.11",
128 .needs_tmpdir = 1,
129 };
130