1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2002 4f08c3bdfSopenharmony_ci * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved. 5f08c3bdfSopenharmony_ci * Author: Jay Huie, Robbie Williamson 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Verify that ftruncate(2) system call returns appropriate error number: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * 1. EINVAL -- the file is a socket 14f08c3bdfSopenharmony_ci * 2. EINVAL -- the file descriptor was opened with O_RDONLY 15f08c3bdfSopenharmony_ci * 3. EINVAL -- the length is negative 16f08c3bdfSopenharmony_ci * 4. EBADF -- the file descriptor is invalid 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#include <sys/types.h> 20f08c3bdfSopenharmony_ci#include <sys/stat.h> 21f08c3bdfSopenharmony_ci#include <fcntl.h> 22f08c3bdfSopenharmony_ci#include <unistd.h> 23f08c3bdfSopenharmony_ci#include <errno.h> 24f08c3bdfSopenharmony_ci#include <string.h> 25f08c3bdfSopenharmony_ci#include <sys/socket.h> 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#include "tst_test.h" 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci#define TESTFILE1 "testfile1" 30f08c3bdfSopenharmony_ci#define TESTFILE2 "testfile2" 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic int sock_fd, read_fd, fd; 33f08c3bdfSopenharmony_cistatic int bad_fd = -1; 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic struct tcase { 36f08c3bdfSopenharmony_ci int *fd; 37f08c3bdfSopenharmony_ci off_t length; 38f08c3bdfSopenharmony_ci int exp_errno; 39f08c3bdfSopenharmony_ci} tcases[] = { 40f08c3bdfSopenharmony_ci {&sock_fd, 4, EINVAL}, 41f08c3bdfSopenharmony_ci {&read_fd, 4, EINVAL}, 42f08c3bdfSopenharmony_ci {&fd, -1, EINVAL}, 43f08c3bdfSopenharmony_ci {&bad_fd, 4, EBADF}, 44f08c3bdfSopenharmony_ci}; 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_cistatic void verify_ftruncate(unsigned int n) 47f08c3bdfSopenharmony_ci{ 48f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci TEST(ftruncate(*tc->fd, tc->length)); 51f08c3bdfSopenharmony_ci if (TST_RET != -1) { 52f08c3bdfSopenharmony_ci tst_res(TFAIL, "ftruncate() succeeded unexpectedly and got %ld", 53f08c3bdfSopenharmony_ci TST_RET); 54f08c3bdfSopenharmony_ci return; 55f08c3bdfSopenharmony_ci } 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci if (TST_ERR == tc->exp_errno) { 58f08c3bdfSopenharmony_ci tst_res(TPASS | TTERRNO, "ftruncate() failed expectedly"); 59f08c3bdfSopenharmony_ci } else { 60f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 61f08c3bdfSopenharmony_ci "ftruncate() failed unexpectedly, got %s, expected", 62f08c3bdfSopenharmony_ci tst_strerrno(tc->exp_errno)); 63f08c3bdfSopenharmony_ci } 64f08c3bdfSopenharmony_ci} 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_cistatic void setup(void) 67f08c3bdfSopenharmony_ci{ 68f08c3bdfSopenharmony_ci sock_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci read_fd = SAFE_OPEN(TESTFILE1, O_RDONLY | O_CREAT, 0644); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE2, O_RDWR | O_CREAT, 0644); 73f08c3bdfSopenharmony_ci} 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_cistatic void cleanup(void) 76f08c3bdfSopenharmony_ci{ 77f08c3bdfSopenharmony_ci if (sock_fd > 0) 78f08c3bdfSopenharmony_ci SAFE_CLOSE(sock_fd); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci if (read_fd > 0) 81f08c3bdfSopenharmony_ci SAFE_CLOSE(read_fd); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci if (fd > 0) 84f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 85f08c3bdfSopenharmony_ci} 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_cistatic struct tst_test test = { 88f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 89f08c3bdfSopenharmony_ci .test = verify_ftruncate, 90f08c3bdfSopenharmony_ci .setup = setup, 91f08c3bdfSopenharmony_ci .cleanup = cleanup, 92f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 93f08c3bdfSopenharmony_ci}; 94