xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getcwd/getcwd01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 EFAULT.
18  * 3) getcwd(2) should return NULL and set errno to ERANGE.
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 #include <errno.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include "tst_test.h"
27 #include "lapi/syscalls.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, EFAULT},
38 	{buffer, 0, ERANGE},
39 	{buffer, 1, ERANGE},
40 	{NULL, 1, ERANGE}
41 };
42 
43 
verify_getcwd(unsigned int n)44 static void verify_getcwd(unsigned int n)
45 {
46 	struct t_case *tc = &tcases[n];
47 
48 	TST_EXP_FAIL2(tst_syscall(__NR_getcwd, tc->buf, tc->size), tc->exp_err);
49 }
50 
51 static struct tst_test test = {
52 	.tcnt = ARRAY_SIZE(tcases),
53 	.test = verify_getcwd
54 };
55