blob: a3cdc9a8406cfea9eb6e195933a6a2dfff295081 [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 * 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"
plars865695b2001-08-27 22:15:12 +000057
58char *TCID = "getcwd01";
plars865695b2001-08-27 22:15:12 +000059char buf[100];
60
61void cleanup(void);
62void setup(void);
63void setup_test4(void);
64
65struct test_case_t {
66 char *desc;
subrata_modak56207ce2009-03-23 13:35:39 +000067 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000068 char *buf;
69 int size;
70 int exp_errno;
plars8b7b0802002-06-12 17:40:59 +000071 char *exp_retval;
plars865695b2001-08-27 22:15:12 +000072} testcases[] = {
vapier7ec19d92006-02-27 04:38:56 +000073#ifndef UCLINUX
74 /* Skip since uClinux does not implement memory protection */
subrata_modak56207ce2009-03-23 13:35:39 +000075 {
76 "Test for EFAULT", NULL, (void *)-1, BUFSIZ, EFAULT, NULL},
vapier7ec19d92006-02-27 04:38:56 +000077#endif
subrata_modak56207ce2009-03-23 13:35:39 +000078 {
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}
plars865695b2001-08-27 22:15:12 +000082};
83
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020084int TST_TOTAL = ARRAY_SIZE(testcases);
vapier83b1dda2006-02-27 04:01:36 +000085
plars74948ad2002-11-14 16:16:14 +000086int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000087{
88 int i;
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 */
plars8b7b0802002-06-12 17:40:59 +000091 char *test_erg;
plars865695b2001-08-27 22:15:12 +000092
Garrett Cooper45e285d2010-11-22 12:19:25 -080093 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000095 }
96 setup();
97
plars865695b2001-08-27 22:15:12 +000098 /*
99 * The following loop checks looping state if -i option given
100 */
101 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800102 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 for (i = 0; i < TST_TOTAL; ++i) {
plars865695b2001-08-27 22:15:12 +0000105 tst_resm(TINFO, "%s", testcases[i].desc);
106
107 if (testcases[i].setupfunc != NULL) {
108 testcases[i].setupfunc();
109 }
110
plars8b7b0802002-06-12 17:40:59 +0000111 errno = 0;
112 test_erg = getcwd(testcases[i].buf, testcases[i].size);
113 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000114
plars8b7b0802002-06-12 17:40:59 +0000115 if (test_erg != testcases[i].exp_retval) {
plars865695b2001-08-27 22:15:12 +0000116 tst_resm(TFAIL, "getcwd(2) failed to return"
plars8b7b0802002-06-12 17:40:59 +0000117 "expected value, expected: %p, "
118 "got: %p", testcases[i].exp_retval,
119 test_erg);
plars865695b2001-08-27 22:15:12 +0000120 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_modak56207ce2009-03-23 13:35:39 +0000128 tst_resm(TPASS, "Test case %d PASSED", i + 1);
plars865695b2001-08-27 22:15:12 +0000129 }
130 }
131 cleanup();
132
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800133 tst_exit();
plars865695b2001-08-27 22:15:12 +0000134}
135
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400136void setup_test4(void)
plars865695b2001-08-27 22:15:12 +0000137{
138 chdir("/");
139}
140
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400141void setup(void)
plars865695b2001-08-27 22:15:12 +0000142{
plars865695b2001-08-27 22:15:12 +0000143 tst_sig(NOFORK, DEF_HANDLER, cleanup);
144
plars865695b2001-08-27 22:15:12 +0000145 TEST_PAUSE;
146
147 /* create a test directory and cd into it */
148 tst_tmpdir();
149}
150
Mike Frysinger7f825dd2013-03-11 15:36:25 -0400151void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000152{
153 /* remove the test directory */
154 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700155}