1/*
2Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions
6are met:
71. Redistributions of source code must retain the above copyright
8   notice, this list of conditions and the following disclaimer.
92. Redistributions in binary form must reproduce the above copyright
10   notice, this list of conditions and the following disclaimer in the
11   documentation and/or other materials provided with the distribution.
12
13THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23SUCH DAMAGE.
24*/
25
26#include <stdio.h>
27#include <unistd.h>
28#include <sys/stat.h>
29
30/*
31 * $FreeBSD$
32 * This program tests to make sure that wraparound writes and reads
33 * are working, assuming that 16K socket buffers are used.  In order
34 * to really stress the pipe code with this test, kernel modifications
35 * nay be necessary.
36 */
37
38int main (void)
39{
40char buffer[32768], buffer2[32768];
41int desc[2];
42int buggy, error, i, successes, total;
43struct stat status;
44pid_t new_pid;
45
46buggy = 0;
47total = 0;
48
49error = pipe(desc);
50
51if (error)
52	err(0, "Couldn't allocate fds\n");
53
54buffer[0] = 'A';
55
56for (i = 1; i < 32768; i++) {
57	buffer[i] = buffer[i - 1] + 1;
58	if (buffer[i] > 'Z')
59		buffer[i] = 'A';
60	}
61
62new_pid = fork();
63
64if (new_pid == 0) {
65	error = write(desc[1], &buffer, 4096);
66	total += error;
67	error = write(desc[1], &buffer[total], 4096);
68	total += error;
69	error = write(desc[1], &buffer[total], 4000);
70	total += error;
71	printf("Wrote %d bytes, sleeping\n", total);
72	usleep(1000000);
73	error = write(desc[1], &buffer[total], 3000);
74	total += error;
75	error = write(desc[1], &buffer[total], 3000);
76	total += error;
77	printf("Wrote another 6000 bytes, %d total, done\n", total);
78} else {
79	usleep(500000);
80	error = read(desc[0], &buffer2, 8192);
81	total += error;
82	printf("Read %d bytes, going back to sleep\n", error);
83	usleep(1000000);
84	error = read(desc[0], &buffer2[total], 16384);
85	total += error;
86	printf("Read %d bytes, done\n", error);
87
88	for (i = 0; i < total; i++) {
89		if (buffer[i] != buffer2[i]) {
90			buggy = 1;
91			printf("Location %d input: %hhx output: %hhx\n",
92					i, buffer[i], buffer2[i]);
93		}
94	}
95
96if (buggy)
97	printf("FAILURE\n");
98else
99	printf("SUCCESS\n");
100}
101
102
103}
104