X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=blobdiff_plain;f=tests%2Fexample_exec.c;h=e13010b8ee17bb6601cbcd31e97c3abcd5ce02c7;hp=bd12b5a71f7b5f5ce42436144d966b5a192e3e6c;hb=8a65b4486febf00e3fad5bafc3773a811e675a4c;hpb=afca2864dc6464201dfe32134ae20e0107297836 diff --git a/tests/example_exec.c b/tests/example_exec.c index bd12b5a..e13010b 100644 --- a/tests/example_exec.c +++ b/tests/example_exec.c @@ -1,7 +1,7 @@ /* * Test execve() and exec*() functions. * - * Copyright (C) 2013 Simon Ruderich + * Copyright (C) 2013-2015 Simon Ruderich * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,11 +19,15 @@ #include +/* For execvpe(). */ +#define _GNU_SOURCE + #include #include #include #include +#include "example.h" #include "../src/compiler.h" @@ -55,6 +59,9 @@ static int find_magic_run_number(char *argv0, int *number) { return 1; } +static int cmp(void const *a, void const *b) { + return strcmp(*(char * const *)a, *(char * const *)b); +} static void dump(char *argv[]) { size_t i; @@ -62,14 +69,23 @@ static void dump(char *argv[]) { while (*argv) { printf("argv[%zu] = |%s|\n", i++, *argv++); } + + /* Order of environment variables is not defined, sort them for consistent + * test results. */ i = 0; char **env = environ; + while (*env++) { + i++; + } + qsort(environ, i, sizeof(*env), cmp); + + i = 0; + env = environ; while (*env) { /* Skip LD_PRELOAD which contains system dependent values. */ if (strncmp(*env, "LD_PRELOAD=", 11)) { - printf("environ[%zu] = |%s|\n", i, *env); + printf("environ[%zu] = |%s|\n", i++, *env); } - i++; env++; } printf("\n"); @@ -120,7 +136,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 +144,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 +172,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 +182,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 +314,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] = |COLORED_STDERR_PRIVATE_FDS=2,|"); + puts("environ[1] = |TEST=54|"); + puts(""); + puts("argv[0] = |./example_exec|"); + puts("argv[1] = |foo|"); + puts("argv[2] = |bar|"); + puts("environ[0] = |COLORED_STDERR_PRIVATE_FDS=2,|"); + puts("environ[1] = |TEST=55|"); + puts(""); +#endif } printf("Done.\n");