1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: truncate01
22  *
23  * Test Description:
24  *  Verify that, truncate(2) succeeds to truncate a file to a specified
25  *  length.
26  *
27  * Expected Result:
28  *  truncate(2) should return a value 0 and the length of the file after
29  *  truncation should be equal to the length it is truncated to.
30  *
31  * Algorithm:
32  *  Setup:
33  *   Setup signal handling.
34  *   Create temporary directory.
35  *   Pause for SIGUSR1 if option specified.
36  *
37  *  Test:
38  *   Loop if the proper options are given.
39  *   Execute system call
40  *   Check return code, if system call failed (return=-1)
41  *   	Log the errno and Issue a FAIL message.
42  *   Otherwise,
43  *   	Verify the Functionality of system call
44  *      if successful,
45  *      	Issue Functionality-Pass message.
46  *      Otherwise,
47  *		Issue Functionality-Fail message.
48  *  Cleanup:
49  *   Print errno log and/or timing stats if options given
50  *   Delete the temporary directory created.
51  *
52  * Usage:  <for command-line>
53  *  truncate01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
54  *	where,  -c n : Run n copies concurrently.
55  *		-e   : Turn on errno logging.
56  *		-f   : Turn off functionality Testing.
57  *		-i n : Execute test n times.
58  *		-I x : Execute test for x seconds.
59  *		-P x : Pause for x seconds between iterations.
60  *		-t   : Turn on syscall timing.
61  *
62  * History
63  *	07/2001 John George
64  *		-Ported
65  *
66  * Restrictions:
67  *  This test should be run by 'non-super-user' only.
68  *
69  */
70 
71 #include <sys/types.h>
72 #include <sys/stat.h>
73 #include <sys/fcntl.h>
74 #include <errno.h>
75 #include <string.h>
76 #include <signal.h>
77 #include <inttypes.h>
78 
79 #include "test.h"
80 
81 #define TESTFILE	"testfile"	/* file under test */
82 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
83 #define BUF_SIZE	256	/* buffer size */
84 #define FILE_SIZE	1024	/* test file size */
85 #define TRUNC_LEN	256	/* truncation length */
86 
87 TCID_DEFINE(truncate01);
88 int TST_TOTAL = 1;
89 
90 void setup();			/* setup function for the test */
91 void cleanup();			/* cleanup function for the test */
92 
main(int ac,char ** av)93 int main(int ac, char **av)
94 {
95 	struct stat stat_buf;	/* stat(2) struct contents */
96 	int lc;
97 	off_t file_length;	/* test file length */
98 
99 	tst_parse_opts(ac, av, NULL, NULL);
100 
101 	setup();
102 
103 	for (lc = 0; TEST_LOOPING(lc); lc++) {
104 
105 		tst_count = 0;
106 
107 		/*
108 		 * Call truncate(2) to truncate a test file to a
109 		 * specified length.
110 		 */
111 		TEST(truncate(TESTFILE, TRUNC_LEN));
112 
113 		if (TEST_RETURN == -1) {
114 			tst_resm(TFAIL,
115 				 "truncate(%s, %d) Failed, errno=%d : %s",
116 				 TESTFILE, TRUNC_LEN, TEST_ERRNO,
117 				 strerror(TEST_ERRNO));
118 		} else {
119 			/*
120 			 * Get the testfile information using
121 			 * stat(2).
122 			 */
123 			if (stat(TESTFILE, &stat_buf) < 0) {
124 				tst_brkm(TFAIL, cleanup, "stat(2) of "
125 					 "%s failed, error:%d",
126 					 TESTFILE, errno);
127 			}
128 			stat_buf.st_mode &= ~S_IFREG;
129 			file_length = stat_buf.st_size;
130 
131 			/*
132 			 * Check for expected size of testfile after
133 			 * truncate(2) on it.
134 			 */
135 			if (file_length != TRUNC_LEN) {
136 				tst_resm(TFAIL, "%s: Incorrect file "
137 					 "size %" PRId64
138 					 ", Expected %d", TESTFILE,
139 					 (int64_t) file_length,
140 					 TRUNC_LEN);
141 			} else {
142 				tst_resm(TPASS, "Functionality of "
143 					 "truncate(%s, %d) successful",
144 					 TESTFILE, TRUNC_LEN);
145 			}
146 		}
147 		tst_count++;	/* incr TEST_LOOP counter */
148 	}
149 
150 	cleanup();
151 	tst_exit();
152 }
153 
154 /*
155  * void
156  * setup() - performs all ONE TIME setup for this test.
157  *  Create a temporary directory and change directory to it.
158  *  Fill the buffer with some arbitrary data to be written to a file.
159  *  Create a test file under temporary directory and close it
160  *  write arbitrary data into testfile.
161  */
setup(void)162 void setup(void)
163 {
164 	int fd, i;		/* file handler for testfile */
165 	int c, c_total = 0;	/* no. bytes to be written to file */
166 	char tst_buff[BUF_SIZE];	/* buffer to hold data */
167 
168 	tst_sig(FORK, DEF_HANDLER, cleanup);
169 
170 	/* Pause if that option was specified
171 	 * TEST_PAUSE contains the code to fork the test with the -i option.
172 	 * You want to make sure you do this before you create your temporary
173 	 * directory.
174 	 */
175 	TEST_PAUSE;
176 
177 	tst_tmpdir();
178 
179 	/* Fill the test buffer with the known data */
180 	for (i = 0; i < BUF_SIZE; i++) {
181 		tst_buff[i] = 'a';
182 	}
183 
184 	/* Creat a testfile under temporary directory */
185 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
186 		tst_brkm(TBROK, cleanup,
187 			 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
188 			 TESTFILE, FILE_MODE, errno, strerror(errno));
189 	}
190 
191 	/* Write to the file 1k data from the buffer */
192 	while (c_total < FILE_SIZE) {
193 		if ((c = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
194 			tst_brkm(TBROK, cleanup,
195 				 "write(2) on %s Failed, errno=%d : %s",
196 				 TESTFILE, errno, strerror(errno));
197 		} else {
198 			c_total += c;
199 		}
200 	}
201 
202 	/* Close the testfile after writing data into it */
203 	if (close(fd) == -1) {
204 		tst_brkm(TBROK, cleanup,
205 			 "close(%s) Failed, errno=%d : %s",
206 			 TESTFILE, errno, strerror(errno));
207 	}
208 }
209 
210 /*
211  * void
212  * cleanup() - performs all ONE TIME cleanup for this test at
213  *	       completion or premature exit.
214  *  Remove the test directory and testfile created in the setup.
215  */
cleanup(void)216 void cleanup(void)
217 {
218 
219 	tst_rmdir();
220 
221 }
222