Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / usd / test / usd_test.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10
11 /* usd_test.c: Tests the usd library by opening a tape device,
12 * writing a few blocks of data to it, doing a fsf, bsf
13 */
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <afs/usd.h>
21 #ifdef AFS_NT40_ENV
22 #include <windows.h>
23 #include <winbase.h>
24 #endif
25
26 static char *whoami;
27 usd_handle_t hTape;
28
29 #define USDTEST_DEBUG 0 /* set to 1, to print tape pos info */
30
31 static int err_exit(char *action, char *msg, int rcode);
32 static int bufeql(char *buf1, char *buf2, int size);
33 static int Rewind(usd_handle_t hTape);
34 static int WriteEOF(usd_handle_t hTape, int cnt);
35 static int ForwardSpace(usd_handle_t hTape, int cnt);
36 static int BackSpace(usd_handle_t hTape, int cnt);
37 static int PrepTape(usd_handle_t hTape);
38 static int ShutdownTape(usd_handle_t hTape);
39 static afs_int64 PrintTapePos(usd_handle_t hTape);
40
41 int
42 main(int argc, char **argv)
43 {
44 int rcode;
45 afs_uint32 xferd;
46 char *filename = argv[1];
47 char buf1[1024], buf2[1024];
48 int i;
49
50 whoami = argv[0];
51 if (argc < 2) {
52 printf(" Usage: %s <tape device name>\n", whoami);
53 exit(1);
54 }
55 rcode =
56 usd_Open(filename, (USD_OPEN_RDWR | USD_OPEN_WLOCK), 0777, &hTape);
57
58 if (rcode) {
59 printf("%s:Can't open device. Err %d\n", whoami, rcode);
60 return 1;
61 }
62 PrepTape(hTape);
63
64 Rewind(hTape);
65 Rewind(hTape);
66 Rewind(hTape);
67 PrintTapePos(hTape);
68 /* Write test - write 10 blocks of random data */
69 printf("%s: Beginning Write Test\n", whoami);
70 i = 10;
71 for (i = 0; i < 10; i++) {
72 rcode = USD_WRITE(hTape, buf1, 1024, &xferd);
73 if (rcode || xferd != 1024) {
74 printf("%s: Write test failed. Err %d\n", whoami, rcode);
75 return 1;
76 }
77 PrintTapePos(hTape);
78 }
79
80 WriteEOF(hTape, 1);
81
82 PrintTapePos(hTape);
83 printf("%s: Write Test Passed.\n", whoami);
84
85 Rewind(hTape);
86 printf("%s: Rewind Test passed\n", whoami);
87 PrintTapePos(hTape);
88
89 /* Read test - read the 10 blocks written */
90 printf("%s: Beginning read test\n", whoami);
91 for (i = 0; i < 10; i++) {
92 rcode = USD_READ(hTape, buf2, 1024, &xferd);
93 err_exit("read", "Read Test Failed", rcode);
94 PrintTapePos(hTape);
95 if (xferd != 1024)
96 err_exit("read", "Read Test Failed. Wrong no. of bytes read.", 1);
97 if (bufeql(buf1, buf2, 1024) == 0) {
98 printf("%s: Read test failed\n", whoami);
99 exit(1);
100 }
101 }
102 printf("%s: Read Test passed\n", whoami);
103
104 Rewind(hTape);
105 PrintTapePos(hTape);
106 /* WEOF - Test */
107 for (i = 0; i < 5; i++) {
108 /* write data block */
109 *buf1 = i;
110 rcode = USD_WRITE(hTape, buf1, 1024, &xferd);
111 err_exit("Write", "Write EOF Test Failed", rcode);
112
113 /* write EOF mark */
114 rcode = WriteEOF(hTape, 1);
115 err_exit("WEOF", "Write EOF Test Failed", rcode);
116 PrintTapePos(hTape);
117 }
118 printf("%s: WEOF Test passed\n", whoami);
119
120 Rewind(hTape);
121 /* fsf/bsf test */
122 ForwardSpace(hTape, 2);
123 PrintTapePos(hTape);
124 rcode = USD_READ(hTape, buf2, 1024, &xferd);
125 err_exit("read", "FSF Test Failed", rcode);
126 if (*buf2 != 2)
127 err_exit("FSF", "FSF Test Failed", 1);
128
129 ForwardSpace(hTape, 2);
130 PrintTapePos(hTape);
131 rcode = USD_READ(hTape, buf2, 1024, &xferd);
132 err_exit("read", "FSF Test Failed", rcode);
133 if (*buf2 != 4)
134 err_exit("FSF", "FSF Test Failed", 1);
135
136 printf("%s: FSF Test passed\n", whoami);
137
138 BackSpace(hTape, 4);
139 ForwardSpace(hTape, 1);
140 rcode = USD_READ(hTape, buf2, 1024, &xferd);
141 err_exit("read", "FSF Test Failed", rcode);
142 if (*buf2 != 1)
143 err_exit("BSF", "FSF Test Failed", 1);
144
145 printf("%s: BSF Test Passed\n", whoami);
146
147 ShutdownTape(hTape);
148 rcode = USD_CLOSE(hTape);
149 if (rcode) {
150 printf("%s:Can't close device. Err %d\n", whoami, rcode);
151 return 1;
152 }
153 printf("%s: usd library, all tests passed!\n", whoami);
154
155 return 0;
156 }
157
158
159 static int
160 err_exit(char *action, char *msg, int rcode)
161 {
162 if (!rcode)
163 return 0;
164 printf("%s:Can't %s device. Err %d\n %s \n", whoami, action, rcode, msg);
165
166 /* Now shutdown and close the tape */
167 ShutdownTape(hTape);
168 rcode = USD_CLOSE(hTape);
169 if (rcode) {
170 printf("%s:Can't close device. Err %d\n", whoami, rcode);
171 return 1;
172 }
173 exit(1);
174 }
175
176
177 static int
178 bufeql(char *buf1, char *buf2, int size)
179 {
180 while (size--) {
181 if (*buf1 != *buf2)
182 return 0;
183
184 ++buf1;
185 ++buf2;
186 }
187 return 1;
188 }
189
190
191 static int
192 Rewind(usd_handle_t hTape)
193 {
194 usd_tapeop_t tapeop;
195 int rcode;
196
197 tapeop.tp_op = USDTAPE_REW;
198 tapeop.tp_count = 1;
199 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
200 err_exit("rewind", "Rewind Test Failed", rcode);
201 return 0;
202 }
203
204 static int
205 WriteEOF(usd_handle_t hTape, int cnt)
206 {
207 usd_tapeop_t tapeop;
208 int rcode;
209
210 tapeop.tp_op = USDTAPE_WEOF;
211 tapeop.tp_count = cnt;
212 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
213 err_exit("rewind", "Rewind Test Failed", rcode);
214 return 0;
215 }
216
217 static int
218 ForwardSpace(usd_handle_t hTape, int cnt)
219 {
220 usd_tapeop_t tapeop;
221 int rcode;
222
223 tapeop.tp_op = USDTAPE_FSF;
224 tapeop.tp_count = cnt;
225 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
226 err_exit("FSF", "FSF Test Failed", rcode);
227 return 0;
228 }
229
230 static int
231 BackSpace(usd_handle_t hTape, int cnt)
232 {
233 usd_tapeop_t tapeop;
234 int rcode;
235
236 tapeop.tp_op = USDTAPE_BSF;
237 tapeop.tp_count = cnt;
238 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
239 err_exit("BSF", "BSF Test Failed", rcode);
240 return 0;
241 }
242
243 static int
244 PrepTape(usd_handle_t hTape)
245 {
246 usd_tapeop_t tapeop;
247 int rcode;
248
249 tapeop.tp_op = USDTAPE_PREPARE;
250 tapeop.tp_count = 0;
251 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
252 err_exit("PREPARE", "Tape Prepare Test Failed", rcode);
253 return 0;
254 }
255
256
257 static int
258 ShutdownTape(usd_handle_t hTape)
259 {
260 usd_tapeop_t tapeop;
261 int rcode;
262
263 tapeop.tp_op = USDTAPE_SHUTDOWN;
264 tapeop.tp_count = 0;
265 rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
266 if (rcode) { /* can't call err_exit() here for fear of recursion */
267 printf("%s: Tape Shutdown Failed with code %d\n", whoami, rcode);
268 exit(1);
269 }
270 return 0;
271 }
272
273 #ifdef AFS_NT40_ENV
274 static afs_int64
275 PrintTapePos(usd_handle_t hTape)
276 {
277 DWORD rcode;
278 DWORD part, offLow, offHi;
279
280 rcode =
281 GetTapePosition(hTape->handle, TAPE_ABSOLUTE_POSITION, &part, &offLow,
282 &offHi);
283 if (rcode != NO_ERROR)
284 err_exit("GetTapePosition", "Get Tape Pos test Failed", 1);
285
286 if (USDTEST_DEBUG)
287 printf("%s: Cur Tape Block : %d \n", whoami, offLow);
288
289 return (offLow);
290 }
291 #else
292 static afs_int64
293 PrintTapePos(usd_handle_t hTape)
294 {
295 afs_int64 startOff, stopOff;
296 int rcode;
297
298 startOff = 0;
299 stopOff = 0;
300
301 rcode = USD_SEEK(hTape, startOff, SEEK_CUR, &stopOff);
302 err_exit("Seek", "Tape Seek Test Failed", rcode);
303
304 if (USDTEST_DEBUG)
305 printf("%s: Cur Tape Pos : %"AFS_INT64_FMT" bytes\n", whoami, stopOff);
306
307 return stopOff;
308 }
309 #endif