Home | History | Annotate | Line # | Download | only in 9994
      1  1.3    martin /*	$NetBSD: 9994_f.c,v 1.3 2008/04/28 20:23:06 martin 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  *
     20  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  perseant  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  perseant  */
     32  1.1  perseant 
     33  1.1  perseant /*
     34  1.1  perseant  * Regression test based on test program submutted with NetBSD PR #9994.
     35  1.1  perseant  * Two files are opened, one in the current directory and another in a
     36  1.1  perseant  * control directory (in another filesystem).  Random read, write, and
     37  1.1  perseant  * truncate operations are performed on both files.
     38  1.1  perseant  *
     39  1.1  perseant  * An error return indicates that an operation failed on one of the files
     40  1.1  perseant  * (control *or* test, either one).
     41  1.1  perseant  *
     42  1.1  perseant  * A zero return indicates success; the two files should be identical.
     43  1.1  perseant  * (This program does not test to make sure that they are identical.)
     44  1.1  perseant  */
     45  1.1  perseant 
     46  1.1  perseant #include <fcntl.h>
     47  1.1  perseant #include <stdio.h>
     48  1.1  perseant #include <stdlib.h>
     49  1.1  perseant #include <time.h>
     50  1.1  perseant #include <sys/types.h>
     51  1.1  perseant #include <unistd.h>
     52  1.1  perseant 
     53  1.1  perseant #define CONTROL_ERROR 1
     54  1.1  perseant #define TEST_ERROR    2
     55  1.1  perseant 
     56  1.1  perseant int main(int argc, char **argv)
     57  1.1  perseant {
     58  1.1  perseant 	int bsize, quiet, usepid;
     59  1.1  perseant 	char *hoge, *buf;
     60  1.1  perseant 	char prop[] = "/\b-\b\\\b|\b";
     61  1.1  perseant 	int testfd, controlfd;
     62  1.1  perseant 	int c, i, j, k, n, p;
     63  1.1  perseant 	char *ctldir;
     64  1.1  perseant 	unsigned long rseed;
     65  1.1  perseant 	const char *prog = getprogname();
     66  1.1  perseant 
     67  1.1  perseant 	bsize = 8192;
     68  1.1  perseant 	n = 10000;
     69  1.1  perseant 	ctldir = "/var/tmp";
     70  1.1  perseant 	quiet = 0;
     71  1.1  perseant 	usepid = 0;
     72  1.1  perseant 	rseed = time(0);
     73  1.1  perseant 
     74  1.1  perseant 	while ((c = getopt(argc, argv, "b:c:n:pqs:")) != -1) {
     75  1.1  perseant 		switch(c) {
     76  1.1  perseant 		    case 'b':
     77  1.1  perseant 			    bsize = atoi(optarg);
     78  1.1  perseant 			    break;
     79  1.1  perseant 		    case 'c':
     80  1.1  perseant 			    ctldir = optarg;
     81  1.1  perseant 			    break;
     82  1.1  perseant 		    case 'n':
     83  1.1  perseant 			    n = atoi(optarg);
     84  1.1  perseant 			    break;
     85  1.1  perseant 		    case 'p':
     86  1.1  perseant 			    ++usepid;
     87  1.1  perseant 			    break;
     88  1.1  perseant 		    case 'q':
     89  1.1  perseant 			    ++quiet;
     90  1.1  perseant 			    break;
     91  1.1  perseant 		    case 's':
     92  1.1  perseant 			    rseed = strtoul(optarg, NULL, 0);
     93  1.1  perseant 			    break;
     94  1.1  perseant 		    default:
     95  1.2     soren 			    errx(1, "usage: %s [-b bsize] [-c control-dir] [-n count] [-pq] [-s randseed]", prog);
     96  1.1  perseant 			    /* NOTREACHED */
     97  1.1  perseant 		}
     98  1.1  perseant 	}
     99  1.1  perseant 
    100  1.1  perseant 	srandom(rseed);
    101  1.1  perseant 	hoge = (char *)malloc(bsize);
    102  1.1  perseant 	buf = (char *)malloc(bsize);
    103  1.1  perseant 
    104  1.1  perseant 	for(i = 0; i < bsize; i++)
    105  1.1  perseant 		hoge[i] = random() & 0xff;
    106  1.1  perseant 
    107  1.1  perseant 	/* Set up test and control file descriptors */
    108  1.1  perseant 
    109  1.1  perseant 	if (usepid)
    110  1.1  perseant 		sprintf(buf, "test.%d", getpid());
    111  1.1  perseant 	else
    112  1.1  perseant 		sprintf(buf, "test");
    113  1.1  perseant 	unlink(buf);
    114  1.1  perseant 	testfd = open(buf, O_RDWR | O_CREAT, 0644);
    115  1.1  perseant 	if (testfd < 0) {
    116  1.1  perseant 		perror("open");
    117  1.1  perseant 		exit(TEST_ERROR);
    118  1.1  perseant 	}
    119  1.1  perseant 
    120  1.1  perseant 	if (usepid)
    121  1.1  perseant 		sprintf(buf, "%s/control.%d", ctldir, getpid());
    122  1.1  perseant 	else
    123  1.1  perseant 		sprintf(buf, "%s/control", ctldir);
    124  1.1  perseant 	unlink(buf);
    125  1.1  perseant 	controlfd = open(buf, O_RDWR | O_CREAT, 0644);
    126  1.1  perseant 	if (controlfd < 0) {
    127  1.1  perseant 		perror("open");
    128  1.1  perseant 		exit(CONTROL_ERROR);
    129  1.1  perseant 	}
    130  1.1  perseant 
    131  1.1  perseant 	for(i = 0, p = 0; i < n; i++) {
    132  1.1  perseant 		if (!quiet) {
    133  1.1  perseant 			/* Draw propeller */
    134  1.1  perseant 			p = (p + 1) % 4;
    135  1.1  perseant 			write(1, prop + 2 * p, 2);
    136  1.1  perseant 		}
    137  1.1  perseant 
    138  1.1  perseant 		j = random() % 3;
    139  1.1  perseant 		k = random() & 0x3fffff;
    140  1.1  perseant 		if (j != 2) {
    141  1.1  perseant 			if (lseek(testfd, k, SEEK_SET) < 0) {
    142  1.1  perseant 				perror("read");
    143  1.1  perseant 				exit(TEST_ERROR);
    144  1.1  perseant 			}
    145  1.1  perseant 			if (lseek(controlfd, k, SEEK_SET) < 0) {
    146  1.1  perseant 				perror("read");
    147  1.1  perseant 				exit(CONTROL_ERROR);
    148  1.1  perseant 			}
    149  1.1  perseant 		}
    150  1.1  perseant 
    151  1.1  perseant 		switch(j) {
    152  1.1  perseant 		    case 0:
    153  1.1  perseant 			    if (read(testfd, buf, bsize) < 0) {
    154  1.1  perseant 				    perror("read");
    155  1.1  perseant 				    exit(TEST_ERROR);
    156  1.1  perseant 			    }
    157  1.1  perseant 			    if (read(controlfd, buf, bsize) < 0) {
    158  1.1  perseant 				    perror("read");
    159  1.1  perseant 				    exit(CONTROL_ERROR);
    160  1.1  perseant 			    }
    161  1.1  perseant 			    break;
    162  1.1  perseant 		    case 1:
    163  1.1  perseant 			    if (write(testfd, hoge, bsize) < 0) {
    164  1.1  perseant 				    perror("write");
    165  1.1  perseant 				    exit(TEST_ERROR);
    166  1.1  perseant 			    }
    167  1.1  perseant 			    if (write(controlfd, hoge, bsize) < 0) {
    168  1.1  perseant 				    perror("write");
    169  1.1  perseant 				    exit(CONTROL_ERROR);
    170  1.1  perseant 			    }
    171  1.1  perseant 			    break;
    172  1.1  perseant 		    case 2:
    173  1.1  perseant 			    if (ftruncate(testfd, k) < 0) {
    174  1.1  perseant 				    perror("write");
    175  1.1  perseant 				    exit(TEST_ERROR);
    176  1.1  perseant 			    }
    177  1.1  perseant 			    if (ftruncate(controlfd, k) < 0) {
    178  1.1  perseant 				    perror("write");
    179  1.1  perseant 				    exit(CONTROL_ERROR);
    180  1.1  perseant 			    }
    181  1.1  perseant 			    break;
    182  1.1  perseant 		}
    183  1.1  perseant 		j = random() % (100 * 1000);
    184  1.1  perseant 		usleep(j);
    185  1.1  perseant 	}
    186  1.1  perseant 	exit(0);
    187  1.1  perseant }
    188  1.1  perseant 
    189