From a92be000fb06dc40ffd58c889e150c54682af6ed Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Sun, 24 Feb 2019 22:02:11 +0100 Subject: [PATCH] tests: Even more work on error handling realpath: Fixing undefined behaviour in the second test. If the call fails the resolved_name argument cannot be used for error checking because its state is undefined by SUSv2. pipe: Changing the order of close and write error handling code. Errors in close could overwrite errno after write errors, returning incorrect error messages. gmtime: Removed duplicate checks Other fixes for fseek, rename, mktime, putwchar --- tests/stdio/fseek.c | 2 +- tests/stdio/rename.c | 5 ++- tests/stdlib/realpath.c | 29 +++++----------- tests/time/gmtime.c | 8 ----- tests/time/mktime.c | 17 ++++++--- tests/unistd/pipe.c | 76 ++++++++++++++++++++++------------------- tests/wchar/putwchar.c | 11 ++---- 7 files changed, 68 insertions(+), 80 deletions(-) diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c index c370cc4e6d..9b8bc1b046 100644 --- a/tests/stdio/fseek.c +++ b/tests/stdio/fseek.c @@ -16,5 +16,5 @@ int main(void) { off_t pos = ftello(f); ERROR_IF(ftello, pos, == -1); - printf("ftell: %ld\n", pos); + printf("ftello: %ld\n", pos); } diff --git a/tests/stdio/rename.c b/tests/stdio/rename.c index 62592a5e28..840916c901 100644 --- a/tests/stdio/rename.c +++ b/tests/stdio/rename.c @@ -49,9 +49,8 @@ int main(void) { UNEXP_IF(remove, rm_status, != 0); // Compare file contents - if (strcmp(str, buf) == 0) { - exit(EXIT_SUCCESS); - } else { + if (strcmp(str, buf) != 0) { + puts("Comparison failed!"); exit(EXIT_FAILURE); } } diff --git a/tests/stdlib/realpath.c b/tests/stdlib/realpath.c index a1583f864d..d6406dd52e 100644 --- a/tests/stdlib/realpath.c +++ b/tests/stdlib/realpath.c @@ -7,25 +7,14 @@ #include "test_helpers.h" int main(void) { - char* path = realpath("stdlib/realpath.c", NULL); - if (!path) { - perror("realpath"); - exit(EXIT_FAILURE); - } - puts(path); + char *path_res = realpath("stdlib/realpath.c", NULL); + ERROR_IF(realpath, path_res, == NULL); + puts(path_res); + free(path_res); - free(path); - - path = malloc(PATH_MAX); - memset(path, 0, PATH_MAX); - - realpath("stdlib/realpath.c", path); - if (!path) { - perror("realpath"); - free(path); - exit(EXIT_FAILURE); - } - puts(path); - - free(path); + char path_arg[PATH_MAX] = { 0 }; + char *res = realpath("stdlib/realpath.c", path_arg); + ERROR_IF(realpath, res, == NULL); + puts(path_arg); + free(path_arg); } diff --git a/tests/time/gmtime.c b/tests/time/gmtime.c index a77d19fb9a..f5a16f2af3 100644 --- a/tests/time/gmtime.c +++ b/tests/time/gmtime.c @@ -18,12 +18,4 @@ int main(void) { info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { exit(EXIT_FAILURE); } - - if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min || - info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday || - info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday || - info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst || - info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { - exit(EXIT_FAILURE); - } } diff --git a/tests/time/mktime.c b/tests/time/mktime.c index 79e5e3cf67..bc8c9c755d 100644 --- a/tests/time/mktime.c +++ b/tests/time/mktime.c @@ -6,11 +6,15 @@ #include "test_helpers.h" int check(time_t input) { - struct tm* t = localtime(&input); + struct tm *t = localtime(&input); + ERROR_IF(localtime, t, == NULL); - printf("%ld = %ld\n", input, mktime(t)); + time_t output = mktime(t); + ERROR_IF(mktime, output, == (time_t)-1); - if (input != mktime(t)) { + printf("%ld = %ld\n", input, output); + + if (input != output) { printf( "Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n", t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec @@ -41,8 +45,13 @@ int main(void) { for (int i = 0; i < 10; i += 1) { time_t input = (time_t) rand(); - struct tm* time = localtime(&input); + + struct tm *time = localtime(&input); + ERROR_IF(localtime, time, == NULL); + time_t output = mktime(time); + ERROR_IF(mktime, output, == (time_t)-1); + if (input != output) { // asctime has newline printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time)); diff --git a/tests/unistd/pipe.c b/tests/unistd/pipe.c index 88403c40ad..85e6db5abf 100644 --- a/tests/unistd/pipe.c +++ b/tests/unistd/pipe.c @@ -7,57 +7,56 @@ #include "test_helpers.h" int main(void) { - int pid, pip[2]; + int pip[2]; char instring[20]; - char * outstring = "Hello World!"; + char *outstring = "Hello World!"; - if (pipe(pip) < 0) { - perror("pipe"); - exit(EXIT_FAILURE); - } + int pipe_status = pipe(pip); + ERROR_IF(pipe, pipe_status, == -1); + UNEXP_IF(pipe, pipe_status, != 0); - pid = fork(); - if (pid == 0) /* child : sends message to parent*/ - { - /* close read end */ - close(pip[0]); + int pid = fork(); + ERROR_IF(fork, pid, == -1); - /* send 7 characters in the string, including end-of-string */ + if (pid == 0) { + // child: sends message to parent + // close read end + int cr = close(pip[0]); + ERROR_IF(close, cr, == -1); + UNEXP_IF(close, cr, != 0); + + // send 7 characters in the string, including end-of-string int bytes = write(pip[1], outstring, strlen(outstring)); + ERROR_IF(write, bytes, == -1); - /* close write end */ - close(pip[1]); - - /* check result */ - if (bytes < 0) { - perror("pipe write"); - exit(EXIT_FAILURE); - } else if (bytes != strlen(outstring)) { + // check result + if (bytes != strlen(outstring)) { fprintf(stderr, "pipe write: %d != %ld\n", bytes, strlen(outstring)); exit(EXIT_FAILURE); } - exit(EXIT_SUCCESS); - } - else /* parent : receives message from child */ - { - /* close write end */ - close(pip[1]); + // close write end + int cw = close(pip[1]); + ERROR_IF(close, cw, == -1); + UNEXP_IF(close, cw, != 0); - /* clear memory */ + exit(EXIT_SUCCESS); + } else { + // parent: receives message from child + // close write end + int cw = close(pip[1]); + ERROR_IF(close, cw, == -1); + UNEXP_IF(close, cw, != 0); + + // clear memory memset(instring, 0, sizeof(instring)); - /* read from the pipe */ + // read from the pipe int bytes = read(pip[0], instring, sizeof(instring) - 1); + ERROR_IF(read, bytes, == -1); - /* close read end */ - close(pip[0]); - - /* check result */ - if (bytes < 0) { - perror("pipe read"); - exit(EXIT_FAILURE); - } else if (bytes != strlen(outstring)) { + // check result + if (bytes != strlen(outstring)) { fprintf(stderr, "pipe read: %d != %ld\n", bytes, strlen(outstring)); exit(EXIT_FAILURE); } else if (memcmp(instring, outstring, strlen(outstring)) != 0) { @@ -67,6 +66,11 @@ int main(void) { printf("%s\n", instring); } + // close read end + int cr = close(pip[0]); + ERROR_IF(close, cr, == -1); + UNEXP_IF(close, cr, != 0); + exit(EXIT_SUCCESS); } } diff --git a/tests/wchar/putwchar.c b/tests/wchar/putwchar.c index 7b11264f7d..fc25911468 100644 --- a/tests/wchar/putwchar.c +++ b/tests/wchar/putwchar.c @@ -8,13 +8,8 @@ int main(void) { wchar_t *wcs = L"zß水🍌"; - int i; - for (i = 0; wcs[i] != L'\0'; i++) - { - if (0xFFFFFFFFu == putwchar(wcs[i])) - { - printf("Unable to putwchar() the wide character.\n"); - exit(EXIT_FAILURE); - } + for (int i = 0; wcs[i] != L'\0'; i++) { + wint_t status = putwchar(wcs[i]); + ERROR_IF(putwchar, status, == WEOF); } }