pipe_reverse_test.c revision 132524
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: head/tools/regression/pipe/pipe-reverse.c 132524 2004-07-22 02:46:25Z silby $ 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 38int main (void) 39{ 40char buffer[65535], buffer2[65535]; 41int desc[2]; 42int buggy, error, i, successes, total; 43struct stat status; 44pid_t new_pid; 45 46buggy = 0; 47 48error = pipe(desc); 49 50if (error) 51 err(0, "Couldn't allocate fds\n"); 52 53buffer[0] = 'A'; 54 55for (i = 0; i < 65535; i++) { 56 buffer[i] = buffer[i - 1] + 1; 57 if (buffer[i] > 'Z') 58 buffer[i] = 'A'; 59 } 60 61new_pid = fork(); 62 63if (new_pid == 0) { 64 error = write(desc[0], &buffer, 4096); 65 total += error; 66 error = write(desc[0], &buffer[total], 4096); 67 total += error; 68 error = write(desc[0], &buffer[total], 4096); 69 total += error; 70 error = write(desc[0], &buffer[total], 4096); 71 total += error; 72 error = write(desc[0], &buffer[total], 4096); 73 total += error; 74 error = write(desc[0], &buffer[total], 4096); 75 total += error; 76 error = write(desc[0], &buffer[total], 4096); 77 total += error; 78 error = write(desc[0], &buffer[total], 4096); 79 total += error; 80 printf("Wrote %d bytes, sleeping\n", total); 81 usleep(1000000); 82 error = write(desc[0], &buffer[total], 4096); 83 total += error; 84 error = write(desc[0], &buffer[total], 4096); 85 total += error; 86 printf("Wrote another 8192 bytes, %d total, done\n", total); 87} else { 88 usleep(500000); 89 error = read(desc[1], &buffer2, 32768); 90 total += error; 91 printf("Read %d bytes, going back to sleep\n", error); 92 usleep(1000000); 93 error = read(desc[1], &buffer2[total], 8192); 94 total += error; 95 printf("Read %d bytes, done\n", error); 96 97 for (i = 0; i < total; i++) { 98 if (buffer[i] != buffer2[i]) { 99 buggy = 1; 100 printf("Location %d input: %hhx output: %hhx\n", 101 i, buffer[i], buffer2[i]); 102 } 103 } 104 105if ((buggy == 1) || (total != 40960)) 106 printf("FAILURE\n"); 107else 108 printf("SUCCESS\n"); 109 110} 111 112} 113