xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/symlink/symlink04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Copyright (c) Linux Test Project, 2001-2023
5  * Author: John George
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Check that a symbolic link may point to an existing file or
12  * to a nonexistent one.
13  */
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include "tst_test.h"
18 
19 #define TESTFILE    "testfile"
20 #define NONFILE     "noexistfile"
21 #define SYMFILE     "slink_file"
22 
23 static char *testfile;
24 static char *nonfile;
25 
26 static struct tcase {
27 	char **srcfile;
28 } tcases[] = {
29 	{&testfile},
30 	{&nonfile},
31 };
32 
setup(void)33 static void setup(void)
34 {
35 	SAFE_TOUCH(TESTFILE, 0644, NULL);
36 }
37 
verify_symlink(unsigned int i)38 static void verify_symlink(unsigned int i)
39 {
40 	struct tcase *tc = &tcases[i];
41 
42 	struct stat stat_buf;
43 
44 	TST_EXP_PASS(symlink(*tc->srcfile, SYMFILE));
45 
46 	SAFE_LSTAT(SYMFILE, &stat_buf);
47 
48 	if (!S_ISLNK(stat_buf.st_mode))
49 		tst_res(TFAIL, "symlink of %s doesn't exist", *tc->srcfile);
50 
51 	SAFE_UNLINK(SYMFILE);
52 }
53 
54 static struct tst_test test = {
55 	.tcnt = ARRAY_SIZE(tcases),
56 	.setup = setup,
57 	.test = verify_symlink,
58 	.bufs = (struct tst_buffers []) {
59 		{&testfile, .str = TESTFILE},
60 		{&nonfile, .str = NONFILE},
61 		{},
62 	},
63 	.needs_tmpdir = 1,
64 };
65