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 | * 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 | |
| 59 | char *TCID = "getcwd01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | char buf[100]; |
| 61 | |
| 62 | void cleanup(void); |
| 63 | void setup(void); |
| 64 | void setup_test4(void); |
| 65 | |
| 66 | struct test_case_t { |
| 67 | char *desc; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | void (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | char *buf; |
| 70 | int size; |
| 71 | int exp_errno; |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 72 | char *exp_retval; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | } testcases[] = { |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 74 | #ifndef UCLINUX |
| 75 | /* Skip since uClinux does not implement memory protection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | { |
| 77 | "Test for EFAULT", NULL, (void *)-1, BUFSIZ, EFAULT, NULL}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 78 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | { |
| 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} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int exp_enos[] = { EFAULT, ENOMEM, EINVAL, ERANGE, 0 }; |
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 | int TST_TOTAL = sizeof(testcases) / sizeof(*testcases); |
vapier | 83b1dda | 2006-02-27 04:01:36 +0000 | [diff] [blame] | 88 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 89 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | { |
| 91 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int lc; /* loop counter */ |
| 93 | char *msg; /* parse_opts() return message */ |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 94 | char *test_erg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 96 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | for (i = 0; i < TST_TOTAL; ++i) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_resm(TINFO, "%s", testcases[i].desc); |
| 112 | |
| 113 | if (testcases[i].setupfunc != NULL) { |
| 114 | testcases[i].setupfunc(); |
| 115 | } |
| 116 | |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 117 | errno = 0; |
| 118 | test_erg = getcwd(testcases[i].buf, testcases[i].size); |
| 119 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | TEST_ERROR_LOG(TEST_ERRNO); |
| 122 | |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 123 | if (test_erg != testcases[i].exp_retval) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_resm(TFAIL, "getcwd(2) failed to return" |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 125 | "expected value, expected: %p, " |
| 126 | "got: %p", testcases[i].exp_retval, |
| 127 | test_erg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | tst_resm(TPASS, "Test case %d PASSED", i + 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | cleanup(); |
| 140 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 141 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | void setup_test4() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
| 146 | chdir("/"); |
| 147 | } |
| 148 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | TEST_PAUSE; |
| 155 | |
| 156 | /* create a test directory and cd into it */ |
| 157 | tst_tmpdir(); |
| 158 | } |
| 159 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
| 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 Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 171 | } |