1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.
16 */
17
18 /*
19 * DESCRIPTION
20 * Testcase to test that getcwd(2) sets errno correctly.
21 * 1) getcwd(2) fails if buf points to a bad address.
22 * 2) getcwd(2) fails if the size is invalid.
23 * 3) getcwd(2) fails if the size is set to 0.
24 * 4) getcwd(2) fails if the size is set to 1.
25 * 5) getcwd(2) fails if buf points to NULL and the size is set to 1.
26 *
27 * Expected Result:
28 * 1) getcwd(2) should return NULL and set errno to EFAULT.
29 * 2) getcwd(2) should return NULL and set errno to ENOMEM.
30 * 3) getcwd(2) should return NULL and set errno to EINVAL.
31 * 4) getcwd(2) should return NULL and set errno to ERANGE.
32 * 5) getcwd(2) should return NULL and set errno to ERANGE.
33 *
34 */
35
36 #include <errno.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include "tst_test.h"
40
41 static char buffer[5];
42
43 static struct t_case {
44 char *buf;
45 size_t size;
46 int exp_err;
47 } tcases[] = {
48 {(void *)-1, PATH_MAX, EFAULT},
49 {NULL, (size_t)-1, ENOMEM},
50 {buffer, 0, EINVAL},
51 {buffer, 1, ERANGE},
52 {NULL, 1, ERANGE}
53 };
54
verify_getcwd(unsigned int n)55 static void verify_getcwd(unsigned int n)
56 {
57 struct t_case *tc = &tcases[n];
58 char *res;
59
60 errno = 0;
61 res = getcwd(tc->buf, tc->size);
62 TEST_ERRNO = errno;
63 if (res) {
64 tst_res(TFAIL, "getcwd() succeeded unexpectedly");
65 return;
66 }
67
68 if (TEST_ERRNO != tc->exp_err) {
69 tst_res(TFAIL | TTERRNO, "getcwd() failed unexpectedly, expected %s",
70 tst_strerrno(tc->exp_err));
71 return;
72 }
73
74 tst_res(TPASS | TTERRNO, "getcwd() failed as expected");
75 }
76
setup(void)77 static void setup(void)
78 {
79 SAFE_CHDIR("/tmp");
80 }
81
82 static struct tst_test test = {
83 .tid = "getcwd01",
84 .setup = setup,
85 .tcnt = ARRAY_SIZE(tcases),
86 .test = verify_getcwd
87 };
88