Procházet zdrojové kódy

[LibOS] Add a regression test for fstat() on a directory

Chia-Che Tsai před 5 roky
rodič
revize
a0803a22b4

+ 13 - 4
LibOS/shim/test/regression/30_stat.py

@@ -7,10 +7,19 @@ loader = sys.argv[1]
 regression = Regression(loader, "stat_invalid_args")
 
 regression.add_check(name="Stat with invalid arguments",
-    check=lambda res: "stat(invalid-path-ptr) correctly returns error" in res[0].out and \
-                      "stat(invalid-buf-ptr) correctly returns error" in res[0].out and \
-                      "lstat(invalid-path-ptr) correctly returns error" in res[0].out and \
-                      "lstat(invalid-buf-ptr) correctly returns error" in res[0].out)
+    check=lambda res: "stat(invalid-path-ptr) correctly returned error" in res[0].out and \
+                      "stat(invalid-buf-ptr) correctly returned error" in res[0].out and \
+                      "lstat(invalid-path-ptr) correctly returned error" in res[0].out and \
+                      "lstat(invalid-buf-ptr) correctly returned error" in res[0].out)
+
+rv = regression.run_checks()
+if rv: sys.exit(rv)
+
+# Running fstat
+regression = Regression(loader, "fstat_cwd")
+
+regression.add_check(name="Fstat on a directory",
+    check=lambda res: "fstat returned the fd type as S_IFDIR" in res[0].out)
 
 rv = regression.run_checks()
 if rv: sys.exit(rv)

+ 33 - 0
LibOS/shim/test/regression/fstat_cwd.c

@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int main (int argc, char** argv) {
+    int r, fd;
+    struct stat buf;
+
+    fd = open(".", O_DIRECTORY);
+    if (fd == -1) {
+        printf("Opening CWD returned error %d\n", errno);
+        return -1;
+    }
+
+    r = fstat(fd, &buf);
+    if (r == -1) {
+        printf("fstat on directory fd returned error %d\n", errno);
+        return -1;
+    }
+
+    close(fd);
+
+    if (S_ISDIR(buf.st_mode))
+        printf("fstat returned the fd type as S_IFDIR\n");
+
+    return 0;
+}

+ 4 - 4
LibOS/shim/test/regression/stat_invalid_args.c

@@ -18,20 +18,20 @@ int main (int argc, char** argv) {
     /* check stat() */
     r = stat(badpath, goodbuf);
     if (r == -1 && errno == EFAULT)
-        printf("stat(invalid-path-ptr) correctly returns error\n");
+        printf("stat(invalid-path-ptr) correctly returned error\n");
 
     r = stat(goodpath, badbuf);
     if (r == -1 && errno == EFAULT)
-        printf("stat(invalid-buf-ptr) correctly returns error\n");
+        printf("stat(invalid-buf-ptr) correctly returned error\n");
 
     /* check lstat() */
     r = lstat(badpath, goodbuf);
     if (r == -1 && errno == EFAULT)
-        printf("lstat(invalid-path-ptr) correctly returns error\n");
+        printf("lstat(invalid-path-ptr) correctly returned error\n");
 
     r = lstat(goodpath, badbuf);
     if (r == -1 && errno == EFAULT)
-        printf("lstat(invalid-buf-ptr) correctly returns error\n");
+        printf("lstat(invalid-buf-ptr) correctly returned error\n");
 
     return 0;
 }