Home | History | Annotate | Line # | Download | only in 9994
9994_f.c revision 1.2
      1  1.2     soren /*	$NetBSD: 9994_f.c,v 1.2 2002/08/09 10:04:37 soren Exp $	*/
      2  1.1  perseant 
      3  1.1  perseant /*-
      4  1.1  perseant  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  perseant  * All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  perseant  * by IWAMOTO Toshihiro <iwamoto (at) sat.t.u-tokyo.ac.jp> and Konrad E. Schroder
      9  1.1  perseant  * <perseant (at) hhhh.org>.
     10  1.1  perseant  *
     11  1.1  perseant  * Redistribution and use in source and binary forms, with or without
     12  1.1  perseant  * modification, are permitted provided that the following conditions
     13  1.1  perseant  * are met:
     14  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     15  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     16  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     19  1.1  perseant  * 3. All advertising materials mentioning features or use of this software
     20  1.1  perseant  *    must display the following acknowledgement:
     21  1.1  perseant  *      This product includes software developed by the NetBSD
     22  1.1  perseant  *      Foundation, Inc. and its contributors.
     23  1.1  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  perseant  *    contributors may be used to endorse or promote products derived
     25  1.1  perseant  *    from this software without specific prior written permission.
     26  1.1  perseant  *
     27  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  perseant  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  perseant  */
     39  1.1  perseant 
     40  1.1  perseant /*
     41  1.1  perseant  * Regression test based on test program submutted with NetBSD PR #9994.
     42  1.1  perseant  * Two files are opened, one in the current directory and another in a
     43  1.1  perseant  * control directory (in another filesystem).  Random read, write, and
     44  1.1  perseant  * truncate operations are performed on both files.
     45  1.1  perseant  *
     46  1.1  perseant  * An error return indicates that an operation failed on one of the files
     47  1.1  perseant  * (control *or* test, either one).
     48  1.1  perseant  *
     49  1.1  perseant  * A zero return indicates success; the two files should be identical.
     50  1.1  perseant  * (This program does not test to make sure that they are identical.)
     51  1.1  perseant  */
     52  1.1  perseant 
     53  1.1  perseant #include <fcntl.h>
     54  1.1  perseant #include <stdio.h>
     55  1.1  perseant #include <stdlib.h>
     56  1.1  perseant #include <time.h>
     57  1.1  perseant #include <sys/types.h>
     58  1.1  perseant #include <unistd.h>
     59  1.1  perseant 
     60  1.1  perseant #define CONTROL_ERROR 1
     61  1.1  perseant #define TEST_ERROR    2
     62  1.1  perseant 
     63  1.1  perseant int main(int argc, char **argv)
     64  1.1  perseant {
     65  1.1  perseant 	int bsize, quiet, usepid;
     66  1.1  perseant 	char *hoge, *buf;
     67  1.1  perseant 	char prop[] = "/\b-\b\\\b|\b";
     68  1.1  perseant 	int testfd, controlfd;
     69  1.1  perseant 	int c, i, j, k, n, p;
     70  1.1  perseant 	char *ctldir;
     71  1.1  perseant 	unsigned long rseed;
     72  1.1  perseant 	const char *prog = getprogname();
     73  1.1  perseant 
     74  1.1  perseant 	bsize = 8192;
     75  1.1  perseant 	n = 10000;
     76  1.1  perseant 	ctldir = "/var/tmp";
     77  1.1  perseant 	quiet = 0;
     78  1.1  perseant 	usepid = 0;
     79  1.1  perseant 	rseed = time(0);
     80  1.1  perseant 
     81  1.1  perseant 	while ((c = getopt(argc, argv, "b:c:n:pqs:")) != -1) {
     82  1.1  perseant 		switch(c) {
     83  1.1  perseant 		    case 'b':
     84  1.1  perseant 			    bsize = atoi(optarg);
     85  1.1  perseant 			    break;
     86  1.1  perseant 		    case 'c':
     87  1.1  perseant 			    ctldir = optarg;
     88  1.1  perseant 			    break;
     89  1.1  perseant 		    case 'n':
     90  1.1  perseant 			    n = atoi(optarg);
     91  1.1  perseant 			    break;
     92  1.1  perseant 		    case 'p':
     93  1.1  perseant 			    ++usepid;
     94  1.1  perseant 			    break;
     95  1.1  perseant 		    case 'q':
     96  1.1  perseant 			    ++quiet;
     97  1.1  perseant 			    break;
     98  1.1  perseant 		    case 's':
     99  1.1  perseant 			    rseed = strtoul(optarg, NULL, 0);
    100  1.1  perseant 			    break;
    101  1.1  perseant 		    default:
    102  1.2     soren 			    errx(1, "usage: %s [-b bsize] [-c control-dir] [-n count] [-pq] [-s randseed]", prog);
    103  1.1  perseant 			    /* NOTREACHED */
    104  1.1  perseant 		}
    105  1.1  perseant 	}
    106  1.1  perseant 
    107  1.1  perseant 	srandom(rseed);
    108  1.1  perseant 	hoge = (char *)malloc(bsize);
    109  1.1  perseant 	buf = (char *)malloc(bsize);
    110  1.1  perseant 
    111  1.1  perseant 	for(i = 0; i < bsize; i++)
    112  1.1  perseant 		hoge[i] = random() & 0xff;
    113  1.1  perseant 
    114  1.1  perseant 	/* Set up test and control file descriptors */
    115  1.1  perseant 
    116  1.1  perseant 	if (usepid)
    117  1.1  perseant 		sprintf(buf, "test.%d", getpid());
    118  1.1  perseant 	else
    119  1.1  perseant 		sprintf(buf, "test");
    120  1.1  perseant 	unlink(buf);
    121  1.1  perseant 	testfd = open(buf, O_RDWR | O_CREAT, 0644);
    122  1.1  perseant 	if (testfd < 0) {
    123  1.1  perseant 		perror("open");
    124  1.1  perseant 		exit(TEST_ERROR);
    125  1.1  perseant 	}
    126  1.1  perseant 
    127  1.1  perseant 	if (usepid)
    128  1.1  perseant 		sprintf(buf, "%s/control.%d", ctldir, getpid());
    129  1.1  perseant 	else
    130  1.1  perseant 		sprintf(buf, "%s/control", ctldir);
    131  1.1  perseant 	unlink(buf);
    132  1.1  perseant 	controlfd = open(buf, O_RDWR | O_CREAT, 0644);
    133  1.1  perseant 	if (controlfd < 0) {
    134  1.1  perseant 		perror("open");
    135  1.1  perseant 		exit(CONTROL_ERROR);
    136  1.1  perseant 	}
    137  1.1  perseant 
    138  1.1  perseant 	for(i = 0, p = 0; i < n; i++) {
    139  1.1  perseant 		if (!quiet) {
    140  1.1  perseant 			/* Draw propeller */
    141  1.1  perseant 			p = (p + 1) % 4;
    142  1.1  perseant 			write(1, prop + 2 * p, 2);
    143  1.1  perseant 		}
    144  1.1  perseant 
    145  1.1  perseant 		j = random() % 3;
    146  1.1  perseant 		k = random() & 0x3fffff;
    147  1.1  perseant 		if (j != 2) {
    148  1.1  perseant 			if (lseek(testfd, k, SEEK_SET) < 0) {
    149  1.1  perseant 				perror("read");
    150  1.1  perseant 				exit(TEST_ERROR);
    151  1.1  perseant 			}
    152  1.1  perseant 			if (lseek(controlfd, k, SEEK_SET) < 0) {
    153  1.1  perseant 				perror("read");
    154  1.1  perseant 				exit(CONTROL_ERROR);
    155  1.1  perseant 			}
    156  1.1  perseant 		}
    157  1.1  perseant 
    158  1.1  perseant 		switch(j) {
    159  1.1  perseant 		    case 0:
    160  1.1  perseant 			    if (read(testfd, buf, bsize) < 0) {
    161  1.1  perseant 				    perror("read");
    162  1.1  perseant 				    exit(TEST_ERROR);
    163  1.1  perseant 			    }
    164  1.1  perseant 			    if (read(controlfd, buf, bsize) < 0) {
    165  1.1  perseant 				    perror("read");
    166  1.1  perseant 				    exit(CONTROL_ERROR);
    167  1.1  perseant 			    }
    168  1.1  perseant 			    break;
    169  1.1  perseant 		    case 1:
    170  1.1  perseant 			    if (write(testfd, hoge, bsize) < 0) {
    171  1.1  perseant 				    perror("write");
    172  1.1  perseant 				    exit(TEST_ERROR);
    173  1.1  perseant 			    }
    174  1.1  perseant 			    if (write(controlfd, hoge, bsize) < 0) {
    175  1.1  perseant 				    perror("write");
    176  1.1  perseant 				    exit(CONTROL_ERROR);
    177  1.1  perseant 			    }
    178  1.1  perseant 			    break;
    179  1.1  perseant 		    case 2:
    180  1.1  perseant 			    if (ftruncate(testfd, k) < 0) {
    181  1.1  perseant 				    perror("write");
    182  1.1  perseant 				    exit(TEST_ERROR);
    183  1.1  perseant 			    }
    184  1.1  perseant 			    if (ftruncate(controlfd, k) < 0) {
    185  1.1  perseant 				    perror("write");
    186  1.1  perseant 				    exit(CONTROL_ERROR);
    187  1.1  perseant 			    }
    188  1.1  perseant 			    break;
    189  1.1  perseant 		}
    190  1.1  perseant 		j = random() % (100 * 1000);
    191  1.1  perseant 		usleep(j);
    192  1.1  perseant 	}
    193  1.1  perseant 	exit(0);
    194  1.1  perseant }
    195  1.1  perseant 
    196