blob: 9f6e93f969f8d900f7ccb05c7204d4a639d39870 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
robbiew67a9f062002-02-12 19:54:47 +00003 * Copyright (c) International Business Machines Corp., 2002
plars865695b2001-08-27 22:15:12 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
robbiew67a9f062002-02-12 19:54:47 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * Test Name: ftruncate03
22 *
23 * Test Description:
24 * Verify that,
plars0b3d5f52001-10-23 12:32:55 +000025 * 1) ftruncate(2) returns -1 and sets errno to EINVAL if the specified
robbiew67a9f062002-02-12 19:54:47 +000026 * socket is invalid.
27 * 2) ftruncate(2) returns -1 and sets errno to EINVAL if the specified
robbiew06d067e2001-09-11 17:37:01 +000028 * file descriptor has an attempt to write, when open for read only.
robbiew67a9f062002-02-12 19:54:47 +000029 * 3) ftruncate(2) returns -1 and sets errno to EBADF if the file descriptor
plars865695b2001-08-27 22:15:12 +000030 * of the specified file is not valid.
31 *
32 * Expected Result:
33 * ftruncate() should fail with return value -1 and set expected errno.
34 *
plars865695b2001-08-27 22:15:12 +000035 * HISTORY
robbiew67a9f062002-02-12 19:54:47 +000036 * 02/2002 Written by Jay Huie
37 * 02/2002 Adapted for and included into the LTP by Robbie Williamson
plars865695b2001-08-27 22:15:12 +000038 *
39 * RESTRICTIONS:
robbiew67a9f062002-02-12 19:54:47 +000040 * This test should be run by 'non-super-user' only.
41 *
plars865695b2001-08-27 22:15:12 +000042 */
43
44#include <stdio.h>
robbiew67a9f062002-02-12 19:54:47 +000045#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000046#include <errno.h>
47#include <string.h>
vapierc20c04b2009-08-28 13:00:33 +000048#include <inttypes.h>
robbiew67a9f062002-02-12 19:54:47 +000049#include <sys/types.h>
50#include <sys/socket.h>
51#include <sys/fcntl.h>
plars865695b2001-08-27 22:15:12 +000052
53#include "test.h"
54#include "usctest.h"
55
robbiew67a9f062002-02-12 19:54:47 +000056#define TESTFILE "ftruncate03_tst_file"
plars865695b2001-08-27 22:15:12 +000057
subrata_modak585950c2008-08-20 10:55:19 +000058TCID_DEFINE(ftruncate03);
subrata_modak56207ce2009-03-23 13:35:39 +000059int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000060
Mike Frysingerc57fba52014-04-09 18:56:30 -040061int main(void)
plars865695b2001-08-27 22:15:12 +000062{
subrata_modak56207ce2009-03-23 13:35:39 +000063 int wjh_ret = -1, wjh_f = -1, count = 0;
64 //used for the 2nd test
65 //make str > trunc_size characters long
66 char str[] = "THIS IS JAYS TEST FILE DATA";
67 int trunc_size = 4;
68 int flag = O_RDONLY;
plars865695b2001-08-27 22:15:12 +000069
vapier01e72112006-02-19 09:10:28 +000070#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000071 printf("Starting test, possible errnos are; EBADF(%d) EINVAL(%d)\n",
72 EBADF, EINVAL);
73 printf("\t\tENOENT(%d) EACCES(%d) EPERM(%d)\n\n", ENOENT, EACCES,
74 EPERM);
vapier01e72112006-02-19 09:10:28 +000075#endif
plars865695b2001-08-27 22:15:12 +000076
subrata_modak56207ce2009-03-23 13:35:39 +000077 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +000078
robbiewb358efb2003-03-26 18:23:34 +000079//TEST1: ftruncate on a socket is not valid, should fail w/ EINVAL
plars865695b2001-08-27 22:15:12 +000080
vapier01e72112006-02-19 09:10:28 +000081#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000082 printf("Starting test1\n");
vapier01e72112006-02-19 09:10:28 +000083#endif
subrata_modak56207ce2009-03-23 13:35:39 +000084 wjh_f = socket(PF_INET, SOCK_STREAM, 0);
85 wjh_ret = ftruncate(wjh_f, 1);
robbiew67a9f062002-02-12 19:54:47 +000086#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000087 printf("DEBUG: fd: %d ret: %d errno(%d) %s\n",
88 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +000089#endif
subrata_modak56207ce2009-03-23 13:35:39 +000090 if (wjh_ret == -1 && errno == EINVAL) {
91 tst_resm(TPASS, "Test Passed");
92 } else {
93 tst_resm(TFAIL,
94 "ftruncate(socket)=%i (wanted -1), errno=%i (wanted EINVAL %i)",
95 wjh_ret, errno, EINVAL);
96 }
97 close(wjh_f);
98 errno = 0;
99 wjh_ret = 0;
100 wjh_f = -1;
plars865695b2001-08-27 22:15:12 +0000101
robbiewb358efb2003-03-26 18:23:34 +0000102//TEST2: ftruncate on fd not open for writing should be EINVAL
plars865695b2001-08-27 22:15:12 +0000103
vapier01e72112006-02-19 09:10:28 +0000104#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000105 printf("\nStarting test2\n");
vapier01e72112006-02-19 09:10:28 +0000106#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000107 //create a file and fill it so we can truncate it in ReadOnly mode
108 //delete it first, ignore if it doesn't exist
109 unlink(TESTFILE);
110 errno = 0;
111 wjh_f = open(TESTFILE, O_RDWR | O_CREAT, 0644);
112 if (wjh_f == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100113 tst_brkm(TFAIL | TERRNO, tst_rmdir, "open(%s) failed",
114 TESTFILE);
subrata_modak56207ce2009-03-23 13:35:39 +0000115 }
116 while (count < strlen(str)) {
117 if ((count += write(wjh_f, str, strlen(str))) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 tst_resm(TFAIL | TERRNO, "write() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000119 close(wjh_f);
120 tst_rmdir();
121 tst_exit();
122 }
123 }
124 close(wjh_f);
125 errno = 0;
plars865695b2001-08-27 22:15:12 +0000126
robbiew67a9f062002-02-12 19:54:47 +0000127//Uncomment below if you want it to succeed, O_RDWR => success
128// flag = O_RDWR;
vapier01e72112006-02-19 09:10:28 +0000129#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000130 if (flag == O_RDWR) {
131 printf("\tLooks like it should succeed!\n");
132 }
vapier01e72112006-02-19 09:10:28 +0000133#endif
plars865695b2001-08-27 22:15:12 +0000134
subrata_modak56207ce2009-03-23 13:35:39 +0000135 wjh_f = open(TESTFILE, flag);
136 if (wjh_f == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100137 tst_brkm(TFAIL | TERRNO, tst_rmdir, "open(%s) failed",
138 TESTFILE);
subrata_modak56207ce2009-03-23 13:35:39 +0000139 }
140 wjh_ret = ftruncate(wjh_f, trunc_size);
robbiew67a9f062002-02-12 19:54:47 +0000141#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000142 printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n",
143 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +0000144#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000145 if ((flag == O_RDONLY) && (wjh_ret == -1) && (errno == EINVAL)) {
146 tst_resm(TPASS, "Test Passed");
147 } else if ((flag == O_RDWR)) {
148 if (wjh_ret == 0) {
149 tst_resm(TPASS, "Test Succeeded!");
150 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 tst_resm(TFAIL | TERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +0000152 "ftruncate(%s) should have succeeded, but didn't! ret="
vapierc20c04b2009-08-28 13:00:33 +0000153 "%d (wanted 0)", TESTFILE, wjh_ret);
subrata_modak56207ce2009-03-23 13:35:39 +0000154 }
155 } else //flag was O_RDONLY but return codes wrong
156 {
157 tst_resm(TFAIL,
158 "ftruncate(rd_only_fd)=%i (wanted -1), errno=%i (wanted %i EINVAL)",
159 wjh_ret, errno, EINVAL);
160 }
161 close(wjh_f);
162 errno = 0;
163 wjh_ret = 0;
164 wjh_f = -1;
plars865695b2001-08-27 22:15:12 +0000165
robbiewb358efb2003-03-26 18:23:34 +0000166//TEST3: invalid socket descriptor should fail w/ EBADF
plars865695b2001-08-27 22:15:12 +0000167
vapier01e72112006-02-19 09:10:28 +0000168#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000169 printf("\nStarting test3\n");
vapier01e72112006-02-19 09:10:28 +0000170#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000171 wjh_f = -999999; //should be a bad file descriptor
172 wjh_ret = ftruncate(wjh_f, trunc_size);
robbiew67a9f062002-02-12 19:54:47 +0000173#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000174 printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n",
175 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +0000176#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000177 if (wjh_ret != -1 || errno != EBADF) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 tst_resm(TFAIL | TERRNO,
vapierc20c04b2009-08-28 13:00:33 +0000179 "ftruncate(invalid_fd)=%d (wanted -1 and EBADF)",
180 wjh_ret);
subrata_modak56207ce2009-03-23 13:35:39 +0000181 } else {
182 tst_resm(TPASS, "Test Passed");
183 }
plars865695b2001-08-27 22:15:12 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 tst_rmdir();
robbiew57dfebc2002-03-19 16:45:42 +0000186
robbiew67a9f062002-02-12 19:54:47 +0000187//Done Testing
Garrett Cooper2c282152010-12-16 00:55:50 -0800188 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700189}