qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/4] linux user: added tests for proc myself


From: aladjev . andrew
Subject: [PATCH 4/4] linux user: added tests for proc myself
Date: Thu, 25 Feb 2021 23:54:48 +0300

From: Andrew Aladjev <aladjev.andrew@gmail.com>

---
 tests/tcg/multiarch/linux-test.c | 85 ++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/tests/tcg/multiarch/linux-test.c b/tests/tcg/multiarch/linux-test.c
index 96bbad5..4918d45 100644
--- a/tests/tcg/multiarch/linux-test.c
+++ b/tests/tcg/multiarch/linux-test.c
@@ -517,6 +517,89 @@ static void test_shm(void)
     chk_error(shmdt(ptr));
 }
 
+static void test_proc_myself_file(void)
+{
+    int fd1, fd2;
+    char link1[PATH_MAX], link2[PATH_MAX];
+    char buf1[PATH_MAX], buf2[PATH_MAX];
+    int buf1_length, buf2_length;
+
+    /* We can open any file that will always exist. */
+    const char *file_path = "/proc/self/comm";
+
+    char file_realpath[PATH_MAX];
+    if (realpath(file_path, file_realpath) == NULL) {
+        error("proc myself: invalid file");
+    }
+
+    fd1 = chk_error(open(file_path, O_RDONLY));
+    sprintf(link1, "/proc/self/fd/%d", fd1);
+
+    /* Lets try to open same file by first link. */
+    fd2 = chk_error(open(link1, O_RDONLY));
+    sprintf(link2, "/proc/self/fd/%d", fd2);
+
+    /* Two links should point to the same file path. */
+    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
+    if (strlen(file_realpath) != buf1_length ||
+        strncmp(file_realpath, buf1, buf1_length) != 0) {
+        error("proc myself: invalid link");
+    }
+    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
+    if (strlen(file_realpath) != buf2_length ||
+        strncmp(file_realpath, buf2, buf2_length) != 0) {
+        error("proc myself: invalid link");
+    }
+
+    /* We should be able to read same data from each fd. */
+    buf1_length = chk_error(read(fd1, buf1, PATH_MAX));
+    buf2_length = chk_error(read(fd2, buf2, PATH_MAX));
+    if (buf1_length == 0 || buf1_length != buf2_length ||
+        strncmp(buf1, buf2, buf2_length) != 0) {
+        error("proc myself: invalid file content");
+    }
+
+    chk_error(close(fd2));
+    chk_error(close(fd1));
+}
+
+static void test_proc_myself_exe(void)
+{
+    int fd1, fd2;
+    char link1[PATH_MAX], link2[PATH_MAX];
+    char buf1[PATH_MAX], buf2[PATH_MAX];
+    int buf1_length, buf2_length;
+
+    const char *exe_path = "/proc/self/exe";
+
+    char exe_realpath[PATH_MAX];
+    if (realpath(exe_path, exe_realpath) == NULL) {
+        error("proc myself: invalid exe");
+    }
+
+    fd1 = chk_error(open(exe_path, O_RDONLY));
+    sprintf(link1, "/proc/self/fd/%d", fd1);
+
+    /* Lets try to open link once again. */
+    fd2 = chk_error(open(link1, O_RDONLY));
+    sprintf(link2, "/proc/self/fd/%d", fd2);
+
+    /* Two links should point to the same exe path. */
+    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
+    if (strlen(exe_realpath) != buf1_length ||
+        strncmp(exe_realpath, buf1, buf1_length) != 0) {
+        error("proc myself: invalid link");
+    }
+    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
+    if (strlen(exe_realpath) != buf2_length ||
+        strncmp(exe_realpath, buf2, buf2_length) != 0) {
+        error("proc myself: invalid link");
+    }
+
+    chk_error(close(fd2));
+    chk_error(close(fd1));
+}
+
 int main(int argc, char **argv)
 {
     test_file();
@@ -532,5 +615,7 @@ int main(int argc, char **argv)
 
     test_signal();
     test_shm();
+    test_proc_myself_file();
+    test_proc_myself_exe();
     return 0;
 }
-- 
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]