diff mbox series

[pseudo,04/20] test-realpath: Verify the realpath behavior matches glibc

Message ID 1768520616-7289-5-git-send-email-mark.hatle@kernel.crashing.org
State New
Headers show
Series Consolidated pseudo patches | expand

Commit Message

Mark Hatle Jan. 15, 2026, 11:43 p.m. UTC
Gauthier HADERER reported that the realpath implementation could return
the wrong value.

Create a test-case to attempt to verify realpath behavior.

Signed-off-by: Mark Hatle <mark.hatle@kernel.crashing.org>
---
 test/test-realpath.c  | 17 ++++++++++++
 test/test-realpath.sh | 63 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 test/test-realpath.c
 create mode 100755 test/test-realpath.sh
diff mbox series

Patch

diff --git a/test/test-realpath.c b/test/test-realpath.c
new file mode 100644
index 0000000..0af92cf
--- /dev/null
+++ b/test/test-realpath.c
@@ -0,0 +1,17 @@ 
+/* Code contributed by Gauthier HADERER via lists.yoctoproject.org */
+
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+int main(int argc, char **argv) {
+    if (argc != 2)
+        return 2;
+    char *rpath = realpath(argv[1], NULL);
+    if (!rpath) {
+        perror("realpath");
+        return 1;
+    }
+    printf("%s\n", rpath);
+    free(rpath);
+    return 0;
+}
diff --git a/test/test-realpath.sh b/test/test-realpath.sh
new file mode 100755
index 0000000..5b42b57
--- /dev/null
+++ b/test/test-realpath.sh
@@ -0,0 +1,63 @@ 
+#!/bin/bash
+#
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# See Yocto Project bugzilla 16028
+#
+# Gauthier HADERER reported differences in realpath behavior
+#
+
+go_exit() {
+  if [ -n "${link}" ]; then
+    rm ${link}
+  fi
+
+  if [ -n "${file}" ]; then
+    rm ${file}
+  fi
+
+  exit $rc
+}
+
+mypath=$(dirname $0)
+
+# Verify that a missing file fails
+${mypath}/test-realpath doesnt-exist
+rc=$?
+
+if [ $rc -eq 0 ]; then
+   echo "Non-zero return code expected!"
+   rc=1
+   go_exit
+fi
+
+# Verify that a regular file passes
+file=$(mktemp test-realpath.XXXXXX)
+
+filepath=$(${mypath}/test-realpath ${file})
+rc=$?
+
+if [ $rc -ne 0 ]; then
+   echo "Zero return code expected!  Unable to find ${file}."
+   go_exit
+fi
+
+link=$(mktemp test-realpath.XXXXXX)
+rm ${link}
+ln -s ${file} ${link}
+linkpath=`${mypath}/test-realpath ${link}`
+rc=$?
+rm ${link}
+
+rm ${file}
+
+if [ $rc -ne 0 ]; then
+    echo "Zero return code expected!  Unable to find ${link}."
+    go_exit
+fi
+
+if [ "${linkpath}" != "${filepath}" ]; then
+    echo "Link didn't return to expected target!"
+    rc=1
+    go_exit
+fi