• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  * Testcase to test that getcwd(2) sets errno correctly.
9  * 1) getcwd(2) fails if buf points to a bad address.
10  * 2) getcwd(2) fails if the size is invalid.
11  * 3) getcwd(2) fails if the size is set to 0.
12  * 4) getcwd(2) fails if the size is set to 1.
13  * 5) getcwd(2) fails if buf points to NULL and the size is set to 1.
14  *
15  * Expected Result:
16  * 1) getcwd(2) should return NULL and set errno to EFAULT.
17  * 2) getcwd(2) should return NULL and set errno to ENOMEM.
18  * 3) getcwd(2) should return NULL and set errno to EINVAL.
19  * 4) getcwd(2) should return NULL and set errno to ERANGE.
20  * 5) getcwd(2) should return NULL and set errno to ERANGE.
21  *
22  */
23 
24 #include <errno.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include "tst_test.h"
28 
29 static char buffer[5];
30 
31 static struct t_case {
32 	char *buf;
33 	size_t size;
34 	int exp_err;
35 } tcases[] = {
36 	{(void *)-1, PATH_MAX, EFAULT},
37 	{NULL, (size_t)-1, ENOMEM},
38 	{buffer, 0, EINVAL},
39 	{buffer, 1, ERANGE},
40 	{NULL, 1, ERANGE}
41 };
42 
verify_getcwd(unsigned int n)43 static void verify_getcwd(unsigned int n)
44 {
45 	struct t_case *tc = &tcases[n];
46 	char *res;
47 
48 	errno = 0;
49 	res = getcwd(tc->buf, tc->size);
50 	TST_ERR = errno;
51 	if (res) {
52 		tst_res(TFAIL, "getcwd() succeeded unexpectedly");
53 		return;
54 	}
55 
56 	if (TST_ERR != tc->exp_err) {
57 		tst_res(TFAIL | TTERRNO, "getcwd() failed unexpectedly, expected %s",
58 			tst_strerrno(tc->exp_err));
59 		return;
60 	}
61 
62 	tst_res(TPASS | TTERRNO, "getcwd() failed as expected");
63 }
64 
65 static struct tst_test test = {
66 	.tcnt = ARRAY_SIZE(tcases),
67 	.test = verify_getcwd
68 };
69