]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/pipe/pipe-reverse.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / pipe / pipe-reverse.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 simply tests writing through the reverse direction of
33  * a pipe.  Nothing too fancy, it's only needed because most pipe-using
34  * programs never touch the reverse direction (it doesn't exist on
35  * Linux.)
36  */
37
38 int main (void)
39 {
40 char buffer[65535], buffer2[65535];
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 < 65535; 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[0], &buffer, 4096);
66         total += error;
67         error = write(desc[0], &buffer[total], 4096);
68         total += error;
69         error = write(desc[0], &buffer[total], 4096);
70         total += error;
71         error = write(desc[0], &buffer[total], 4096);
72         total += error;
73         error = write(desc[0], &buffer[total], 4096);
74         total += error;
75         error = write(desc[0], &buffer[total], 4096);
76         total += error;
77         error = write(desc[0], &buffer[total], 4096);
78         total += error;
79         error = write(desc[0], &buffer[total], 4096);
80         total += error;
81         printf("Wrote %d bytes, sleeping\n", total);
82         usleep(1000000);
83         error = write(desc[0], &buffer[total], 4096);
84         total += error;
85         error = write(desc[0], &buffer[total], 4096);
86         total += error;
87         printf("Wrote another 8192 bytes, %d total, done\n", total);
88 } else {
89         usleep(500000);
90         error = read(desc[1], &buffer2, 32768);
91         total += error;
92         printf("Read %d bytes, going back to sleep\n", error);
93         usleep(1000000);
94         error = read(desc[1], &buffer2[total], 8192);
95         total += error;
96         printf("Read %d bytes, done\n", error);
97
98         for (i = 0; i < total; i++) {
99                 if (buffer[i] != buffer2[i]) {
100                         buggy = 1;
101                         printf("Location %d input: %hhx output: %hhx\n",
102                                         i, buffer[i], buffer2[i]);
103                 }
104         }
105
106 if ((buggy == 1) || (total != 40960))
107         printf("FAILURE\n");
108 else
109         printf("SUCCESS\n");
110
111 }
112
113 }