blob: 137306fb1c245cf03a9b6e387e7503ee173b6a46 [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/*
21 * NAME
22 * getcwd01
23 *
24 * DESCRIPTION
25 * Testcase to test that getcwd(2) sets errno correctly.
26 *
27 * ALGORITHM
28 * Test 1: Call getcwd(2) with a buf pointing outside the address space of
29 * process, and a valid size, and expect EFAULT to be
30 * set in errno.
31 * Test 2: Call getcwd(2) with buf = NULL, size = -1, and expect ENOMEM
32 * to be set in errno.
33 * Test 3: Call getcwd(2) with a valid buf, and size = 0, and expect
34 * EINVAL to be set in errno.
35 * Test 4: Call getcwd(2) on the root directory, and set size to 1, expect
36 * ERANGE to be set in errno.
37 *
38 * USAGE: <for command-line>
39 * getcwd01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
40 * where, -c n : Run n copies concurrently.
41 * -e : Turn on errno logging.
42 * -i n : Execute test n times.
43 * -I x : Execute test for x seconds.
44 * -P x : Pause for x seconds between iterations.
45 * -t : Turn on syscall timing.
46 *
47 * HISTORY
48 * 07/2001 Ported by Wayne Boyer
49 *
50 * RESTRICTIONS
51 * NONE
52 */
53#include <stdio.h>
54#include <string.h>
55#include <errno.h>
56#include "test.h"
57#include "usctest.h"
58
59char *TCID = "getcwd01";
plars865695b2001-08-27 22:15:12 +000060char buf[100];
61
62void cleanup(void);
63void setup(void);
64void setup_test4(void);
65
66struct test_case_t {
67 char *desc;
subrata_modak56207ce2009-03-23 13:35:39 +000068 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000069 char *buf;
70 int size;
71 int exp_errno;
plars8b7b0802002-06-12 17:40:59 +000072 char *exp_retval;
plars865695b2001-08-27 22:15:12 +000073} testcases[] = {
vapier7ec19d92006-02-27 04:38:56 +000074#ifndef UCLINUX
75 /* Skip since uClinux does not implement memory protection */
subrata_modak56207ce2009-03-23 13:35:39 +000076 {
77 "Test for EFAULT", NULL, (void *)-1, BUFSIZ, EFAULT, NULL},
vapier7ec19d92006-02-27 04:38:56 +000078#endif
subrata_modak56207ce2009-03-23 13:35:39 +000079 {
80 "Test for ENOMEM", NULL, NULL, -1, ENOMEM, NULL}, {
81 "Test for EINVAL", NULL, buf, 0, EINVAL, NULL}, {
82 "Test for ERANGE", (void *)setup_test4, buf, 1, ERANGE, NULL}
plars865695b2001-08-27 22:15:12 +000083};
84
subrata_modak56207ce2009-03-23 13:35:39 +000085int exp_enos[] = { EFAULT, ENOMEM, EINVAL, ERANGE, 0 };
plars865695b2001-08-27 22:15:12 +000086
subrata_modak56207ce2009-03-23 13:35:39 +000087int TST_TOTAL = sizeof(testcases) / sizeof(*testcases);
vapier83b1dda2006-02-27 04:01:36 +000088
plars74948ad2002-11-14 16:16:14 +000089int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000090{
91 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000092 int lc; /* loop counter */
93 char *msg; /* parse_opts() return message */
plars8b7b0802002-06-12 17:40:59 +000094 char *test_erg;
plars865695b2001-08-27 22:15:12 +000095
Garrett Cooper45e285d2010-11-22 12:19:25 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000098 }
99 setup();
100
101 /* set up expected error numbers */
102 TEST_EXP_ENOS(exp_enos);
103
104 /*
105 * The following loop checks looping state if -i option given
106 */
107 for (lc = 0; TEST_LOOPING(lc); lc++) {
108 Tst_count = 0;
109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 for (i = 0; i < TST_TOTAL; ++i) {
plars865695b2001-08-27 22:15:12 +0000111 tst_resm(TINFO, "%s", testcases[i].desc);
112
113 if (testcases[i].setupfunc != NULL) {
114 testcases[i].setupfunc();
115 }
116
plars8b7b0802002-06-12 17:40:59 +0000117 errno = 0;
118 test_erg = getcwd(testcases[i].buf, testcases[i].size);
119 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000120
121 TEST_ERROR_LOG(TEST_ERRNO);
122
plars8b7b0802002-06-12 17:40:59 +0000123 if (test_erg != testcases[i].exp_retval) {
plars865695b2001-08-27 22:15:12 +0000124 tst_resm(TFAIL, "getcwd(2) failed to return"
plars8b7b0802002-06-12 17:40:59 +0000125 "expected value, expected: %p, "
126 "got: %p", testcases[i].exp_retval,
127 test_erg);
plars865695b2001-08-27 22:15:12 +0000128 continue;
129 }
130 if (TEST_ERRNO != testcases[i].exp_errno) {
131 tst_resm(TFAIL, "getcwd returned unexpected "
132 "errno, expected: %d, got: %d",
133 testcases[i].exp_errno, TEST_ERRNO);
134 continue;
135 }
subrata_modak56207ce2009-03-23 13:35:39 +0000136 tst_resm(TPASS, "Test case %d PASSED", i + 1);
plars865695b2001-08-27 22:15:12 +0000137 }
138 }
139 cleanup();
140
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800141 tst_exit();
plars865695b2001-08-27 22:15:12 +0000142}
143
subrata_modak56207ce2009-03-23 13:35:39 +0000144void setup_test4()
plars865695b2001-08-27 22:15:12 +0000145{
146 chdir("/");
147}
148
subrata_modak56207ce2009-03-23 13:35:39 +0000149void setup()
plars865695b2001-08-27 22:15:12 +0000150{
Garrett Cooper2c282152010-12-16 00:55:50 -0800151
plars865695b2001-08-27 22:15:12 +0000152 tst_sig(NOFORK, DEF_HANDLER, cleanup);
153
plars865695b2001-08-27 22:15:12 +0000154 TEST_PAUSE;
155
156 /* create a test directory and cd into it */
157 tst_tmpdir();
158}
159
subrata_modak56207ce2009-03-23 13:35:39 +0000160void cleanup()
plars865695b2001-08-27 22:15:12 +0000161{
162 /* remove the test directory */
163 tst_rmdir();
164
165 /*
166 * print timing stats if that option was specified.
167 * print errno log if that option was specified.
168 */
169 TEST_CLEANUP;
170
Chris Dearmanec6edca2012-10-17 19:54:01 -0700171}