浏览代码

tests: Work on more thorough error handling

Tibor Nagy 6 年之前
父节点
当前提交
f60c95d2ca

+ 1 - 2
tests/args.c

@@ -2,8 +2,7 @@
 #include <unistd.h>
 
 int main(int argc, char *argv[]) {
-    int i;
-    for(i = 0; i < argc; i++) {
+    for(int i = 0; i < argc; i++) {
         write(STDOUT_FILENO, argv[i], strlen(argv[i]));
         write(STDOUT_FILENO, " ", 1);
     }

+ 6 - 1
tests/resource/getrusage.c

@@ -1,5 +1,6 @@
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/resource.h>
 
 void ptimeval(struct timeval* val) {
@@ -8,14 +9,18 @@ void ptimeval(struct timeval* val) {
 
 int main(void) {
     struct rusage r_usage;
-    if (getrusage(RUSAGE_SELF, &r_usage) < 0) {
+
+    if (getrusage(RUSAGE_SELF, &r_usage) == -1) {
         perror("getrusage");
         exit(EXIT_FAILURE);
     }
+
     printf("ru_utime:");
     ptimeval(&r_usage.ru_utime);
+
     printf("ru_stime:");
     ptimeval(&r_usage.ru_utime);
+
     printf("ru_maxrss: %ld\n", r_usage.ru_maxrss);
     printf("ru_ixrss: %ld\n", r_usage.ru_ixrss);
     printf("ru_idrss: %ld\n", r_usage.ru_idrss);

+ 9 - 1
tests/unistd/brk.c

@@ -1,7 +1,15 @@
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 int main(void) {
     int status = brk((void*)100);
-    printf("brk exited with status code %d\n", status);
+
+    if (status == -1) {
+        perror("brk");
+        exit(EXIT_FAILURE);
+    } else if (status != 0) {
+        printf("brk returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
+    }
 }

+ 34 - 9
tests/unistd/chdir.c

@@ -1,15 +1,40 @@
+#include <limits.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 int main(void) {
-    char* cwd1 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
-    getcwd(cwd1, 4096);
-    printf("initial cwd: %s\n", cwd1);
-    free(cwd1);
-    chdir("..");
-    char* cwd2 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
-    getcwd(cwd2, 4096);
-    printf("final cwd: %s\n", cwd2);
-    free(cwd2);
+    char cwd[PATH_MAX] = { 0 };
+    char *cwd_result = NULL;
+
+    cwd_result = getcwd(cwd, PATH_MAX);
+    if (cwd_result == NULL) {
+        perror("getcwd");
+        exit(EXIT_FAILURE);
+    } else if (cwd_result != cwd) {
+        puts("getcwd returned something else than the buf argument");
+        exit(EXIT_FAILURE);
+    }
+
+    printf("getcwd before chdir: %s\n", cwd);
+
+    int status = chdir("..");
+    if (status == -1) {
+        perror("chdir");
+        exit(EXIT_FAILURE);
+    } else if (status != 0) {
+        printf("chdir returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
+    }
+
+    cwd_result = getcwd(cwd, PATH_MAX);
+    if (cwd_result == NULL) {
+        perror("getcwd");
+        exit(EXIT_FAILURE);
+    } else if (cwd_result != cwd) {
+        puts("getcwd returned something else than the buf argument");
+        exit(EXIT_FAILURE);
+    }
+
+    printf("getcwd after chdir: %s\n", cwd);
 }

+ 7 - 2
tests/unistd/exec.c

@@ -1,8 +1,13 @@
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 int main(void) {
     char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL};
-    execv("/bin/sh", args);
-    perror("execv");
+
+    int status = execv("/bin/sh", args);
+    if (status == -1) {
+        perror("execv");
+        exit(EXIT_FAILURE);
+    }
 }

+ 26 - 4
tests/unistd/fchdir.c

@@ -1,11 +1,33 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 int main(void) {
     int fd = open("..", 0, 0);
-    int status;
-    status = fchdir(fd);
-    printf("fchdir exited with status code %d\n", status);
-    close(fd);
+    if (fd == -1) {
+        perror("open");
+        exit(EXIT_FAILURE);
+    } else if (fd < 0) {
+        printf("open returned %d, unexpected result\n", fd);
+        exit(EXIT_FAILURE);
+    }
+
+    int status = fchdir(fd);
+    if (status == -1) {
+        perror("fchdir");
+        exit(EXIT_FAILURE);
+    } else if (status != 0) {
+        printf("fchdir returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
+    }
+
+    int c = close(fd);
+    if (c == -1) {
+        perror("close");
+        exit(EXIT_FAILURE);
+    } else if (c != 0) {
+        printf("close returned %d, unexpected result\n", c);
+        exit(EXIT_FAILURE);
+    }
 }

+ 26 - 4
tests/unistd/fsync.c

@@ -1,11 +1,33 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 int main(void) {
     int fd = open(".", 0, 0);
-    int status;
-    status = fsync(fd);
-    printf("fsync exited with status code %d\n", status);
-    close(fd);
+    if (fd == -1) {
+        perror("open");
+        exit(EXIT_FAILURE);
+    } else if (fd < 0) {
+        printf("open returned %d, unexpected result\n", fd);
+        exit(EXIT_FAILURE);
+    }
+
+    int status = fsync(fd);
+    if (status == -1) {
+        perror("fsync");
+        exit(EXIT_FAILURE);
+    } else if (status != 0) {
+        printf("fsync returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
+    }
+
+    int c = close(fd);
+    if (c == -1) {
+        perror("close");
+        exit(EXIT_FAILURE);
+    } else if (c != 0) {
+        printf("close returned %d, unexpected result\n", c);
+        exit(EXIT_FAILURE);
+    }
 }

+ 26 - 4
tests/unistd/ftruncate.c

@@ -1,11 +1,33 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 int main(void) {
     int fd = creat("ftruncate.out", 0777);
-    int status;
-    status = ftruncate(fd, 100);
-    printf("ftruncate exited with status code %d\n", status);
-    close(fd);
+    if (fd == -1) {
+        perror("creat");
+        exit(EXIT_FAILURE);
+    } else if (fd < 0) {
+        printf("creat returned %d, unexpected result\n", fd);
+        exit(EXIT_FAILURE);
+    }
+
+    int status = ftruncate(fd, 100);
+    if (status == -1) {
+        perror("ftruncate");
+        exit(EXIT_FAILURE);
+    } else if (status != 0) {
+        printf("ftruncate returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
+    }
+
+    int c = close(fd);
+    if (c == -1) {
+        perror("close");
+        exit(EXIT_FAILURE);
+    } else if (c != 0) {
+        printf("close returned %d, unexpected result\n", c);
+        exit(EXIT_FAILURE);
+    }
 }

+ 1 - 1
tests/unistd/getcwd.c

@@ -5,7 +5,7 @@
 #include <unistd.h>
 
 int main(void) {
-    char first[PATH_MAX];
+    char first[PATH_MAX] = { 0 };
     getcwd(first, PATH_MAX);
     puts(first);
 

+ 9 - 3
tests/unistd/gethostname.c

@@ -3,10 +3,16 @@
 #include <unistd.h>
 
 int main(void) {
-    char* hostname = malloc(256);
-    if (gethostname(hostname, 256) == 0) {
+    char hostname[256] = { 0 };
+
+    int status = gethostname(hostname, 256);
+    if (status == 0) {
         printf("Hostname: %s\n", hostname);
+    } else if (status == -1) {
+        perror("gethostname");
+        exit(EXIT_FAILURE);
     } else {
-        puts("error getting hostname");
+        printf("gethostname returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
     }
 }

+ 16 - 3
tests/unistd/isatty.c

@@ -1,11 +1,24 @@
+#include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 int main(void) {
-    // 1 is stdout
-    if (isatty(1)) {
+    int status = isatty(STDOUT_FILENO);
+
+    if (status == 1) {
         puts("'Tis a tty :D");
+    } else if (status == 0) {
+        if (errno == ENOTTY) {
+            // I wouldn't consider stdout not being a TTY an error
+            // (CI runners, etc.) 
+            puts("Whatever a tty is, it's not me");
+        } else {
+            perror("isatty");
+            exit(EXIT_FAILURE);
+        }
     } else {
-        puts("Whatever a tty is, it's not me");
+        printf("isatty returned %d, unexpected result\n", status);
+        exit(EXIT_FAILURE);
     }
 }