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