]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blobdiff - tests/example_exec.c
tests: Check return values of write() and dup2().
[coloredstderr/coloredstderr.git] / tests / example_exec.c
index bd12b5a71f7b5f5ce42436144d966b5a192e3e6c..1220f853618bd326539de885c8f58735c31327f2 100644 (file)
 
 #include <config.h>
 
+/* For execvpe(). */
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#include "example.h"
 #include "../src/compiler.h"
 
 
@@ -120,7 +124,7 @@ int main(int argc unused, char **argv) {
         printf("\nCHECKING COLORING.\n\n");
         fflush(stdout);
 
-        dup2(2, 3);
+        xdup2(2, 3);
 
         char *args[] = { argv0, NULL };
         char *envp[] = { ldpreload, NULL };
@@ -128,24 +132,24 @@ int main(int argc unused, char **argv) {
         execve(argv[0], args, envp);
         return EXIT_FAILURE;
     } else if (!skip--) {
-        dup2(2, 4);
+        xdup2(2, 4);
 
         execl(argv[0], argv0, NULL);
         return EXIT_FAILURE;
     } else if (!skip--) {
-        dup2(2, 5);
+        xdup2(2, 5);
 
         execlp(argv[0], argv0, NULL);
         return EXIT_FAILURE;
     } else if (!skip--) {
-        dup2(2, 6);
+        xdup2(2, 6);
 
         char *envp[] = { ldpreload, NULL };
 
         execle(argv[0], argv0, NULL, envp);
         return EXIT_FAILURE;
     } else if (!skip--) {
-        dup2(2, 7);
+        xdup2(2, 7);
 
         /* Test closing a few descriptors. */
         close(5);
@@ -156,7 +160,7 @@ int main(int argc unused, char **argv) {
         execv(argv[0], args);
         return EXIT_FAILURE;
     } else if (!skip--) {
-        dup2(2, 8);
+        xdup2(2, 8);
 
         /* And a few more. */
         close(7);
@@ -166,9 +170,38 @@ int main(int argc unused, char **argv) {
 
         execvp(argv[0], args);
         return EXIT_FAILURE;
-    } else {
+
+    /* Test handling of COLORED_STDERR_FDS. */
+
+    } else if (!skip--) {
+        /* And the rest. */
         close(3);
         close(8);
+
+        xdup2(2, 5);
+
+        char *args[] = { argv0, NULL };
+        char *envp[] = { ldpreload, "COLORED_STDERR_FDS=5,", NULL };
+
+        execve(argv[0], args, envp);
+        return EXIT_FAILURE;
+    } else if (!skip--) {
+        char *args[] = { argv0, NULL };
+        char *envp[] = { ldpreload, NULL };
+
+        xdup2(5, 6);
+        close(5);
+
+        execve(argv[0], args, envp);
+        return EXIT_FAILURE;
+    } else if (!skip--) {
+        close(6);
+
+        char *args[] = { argv0, NULL };
+        setenv("COLORED_STDERR_FDS", "2,", 1);
+
+        execv(argv[0], args);
+        return EXIT_FAILURE;
     }
 
 
@@ -269,6 +302,35 @@ int main(int argc unused, char **argv) {
 
         execvp(argv[0], args);
         return EXIT_FAILURE;
+
+#ifdef HAVE_EXECVPE
+    /* execvpe(3), but not testing the p(ath) part */
+    } else if (!skip--) {
+        char *args[] = { argv0, NULL };
+        char *envp[] = { "TEST=54", ldpreload, NULL };
+
+        execvpe(argv[0], args, envp);
+        return EXIT_FAILURE;
+    } else if (!skip--) {
+        char *args[] = { argv0, "foo", "bar", NULL };
+        char *envp[] = { "TEST=55", ldpreload, NULL };
+
+        execvpe(argv[0], args, envp);
+        return EXIT_FAILURE;
+#else
+    /* Fake output to let the test pass. */
+    } else if (!skip--) {
+        puts("argv[0] = |./example_exec|");
+        puts("environ[0] = |TEST=54|");
+        puts("environ[2] = |COLORED_STDERR_PRIVATE_FDS=2,|");
+        puts("");
+        puts("argv[0] = |./example_exec|");
+        puts("argv[1] = |foo|");
+        puts("argv[2] = |bar|");
+        puts("environ[0] = |TEST=55|");
+        puts("environ[2] = |COLORED_STDERR_PRIVATE_FDS=2,|");
+        puts("");
+#endif
     }
 
     printf("Done.\n");