blob: 49033c0182492089b29b26bd97fcc17e6e620c9f [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * Test Name: lseek04
22 *
23 * Test Description:
24 * Verify that, lseek() call succeeds to set the file pointer position
25 * to the current specified location, when 'whence' value is set to
26 * SEEK_CUR and the data read from the specified location should match
27 * the expected data.
28 *
29 * Expected Result:
30 * lseek() should return the specified offset from the beginning of the file
31 * measured in bytes. The data read from this location should match the
32 * expected data.
33 *
34 * Algorithm:
35 * Setup:
36 * Setup signal handling.
37 * Create temporary directory.
38 * Pause for SIGUSR1 if option specified.
39 *
40 * Test:
41 * Loop if the proper options are given.
42 * Execute system call
43 * Check return code, if system call failed (return=-1)
44 * Log the errno and Issue a FAIL message.
45 * Otherwise,
46 * Verify the Functionality of system call
47 * if successful,
48 * Issue Functionality-Pass message.
49 * Otherwise,
50 * Issue Functionality-Fail message.
51 * Cleanup:
52 * Print errno log and/or timing stats if options given
53 * Delete the temporary directory created.
54 *
55 * Usage: <for command-line>
56 * lseek04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
57 * where, -c n : Run n copies concurrently.
58 * -f : Turn off functionality Testing.
59 * -i n : Execute test n times.
60 * -I x : Execute test for x seconds.
61 * -P x : Pause for x seconds between iterations.
62 * -t : Turn on syscall timing.
63 *
64 * HISTORY
65 * 07/2001 Ported by Wayne Boyer
66 *
67 * RESTRICTIONS:
68 * None.
69 */
70
71#include <stdio.h>
72#include <unistd.h>
73#include <sys/types.h>
74#include <errno.h>
75#include <fcntl.h>
76#include <utime.h>
77#include <string.h>
78#include <sys/stat.h>
79#include <signal.h>
80
81#include "test.h"
82#include "usctest.h"
83
84#define TEMP_FILE "tmp_file"
85#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
86
87char *TCID="lseek04"; /* Test program identifier. */
88int TST_TOTAL=1; /* Total number of test cases. */
89extern int Tst_count; /* Test Case counter for tst_* routines */
90int fildes; /* file handle for temp file */
91size_t file_size; /* total size of file after data write */
92
93void setup(); /* Main setup function of test */
94void cleanup(); /* cleanup function for the test */
95
96int
97main(int ac, char **av)
98{
99 int lc; /* loop counter */
100 int rval;
101 char *msg; /* message returned from parse_opts */
102 char read_buf[BUFSIZ]; /* data read from temp. file */
103
104 /* Parse standard options given to run the test. */
105 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
106 if (msg != (char *) NULL) {
107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
108 tst_exit();
109 }
110
111 /* Perform global setup for test */
112 setup();
113
114 /* Check looping state if -i option given */
115 for (lc = 0; TEST_LOOPING(lc); lc++) {
116 /* Reset Tst_count in case we are looping. */
117 Tst_count=0;
118
119 /*
120 * Invoke lseek(2) to set the file
121 * pointer/handle from the current location
122 * of the file descriptor + specified offset.
123 */
124 TEST(lseek(fildes, 1, SEEK_CUR));
125
126 /* check return code of lseek(2) */
127 if (TEST_RETURN == (off_t)-1) {
128 tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s",
129 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
130 continue;
131 }
132 /*
133 * Perform functional verification if test
134 * executed without (-f) option.
135 */
136 if (STD_FUNCTIONAL_TEST) {
137 /*
138 * Check if the return value from lseek(2) is equal
139 * to the 3 positions from the beginning of the file.
140 * ie, 2 positions from lseek() in the setup +
141 * 1 position from above above.
142 */
143 if (TEST_RETURN != 3) {
144 tst_resm(TFAIL, "lseek() returned incorrect "
145 "value %d, expected 4", TEST_RETURN);
146 continue;
147 }
148 /*
149 * Read the data byte from this location.
150 */
151 read_buf[0] = '\0';
152 if (read(fildes, &read_buf, (file_size - 3)) < 0) {
153 tst_brkm(TFAIL, cleanup,
154 "read() failed on %s, error=%d",
155 TEMP_FILE, errno);
156 } else {
157 /*
158 * Check if read data contains
159 * expected characters
160 * From pos 4 ---> 'defg'.
161 */
162 if (strcmp(read_buf, "defg")) {
163 tst_resm(TFAIL, "Incorrect data read "
164 "from file %s", TEMP_FILE);
165 } else {
166 tst_resm(TPASS, "Functionality of "
167 "lseek() on %s successful",
168 TEMP_FILE);
169 }
170 }
171 } else {
172 tst_resm(TPASS, "call succeeded");
173 }
174
175 /* reset file pointer in case we are looping */
176 if (lseek(fildes, 2, SEEK_SET) == -1) {
177 tst_brkm(TBROK, cleanup, "lseek failed - could not "
178 "reset file pointer");
179 }
180 } /* End for TEST_LOOPING */
181
182 /* Call cleanup() to undo setup done for the test. */
183 cleanup();
184
185 /*NOTREACHED*/
186} /* End main */
187
188/*
189 * setup() - performs all ONE TIME setup for this test.
190 * Create a temporary directory and change directory to it.
191 * Create a test file under temporary directory and write some
192 * data into it.
193 * Get the size of the file using fstat().
194 */
195void
196setup()
197{
198 struct stat stat_buf; /* buffer to hold stat info. */
199 char write_buf[BUFSIZ]; /* buffer to hold data */
200
201 /* capture signals */
202 tst_sig(NOFORK, DEF_HANDLER, cleanup);
203
204 /* Pause if that option was specified */
205 TEST_PAUSE;
206
207 /* make a temp directory and cd to it */
208 tst_tmpdir();
209
210 /* Get the data to be written to temporary file */
211 strcpy(write_buf, "abcdefg");
212
213 /* Creat/open a temporary file under above directory */
214 if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
215 tst_brkm(TBROK, cleanup,
216 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s",
217 TEMP_FILE, FILE_MODE, errno, strerror(errno));
218 }
219
220 /* Write data into temporary file */
221 if(write(fildes, write_buf, strlen(write_buf)) <= 0) {
222 tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s",
223 TEMP_FILE, errno, strerror(errno));
224 }
225
226 /* Get the temporary file information */
227 if (fstat(fildes, &stat_buf) < 0) {
228 tst_brkm(TBROK, cleanup, "fstat(2) on %s Failed, errno=%d : %s",
229 TEMP_FILE, errno, strerror(errno));
230 }
231
232 file_size = stat_buf.st_size;
233
234 /*
235 * Reset the file pointer position to the specified offset
236 * from the beginning of the file.
237 */
238 if (lseek(fildes, 2, SEEK_SET) != 2) {
239 tst_brkm(TBROK, cleanup,
240 "lseek() fails to set file ptr. to specified offset");
241 }
242}
243
244/*
245 * cleanup() - performs all ONE TIME cleanup for this test at
246 * completion or premature exit.
247 * Remove the test directory and testfile created in the setup.
248 */
249void
250cleanup()
251{
252 /*
253 * print timing stats if that option was specified.
254 * print errno log if that option was specified.
255 */
256 TEST_CLEANUP;
257
258 /* Close the temporary file created */
259 if (close(fildes) < 0) {
260 tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:",
261 TEMP_FILE, errno, strerror(errno));
262 }
263
264 /* Remove tmp dir and all files in it */
265 tst_rmdir();
266
267 /* exit with return code appropriate for results */
268 tst_exit();
269}