xref: /openbsd-src/regress/lib/libc/fread/fread.c (revision b2d3e8feedc99a458434fbbc53260403888d7639)
1 /*
2  * Copyright (c) 2018 Todd C. Miller <millert@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/socket.h>
18 #include <sys/wait.h>
19 
20 #include <err.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 /*
27  * Test reading from a socket until EOF with multiple writes on
28  * the other end.  The send and receive buffer sizes are reduced
29  * to force multiple read(2) and write(2) calls to happen.
30  *
31  * Tests unbuffered, line buffered and fully-buffers.
32  *
33  * This test catches bugs in stdio/fread.c revs 1.13 and 1.17.
34  */
35 
36 static char test_string[] =
37 	"Now is the time for all good men to come to the aid of the party\n"
38 	"The quick brown fox jumps over the lazy dog\n"
39 	"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
40 	"Insert test text here..\n";
41 
42 static char *
iomode2str(int iomode)43 iomode2str(int iomode)
44 {
45 	switch (iomode) {
46 	case _IOFBF:
47 		return "fully buffered";
48 	case _IOLBF:
49 		return "line buffered";
50 	case _IONBF:
51 		return "unbuffered";
52 	default:
53 		return "unknown";
54 	}
55 }
56 
57 static void
dochild(int fd)58 dochild(int fd)
59 {
60 	size_t left;
61 	ssize_t nwritten;
62 	char *ts = test_string;
63 
64 	left = strlen(test_string);
65 	while (left != 0) {
66 		nwritten = write(fd, ts, left);
67 		if (nwritten == -1)
68 			err(1, "write");
69 		left -= nwritten;
70 		ts += nwritten;
71 	}
72 	close(fd);
73 	_exit(0);
74 }
75 
76 int
dotest(int iomode,char * iobuf,size_t iolen)77 dotest(int iomode, char *iobuf, size_t iolen)
78 {
79     char *ts = test_string;
80     size_t nread, total = 0, off = 0;
81     int sv[2], val;
82     char buf[21];
83     pid_t child;
84     FILE *fp;
85 
86     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1)
87 	    err(1, "socketpair");
88     val = 16;
89     if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1)
90 	    err(1, "setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF)");
91     if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
92 	    err(1, "setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF)");
93     if (setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1)
94 	    err(1, "setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF)");
95     if (setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
96 	    err(1, "setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF)");
97 
98     if ((fp = fdopen(sv[0], "r")) == NULL)
99 	    err(1, "fdopen");
100 
101     setvbuf(fp, iobuf, iomode, iolen);
102 
103     switch ((child = fork())) {
104     case -1:
105 	    err(1, "fork");
106     case 0:
107 	    close(sv[0]);
108 	    dochild(sv[1]);
109     default:
110 	    close(sv[1]);
111 	    break;
112     }
113 
114     while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) {
115 	    if (nread > sizeof(buf)) {
116 		    warnx("%s: max %zu bytes but got %zu",
117 			iomode2str(iomode), sizeof(buf), nread);
118 		    return 1;
119 	    }
120 	    if (strncmp(buf, test_string + off, nread) != 0) {
121 		    warnx("%s: mismatch: expected %.*s, got %.*s",
122 			iomode2str(iomode), (int)nread, test_string + off,
123 			(int)nread, buf);
124 		    return 1;
125 	    }
126 	    total += nread;
127 	    off += nread;
128     }
129     if (!feof(fp)) {
130 	    if (ferror(fp))
131 		    warn("%s: read error", iomode2str(iomode));
132 	    else
133 		    warnx("%s: missing EOF", iomode2str(iomode));
134 	    return 1;
135     }
136     fclose(fp);
137     waitpid(child, NULL, 0);
138 
139     return 0;
140 }
141 
142 int
main(int argc,char * argv[])143 main(int argc, char *argv[])
144 {
145     char iobuf[4096];
146     int errors = 0;
147 
148     errors += dotest(_IOFBF, iobuf, sizeof(iobuf));
149     errors += dotest(_IOLBF, iobuf, sizeof(iobuf));
150     errors += dotest(_IONBF, NULL, 0);
151 
152     return errors;
153 }
154