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