blob: 0fccec977798f77650fbac644c2c0d51dfa9c5a1 [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 * getcwd02
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of the getcwd(2) system call.
26 *
27 * ALGORITHM
28 * Get the path name of the current working directory from the current
29 * shell through a pipe, and compare it with what is returned by
30 * getcwd(2) system call.
31 *
robbiew1b8e0bb2002-03-01 17:22:04 +000032 * Blocks 1-4 are with char[], #4 is special case where address is -1
33 *
34 * Block 1: Call getcwd(2) with valid cwd[]:
35 * Should work fine
36 * Block 2: Call getcwd(2) with valid cwd[], size = 0:
37 * Should return NULL, errno = EINVAL
38 * Block 3: Call getcwd(2) with valid cwd[], size <= strlen(path):
39 * i.e. size = 1, Should return NULL, errno = ERANGE
40 * Block 4: Call getcwd(2) with cwd address = -1, size > strlen(path):
41 * Should return NULL, errno = EFAULT
42 *
43 * Blocks 5-7 are with char*
44 *
45 * Block 5: Call getcwd(2) with *buffer = NULL, size = 0:
46 * Should allocate buffer, and work fine
47 * Block 6: Call getcwd(2) with *buffer = NULL, size <= strlen(path):
48 * i.e. size = 1, Should return NULL, errno = ERANGE
49 * Block 7: Call getcwd(2) with *buffer = NULL, size > strlen(path):
50 * Should work fine and allocate buffer
plars865695b2001-08-27 22:15:12 +000051 *
52 * HISTORY
53 * 07/2001 Ported by Wayne Boyer
robbiew1b8e0bb2002-03-01 17:22:04 +000054 * 02/2002 Added more testcases, cleaned up by wjh
plars865695b2001-08-27 22:15:12 +000055 *
56 * RESTRICTIONS
57 * NONE
58 */
59#include <stdio.h>
60#include <string.h>
61#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080062#include "test.h"
plars865695b2001-08-27 22:15:12 +000063#define FAILED 1
64
65char *pwd = "/bin/pwd";
66int flag;
67char *TCID = "getcwd02";
robbiew1b8e0bb2002-03-01 17:22:04 +000068int TST_TOTAL = 7;
plars865695b2001-08-27 22:15:12 +000069
70void cleanup(void);
71void setup(void);
Mike Frysinger7f825dd2013-03-11 15:36:25 -040072void do_block1(void);
73void do_block2(void);
74void do_block3(void);
75void do_block4(void);
76void do_block5(void);
77void do_block6(void);
78void do_block7(void);
robbiew1b8e0bb2002-03-01 17:22:04 +000079
subrata_modak56207ce2009-03-23 13:35:39 +000080char pwd_buf[BUFSIZ]; //holds results of pwd pipe
81char cwd[BUFSIZ]; //used as our valid buffer
82char *buffer = NULL; //catches the return value from getcwd when passing NULL
83char *cwd_ptr = NULL; //catches the return value from getcwd() when passing cwd[]
plars865695b2001-08-27 22:15:12 +000084
plars74948ad2002-11-14 16:16:14 +000085int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000086{
subrata_modak56207ce2009-03-23 13:35:39 +000087 FILE *fin;
Mike Frysingera3127592013-03-20 17:31:05 -040088 char *cp;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020089 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg; /* parse_opts() return message */
plars865695b2001-08-27 22:15:12 +000091
Garrett Cooper45e285d2010-11-22 12:19:25 -080092 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080093 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +000094 }
95 setup();
plars865695b2001-08-27 22:15:12 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097 /*
98 * The following loop checks looping state if -i option given
99 */
100 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
subrata_modak4bb656a2009-02-26 12:02:09 +0000102
subrata_modak56207ce2009-03-23 13:35:39 +0000103 if ((fin = popen(pwd, "r")) == NULL) {
104 tst_resm(TINFO, "%s: can't run %s", TCID, pwd);
105 tst_brkm(TBROK, cleanup, "%s FAILED", TCID);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 }
subrata_modak56207ce2009-03-23 13:35:39 +0000107 while (fgets(pwd_buf, sizeof(pwd_buf), fin) != NULL) {
Garrett Cooper45e285d2010-11-22 12:19:25 -0800108 if ((cp = strchr(pwd_buf, '\n')) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000109 tst_brkm(TBROK, cleanup, "pwd output too long");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 }
subrata_modak56207ce2009-03-23 13:35:39 +0000111 *cp = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000112 }
113 pclose(fin);
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 do_block1();
116 do_block2();
117 do_block3();
118 do_block4();
119 do_block5();
120 do_block6();
121 do_block7();
122 }
123 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800124 tst_exit();
plars865695b2001-08-27 22:15:12 +0000125}
126
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400127void do_block1(void) //valid cwd[]: -> Should work fine
robbiew1b8e0bb2002-03-01 17:22:04 +0000128{
subrata_modak56207ce2009-03-23 13:35:39 +0000129 int flag = 0;
130 tst_resm(TINFO, "Enter Block 1");
robbiew1b8e0bb2002-03-01 17:22:04 +0000131
subrata_modak56207ce2009-03-23 13:35:39 +0000132 if ((cwd_ptr = getcwd(cwd, sizeof(cwd))) == NULL) {
Mike Frysingera3127592013-03-20 17:31:05 -0400133 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000134 flag = FAILED;
135 }
136 if ((flag != FAILED) && (strcmp(pwd_buf, cwd) != 0)) {
137 tst_resm(TFAIL, "getcwd() returned unexpected working "
138 "directory: expected: %s, got: %s\n", pwd_buf, cwd);
139 flag = FAILED;
140 }
141 tst_resm(TINFO, "Exit Block 1");
142 if (flag == FAILED) {
143 tst_resm(TFAIL, "Block 1 FAILED");
144 } else {
145 tst_resm(TPASS, "Block 1 PASSED");
146 }
robbiew1b8e0bb2002-03-01 17:22:04 +0000147}
148
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400149void do_block2(void) //valid cwd[], size = 0: -> Should return NULL, errno = EINVAL
robbiew1b8e0bb2002-03-01 17:22:04 +0000150{
subrata_modak56207ce2009-03-23 13:35:39 +0000151 int flag = 0;
152 tst_resm(TINFO, "Enter Block 2");
robbiew1b8e0bb2002-03-01 17:22:04 +0000153
subrata_modak56207ce2009-03-23 13:35:39 +0000154 if (((cwd_ptr = getcwd(cwd, 0)) == NULL)
155 && (errno != EINVAL)) {
Mike Frysingera3127592013-03-20 17:31:05 -0400156 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted EINVAL)");
subrata_modak56207ce2009-03-23 13:35:39 +0000157 flag = FAILED;
158 }
159 tst_resm(TINFO, "Exit Block 2");
160 if (flag == FAILED) {
161 tst_resm(TFAIL, "Block 2 FAILED");
162 } else {
163 tst_resm(TPASS, "Block 2 PASSED");
164 }
robbiew1b8e0bb2002-03-01 17:22:04 +0000165}
166
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400167void do_block3(void) //valid cwd[], size = 1 -> Should return NULL, errno = ERANGE
robbiew1b8e0bb2002-03-01 17:22:04 +0000168{
subrata_modak56207ce2009-03-23 13:35:39 +0000169 int flag = 0;
170 tst_resm(TINFO, "Enter Block 3");
robbiew1b8e0bb2002-03-01 17:22:04 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 if (((cwd_ptr = getcwd(cwd, 1)) != NULL)
173 || (errno != ERANGE)) {
Mike Frysingera3127592013-03-20 17:31:05 -0400174 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted ERANGE)");
subrata_modak56207ce2009-03-23 13:35:39 +0000175 flag = FAILED;
176 }
177 tst_resm(TINFO, "Exit Block 3");
178 if (flag == FAILED) {
179 tst_resm(TFAIL, "Block 3 FAILED");
180 } else {
181 tst_resm(TPASS, "Block 3 PASSED");
182 }
robbiew1b8e0bb2002-03-01 17:22:04 +0000183}
184
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400185void do_block4(void) //invalid cwd[] = -1, size = BUFSIZ: -> return NULL, errno = FAULT
robbiew1b8e0bb2002-03-01 17:22:04 +0000186{
vapiere691bd02006-02-27 03:29:59 +0000187/* Skip since uClinux does not implement memory protection */
188#ifndef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000189 int flag = 0;
190 tst_resm(TINFO, "Enter Block 4");
robbiew1b8e0bb2002-03-01 17:22:04 +0000191
subrata_modak56207ce2009-03-23 13:35:39 +0000192 if (((cwd_ptr = getcwd((char *)-1, sizeof(cwd))) != NULL)
193 || (errno != EFAULT)) {
Mike Frysingera3127592013-03-20 17:31:05 -0400194 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted EFAULT)");
subrata_modak56207ce2009-03-23 13:35:39 +0000195 flag = FAILED;
196 }
197 tst_resm(TINFO, "Exit Block 4");
198 if (flag == FAILED) {
199 tst_resm(TFAIL, "Block 4 FAILED");
200 } else {
201 tst_resm(TPASS, "Block 4 PASSED");
202 }
vapiere691bd02006-02-27 03:29:59 +0000203#else
204 tst_resm(TINFO, "Skipping Block 4 on uClinux");
205#endif
robbiew1b8e0bb2002-03-01 17:22:04 +0000206}
207
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400208void do_block5(void) //buffer = NULL, and size = 0, should succeed
robbiew1b8e0bb2002-03-01 17:22:04 +0000209{
subrata_modak56207ce2009-03-23 13:35:39 +0000210 int flag = 0;
211 tst_resm(TINFO, "Enter Block 5");
robbiew1b8e0bb2002-03-01 17:22:04 +0000212
subrata_modak56207ce2009-03-23 13:35:39 +0000213 if ((buffer = getcwd(NULL, 0)) == NULL) {
Mike Frysingera3127592013-03-20 17:31:05 -0400214 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000215 flag = FAILED;
216 }
217 if ((flag != FAILED) && (strcmp(pwd_buf, buffer) != 0)) {
218 tst_resm(TFAIL, "getcwd() returned unexpected working "
219 "directory: expected: %s, got: %s\n", pwd_buf, buffer);
220 flag = FAILED;
221 }
222 tst_resm(TINFO, "Exit Block 5");
223 if (flag == FAILED) {
224 tst_resm(TFAIL, "Block 5 FAILED");
225 } else {
226 tst_resm(TPASS, "Block 5 PASSED");
227 }
228 free(buffer);
229 buffer = NULL;
robbiew1b8e0bb2002-03-01 17:22:04 +0000230}
231
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400232void do_block6(void) //buffer = NULL, size = 1: -> return NULL, errno = ERANGE
robbiew1b8e0bb2002-03-01 17:22:04 +0000233{
subrata_modak56207ce2009-03-23 13:35:39 +0000234 int flag = 0;
235 tst_resm(TINFO, "Enter Block 6");
robbiew1b8e0bb2002-03-01 17:22:04 +0000236
subrata_modak56207ce2009-03-23 13:35:39 +0000237 if (((buffer = getcwd(NULL, 1)) != NULL)
238 || (errno != ERANGE)) {
Mike Frysingera3127592013-03-20 17:31:05 -0400239 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted ERANGE)");
subrata_modak56207ce2009-03-23 13:35:39 +0000240 flag = FAILED;
241 }
242 tst_resm(TINFO, "Exit Block 6");
243 if (flag == FAILED) {
244 tst_resm(TFAIL, "Block 6 FAILED");
245 } else {
246 tst_resm(TPASS, "Block 6 PASSED");
247 }
robbiew1b8e0bb2002-03-01 17:22:04 +0000248}
249
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400250void do_block7(void) //buffer = NULL, size = BUFSIZ: -> work fine, allocate buffer
robbiew1b8e0bb2002-03-01 17:22:04 +0000251{
subrata_modak56207ce2009-03-23 13:35:39 +0000252 int flag = 0;
253 tst_resm(TINFO, "Enter Block 7");
robbiew1b8e0bb2002-03-01 17:22:04 +0000254
subrata_modak56207ce2009-03-23 13:35:39 +0000255 if ((buffer = getcwd(NULL, sizeof(cwd))) == NULL) {
Mike Frysingera3127592013-03-20 17:31:05 -0400256 tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly");
subrata_modak56207ce2009-03-23 13:35:39 +0000257 flag = FAILED;
258 }
259 if ((flag != FAILED) && (strcmp(pwd_buf, buffer) != 0)) {
260 tst_resm(TFAIL, "getcwd() returned unexpected working "
261 "directory: expected: %s, got: %s\n", pwd_buf, buffer);
262 flag = FAILED;
263 }
264 tst_resm(TINFO, "Exit Block 7");
265 if (flag == FAILED) {
266 tst_resm(TFAIL, "Block 7 FAILED");
267 } else {
268 tst_resm(TPASS, "Block 7 PASSED");
269 }
270 free(buffer);
271 buffer = NULL;
robbiew1b8e0bb2002-03-01 17:22:04 +0000272}
273
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400274void setup(void)
plars865695b2001-08-27 22:15:12 +0000275{
plars865695b2001-08-27 22:15:12 +0000276 /* FORK is set here because of the popen() call above */
277 tst_sig(FORK, DEF_HANDLER, cleanup);
278
plars865695b2001-08-27 22:15:12 +0000279 TEST_PAUSE;
280
281 /* create a test directory and cd into it */
282 tst_tmpdir();
283}
284
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400285void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000286{
287 /* remove the test directory */
288 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700289}