diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2020-08-17 16:17:40 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2020-08-17 16:17:40 +0000 |
commit | a2e56088f9b97c1fdb63b1b5ae21d4fe7b9dc9a7 (patch) | |
tree | e1a841762f80e98fd94eff3c8451520478933efc /regress/lib/libc | |
parent | a147cf7fbd947555dfde0f8cae5fce3108a64cef (diff) |
Fix append mode so it always writes to the end and expand regress.
OK deraadt@ martijn@
Diffstat (limited to 'regress/lib/libc')
-rw-r--r-- | regress/lib/libc/fmemopen/fmemopentest.c | 103 |
1 files changed, 77 insertions, 26 deletions
diff --git a/regress/lib/libc/fmemopen/fmemopentest.c b/regress/lib/libc/fmemopen/fmemopentest.c index 203c93be650..dd0c123fe69 100644 --- a/regress/lib/libc/fmemopen/fmemopentest.c +++ b/regress/lib/libc/fmemopen/fmemopentest.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fmemopentest.c,v 1.3 2013/03/28 09:35:58 mpi Exp $ */ +/* $OpenBSD: fmemopentest.c,v 1.4 2020/08/17 16:17:39 millert Exp $ */ /* * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> @@ -70,7 +70,7 @@ simpletest(void) } int -updatetest(void) +appendtest(const char *mode) { FILE *s1; char string[] = "hello\0 test number 2"; @@ -78,35 +78,44 @@ updatetest(void) size_t len; int failures = 0; - s1 = fmemopen(string, 19, "a+"); + s1 = fmemopen(string, 19, mode); if (s1 == NULL) return (1); + fseek(s1, 0, SEEK_SET); + if (ftell(s1) != 0) { + warnx("failed seek test [%s] (4)", mode); + failures++; + } + + /* write should append even though seek position is 0 */ len = fwrite(" world", 1, 6, s1); if (len != 6) { - warnx("failed write test (4)"); + warnx("failed write test [%s] (5)", mode); failures++; } - fseek(s1, 0, SEEK_SET); - if (ftell(s1) != 0) { - warnx("failed seek test (5)"); + if (ftell(s1) != strlen("hello world")) { + warnx("failed seek test [%s] (6)", mode); failures++; } - len = fread(buffer, 1, sizeof(buffer) - 1, s1); - if (strncmp(string, buffer, len)) { - warnx("failed compare test (6)"); - failures++; + if (mode[1] == '+') { + fseek(s1, 0, SEEK_SET); + len = fread(buffer, 1, sizeof(buffer) - 1, s1); + if (len == 0 || strncmp(string, buffer, len)) { + warnx("failed compare test [%s] (7)", mode); + failures++; + } } if (strcmp(string, "hello world")) { - warnx("failed compare test (7)"); + warnx("failed compare test [%s] (8)", mode); failures++; } if (strcmp(string + strlen(string) + 1, "number 2")) { - warnx("failed compare test (8)"); + warnx("failed compare test [%s] (9)", mode); failures++; } @@ -114,7 +123,46 @@ updatetest(void) } int -writetest(void) +updatetest(void) +{ + FILE *s1; + char string[] = "bah, what a day"; + char buffer[256]; + size_t len; + int failures = 0; + + s1 = fmemopen(string, 19, "r+"); + if (s1 == NULL) + return (1); + + if (ftell(s1) != 0) { + warnx("failed seek test (10)"); + failures++; + } + + len = fwrite("oh frabjous", 1, 11, s1); + if (len != 11) { + warnx("failed write test (11)"); + failures++; + } + + fseek(s1, 0, SEEK_SET); + len = fread(buffer, 1, sizeof(buffer) - 1, s1); + if (len == 0 || strncmp(string, buffer, len)) { + warnx("failed compare test (12)"); + failures++; + } + + if (strcmp(string, "oh frabjous day")) { + warnx("failed compare test (13)"); + failures++; + } + + return (failures); +} + +int +writetest(const char *mode) { FILE *s1; char string[] = "super test number 3"; @@ -124,36 +172,36 @@ writetest(void) slen = strlen(string) + 1; - s1 = fmemopen(string, slen, "w"); + s1 = fmemopen(string, slen, mode); if (s1 == NULL) return (1); len = fwrite("short", 1, 5, s1); if (len != strlen("short")) { - warnx("failed write test (9)"); + warnx("failed write test [%s] (14)", mode); failures++; } fclose(s1); s1 = fmemopen(string, slen, "r"); if (s1 == NULL) { - warnx("failed open test (10)"); + warnx("failed open test [%s] (15)", mode); failures++; } len = fread(buffer, 1, sizeof(buffer) - 1, s1); - if (strncmp(string, buffer, len)) { - warnx("failed compare test (11)"); + if (len == 0 || strncmp(string, buffer, len)) { + warnx("failed compare test [%s] (16)", mode); failures++; } if (strcmp(string, "short")) { - warnx("failed compare test (12)"); + warnx("failed compare test [%s] (17)", mode); failures++; } if (strcmp(string + strlen(string) + 1, "test number 3")) { - warnx("failed compare test (13)"); + warnx("failed compare test [%s] (18)", mode); failures++; } @@ -175,23 +223,23 @@ seektest(void) return (1); if (fseek(s1, 8, SEEK_SET) != 0) { - warnx("failed to fseek. (14)"); + warnx("failed to fseek. (19)"); failures++; } if (ftell(s1) != 8) { - warnx("failed seek test. (15)"); + warnx("failed seek test. (20)"); failures++; } /* Try to seek backward */ if (fseek(s1, -1, SEEK_CUR) != 0) { - warnx("failed to fseek. (16)"); + warnx("failed to fseek. (21)"); failures++; } if (ftell(s1) != 7) { - warnx("failed seeking backward. (17)"); + warnx("failed seeking backward. (22)"); failures++; } @@ -204,8 +252,11 @@ main(void) int failures = 0; failures += simpletest(); + failures += appendtest("a"); + failures += appendtest("a+"); failures += updatetest(); - failures += writetest(); + failures += writetest("w"); + failures += writetest("w+"); failures += seektest(); return (failures); |