diff options
author | mpi <> | 2013-01-01 17:43:07 +0000 |
---|---|---|
committer | mpi <> | 2013-01-01 17:43:07 +0000 |
commit | cb6e574cb86d4a97e7a9cc6ae79f181476188b21 (patch) | |
tree | 53d2af9885a4ad7ec3a7e5d1e0d59ea5195e5d3a | |
parent | 6453f215f5463a02d8367c503969b10d3f8e0ccf (diff) | |
download | openbsd-cb6e574cb86d4a97e7a9cc6ae79f181476188b21.tar.gz openbsd-cb6e574cb86d4a97e7a9cc6ae79f181476188b21.tar.bz2 openbsd-cb6e574cb86d4a97e7a9cc6ae79f181476188b21.zip |
Simple tests for fmemopen(3) and open_memstream(3)
-rw-r--r-- | src/regress/lib/libc/fmemopen/Makefile | 5 | ||||
-rw-r--r-- | src/regress/lib/libc/fmemopen/fmemopentest.c | 68 | ||||
-rw-r--r-- | src/regress/lib/libc/open_memstream/Makefile | 5 | ||||
-rw-r--r-- | src/regress/lib/libc/open_memstream/open_memstreamtest.c | 97 |
4 files changed, 175 insertions, 0 deletions
diff --git a/src/regress/lib/libc/fmemopen/Makefile b/src/regress/lib/libc/fmemopen/Makefile new file mode 100644 index 0000000000..9cfbfaa99c --- /dev/null +++ b/src/regress/lib/libc/fmemopen/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2013/01/01 17:43:07 mpi Exp $ | ||
2 | |||
3 | PROG= fmemopentest | ||
4 | |||
5 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/fmemopen/fmemopentest.c b/src/regress/lib/libc/fmemopen/fmemopentest.c new file mode 100644 index 0000000000..ef7e9ff64f --- /dev/null +++ b/src/regress/lib/libc/fmemopen/fmemopentest.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* $OpenBSD: fmemopentest.c,v 1.1 2013/01/01 17:43:07 mpi Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <err.h> | ||
19 | #include <stdio.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <string.h> | ||
22 | #include <unistd.h> | ||
23 | |||
24 | int | ||
25 | main(void) | ||
26 | { | ||
27 | FILE *s1, *s2; | ||
28 | char c, string[] = "fmemopen test string!"; | ||
29 | char buffer[1024], *buf = NULL; | ||
30 | size_t len; | ||
31 | int failures = 0; | ||
32 | |||
33 | s1 = fmemopen(string, strlen(string), "r"); | ||
34 | if (s1 == NULL) { | ||
35 | warn("unable to open a stream s1"); | ||
36 | return (1); | ||
37 | } | ||
38 | |||
39 | s2 = fmemopen(buf, 22, "w+"); | ||
40 | if (s2 == NULL) { | ||
41 | warn("unable to create a stream s2"); | ||
42 | fclose(s1); | ||
43 | return (1); | ||
44 | } | ||
45 | |||
46 | while ((c = fgetc(s1)) != EOF) | ||
47 | fputc(c, s2); | ||
48 | |||
49 | if (ftell(s2) != strlen(string)) { | ||
50 | warnx("failed copy test (1)"); | ||
51 | failures++; | ||
52 | } | ||
53 | fclose(s1); | ||
54 | |||
55 | fseek(s2, 0, SEEK_SET); | ||
56 | if (ftell(s2) != 0) { | ||
57 | warnx("failed seek test (2)"); | ||
58 | failures++; | ||
59 | } | ||
60 | |||
61 | len = fread(buffer, 1, sizeof(buffer) - 1, s2); | ||
62 | if (len != strlen(string)) { | ||
63 | warnx("failed read test (3)"); | ||
64 | failures++; | ||
65 | } | ||
66 | |||
67 | return (failures); | ||
68 | } | ||
diff --git a/src/regress/lib/libc/open_memstream/Makefile b/src/regress/lib/libc/open_memstream/Makefile new file mode 100644 index 0000000000..1b773a403d --- /dev/null +++ b/src/regress/lib/libc/open_memstream/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2013/01/01 17:43:07 mpi Exp $ | ||
2 | |||
3 | PROG= open_memstreamtest | ||
4 | |||
5 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/open_memstream/open_memstreamtest.c b/src/regress/lib/libc/open_memstream/open_memstreamtest.c new file mode 100644 index 0000000000..09194908e9 --- /dev/null +++ b/src/regress/lib/libc/open_memstream/open_memstreamtest.c | |||
@@ -0,0 +1,97 @@ | |||
1 | /* $OpenBSD: open_memstreamtest.c,v 1.1 2013/01/01 17:43:07 mpi Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <err.h> | ||
19 | #include <stdio.h> | ||
20 | #include <stdlib.h> | ||
21 | #include <string.h> | ||
22 | #include <unistd.h> | ||
23 | |||
24 | #define OFFSET 16384 | ||
25 | |||
26 | int | ||
27 | main(void) | ||
28 | { | ||
29 | FILE *fp; | ||
30 | char *buf = (char *)0xff; | ||
31 | size_t size = 0; | ||
32 | off_t off; | ||
33 | int i = 0, failures = 0; | ||
34 | |||
35 | if ((fp = open_memstream(&buf, &size)) == NULL) { | ||
36 | warn("open_memstream failed"); | ||
37 | return (1); | ||
38 | } | ||
39 | |||
40 | off = ftello(fp); | ||
41 | if (off != 0) { | ||
42 | warnx("ftello failed. (1)"); | ||
43 | failures++; | ||
44 | } | ||
45 | |||
46 | if (fflush(fp) != 0) { | ||
47 | warnx("fflush failed. (2)"); | ||
48 | failures++; | ||
49 | } | ||
50 | |||
51 | if (size != 0) { | ||
52 | warnx("string should be empty. (3)"); | ||
53 | failures++; | ||
54 | } | ||
55 | |||
56 | if (buf == (char *)0xff) { | ||
57 | warnx("buf not updated. (4)"); | ||
58 | failures++; | ||
59 | } | ||
60 | |||
61 | if (fseek(fp, OFFSET, SEEK_SET) != 0) { | ||
62 | warnx("failed to fseek. (5)"); | ||
63 | failures++; | ||
64 | } | ||
65 | |||
66 | if (fprintf(fp, "hello") == EOF) { | ||
67 | warnx("fprintf failed. (6)"); | ||
68 | failures++; | ||
69 | } | ||
70 | |||
71 | if (fclose(fp) == EOF) { | ||
72 | warnx("fclose failed. (7)"); | ||
73 | failures++; | ||
74 | } | ||
75 | |||
76 | if (size != OFFSET + 5) { | ||
77 | warnx("failed, size %zu should be %u\n", size, OFFSET + 5); | ||
78 | failures++; | ||
79 | } | ||
80 | |||
81 | /* Needed for sparse files */ | ||
82 | while (i != OFFSET) | ||
83 | if (buf[i++] != '\0') { | ||
84 | warnx("failed, buffer non zero'ed (offset %d). (8)", i); | ||
85 | failures++; | ||
86 | break; | ||
87 | } | ||
88 | |||
89 | if (memcmp(buf + OFFSET, "hello", 5) != 0) { | ||
90 | warnx("written string incorrect. (9)"); | ||
91 | failures++; | ||
92 | } | ||
93 | |||
94 | free(buf); | ||
95 | |||
96 | return (failures); | ||
97 | } | ||