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