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