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 <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 "safe_macros.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);
89int TST_TOTAL = 1;
90
91void setup();			/* setup function for the test */
92void cleanup();			/* cleanup function for the test */
93
94int main(int ac, char **av)
95{
96	struct stat stat_buf;	/* stat(2) struct contents */
97	int lc;
98	off_t file_length;	/* test file length */
99
100	tst_parse_opts(ac, av, NULL, NULL);
101
102	setup();
103
104	for (lc = 0; TEST_LOOPING(lc); lc++) {
105
106		tst_count = 0;
107
108		/*
109		 * Call truncate(2) to truncate a test file to a
110		 * specified length.
111		 */
112		TEST(truncate(TESTFILE, TRUNC_LEN));
113
114		if (TEST_RETURN == -1) {
115			tst_resm(TFAIL,
116				 "truncate(%s, %d) Failed, errno=%d : %s",
117				 TESTFILE, TRUNC_LEN, TEST_ERRNO,
118				 strerror(TEST_ERRNO));
119		} else {
120			/*
121			 * Get the testfile information using
122			 * stat(2).
123			 */
124			if (stat(TESTFILE, &stat_buf) < 0) {
125				tst_brkm(TFAIL, cleanup, "stat(2) of "
126					 "%s failed, error:%d",
127					 TESTFILE, errno);
128			}
129			stat_buf.st_mode &= ~S_IFREG;
130			file_length = stat_buf.st_size;
131
132			/*
133			 * Check for expected size of testfile after
134			 * truncate(2) on it.
135			 */
136			if (file_length != TRUNC_LEN) {
137				tst_resm(TFAIL, "%s: Incorrect file "
138					 "size %" PRId64
139					 ", Expected %d", TESTFILE,
140					 (int64_t) file_length,
141					 TRUNC_LEN);
142			} else {
143				tst_resm(TPASS, "Functionality of "
144					 "truncate(%s, %d) successful",
145					 TESTFILE, TRUNC_LEN);
146			}
147		}
148		tst_count++;	/* incr TEST_LOOP counter */
149	}
150
151	cleanup();
152	tst_exit();
153}
154
155/*
156 * void
157 * setup() - performs all ONE TIME setup for this test.
158 *  Create a temporary directory and change directory to it.
159 *  Fill the buffer with some arbitrary data to be written to a file.
160 *  Create a test file under temporary directory and close it
161 *  write arbitrary data into testfile.
162 */
163void setup(void)
164{
165	int fd, i;		/* file handler for testfile */
166	int c, c_total = 0;	/* no. bytes to be written to file */
167	char tst_buff[BUF_SIZE];	/* buffer to hold data */
168
169	tst_sig(FORK, DEF_HANDLER, cleanup);
170
171	/* Pause if that option was specified
172	 * TEST_PAUSE contains the code to fork the test with the -i option.
173	 * You want to make sure you do this before you create your temporary
174	 * directory.
175	 */
176	TEST_PAUSE;
177
178	tst_tmpdir();
179
180	/* Fill the test buffer with the known data */
181	for (i = 0; i < BUF_SIZE; i++) {
182		tst_buff[i] = 'a';
183	}
184
185	/* Creat a testfile under temporary directory */
186	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
187		tst_brkm(TBROK, cleanup,
188			 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
189			 TESTFILE, FILE_MODE, errno, strerror(errno));
190	}
191
192	/* Write to the file 1k data from the buffer */
193	while (c_total < FILE_SIZE) {
194		if ((c = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
195			tst_brkm(TBROK, cleanup,
196				 "write(2) on %s Failed, errno=%d : %s",
197				 TESTFILE, errno, strerror(errno));
198		} else {
199			c_total += c;
200		}
201	}
202
203	/* Close the testfile after writing data into it */
204	SAFE_CLOSE(cleanup, fd);
205}
206
207/*
208 * void
209 * cleanup() - performs all ONE TIME cleanup for this test at
210 *	       completion or premature exit.
211 *  Remove the test directory and testfile created in the setup.
212 */
213void cleanup(void)
214{
215
216	tst_rmdir();
217
218}
219