summaryrefslogtreecommitdiff
path: root/regress/lib/libc/fmemopen/fmemopentest.c
blob: 203c93be6508f0257b8858976f3b6c8d12321c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*	$OpenBSD: fmemopentest.c,v 1.3 2013/03/28 09:35:58 mpi Exp $ */

/*
 * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
simpletest(void)
{
	FILE	*s1, *s2;
	char	 string[] = "fmemopen test string!";
	char	 buffer[1024], *buf = NULL;
	size_t	 len;
	int	 c, failures = 0;

	s1 = fmemopen(string, strlen(string) + 1, "r");
	if (s1 == NULL) {
		warn("unable to open a stream s1");
		return (1);
	}

	s2 = fmemopen(buf, 22, "w+");
	if (s2 == NULL) {
		warn("unable to create a stream s2");
		fclose(s1);
		return (1);
	}

	while ((c = fgetc(s1)) != EOF)
		fputc(c, s2);

	if (ftell(s2) != strlen(string) + 1) {
		warnx("failed copy test (1)");
		failures++;
	}
	fclose(s1);

	fseek(s2, 0, SEEK_SET);
	if (ftell(s2) != 0) {
		warnx("failed seek test (2)");
		failures++;
	}

	len = fread(buffer, 1, sizeof(buffer) - 1, s2);
	if (len != strlen(string) + 1) {
		warnx("failed read test (3) %zu != %zu",
		    len, strlen(string) + 1);
		failures++;
	}

	return (failures);
}

int
updatetest(void)
{
	FILE	*s1;
	char	 string[] = "hello\0 test number 2";
	char	 buffer[256];
	size_t	 len;
	int	 failures = 0;

	s1 = fmemopen(string, 19, "a+");
	if (s1 == NULL)
		return (1);

	len = fwrite(" world", 1, 6, s1);
	if (len != 6) {
		warnx("failed write test (4)");
		failures++;
	}

	fseek(s1, 0, SEEK_SET);
	if (ftell(s1) != 0) {
		warnx("failed seek test (5)");
		failures++;
	}

	len = fread(buffer, 1, sizeof(buffer) - 1, s1);
	if (strncmp(string, buffer, len)) {
		warnx("failed compare test (6)");
		failures++;
	}

	if (strcmp(string, "hello world")) {
		warnx("failed compare test (7)");
		failures++;
	}

	if (strcmp(string + strlen(string) + 1, "number 2")) {
		warnx("failed compare test (8)");
		failures++;
	}

	return (failures);
}

int
writetest(void)
{
	FILE	*s1;
	char	 string[] = "super test number 3";
	char	 buffer[256];
	size_t	 len, slen;
	int	 failures = 0;

	slen = strlen(string) + 1;

	s1 = fmemopen(string, slen, "w");
	if (s1 == NULL)
		return (1);

	len = fwrite("short", 1, 5, s1);
	if (len != strlen("short")) {
		warnx("failed write test (9)");
		failures++;
	}
	fclose(s1);

	s1 = fmemopen(string, slen, "r");
	if (s1 == NULL) {
		warnx("failed open test (10)");
		failures++;
	}

	len = fread(buffer, 1, sizeof(buffer) - 1, s1);
	if (strncmp(string, buffer, len)) {
		warnx("failed compare test (11)");
		failures++;
	}

	if (strcmp(string, "short")) {
		warnx("failed compare test (12)");
		failures++;
	}

	if (strcmp(string + strlen(string) + 1, "test number 3")) {
		warnx("failed compare test (13)");
		failures++;
	}

	return (failures);
}

int
seektest(void)
{
	FILE	*s1;
	char	 string[] = "long string for testing seek";
	size_t	 len, slen;
	int	 failures = 0;

	slen = strlen(string) + 1;

	s1 = fmemopen(string, slen, "r");
	if (s1 == NULL)
		return (1);

	if (fseek(s1, 8, SEEK_SET) != 0) {
		warnx("failed to fseek. (14)");
		failures++;
	}

	if (ftell(s1) != 8) {
		warnx("failed seek test. (15)");
		failures++;
	}

	/* Try to seek backward */
	if (fseek(s1, -1, SEEK_CUR) != 0) {
		warnx("failed to fseek. (16)");
		failures++;
	}

	if (ftell(s1) != 7) {
		warnx("failed seeking backward. (17)");
		failures++;
	}

	return (failures);
}

int
main(void)
{
	int	 failures = 0;

	failures += simpletest();
	failures += updatetest();
	failures += writetest();
	failures += seektest();

 	return (failures);
}