]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - tools/regression/pipe/pipe-wraparound.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / tools / regression / pipe / pipe-wraparound.c
1 /*
2 Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
3  
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 2. 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
13 THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 SUCH 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
38 int main (void)
39 {
40 char buffer[32768], buffer2[32768];
41 int desc[2];
42 int buggy, error, i, successes, total;
43 struct stat status;
44 pid_t new_pid;
45
46 buggy = 0;
47 total = 0;
48
49 error = pipe(desc);
50
51 if (error)
52         err(0, "Couldn't allocate fds\n");
53
54 buffer[0] = 'A';
55
56 for (i = 1; i < 32768; i++) {
57         buffer[i] = buffer[i - 1] + 1;
58         if (buffer[i] > 'Z')
59                 buffer[i] = 'A';
60         }
61
62 new_pid = fork();
63
64 if (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
96 if (buggy)
97         printf("FAILURE\n");
98 else
99         printf("SUCCESS\n");
100 }
101
102
103 }