plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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 | * |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 32 | * 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | * |
| 52 | * HISTORY |
| 53 | * 07/2001 Ported by Wayne Boyer |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 54 | * 02/2002 Added more testcases, cleaned up by wjh |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | * |
| 56 | * RESTRICTIONS |
| 57 | * NONE |
| 58 | */ |
| 59 | #include <stdio.h> |
| 60 | #include <string.h> |
| 61 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 62 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | #define FAILED 1 |
| 64 | |
| 65 | char *pwd = "/bin/pwd"; |
| 66 | int flag; |
| 67 | char *TCID = "getcwd02"; |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 68 | int TST_TOTAL = 7; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | void cleanup(void); |
| 71 | void setup(void); |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 72 | void do_block1(void); |
| 73 | void do_block2(void); |
| 74 | void do_block3(void); |
| 75 | void do_block4(void); |
| 76 | void do_block5(void); |
| 77 | void do_block6(void); |
| 78 | void do_block7(void); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | char pwd_buf[BUFSIZ]; //holds results of pwd pipe |
| 81 | char cwd[BUFSIZ]; //used as our valid buffer |
| 82 | char *buffer = NULL; //catches the return value from getcwd when passing NULL |
| 83 | char *cwd_ptr = NULL; //catches the return value from getcwd() when passing cwd[] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 85 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | FILE *fin; |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 88 | char *cp; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 89 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 90 | const char *msg; /* parse_opts() return message */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 92 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 93 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | } |
| 95 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | /* |
| 98 | * The following loop checks looping state if -i option given |
| 99 | */ |
| 100 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | tst_count = 0; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | while (fgets(pwd_buf, sizeof(pwd_buf), fin) != NULL) { |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 108 | if ((cp = strchr(pwd_buf, '\n')) == NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | tst_brkm(TBROK, cleanup, "pwd output too long"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | *cp = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | } |
| 113 | pclose(fin); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | do_block1(); |
| 116 | do_block2(); |
| 117 | do_block3(); |
| 118 | do_block4(); |
| 119 | do_block5(); |
| 120 | do_block6(); |
| 121 | do_block7(); |
| 122 | } |
| 123 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 124 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 127 | void do_block1(void) //valid cwd[]: -> Should work fine |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 128 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | int flag = 0; |
| 130 | tst_resm(TINFO, "Enter Block 1"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 131 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | if ((cwd_ptr = getcwd(cwd, sizeof(cwd))) == NULL) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 133 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | 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 | } |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 149 | void do_block2(void) //valid cwd[], size = 0: -> Should return NULL, errno = EINVAL |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 150 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | int flag = 0; |
| 152 | tst_resm(TINFO, "Enter Block 2"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | if (((cwd_ptr = getcwd(cwd, 0)) == NULL) |
| 155 | && (errno != EINVAL)) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 156 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted EINVAL)"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | 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 | } |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 167 | void do_block3(void) //valid cwd[], size = 1 -> Should return NULL, errno = ERANGE |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 168 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | int flag = 0; |
| 170 | tst_resm(TINFO, "Enter Block 3"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 171 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 172 | if (((cwd_ptr = getcwd(cwd, 1)) != NULL) |
| 173 | || (errno != ERANGE)) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 174 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted ERANGE)"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | 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 | } |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 185 | void do_block4(void) //invalid cwd[] = -1, size = BUFSIZ: -> return NULL, errno = FAULT |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 186 | { |
vapier | e691bd0 | 2006-02-27 03:29:59 +0000 | [diff] [blame] | 187 | /* Skip since uClinux does not implement memory protection */ |
| 188 | #ifndef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | int flag = 0; |
| 190 | tst_resm(TINFO, "Enter Block 4"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 191 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | if (((cwd_ptr = getcwd((char *)-1, sizeof(cwd))) != NULL) |
| 193 | || (errno != EFAULT)) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 194 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted EFAULT)"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | 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 | } |
vapier | e691bd0 | 2006-02-27 03:29:59 +0000 | [diff] [blame] | 203 | #else |
| 204 | tst_resm(TINFO, "Skipping Block 4 on uClinux"); |
| 205 | #endif |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 208 | void do_block5(void) //buffer = NULL, and size = 0, should succeed |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 209 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | int flag = 0; |
| 211 | tst_resm(TINFO, "Enter Block 5"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 212 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | if ((buffer = getcwd(NULL, 0)) == NULL) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 214 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | 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; |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 232 | void do_block6(void) //buffer = NULL, size = 1: -> return NULL, errno = ERANGE |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 233 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | int flag = 0; |
| 235 | tst_resm(TINFO, "Enter Block 6"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 236 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | if (((buffer = getcwd(NULL, 1)) != NULL) |
| 238 | || (errno != ERANGE)) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 239 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly (wanted ERANGE)"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | 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 | } |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 250 | void do_block7(void) //buffer = NULL, size = BUFSIZ: -> work fine, allocate buffer |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 251 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | int flag = 0; |
| 253 | tst_resm(TINFO, "Enter Block 7"); |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 254 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 255 | if ((buffer = getcwd(NULL, sizeof(cwd))) == NULL) { |
Mike Frysinger | a312759 | 2013-03-20 17:31:05 -0400 | [diff] [blame] | 256 | tst_resm(TFAIL|TERRNO, "getcwd() failed unexpectedly"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | 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; |
robbiew | 1b8e0bb | 2002-03-01 17:22:04 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 274 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 275 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 276 | /* FORK is set here because of the popen() call above */ |
| 277 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 278 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 279 | TEST_PAUSE; |
| 280 | |
| 281 | /* create a test directory and cd into it */ |
| 282 | tst_tmpdir(); |
| 283 | } |
| 284 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 285 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | { |
| 287 | /* remove the test directory */ |
| 288 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 289 | } |