xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/write/write02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2017 Carlo Marcelo Arenas Belon <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2003-2023
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  * Tests for a special case NULL buffer with size 0 is expected to return 0.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker static int fd;
17*49cdfc7eSAndroid Build Coastguard Worker 
verify_write(void)18*49cdfc7eSAndroid Build Coastguard Worker static void verify_write(void)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_POSITIVE(write(fd, NULL, 0));
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(TST_RET == 0, "write(fd, NULL, %ld) == %d", TST_RET, 0);
23*49cdfc7eSAndroid Build Coastguard Worker }
24*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)25*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("test_file", O_RDWR | O_CREAT, 0700);
28*49cdfc7eSAndroid Build Coastguard Worker }
29*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)30*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
33*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
37*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
38*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
39*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_write,
40*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
41*49cdfc7eSAndroid Build Coastguard Worker };
42