1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2002-2022
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Verify that unlink(2) fails with
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * - ENOENT when file does not exist
13f08c3bdfSopenharmony_ci * - ENOENT when pathname is empty
14f08c3bdfSopenharmony_ci * - ENOENT when a component in pathname does not exist
15f08c3bdfSopenharmony_ci * - EFAULT when pathname points outside the accessible address space
16f08c3bdfSopenharmony_ci * - ENOTDIR when a component used as a directory in pathname is not,
17f08c3bdfSopenharmony_ci * in fact, a directory
18f08c3bdfSopenharmony_ci * - ENAMETOOLONG when pathname is too long
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <errno.h>
22f08c3bdfSopenharmony_ci#include <limits.h>
23f08c3bdfSopenharmony_ci#include <string.h>
24f08c3bdfSopenharmony_ci#include <unistd.h>
25f08c3bdfSopenharmony_ci#include "tst_test.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic char longpathname[PATH_MAX + 2];
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic struct test_case_t {
30f08c3bdfSopenharmony_ci	char *name;
31f08c3bdfSopenharmony_ci	char *desc;
32f08c3bdfSopenharmony_ci	int exp_errno;
33f08c3bdfSopenharmony_ci} tcases[] = {
34f08c3bdfSopenharmony_ci	{"nonexistfile", "non-existent file", ENOENT},
35f08c3bdfSopenharmony_ci	{"", "path is empty string", ENOENT},
36f08c3bdfSopenharmony_ci	{"nefile/file", "path contains a non-existent file", ENOENT},
37f08c3bdfSopenharmony_ci	{NULL, "invalid address", EFAULT},
38f08c3bdfSopenharmony_ci	{"file/file", "path contains a regular file", ENOTDIR},
39f08c3bdfSopenharmony_ci	{longpathname, "pathname too long", ENAMETOOLONG},
40f08c3bdfSopenharmony_ci};
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void verify_unlink(unsigned int n)
43f08c3bdfSopenharmony_ci{
44f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tcases[n];
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	TST_EXP_FAIL(unlink(tc->name), tc->exp_errno, "%s", tc->desc);
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic void setup(void)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	size_t n;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	SAFE_TOUCH("file", 0777, NULL);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	memset(longpathname, 'a', PATH_MAX + 2);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	for (n = 0; n < ARRAY_SIZE(tcases); n++) {
58f08c3bdfSopenharmony_ci		if (!tcases[n].name)
59f08c3bdfSopenharmony_ci			tcases[n].name = tst_get_bad_addr(NULL);
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic struct tst_test test = {
64f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
65f08c3bdfSopenharmony_ci	.setup = setup,
66f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
67f08c3bdfSopenharmony_ci	.test = verify_unlink,
68f08c3bdfSopenharmony_ci};
69