diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-10-10 12:32:41 +0200 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-10-10 12:32:41 +0200 |
commit | 04a1354010d9cf7f40ead72a1d53fddef5d905bc (patch) | |
tree | 2d4b0ce749052836373ad94bfb2898573c9dcbae | |
parent | 7fd73cb1d70907b7a34734045aafb45af13d8cbf (diff) | |
download | u3-04a1354010d9cf7f40ead72a1d53fddef5d905bc.tar.gz u3-04a1354010d9cf7f40ead72a1d53fddef5d905bc.tar.bz2 u3-04a1354010d9cf7f40ead72a1d53fddef5d905bc.zip |
add: use custom file in stdout redirection
-rw-r--r-- | tst/rev-test.c | 30 | ||||
-rw-r--r-- | tst/seq-test.c | 25 | ||||
-rw-r--r-- | tst/std-redirects.c | 24 | ||||
-rw-r--r-- | tst/std-redirects.h | 6 |
4 files changed, 51 insertions, 34 deletions
diff --git a/tst/rev-test.c b/tst/rev-test.c index fd1ccee..59a1f1b 100644 --- a/tst/rev-test.c +++ b/tst/rev-test.c @@ -42,6 +42,9 @@ mt_defs(); #define REV_TEST_FILE "./rev-test-file" +#define REV_TEST_STDOUT "./rev-test-stdout" +#define REV_TEST_STDERR "./rev-test-stderr" +#define REV_TEST_STDIN "./rev-test-stdin" /* ========================================================================== @@ -60,7 +63,7 @@ mt_defs(); static void prepare_test(void) { - stdout_to_file(); + stdout_to_file(REV_TEST_STDOUT); } @@ -72,6 +75,9 @@ static void cleanup_test(void) { restore_stdout(); unlink(REV_TEST_FILE); + unlink(REV_TEST_STDOUT); + unlink(REV_TEST_STDERR); + unlink(REV_TEST_STDIN); } @@ -208,7 +214,7 @@ static void rev_lib_print_help(void) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_fok(u3_rev_main(argc, argv)); /* check if help really did print, just check first line @@ -316,7 +322,7 @@ static void rev_lib_single_overflow_line(void) #else /* ENABLE_MALLOC */ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), ENOBUFS); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); mt_fail(buf[0] == '\0'); @@ -426,7 +432,7 @@ static void rev_lib_single_overflow_line_no_nl(void) #else /* ENABLE_MALLOC */ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), ENOBUFS); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); mt_fail(buf[0] == '\0'); @@ -538,7 +544,7 @@ static void rev_lib_multi_overflow_line(void) #else /* ENABLE_MALLOC */ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), ENOBUFS); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); mt_fail(buf[0] == '\0'); @@ -649,7 +655,7 @@ static void rev_lib_multi_overflow_line_no_nl(void) #else /* ENABLE_MALLOC */ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), ENOBUFS); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); mt_fail(buf[0] == '\0'); @@ -677,7 +683,7 @@ static void rev_lib_zero_arg(void) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stdin_from_file(); + stdin_from_file(REV_TEST_STDIN); strcpy(expected, "987654321"); write_stdin_file("123456789", 9); rewind_stdin_file(); @@ -703,7 +709,7 @@ static void rev_lib_one_arg(void) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stdin_from_file(); + stdin_from_file(REV_TEST_STDIN); strcpy(expected, "987654321"); write_stdin_file("123456789", 9); rewind_stdin_file(); @@ -728,7 +734,7 @@ static void rev_lib_three_args(void) char *expected = "usage: rev [ -v | -h | <file> ]\n"; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), EINVAL); rewind_stderr_file(); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); @@ -750,7 +756,7 @@ static void rev_lib_file_not_found(void) char *expected = "e/fopen(): "; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), ENOENT); rewind_stderr_file(); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); @@ -774,7 +780,7 @@ static void rev_lib_permision_denied(void) char *expected = "e/fopen(): "; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); rev_gen_data(n, 0, REV_TEST_FILE, trash); chmod(REV_TEST_FILE, 0200); mt_ferr(u3_rev_main(argc, argv), EACCES); @@ -799,7 +805,7 @@ static void rev_lib_invalid_arg(void) char *expected = "e/invalid option -a\nusage: rev [ -v | -h | <file> ]\n"; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(REV_TEST_STDERR); mt_ferr(u3_rev_main(argc, argv), EINVAL); rewind_stderr_file(); mt_fail(read_stdout_file(buf, sizeof(buf)) == 0); diff --git a/tst/seq-test.c b/tst/seq-test.c index d730fc0..b8b5c36 100644 --- a/tst/seq-test.c +++ b/tst/seq-test.c @@ -17,15 +17,16 @@ # include <linux/limits.h> #endif +#include <errno.h> #include <limits.h> #include <stdio.h> #include <string.h> -#include <errno.h> +#include <unistd.h> +#include "fops.h" #include "mtest.h" #include "std-redirects.h" #include "u3.h" -#include "fops.h" /* ========================================================================== @@ -40,6 +41,8 @@ mt_defs(); #define EXPECTED_DIR TEST_DATA_DIR"/seq" +#define SEQ_TEST_STDOUT "./seq-test-stdout" +#define SEQ_TEST_STDERR "./seq-test-stderr" /* ========================================================================== @@ -99,7 +102,7 @@ char long_min[32]; /* string representation of LONG_MIN */ static void prepare_test(void) { - stdout_to_file(); + stdout_to_file(SEQ_TEST_STDOUT); } @@ -110,6 +113,8 @@ static void prepare_test(void) static void cleanup_test(void) { restore_stdout(); + unlink(SEQ_TEST_STDOUT); + unlink(SEQ_TEST_STDERR); } @@ -179,7 +184,7 @@ static void valid_test_1 mt_fok(u3_seq_main(argc, argv)); rewind_stdout_file(); - mt_fail(file_equal(expected_file, "./stdout") == 1); + mt_fail(file_equal(expected_file, SEQ_TEST_STDOUT) == 1); } @@ -209,7 +214,7 @@ static void valid_test_2 mt_fok(u3_seq_main(argc, argv)); rewind_stdout_file(); - mt_fail(file_equal(expected_file, "./stdout") == 1); + mt_fail(file_equal(expected_file, SEQ_TEST_STDOUT) == 1); } @@ -243,7 +248,7 @@ static void valid_test_3 mt_fok(u3_seq_main(argc, argv)); rewind_stdout_file(); - mt_fail(file_equal(expected_file, "./stdout") == 1); + mt_fail(file_equal(expected_file, SEQ_TEST_STDOUT) == 1); } @@ -266,7 +271,7 @@ static void invalid_test_1 argv[1] = p->last; buf = '\0'; - stderr_to_file(); + stderr_to_file(SEQ_TEST_STDERR); mt_ferr(u3_seq_main(argc, argv), p->errnum); restore_stderr(); rewind_stdout_file(); @@ -296,7 +301,7 @@ static void invalid_test_2 argv[2] = p->last; buf = '\0'; - stderr_to_file(); + stderr_to_file(SEQ_TEST_STDERR); mt_ferr(u3_seq_main(argc, argv), p->errnum); restore_stderr(); rewind_stdout_file(); @@ -327,7 +332,7 @@ static void invalid_test_3 argv[3] = p->last; buf = '\0'; - stderr_to_file(); + stderr_to_file(SEQ_TEST_STDERR); mt_ferr(u3_seq_main(argc, argv), p->errnum); restore_stderr(); rewind_stdout_file(); @@ -619,7 +624,7 @@ static void seq_print_help(void) " seq <first> <increment> <last>\n"; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ - stderr_to_file(); + stderr_to_file(SEQ_TEST_STDERR); mt_ferr(u3_seq_main(argc, argv), EINVAL); rewind_stderr_file(); diff --git a/tst/std-redirects.c b/tst/std-redirects.c index c791b5f..35e4bea 100644 --- a/tst/std-redirects.c +++ b/tst/std-redirects.c @@ -137,7 +137,10 @@ static int restore_fd ========================================================================== */ -int stdout_to_file(void) +int stdout_to_file +( + const char *file /* file where stdout should be redirected */ +) { /* flush stdout in case there is some cached data in there - it is * possible fflush occurs after our redirect and thus data that @@ -147,7 +150,7 @@ int stdout_to_file(void) fflush(stdout); - fd_stdout_file = open("./stdout", O_RDWR | O_CREAT | O_TRUNC, 0600); + fd_stdout_file = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600); if (fd_stdout_file < 0) { perror("open()"); @@ -171,9 +174,12 @@ int stdout_to_file(void) ========================================================================== */ -int stderr_to_file(void) +int stderr_to_file +( + const char *file /* file where stderr should be redirected */ +) { - fd_stderr_file = open("./stderr", O_RDWR | O_CREAT | O_TRUNC, 0600); + fd_stderr_file = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600); if (fd_stderr_file < 0) { perror("open()"); @@ -197,10 +203,13 @@ int stderr_to_file(void) ========================================================================== */ -int stdin_from_file(void) +int stdin_from_file +( + const char *file /* file where stdin should be redirected */ +) { - fd_stdin_file = open("./stdin", O_RDWR | O_CREAT | O_TRUNC, 0600); + fd_stdin_file = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600); if (fd_stdin_file < 0) { perror("open()"); @@ -235,7 +244,6 @@ int restore_stdout(void) close(fd_stdout); fd_stdout_file = -1; fd_stdout = -1; - unlink("./stdout"); return 0; } @@ -256,7 +264,6 @@ int restore_stderr(void) close(fd_stderr); fd_stderr_file = -1; fd_stderr = -1; - unlink("./stderr"); return 0; } @@ -277,7 +284,6 @@ int restore_stdin(void) close(fd_stdin); fd_stdin_file = -1; fd_stdin = -1; - unlink("./stdin"); return 0; } diff --git a/tst/std-redirects.h b/tst/std-redirects.h index 7dda3a5..d2cf72f 100644 --- a/tst/std-redirects.h +++ b/tst/std-redirects.h @@ -8,9 +8,9 @@ #include <sys/types.h> -int stdout_to_file(void); -int stderr_to_file(void); -int stdin_from_file(void); +int stdout_to_file(const char *file); +int stderr_to_file(const char *file); +int stdin_from_file(const char *file); int restore_stdout(void); int restore_stderr(void); int restore_stdin(void); |