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