blob: 190997af162e153ac89ddb388575b6d9bab13e06 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
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
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * 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:
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * truncate(2) should return a value 0 and the length of the file after
plars865695b2001-08-27 22:15:12 +000029 * truncation should be equal to the length it is truncated to.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * 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>
subrata_modak923b23f2009-11-02 13:57:16 +000077#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000078
79#include "test.h"
plars865695b2001-08-27 22:15:12 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081#define TESTFILE "testfile" /* file under test */
plars865695b2001-08-27 22:15:12 +000082#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
subrata_modak56207ce2009-03-23 13:35:39 +000083#define BUF_SIZE 256 /* buffer size */
84#define FILE_SIZE 1024 /* test file size */
85#define TRUNC_LEN 256 /* truncation length */
plars865695b2001-08-27 22:15:12 +000086
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020087TCID_DEFINE(truncate01);
Cyril Hrubis605fa332015-02-04 13:11:20 +010088int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000089
90void setup(); /* setup function for the test */
91void cleanup(); /* cleanup function for the test */
92
subrata_modak56207ce2009-03-23 13:35:39 +000093int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000094{
95 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020096 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020097 const char *msg;
plars865695b2001-08-27 22:15:12 +000098 off_t file_length; /* test file length */
subrata_modak56207ce2009-03-23 13:35:39 +000099
Garrett Cooper45e285d2010-11-22 12:19:25 -0800100 msg = parse_opts(ac, av, NULL, NULL);
101 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
plars865695b2001-08-27 22:15:12 +0000104 }
105
plars865695b2001-08-27 22:15:12 +0000106 setup();
107
plars865695b2001-08-27 22:15:12 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
Caspar Zhangd59a6592013-03-07 14:59:12 +0800110 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000111
subrata_modak4bb656a2009-02-26 12:02:09 +0000112 /*
113 * Call truncate(2) to truncate a test file to a
plars865695b2001-08-27 22:15:12 +0000114 * specified length.
115 */
116 TEST(truncate(TESTFILE, TRUNC_LEN));
117
plars865695b2001-08-27 22:15:12 +0000118 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +0000119 tst_resm(TFAIL,
120 "truncate(%s, %d) Failed, errno=%d : %s",
121 TESTFILE, TRUNC_LEN, TEST_ERRNO,
122 strerror(TEST_ERRNO));
123 } else {
124 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200125 * Get the testfile information using
126 * stat(2).
plars865695b2001-08-27 22:15:12 +0000127 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 if (stat(TESTFILE, &stat_buf) < 0) {
129 tst_brkm(TFAIL, cleanup, "stat(2) of "
130 "%s failed, error:%d",
131 TESTFILE, errno);
132 }
133 stat_buf.st_mode &= ~S_IFREG;
134 file_length = stat_buf.st_size;
plars865695b2001-08-27 22:15:12 +0000135
Cyril Hrubise38b9612014-06-02 17:20:57 +0200136 /*
137 * Check for expected size of testfile after
138 * truncate(2) on it.
139 */
140 if (file_length != TRUNC_LEN) {
141 tst_resm(TFAIL, "%s: Incorrect file "
142 "size %" PRId64
143 ", Expected %d", TESTFILE,
144 (int64_t) file_length,
145 TRUNC_LEN);
plars865695b2001-08-27 22:15:12 +0000146 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200147 tst_resm(TPASS, "Functionality of "
148 "truncate(%s, %d) successful",
149 TESTFILE, TRUNC_LEN);
plars865695b2001-08-27 22:15:12 +0000150 }
151 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800152 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800153 }
plars865695b2001-08-27 22:15:12 +0000154
plars865695b2001-08-27 22:15:12 +0000155 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800156 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800157}
plars865695b2001-08-27 22:15:12 +0000158
159/*
160 * void
161 * setup() - performs all ONE TIME setup for this test.
162 * Create a temporary directory and change directory to it.
163 * Fill the buffer with some arbitrary data to be written to a file.
164 * Create a test file under temporary directory and close it
165 * write arbitrary data into testfile.
166 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167void setup(void)
plars865695b2001-08-27 22:15:12 +0000168{
subrata_modak56207ce2009-03-23 13:35:39 +0000169 int fd, i; /* file handler for testfile */
170 int c, c_total = 0; /* no. bytes to be written to file */
plars865695b2001-08-27 22:15:12 +0000171 char tst_buff[BUF_SIZE]; /* buffer to hold data */
172
plars865695b2001-08-27 22:15:12 +0000173 tst_sig(FORK, DEF_HANDLER, cleanup);
174
plars865695b2001-08-27 22:15:12 +0000175 /* Pause if that option was specified
176 * TEST_PAUSE contains the code to fork the test with the -i option.
177 * You want to make sure you do this before you create your temporary
178 * directory.
179 */
180 TEST_PAUSE;
181
plars865695b2001-08-27 22:15:12 +0000182 tst_tmpdir();
183
184 /* Fill the test buffer with the known data */
185 for (i = 0; i < BUF_SIZE; i++) {
186 tst_buff[i] = 'a';
187 }
188
189 /* Creat a testfile under temporary directory */
subrata_modak56207ce2009-03-23 13:35:39 +0000190 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000191 tst_brkm(TBROK, cleanup,
192 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
193 TESTFILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 }
plars865695b2001-08-27 22:15:12 +0000195
196 /* Write to the file 1k data from the buffer */
197 while (c_total < FILE_SIZE) {
198 if ((c = write(fd, tst_buff, sizeof(tst_buff))) <= 0) {
199 tst_brkm(TBROK, cleanup,
200 "write(2) on %s Failed, errno=%d : %s",
201 TESTFILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 } else {
plars865695b2001-08-27 22:15:12 +0000203 c_total += c;
204 }
205 }
206
207 /* Close the testfile after writing data into it */
208 if (close(fd) == -1) {
209 tst_brkm(TBROK, cleanup,
210 "close(%s) Failed, errno=%d : %s",
211 TESTFILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800212 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800213}
plars865695b2001-08-27 22:15:12 +0000214
215/*
216 * void
217 * cleanup() - performs all ONE TIME cleanup for this test at
218 * completion or premature exit.
219 * Remove the test directory and testfile created in the setup.
220 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400221void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000222{
plars865695b2001-08-27 22:15:12 +0000223
plars865695b2001-08-27 22:15:12 +0000224 tst_rmdir();
225
Chris Dearmanec6edca2012-10-17 19:54:01 -0700226}