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 */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Description:
8f08c3bdfSopenharmony_ci * The testcase checks the basic functionality of the unlink(2).
9f08c3bdfSopenharmony_ci * 1) unlink() can delete regular file successfully.
10f08c3bdfSopenharmony_ci * 2) unlink() can delete fifo file successfully.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <sys/types.h>
15f08c3bdfSopenharmony_ci#include <unistd.h>
16f08c3bdfSopenharmony_ci#include <stdio.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void file_create(char *);
20f08c3bdfSopenharmony_cistatic void fifo_create(char *);
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic struct test_case_t {
23f08c3bdfSopenharmony_ci	void (*setupfunc)(char *);
24f08c3bdfSopenharmony_ci	char *desc;
25f08c3bdfSopenharmony_ci} tcases[] = {
26f08c3bdfSopenharmony_ci	{file_create, "file"},
27f08c3bdfSopenharmony_ci	{fifo_create, "fifo"},
28f08c3bdfSopenharmony_ci};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void file_create(char *name)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	sprintf(name, "tfile_%d", getpid());
33f08c3bdfSopenharmony_ci	SAFE_TOUCH(name, 0777, NULL);
34f08c3bdfSopenharmony_ci}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void fifo_create(char *name)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	sprintf(name, "tfifo_%d", getpid());
39f08c3bdfSopenharmony_ci	SAFE_MKFIFO(name, 0777);
40f08c3bdfSopenharmony_ci}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void verify_unlink(unsigned int n)
43f08c3bdfSopenharmony_ci{
44f08c3bdfSopenharmony_ci	char fname[255];
45f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tcases[n];
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	tc->setupfunc(fname);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	TEST(unlink(fname));
50f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
51f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "unlink(%s) failed", tc->desc);
52f08c3bdfSopenharmony_ci		return;
53f08c3bdfSopenharmony_ci	}
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	if (!access(fname, F_OK)) {
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "unlink(%s) succeeded, but %s still existed",
57f08c3bdfSopenharmony_ci			tc->desc, tc->desc);
58f08c3bdfSopenharmony_ci		return;
59f08c3bdfSopenharmony_ci	}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	tst_res(TPASS, "unlink(%s) succeeded", tc->desc);
62f08c3bdfSopenharmony_ci}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cistatic struct tst_test test = {
65f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
66f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
67f08c3bdfSopenharmony_ci	.test = verify_unlink,
68f08c3bdfSopenharmony_ci};
69