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