summaryrefslogtreecommitdiff
path: root/tools/testing/selftests
diff options
context:
space:
mode:
authorMark Brown <broonie@kernel.org>2026-07-09 16:27:23 +0100
committerMark Brown <broonie@kernel.org>2026-07-09 16:27:23 +0100
commit448b872712a291b885daa7fb7948aa1287ed90a4 (patch)
treed3ae38e1104ad1148ebefeac4e9bd41f6ac7d143 /tools/testing/selftests
parent02f6901f6d723f295d8061d695f597b3b34ea765 (diff)
parenta3b2181459a2c74c03ddbad585f884eefc8ff8ff (diff)
downloadlinux-next-448b872712a291b885daa7fb7948aa1287ed90a4.tar.gz
linux-next-448b872712a291b885daa7fb7948aa1287ed90a4.zip
Merge branch 'for-next' of https://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc.git
Diffstat (limited to 'tools/testing/selftests')
-rw-r--r--tools/testing/selftests/nolibc/nolibc-test.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index c1c1ce43a047..996e8d13508e 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -854,6 +854,58 @@ static int test_dirent(void)
return 0;
}
+int test_getcwd(void)
+{
+ char cwd_syscall[PATH_MAX];
+ char cwd_proc[PATH_MAX];
+ 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 __LINE__;
+
+ /* Terminate the string from readlink() */
+ cwd_proc[len] = '\0';
+
+ /* Get the cwd via syscall */
+ if (getcwd(cwd_syscall, sizeof(cwd_syscall)) == NULL)
+ return __LINE__;
+
+ /* Fail if they aren't the same */
+ if (strcmp(cwd_proc, cwd_syscall) != 0)
+ return __LINE__;
+
+ /* Try getcwd() with NULL for the buffer,
+ * should return NULL and an error in errno.
+ * Other libc's allow this by allocating a buffer
+ * internally.
+ */
+ if (is_nolibc) {
+ errno = 0;
+ if (getcwd(NULL, 0) != NULL || !errno)
+ return __LINE__;
+ }
+
+ /* 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 __LINE__;
+
+ /* 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 __LINE__;
+
+ return 0;
+}
+
int test_getrandom(void)
{
uint64_t rng = 0;
@@ -1555,6 +1607,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;