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 * Test statx
9 *
10 * This code tests if expected error values are returned for specific cases by
11 * statx.
12 * The error cases are simulated and the return value is checked against
13 * expected error number value.
14 * The following error values are tested:
15 * 1) EBADF - Bad file descriptor
16 * 2) EFAULT - Bad address
17 * 3) EINVAL - Invalid argument
18 * 4) ENOENT - No such file or directory
19 * 5) ENOTDIR - Not a directory
20 * 6) ENAMETOOLONG - Filename too long
21 *
22 * Error scenario is simulated for each listed flag by passing
23 * respective arguments.
24 * The obtained error flag is checked against the expected
25 * flag value for that scenario.
26 *
27 * Minimum Kernel version required is 4.11.
28 */
29
30 #define _GNU_SOURCE
31 #include <stdio.h>
32 #include <string.h>
33 #include "tst_test.h"
34 #include "tst_safe_macros.h"
35 #include "tst_get_bad_addr.h"
36 #include "lapi/stat.h"
37
38 #define TESTFILE "test_file"
39 #define MODE 0644
40
41 static char long_pathname[257];
42
43 static char *test_fname = TESTFILE;
44 static char *efault_fname;
45 static char *empty_fname = "";
46 static char *etoolong_fname = long_pathname;
47
48 static struct test_case {
49 uint32_t dfd;
50 char **filename;
51 uint32_t flag;
52 uint32_t mask;
53 int32_t errnum;
54 } tcases[] = {
55 {.dfd = -1, .filename = &test_fname, .flag = 0,
56 .mask = 0, .errnum = EBADF},
57
58 {.dfd = AT_FDCWD, .filename = &efault_fname, .flag = 0,
59 .mask = 0, .errnum = EFAULT},
60
61 {.dfd = AT_FDCWD, .filename = &test_fname, .flag = -1,
62 .mask = 0, .errnum = EINVAL},
63
64 {.dfd = AT_FDCWD, .filename = &test_fname, .flag = 0,
65 .mask = -1, .errnum = EINVAL},
66
67 {.dfd = AT_FDCWD, .filename = &empty_fname, .flag = 0,
68 .mask = 0, .errnum = ENOENT},
69
70 {.dfd = 1, .filename = &test_fname, .flag = 0,
71 .mask = 0, .errnum = ENOTDIR},
72
73 {.dfd = AT_FDCWD, .filename = &etoolong_fname, .flag = 0,
74 .mask = 0, .errnum = ENAMETOOLONG},
75 };
76
run_test(unsigned int i)77 static void run_test(unsigned int i)
78 {
79 struct statx buf;
80 struct test_case *tc = &tcases[i];
81
82 TEST(statx(tc->dfd, *(tc->filename), tc->flag,
83 tc->mask, &buf));
84
85 if (TST_RET != -1) {
86 tst_res(TFAIL, "statx() returned with %ld", TST_RET);
87 return;
88 }
89
90 if (tc->errnum == TST_ERR) {
91 tst_res(TPASS | TTERRNO, "statx() failed with");
92 return;
93 }
94
95 tst_res(TFAIL | TTERRNO,
96 "statx() should fail with %s", tst_strerrno(tc->errnum));
97 }
98
setup(void)99 static void setup(void)
100 {
101 int file_fd;
102
103 file_fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, MODE);
104 SAFE_CLOSE(file_fd);
105
106 memset(long_pathname, '@', sizeof(long_pathname));
107 long_pathname[sizeof(long_pathname) - 1] = 0;
108
109 efault_fname = tst_get_bad_addr(NULL);
110 }
111
112 static struct tst_test test = {
113 .tcnt = ARRAY_SIZE(tcases),
114 .test = run_test,
115 .setup = setup,
116 .min_kver = "4.11",
117 .needs_tmpdir = 1,
118 };
119