[PATCH v2 3/3] selftests/nolibc: Add test for getcwd() and readlink()

From: Daniel Palmer

Date: Tue Jun 30 2026 - 09:27:28 EST


Add a test that uses both getcwd() and readlink() so that both
are exercised.

First the happy path is tested by fetching what should be the
same string via getcwd() and readlink() and checking they match.

Then a few different combinations of bad parameters are passed to
getcwd() to make sure it returns NULL and sets errno in those
cases.

Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 48 ++++++++++++++++++++
1 file changed, 48 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index c1c1ce43a047..d54e7188e2ab 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -854,6 +854,53 @@ static int test_dirent(void)
return 0;
}

+static int test_getcwd(void)
+{
+ char cwd_syscall[1024];
+ char cwd_proc[1024];
+ ssize_t len;
+
+ /* Read where the link /proc/self/cwd points */
+ len = readlink("/proc/self/cwd", cwd_proc, sizeof(cwd_proc) - 1);
+ if (len <= 0)
+ return -1;
+
+ /* Terminate the string from readlink() */
+ cwd_proc[len] = '\0';
+
+ /* Get the cwd via syscall */
+ if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL)
+ return -1;
+
+ /* Fail if they aren't the same */
+ if (strcmp(cwd_proc, cwd_syscall) != 0)
+ return -1;
+
+ /* Try getcwd() with NULL for the buffer,
+ * should return NULL and an error in errno.
+ */
+ if (getcwd(NULL, 0) != NULL || !errno)
+ return -1;
+
+ /* Try getcwd() with a buffer but make the size 0,
+ * should return NULL and an error in errno.
+ */
+ errno = 0;
+ if (getcwd(cwd_syscall, 0) != NULL || !errno)
+ return -1;
+
+ /* Try getcwd() with a buffer but make the size 1,
+ * should return NULL and an error in errno because
+ * the string written to the buffer is terminated
+ * so you need at least 2 bytes even for "/".
+ */
+ errno = 0;
+ if (getcwd(cwd_syscall, 1) != NULL || !errno)
+ return -1;
+
+ return 0;
+}
+
int test_getrandom(void)
{
uint64_t rng = 0;
@@ -1555,6 +1602,7 @@ int run_syscall(int min, int max)
CASE_TEST(clock_getres); EXPECT_SYSZR(1, clock_getres(CLOCK_MONOTONIC, &ts)); break;
CASE_TEST(clock_gettime); EXPECT_SYSZR(1, clock_gettime(CLOCK_MONOTONIC, &ts)); break;
CASE_TEST(clock_settime); EXPECT_SYSER(1, clock_settime(CLOCK_MONOTONIC, &ts), -1, EINVAL); break;
+ CASE_TEST(getcwd); EXPECT_SYSZR(proc, test_getcwd()); break;
CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break;
CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break;
CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break;
--
2.53.0