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