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