blob: 379451c64cd305c3daf343fc51e71bb884c765c4 [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"
plars865695b2001-08-27 22:15:12 +000054
robbiew67a9f062002-02-12 19:54:47 +000055#define TESTFILE "ftruncate03_tst_file"
plars865695b2001-08-27 22:15:12 +000056
subrata_modak585950c2008-08-20 10:55:19 +000057TCID_DEFINE(ftruncate03);
subrata_modak56207ce2009-03-23 13:35:39 +000058int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000059
Mike Frysingerc57fba52014-04-09 18:56:30 -040060int main(void)
plars865695b2001-08-27 22:15:12 +000061{
subrata_modak56207ce2009-03-23 13:35:39 +000062 int wjh_ret = -1, wjh_f = -1, count = 0;
63 //used for the 2nd test
64 //make str > trunc_size characters long
65 char str[] = "THIS IS JAYS TEST FILE DATA";
66 int trunc_size = 4;
67 int flag = O_RDONLY;
plars865695b2001-08-27 22:15:12 +000068
vapier01e72112006-02-19 09:10:28 +000069#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000070 printf("Starting test, possible errnos are; EBADF(%d) EINVAL(%d)\n",
71 EBADF, EINVAL);
72 printf("\t\tENOENT(%d) EACCES(%d) EPERM(%d)\n\n", ENOENT, EACCES,
73 EPERM);
vapier01e72112006-02-19 09:10:28 +000074#endif
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +000077
robbiewb358efb2003-03-26 18:23:34 +000078//TEST1: ftruncate on a socket is not valid, should fail w/ EINVAL
plars865695b2001-08-27 22:15:12 +000079
vapier01e72112006-02-19 09:10:28 +000080#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000081 printf("Starting test1\n");
vapier01e72112006-02-19 09:10:28 +000082#endif
subrata_modak56207ce2009-03-23 13:35:39 +000083 wjh_f = socket(PF_INET, SOCK_STREAM, 0);
84 wjh_ret = ftruncate(wjh_f, 1);
robbiew67a9f062002-02-12 19:54:47 +000085#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +000086 printf("DEBUG: fd: %d ret: %d errno(%d) %s\n",
87 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +000088#endif
subrata_modak56207ce2009-03-23 13:35:39 +000089 if (wjh_ret == -1 && errno == EINVAL) {
90 tst_resm(TPASS, "Test Passed");
91 } else {
92 tst_resm(TFAIL,
93 "ftruncate(socket)=%i (wanted -1), errno=%i (wanted EINVAL %i)",
94 wjh_ret, errno, EINVAL);
95 }
96 close(wjh_f);
97 errno = 0;
98 wjh_ret = 0;
99 wjh_f = -1;
plars865695b2001-08-27 22:15:12 +0000100
robbiewb358efb2003-03-26 18:23:34 +0000101//TEST2: ftruncate on fd not open for writing should be EINVAL
plars865695b2001-08-27 22:15:12 +0000102
vapier01e72112006-02-19 09:10:28 +0000103#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000104 printf("\nStarting test2\n");
vapier01e72112006-02-19 09:10:28 +0000105#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000106 //create a file and fill it so we can truncate it in ReadOnly mode
107 //delete it first, ignore if it doesn't exist
108 unlink(TESTFILE);
109 errno = 0;
110 wjh_f = open(TESTFILE, O_RDWR | O_CREAT, 0644);
111 if (wjh_f == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100112 tst_brkm(TFAIL | TERRNO, tst_rmdir, "open(%s) failed",
113 TESTFILE);
subrata_modak56207ce2009-03-23 13:35:39 +0000114 }
115 while (count < strlen(str)) {
116 if ((count += write(wjh_f, str, strlen(str))) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 tst_resm(TFAIL | TERRNO, "write() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000118 close(wjh_f);
119 tst_rmdir();
120 tst_exit();
121 }
122 }
123 close(wjh_f);
124 errno = 0;
plars865695b2001-08-27 22:15:12 +0000125
robbiew67a9f062002-02-12 19:54:47 +0000126//Uncomment below if you want it to succeed, O_RDWR => success
127// flag = O_RDWR;
vapier01e72112006-02-19 09:10:28 +0000128#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if (flag == O_RDWR) {
130 printf("\tLooks like it should succeed!\n");
131 }
vapier01e72112006-02-19 09:10:28 +0000132#endif
plars865695b2001-08-27 22:15:12 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 wjh_f = open(TESTFILE, flag);
135 if (wjh_f == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100136 tst_brkm(TFAIL | TERRNO, tst_rmdir, "open(%s) failed",
137 TESTFILE);
subrata_modak56207ce2009-03-23 13:35:39 +0000138 }
139 wjh_ret = ftruncate(wjh_f, trunc_size);
robbiew67a9f062002-02-12 19:54:47 +0000140#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000141 printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n",
142 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +0000143#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000144 if ((flag == O_RDONLY) && (wjh_ret == -1) && (errno == EINVAL)) {
145 tst_resm(TPASS, "Test Passed");
146 } else if ((flag == O_RDWR)) {
147 if (wjh_ret == 0) {
148 tst_resm(TPASS, "Test Succeeded!");
149 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_resm(TFAIL | TERRNO,
subrata_modak56207ce2009-03-23 13:35:39 +0000151 "ftruncate(%s) should have succeeded, but didn't! ret="
vapierc20c04b2009-08-28 13:00:33 +0000152 "%d (wanted 0)", TESTFILE, wjh_ret);
subrata_modak56207ce2009-03-23 13:35:39 +0000153 }
154 } else //flag was O_RDONLY but return codes wrong
155 {
156 tst_resm(TFAIL,
157 "ftruncate(rd_only_fd)=%i (wanted -1), errno=%i (wanted %i EINVAL)",
158 wjh_ret, errno, EINVAL);
159 }
160 close(wjh_f);
161 errno = 0;
162 wjh_ret = 0;
163 wjh_f = -1;
plars865695b2001-08-27 22:15:12 +0000164
robbiewb358efb2003-03-26 18:23:34 +0000165//TEST3: invalid socket descriptor should fail w/ EBADF
plars865695b2001-08-27 22:15:12 +0000166
vapier01e72112006-02-19 09:10:28 +0000167#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000168 printf("\nStarting test3\n");
vapier01e72112006-02-19 09:10:28 +0000169#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000170 wjh_f = -999999; //should be a bad file descriptor
171 wjh_ret = ftruncate(wjh_f, trunc_size);
robbiew67a9f062002-02-12 19:54:47 +0000172#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000173 printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n",
174 wjh_f, wjh_ret, errno, strerror(errno));
robbiew67a9f062002-02-12 19:54:47 +0000175#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000176 if (wjh_ret != -1 || errno != EBADF) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 tst_resm(TFAIL | TERRNO,
vapierc20c04b2009-08-28 13:00:33 +0000178 "ftruncate(invalid_fd)=%d (wanted -1 and EBADF)",
179 wjh_ret);
subrata_modak56207ce2009-03-23 13:35:39 +0000180 } else {
181 tst_resm(TPASS, "Test Passed");
182 }
plars865695b2001-08-27 22:15:12 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 tst_rmdir();
robbiew57dfebc2002-03-19 16:45:42 +0000185
robbiew67a9f062002-02-12 19:54:47 +0000186//Done Testing
Garrett Cooper2c282152010-12-16 00:55:50 -0800187 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700188}