1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker
3*49cdfc7eSAndroid Build Coastguard Worker /*
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2002
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2013 Wanlong Gao <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * This test verifies that a file opened with O_RDONLY can't be writable
13*49cdfc7eSAndroid Build Coastguard Worker * and it verifies that a file opened with O_WRONLY can't be readable.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "testfile"
19*49cdfc7eSAndroid Build Coastguard Worker
setup(void)20*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker SAFE_CREAT(TEMPFILE, 0600);
23*49cdfc7eSAndroid Build Coastguard Worker }
24*49cdfc7eSAndroid Build Coastguard Worker
verify_open(unsigned int nr)25*49cdfc7eSAndroid Build Coastguard Worker static void verify_open(unsigned int nr)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker char pbuf[BUFSIZ];
28*49cdfc7eSAndroid Build Coastguard Worker int fd = 0;
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker if (nr == 0) {
31*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEMPFILE, O_RDONLY);
32*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(write(fd, pbuf, 1), EBADF);
33*49cdfc7eSAndroid Build Coastguard Worker } else {
34*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEMPFILE, O_WRONLY);
35*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(read(fd, pbuf, 1), EBADF);
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
41*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
42*49cdfc7eSAndroid Build Coastguard Worker .test = verify_open,
43*49cdfc7eSAndroid Build Coastguard Worker .tcnt = 2,
44*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
45*49cdfc7eSAndroid Build Coastguard Worker };
46