blob: 695e6f15cccaaeaa6396fb5ab9dda2e11933a92c [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
20/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name : symlink04
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description :
24 * Verify that, symlink will succeed to creat a symbolic link of an existing
25 * object name path.
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * Expected Result:
28 * symlink() should return value 0 on success and symbolic link of an
29 * existing object should be created.
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,
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>
nstrazfa31d552002-05-14 16:50:06 +000053 * symlink04 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * 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 * None.
68 *
69 */
70
71#include <stdio.h>
72#include <sys/types.h>
73#include <sys/fcntl.h>
74#include <errno.h>
75#include <string.h>
76#include <signal.h>
77#include <sys/stat.h>
78#include "test.h"
79#include "usctest.h"
80
81#define TESTFILE "testfile"
82#define SYMFILE "slink_file"
83#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
84
subrata_modak56207ce2009-03-23 13:35:39 +000085char *TCID = "symlink04"; /* Test program identifier. */
86int TST_TOTAL = 1; /* Total number of test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +000087int exp_enos[] = { 0 };
plars865695b2001-08-27 22:15:12 +000088
subrata_modak56207ce2009-03-23 13:35:39 +000089void setup(); /* Setup function for the test */
90void cleanup(); /* Cleanup function for the test */
plars865695b2001-08-27 22:15:12 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
94 struct stat stat_buf; /* stat structure buffer */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020095 int lc;
96 char *msg;
plars865695b2001-08-27 22:15:12 +000097
98 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 msg = parse_opts(ac, av, NULL, NULL);
100 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800102
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 }
plars865695b2001-08-27 22:15:12 +0000104
plars865695b2001-08-27 22:15:12 +0000105 setup();
106
107 /* set the expected errnos... */
108 TEST_EXP_ENOS(exp_enos);
109
plars865695b2001-08-27 22:15:12 +0000110 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
Caspar Zhangd59a6592013-03-07 14:59:12 +0800112 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000113
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 /*
115 * Call symlink(2) to create a symlink of
plars865695b2001-08-27 22:15:12 +0000116 * testfile.
117 */
118 TEST(symlink(TESTFILE, SYMFILE));
subrata_modakbdbaec52009-02-26 12:14:51 +0000119
plars865695b2001-08-27 22:15:12 +0000120 if (TEST_RETURN == -1) {
121 TEST_ERROR_LOG(TEST_ERRNO);
122 tst_resm(TFAIL, "symlink(%s, %s) Failed, errno=%d : %s",
123 TESTFILE, SYMFILE, TEST_ERRNO,
124 strerror(TEST_ERRNO));
125 } else {
126 /*
127 * Perform functional verification if test
128 * executed without (-f) option.
129 */
130 if (STD_FUNCTIONAL_TEST) {
131 /*
132 * Get the symlink file status information
133 * using lstat(2).
134 */
135 if (lstat(SYMFILE, &stat_buf) < 0) {
136 tst_brkm(TFAIL, cleanup, "lstat(2) of "
subrata_modak56207ce2009-03-23 13:35:39 +0000137 "%s failed, error:%d", SYMFILE,
138 errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 }
plars865695b2001-08-27 22:15:12 +0000140
141 /* Check if the st_mode contains a link */
142 if (!S_ISLNK(stat_buf.st_mode)) {
143 tst_resm(TFAIL,
144 "symlink of %s doesn't exist",
145 TESTFILE);
146 } else {
147 tst_resm(TPASS, "symlink(%s, %s) "
subrata_modak56207ce2009-03-23 13:35:39 +0000148 "functionality successful",
149 TESTFILE, SYMFILE);
plars865695b2001-08-27 22:15:12 +0000150 }
151 } else {
152 tst_resm(TPASS, "Call succeeded");
153 }
154 }
155
156 /* Unlink the symlink file for next loop */
157 if (unlink(SYMFILE) == -1) {
158 tst_brkm(TBROK, cleanup,
159 "unlink(%s) Failed, errno=%d : %s",
160 SYMFILE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800162 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800163 }
plars865695b2001-08-27 22:15:12 +0000164
plars865695b2001-08-27 22:15:12 +0000165 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800166 tst_exit();
plars865695b2001-08-27 22:15:12 +0000167
Garrett Cooper2c282152010-12-16 00:55:50 -0800168}
plars865695b2001-08-27 22:15:12 +0000169
170/*
171 * void
172 * setup() - performs all ONE TIME setup for this test.
173 * Create a temporary directory and change directory to it.
174 * Create a test file under temporary directory and close it
175 */
subrata_modak56207ce2009-03-23 13:35:39 +0000176void setup()
plars865695b2001-08-27 22:15:12 +0000177{
178 int fd; /* file handle for testfile */
179
plars865695b2001-08-27 22:15:12 +0000180 tst_sig(NOFORK, DEF_HANDLER, cleanup);
181
182 /* Pause if that option was specified
183 * TEST_PAUSE contains the code to fork the test with the -i option.
184 * You want to make sure you do this before you create your temporary
185 * directory.
186 */
187 TEST_PAUSE;
188
plars865695b2001-08-27 22:15:12 +0000189 tst_tmpdir();
190
191 /* creat/open a testfile */
subrata_modak56207ce2009-03-23 13:35:39 +0000192 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000193 tst_brkm(TBROK, cleanup,
194 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
195 TESTFILE, FILE_MODE, errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 }
plars865695b2001-08-27 22:15:12 +0000197
198 /* Close the temporary file created above */
199 if (close(fd) == -1) {
200 tst_resm(TBROK, "close(%s) Failed, errno=%d : %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000201 TESTFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000202 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800203}
plars865695b2001-08-27 22:15:12 +0000204
205/*
206 * void
207 * cleanup() - performs all ONE TIME cleanup for this test at
208 * completion or premature exit.
209 * Remove the test directory and testfile created in the setup.
210 */
subrata_modak56207ce2009-03-23 13:35:39 +0000211void cleanup()
plars865695b2001-08-27 22:15:12 +0000212{
213 /*
214 * print timing stats if that option was specified.
215 * print errno log if that option was specified.
216 */
217 TEST_CLEANUP;
218
plars865695b2001-08-27 22:15:12 +0000219 tst_rmdir();
220
Chris Dearmanec6edca2012-10-17 19:54:01 -0700221}