truncate01.c revision 45e285d46ab47b0ff76c88acb5ba97b0bd5f753d
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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#include "usctest.h"
81
82#define TESTFILE	"testfile"	/* file under test */
83#define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
84#define BUF_SIZE	256	/* buffer size */
85#define FILE_SIZE	1024	/* test file size */
86#define TRUNC_LEN	256	/* truncation length */
87
88TCID_DEFINE(truncate01);	/* Test program identifier.    */
89int TST_TOTAL = 1;		/* Total number of test conditions */
90extern int Tst_count;		/* Test Case counter for tst_* routines */
91int exp_enos[] = { 0 };
92
93void setup();			/* setup function for the test */
94void cleanup();			/* cleanup function for the test */
95
96int main(int ac, char **av)
97{
98	struct stat stat_buf;	/* stat(2) struct contents */
99	int lc;			/* loop counter */
100	char *msg;		/* message returned from parse_opts */
101	off_t file_length;	/* test file length */
102
103	/* Parse standard options given to run the test. */
104	msg = parse_opts(ac, av, NULL, NULL);
105	if (msg != NULL) {
106		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
107		tst_exit();
108	}
109
110	/* Perform global setup for test */
111	setup();
112
113	/* set the expected errnos... */
114	TEST_EXP_ENOS(exp_enos);
115
116	/* Check looping state if -i option given */
117	for (lc = 0; TEST_LOOPING(lc); lc++) {
118		/* Reset Tst_count in case we are looping. */
119		Tst_count = 0;
120
121		/*
122		 * Call truncate(2) to truncate a test file to a
123		 * specified length.
124		 */
125		TEST(truncate(TESTFILE, TRUNC_LEN));
126
127		/* check return code of truncate(2) */
128		if (TEST_RETURN == -1) {
129			TEST_ERROR_LOG(TEST_ERRNO);
130			tst_resm(TFAIL,
131				 "truncate(%s, %d) Failed, errno=%d : %s",
132				 TESTFILE, TRUNC_LEN, TEST_ERRNO,
133				 strerror(TEST_ERRNO));
134		} else {
135			/*
136			 * Perform functional verification if test
137			 * executed without (-f) option.
138			 */
139			if (STD_FUNCTIONAL_TEST) {
140				/*
141				 * Get the testfile information using
142				 * stat(2).
143				 */
144				if (stat(TESTFILE, &stat_buf) < 0) {
145					tst_brkm(TFAIL, cleanup, "stat(2) of "
146						 "%s failed, error:%d",
147						 TESTFILE, errno);
148				 /*NOTREACHED*/}
149				stat_buf.st_mode &= ~S_IFREG;
150				file_length = stat_buf.st_size;
151
152				/*
153				 * Check for expected size of testfile after
154				 * truncate(2) on it.
155				 */
156				if (file_length != TRUNC_LEN) {
157					tst_resm(TFAIL, "%s: Incorrect file "
158						 "size %"PRId64", Expected %d",
159						 TESTFILE, (int64_t)file_length,
160						 TRUNC_LEN);
161				} else {
162					tst_resm(TPASS, "Functionality of "
163						 "truncate(%s, %d) successful",
164						 TESTFILE, TRUNC_LEN);
165				}
166			} else {
167				tst_resm(TPASS, "%s call succeeded", TCID);
168			}
169		}
170		Tst_count++;	/* incr TEST_LOOP counter */
171	}			/* End for TEST_LOOPING */
172
173	/* Call cleanup() to undo setup done for the test. */
174	cleanup();
175	 /*NOTREACHED*/ return 0;
176
177}				/* End main */
178
179/*
180 * void
181 * setup() - performs all ONE TIME setup for this test.
182 *  Create a temporary directory and change directory to it.
183 *  Fill the buffer with some arbitrary data to be written to a file.
184 *  Create a test file under temporary directory and close it
185 *  write arbitrary data into testfile.
186 */
187void setup()
188{
189	int fd, i;		/* file handler for testfile */
190	int c, c_total = 0;	/* no. bytes to be written to file */
191	char tst_buff[BUF_SIZE];	/* buffer to hold data */
192
193	/* capture signals */
194	tst_sig(FORK, DEF_HANDLER, cleanup);
195
196	/* Pause if that option was specified
197	 * TEST_PAUSE contains the code to fork the test with the -i option.
198	 * You want to make sure you do this before you create your temporary
199	 * directory.
200	 */
201	TEST_PAUSE;
202
203	/* make a temp directory and cd to it */
204	tst_tmpdir();
205
206	/* Fill the test buffer with the known data */
207	for (i = 0; i < BUF_SIZE; i++) {
208		tst_buff[i] = 'a';
209	}
210
211	/* Creat a testfile under temporary directory */
212	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
213		tst_brkm(TBROK, cleanup,
214			 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
215			 TESTFILE, FILE_MODE, errno, strerror(errno));
216	 /*NOTREACHED*/}
217
218	/* Write to the file 1k data from the buffer */
219	while (c_total < FILE_SIZE) {
220		if ((c = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
221			tst_brkm(TBROK, cleanup,
222				 "write(2) on %s Failed, errno=%d : %s",
223				 TESTFILE, errno, strerror(errno));
224		 /*NOTREACHED*/} else {
225			c_total += c;
226		}
227	}
228
229	/* Close the testfile after writing data into it */
230	if (close(fd) == -1) {
231		tst_brkm(TBROK, cleanup,
232			 "close(%s) Failed, errno=%d : %s",
233			 TESTFILE, errno, strerror(errno));
234	 /*NOTREACHED*/}
235}				/* End setup() */
236
237/*
238 * void
239 * cleanup() - performs all ONE TIME cleanup for this test at
240 *	       completion or premature exit.
241 *  Remove the test directory and testfile created in the setup.
242 */
243void cleanup()
244{
245	/*
246	 * print timing stats if that option was specified.
247	 */
248	TEST_CLEANUP;
249
250	/* Remove tmp dir and all files in it */
251	tst_rmdir();
252
253	/* exit with return code appropriate for results */
254	tst_exit();
255}				/* End cleanup() */
256