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 * 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
58 char *TCID = "getcwd01";
59 char buf[100];
60
61 void cleanup(void);
62 void setup(void);
63 void setup_test4(void);
64
65 struct test_case_t {
66 char *desc;
67 void (*setupfunc) ();
68 char *buf;
69 int size;
70 int exp_errno;
71 char *exp_retval;
72 } testcases[] = {
73 #ifndef UCLINUX
74 /* Skip since uClinux does not implement memory protection */
75 {
76 "Test for EFAULT", NULL, (void *)-1, BUFSIZ, EFAULT, NULL},
77 #endif
78 {
79 "Test for ENOMEM", NULL, NULL, -1, ENOMEM, NULL}, {
80 "Test for EINVAL", NULL, buf, 0, EINVAL, NULL}, {
81 "Test for ERANGE", (void *)setup_test4, buf, 1, ERANGE, NULL}
82 };
83
84 int TST_TOTAL = ARRAY_SIZE(testcases);
85
main(int ac,char ** av)86 int main(int ac, char **av)
87 {
88 int i;
89 int lc;
90 char *test_erg;
91
92 tst_parse_opts(ac, av, NULL, NULL);
93 setup();
94
95 /*
96 * The following loop checks looping state if -i option given
97 */
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99 tst_count = 0;
100
101 for (i = 0; i < TST_TOTAL; ++i) {
102 tst_resm(TINFO, "%s", testcases[i].desc);
103
104 if (testcases[i].setupfunc != NULL) {
105 testcases[i].setupfunc();
106 }
107
108 errno = 0;
109 test_erg = getcwd(testcases[i].buf, testcases[i].size);
110 TEST_ERRNO = errno;
111
112 if (test_erg != testcases[i].exp_retval) {
113 tst_resm(TFAIL, "getcwd(2) failed to return"
114 "expected value, expected: %p, "
115 "got: %p", testcases[i].exp_retval,
116 test_erg);
117 continue;
118 }
119 if (TEST_ERRNO != testcases[i].exp_errno) {
120 tst_resm(TFAIL, "getcwd returned unexpected "
121 "errno, expected: %d, got: %d",
122 testcases[i].exp_errno, TEST_ERRNO);
123 continue;
124 }
125 tst_resm(TPASS, "Test case %d PASSED", i + 1);
126 }
127 }
128 cleanup();
129
130 tst_exit();
131 }
132
setup_test4(void)133 void setup_test4(void)
134 {
135 chdir("/");
136 }
137
setup(void)138 void setup(void)
139 {
140 tst_sig(NOFORK, DEF_HANDLER, cleanup);
141
142 TEST_PAUSE;
143
144 /* create a test directory and cd into it */
145 tst_tmpdir();
146 }
147
cleanup(void)148 void cleanup(void)
149 {
150 /* remove the test directory */
151 tst_rmdir();
152 }
153