1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check the basic functionality of the getcwd(2) system call.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * 1. getcwd(2) works fine if buf and size are valid.
12*49cdfc7eSAndroid Build Coastguard Worker * 2. getcwd(2) works fine if buf points to NULL and size is set to 0.
13*49cdfc7eSAndroid Build Coastguard Worker * 3. getcwd(2) works fine if buf points to NULL and size is greater than strlen(path).
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker static char exp_buf[PATH_MAX];
26*49cdfc7eSAndroid Build Coastguard Worker static char buffer[PATH_MAX];
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static struct t_case {
29*49cdfc7eSAndroid Build Coastguard Worker char *buf;
30*49cdfc7eSAndroid Build Coastguard Worker size_t size;
31*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
32*49cdfc7eSAndroid Build Coastguard Worker {buffer, sizeof(buffer)},
33*49cdfc7eSAndroid Build Coastguard Worker {NULL, 0},
34*49cdfc7eSAndroid Build Coastguard Worker {NULL, PATH_MAX}
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker
dir_exists(const char * dirpath)37*49cdfc7eSAndroid Build Coastguard Worker static int dir_exists(const char *dirpath)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker struct stat sb;
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker if (!stat(dirpath, &sb) && S_ISDIR(sb.st_mode))
42*49cdfc7eSAndroid Build Coastguard Worker return 1;
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker return 0;
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
verify_getcwd(unsigned int n)47*49cdfc7eSAndroid Build Coastguard Worker static void verify_getcwd(unsigned int n)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker struct t_case *tc = &tcases[n];
50*49cdfc7eSAndroid Build Coastguard Worker char *res = NULL;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
53*49cdfc7eSAndroid Build Coastguard Worker res = getcwd(tc->buf, tc->size);
54*49cdfc7eSAndroid Build Coastguard Worker TST_ERR = errno;
55*49cdfc7eSAndroid Build Coastguard Worker if (!res) {
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "getcwd() failed");
57*49cdfc7eSAndroid Build Coastguard Worker goto end;
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker if (strcmp(exp_buf, res)) {
61*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "getcwd() returned unexpected directory: %s, "
62*49cdfc7eSAndroid Build Coastguard Worker "expected: %s", res, exp_buf);
63*49cdfc7eSAndroid Build Coastguard Worker goto end;
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "getcwd() returned expected directory: %s", res);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker end:
69*49cdfc7eSAndroid Build Coastguard Worker if (!tc->buf)
70*49cdfc7eSAndroid Build Coastguard Worker free(res);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
setup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker const char *tmpdir = tst_get_tmpdir_root();
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker if (!dir_exists(tmpdir))
78*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "TMPDIR '%s' doesn't exist", tmpdir);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHDIR(tmpdir);
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker if (!realpath(tmpdir, exp_buf))
83*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "realpath() failed");
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Expected path '%s'", exp_buf);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
89*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
91*49cdfc7eSAndroid Build Coastguard Worker .test = verify_getcwd
92*49cdfc7eSAndroid Build Coastguard Worker };
93