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