Jan Stancek | 927a15c | 2014-09-11 11:11:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Linux Test Project, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <string.h> |
| 23 | #include "test.h" |
| 24 | #include "safe_macros.h" |
Jan Stancek | 927a15c | 2014-09-11 11:11:45 +0200 | [diff] [blame] | 25 | |
| 26 | #define OUTPUT_FNAME "output" |
| 27 | |
| 28 | char *TCID = "trerrno"; |
| 29 | int TST_TOTAL = 1; |
| 30 | |
| 31 | static void setup(void) |
| 32 | { |
| 33 | tst_tmpdir(); |
| 34 | } |
| 35 | |
| 36 | static void cleanup(void) |
| 37 | { |
| 38 | tst_rmdir(); |
| 39 | } |
| 40 | |
| 41 | int main(void) |
| 42 | { |
| 43 | int fd, stdout_fd; |
| 44 | char msg[4096], *pos; |
| 45 | FILE *f; |
| 46 | |
| 47 | setup(); |
| 48 | |
| 49 | /* redirect stdout to file */ |
| 50 | stdout_fd = dup(fileno(stdout)); |
| 51 | fd = SAFE_OPEN(NULL, OUTPUT_FNAME, O_RDWR | O_CREAT, 0666); |
| 52 | TEST(dup2(fd, fileno(stdout))); |
| 53 | if (TEST_RETURN == -1) |
| 54 | tst_brkm(TBROK | TTERRNO, cleanup, "dup2"); |
| 55 | |
| 56 | errno = EPERM; |
| 57 | TEST_ERRNO = EPERM; |
| 58 | TEST_RETURN = EINVAL; |
| 59 | tst_resm(TINFO | TRERRNO, "test"); |
| 60 | tst_flush(); |
| 61 | |
| 62 | /* restore stdout */ |
| 63 | TEST(dup2(stdout_fd, fileno(stdout))); |
| 64 | if (TEST_RETURN == -1) |
| 65 | tst_brkm(TBROK | TTERRNO, cleanup, "dup2"); |
| 66 | |
| 67 | /* read file and verify that output is as expected */ |
| 68 | SAFE_LSEEK(cleanup, fd, 0, SEEK_SET); |
| 69 | f = fdopen(fd, "r"); |
| 70 | if (!f) |
| 71 | tst_brkm(TBROK | TERRNO, cleanup, "fdopen"); |
| 72 | if (!fgets(msg, sizeof(msg), f)) |
| 73 | tst_brkm(TBROK, cleanup, "fgets"); |
| 74 | fclose(f); |
| 75 | |
| 76 | pos = strchr(msg, '\n'); |
| 77 | if (pos != NULL) |
| 78 | *pos = '\0'; |
| 79 | |
| 80 | tst_resm(TINFO, "%s", msg); |
| 81 | if (strstr(msg, "EPERM")) |
| 82 | tst_resm(TFAIL, "EPERM shouldn't be in TRERRNO output"); |
| 83 | if (strstr(msg, "EINVAL")) |
| 84 | tst_resm(TPASS, "EINVAL should be in TRERRNO output"); |
| 85 | else |
| 86 | tst_resm(TFAIL, "EINVAL not found in TRERRNO output"); |
| 87 | |
| 88 | cleanup(); |
| 89 | tst_exit(); |
| 90 | } |