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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | char *TCID = "getcwd01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | void (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | char *buf; |
| 69 | int size; |
| 70 | int exp_errno; |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 71 | char *exp_retval; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | } testcases[] = { |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 73 | #ifndef UCLINUX |
| 74 | /* Skip since uClinux does not implement memory protection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | { |
| 76 | "Test for EFAULT", NULL, (void *)-1, BUFSIZ, EFAULT, NULL}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 77 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 84 | int TST_TOTAL = ARRAY_SIZE(testcases); |
vapier | 83b1dda | 2006-02-27 04:01:36 +0000 | [diff] [blame] | 85 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 86 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | { |
| 88 | int i; |
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 | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 91 | char *test_erg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 93 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | } |
| 96 | setup(); |
| 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | /* |
| 99 | * The following loop checks looping state if -i option given |
| 100 | */ |
| 101 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 102 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | for (i = 0; i < TST_TOTAL; ++i) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_resm(TINFO, "%s", testcases[i].desc); |
| 106 | |
| 107 | if (testcases[i].setupfunc != NULL) { |
| 108 | testcases[i].setupfunc(); |
| 109 | } |
| 110 | |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 111 | errno = 0; |
| 112 | test_erg = getcwd(testcases[i].buf, testcases[i].size); |
| 113 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 115 | if (test_erg != testcases[i].exp_retval) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | tst_resm(TFAIL, "getcwd(2) failed to return" |
plars | 8b7b080 | 2002-06-12 17:40:59 +0000 | [diff] [blame] | 117 | "expected value, expected: %p, " |
| 118 | "got: %p", testcases[i].exp_retval, |
| 119 | test_erg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | if (TEST_ERRNO != testcases[i].exp_errno) { |
| 123 | tst_resm(TFAIL, "getcwd returned unexpected " |
| 124 | "errno, expected: %d, got: %d", |
| 125 | testcases[i].exp_errno, TEST_ERRNO); |
| 126 | continue; |
| 127 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | tst_resm(TPASS, "Test case %d PASSED", i + 1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | cleanup(); |
| 132 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 133 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 136 | void setup_test4(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | { |
| 138 | chdir("/"); |
| 139 | } |
| 140 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 141 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | TEST_PAUSE; |
| 146 | |
| 147 | /* create a test directory and cd into it */ |
| 148 | tst_tmpdir(); |
| 149 | } |
| 150 | |
Mike Frysinger | 7f825dd | 2013-03-11 15:36:25 -0400 | [diff] [blame] | 151 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
| 153 | /* remove the test directory */ |
| 154 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 155 | } |