xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/truncate/truncate02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * 07/2001 John George
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - truncate(2) truncates a file to a specified length successfully.
13*49cdfc7eSAndroid Build Coastguard Worker  * - If the file is larger than the specified length, the extra data is lost.
14*49cdfc7eSAndroid Build Coastguard Worker  * - If the file is shorter than the specified length, the extra data is filled by '0'.
15*49cdfc7eSAndroid Build Coastguard Worker  * - truncate(2) doesn't change offset.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_prw.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"testfile"
28*49cdfc7eSAndroid Build Coastguard Worker #define FILE_SIZE	1024
29*49cdfc7eSAndroid Build Coastguard Worker #define TRUNC_LEN1	256
30*49cdfc7eSAndroid Build Coastguard Worker #define TRUNC_LEN2	512
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static int fd;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
35*49cdfc7eSAndroid Build Coastguard Worker 	off_t trunc_len;
36*49cdfc7eSAndroid Build Coastguard Worker 	off_t read_off;
37*49cdfc7eSAndroid Build Coastguard Worker 	off_t read_count;
38*49cdfc7eSAndroid Build Coastguard Worker 	char exp_char;
39*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
40*49cdfc7eSAndroid Build Coastguard Worker 	{TRUNC_LEN1, 0, TRUNC_LEN1, 'a'},
41*49cdfc7eSAndroid Build Coastguard Worker 	{TRUNC_LEN2, TRUNC_LEN1, TRUNC_LEN1, '\0'},
42*49cdfc7eSAndroid Build Coastguard Worker };
43*49cdfc7eSAndroid Build Coastguard Worker 
verify_truncate(unsigned int n)44*49cdfc7eSAndroid Build Coastguard Worker static void verify_truncate(unsigned int n)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
47*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
48*49cdfc7eSAndroid Build Coastguard Worker 	char read_buf[tc->read_count];
49*49cdfc7eSAndroid Build Coastguard Worker 	int i;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	memset(read_buf, 'b', tc->read_count);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	TEST(truncate(TESTFILE, tc->trunc_len));
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "truncate(%s, %ld) failed",
56*49cdfc7eSAndroid Build Coastguard Worker 			TESTFILE, tc->trunc_len);
57*49cdfc7eSAndroid Build Coastguard Worker 		return;
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
62*49cdfc7eSAndroid Build Coastguard Worker 			"truncate(%s, %ld) returned invalid value %ld",
63*49cdfc7eSAndroid Build Coastguard Worker 			TESTFILE, tc->trunc_len, TST_RET);
64*49cdfc7eSAndroid Build Coastguard Worker 		return;
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TESTFILE, &stat_buf);
68*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_size != tc->trunc_len) {
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: Incorrect file size %ld, expected %ld",
70*49cdfc7eSAndroid Build Coastguard Worker 			TESTFILE, stat_buf.st_size, tc->trunc_len);
71*49cdfc7eSAndroid Build Coastguard Worker 		return;
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	if (SAFE_LSEEK(fd, 0, SEEK_CUR)) {
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "truncate(%s, %ld) changes offset",
76*49cdfc7eSAndroid Build Coastguard Worker 			TESTFILE, tc->trunc_len);
77*49cdfc7eSAndroid Build Coastguard Worker 		return;
78*49cdfc7eSAndroid Build Coastguard Worker 	}
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PREAD(1, fd, read_buf, tc->read_count, tc->read_off);
81*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < tc->read_count; i++) {
82*49cdfc7eSAndroid Build Coastguard Worker 		if (read_buf[i] != tc->exp_char) {
83*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "%s: wrong content %c, expected %c",
84*49cdfc7eSAndroid Build Coastguard Worker 				TESTFILE, read_buf[i], tc->exp_char);
85*49cdfc7eSAndroid Build Coastguard Worker 			return;
86*49cdfc7eSAndroid Build Coastguard Worker 		}
87*49cdfc7eSAndroid Build Coastguard Worker 	}
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "truncate(%s, %ld) succeeded",
90*49cdfc7eSAndroid Build Coastguard Worker 		TESTFILE, tc->trunc_len);
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)93*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, 0644);
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	tst_fill_fd(fd, 'a', FILE_SIZE, 1);
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)102*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
103*49cdfc7eSAndroid Build Coastguard Worker {
104*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
105*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
106*49cdfc7eSAndroid Build Coastguard Worker }
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
109*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
110*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
111*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
112*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
113*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_truncate,
114*49cdfc7eSAndroid Build Coastguard Worker };
115