2 * Copyright 2000, International Business Machines Corporation and others.
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
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
14 #include <afsconfig.h>
15 #include <afs/param.h>
29 #define USDTEST_DEBUG 0 /* set to 1, to print tape pos info */
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
);
42 main(int argc
, char **argv
)
46 char *filename
= argv
[1];
47 char buf1
[1024], buf2
[1024];
52 printf(" Usage: %s <tape device name>\n", whoami
);
56 usd_Open(filename
, (USD_OPEN_RDWR
| USD_OPEN_WLOCK
), 0777, &hTape
);
59 printf("%s:Can't open device. Err %d\n", whoami
, rcode
);
68 /* Write test - write 10 blocks of random data */
69 printf("%s: Beginning Write Test\n", whoami
);
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
);
83 printf("%s: Write Test Passed.\n", whoami
);
86 printf("%s: Rewind Test passed\n", whoami
);
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
);
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
);
102 printf("%s: Read Test passed\n", whoami
);
107 for (i
= 0; i
< 5; i
++) {
108 /* write data block */
110 rcode
= USD_WRITE(hTape
, buf1
, 1024, &xferd
);
111 err_exit("Write", "Write EOF Test Failed", rcode
);
114 rcode
= WriteEOF(hTape
, 1);
115 err_exit("WEOF", "Write EOF Test Failed", rcode
);
118 printf("%s: WEOF Test passed\n", whoami
);
122 ForwardSpace(hTape
, 2);
124 rcode
= USD_READ(hTape
, buf2
, 1024, &xferd
);
125 err_exit("read", "FSF Test Failed", rcode
);
127 err_exit("FSF", "FSF Test Failed", 1);
129 ForwardSpace(hTape
, 2);
131 rcode
= USD_READ(hTape
, buf2
, 1024, &xferd
);
132 err_exit("read", "FSF Test Failed", rcode
);
134 err_exit("FSF", "FSF Test Failed", 1);
136 printf("%s: FSF Test passed\n", whoami
);
139 ForwardSpace(hTape
, 1);
140 rcode
= USD_READ(hTape
, buf2
, 1024, &xferd
);
141 err_exit("read", "FSF Test Failed", rcode
);
143 err_exit("BSF", "FSF Test Failed", 1);
145 printf("%s: BSF Test Passed\n", whoami
);
148 rcode
= USD_CLOSE(hTape
);
150 printf("%s:Can't close device. Err %d\n", whoami
, rcode
);
153 printf("%s: usd library, all tests passed!\n", whoami
);
160 err_exit(char *action
, char *msg
, int rcode
)
164 printf("%s:Can't %s device. Err %d\n %s \n", whoami
, action
, rcode
, msg
);
166 /* Now shutdown and close the tape */
168 rcode
= USD_CLOSE(hTape
);
170 printf("%s:Can't close device. Err %d\n", whoami
, rcode
);
178 bufeql(char *buf1
, char *buf2
, int size
)
192 Rewind(usd_handle_t hTape
)
197 tapeop
.tp_op
= USDTAPE_REW
;
199 rcode
= USD_IOCTL(hTape
, USD_IOCTL_TAPEOPERATION
, (void *)&tapeop
);
200 err_exit("rewind", "Rewind Test Failed", rcode
);
205 WriteEOF(usd_handle_t hTape
, int cnt
)
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
);
218 ForwardSpace(usd_handle_t hTape
, int cnt
)
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
);
231 BackSpace(usd_handle_t hTape
, int cnt
)
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
);
244 PrepTape(usd_handle_t hTape
)
249 tapeop
.tp_op
= USDTAPE_PREPARE
;
251 rcode
= USD_IOCTL(hTape
, USD_IOCTL_TAPEOPERATION
, (void *)&tapeop
);
252 err_exit("PREPARE", "Tape Prepare Test Failed", rcode
);
258 ShutdownTape(usd_handle_t hTape
)
263 tapeop
.tp_op
= USDTAPE_SHUTDOWN
;
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
);
275 PrintTapePos(usd_handle_t hTape
)
278 DWORD part
, offLow
, offHi
;
281 GetTapePosition(hTape
->handle
, TAPE_ABSOLUTE_POSITION
, &part
, &offLow
,
283 if (rcode
!= NO_ERROR
)
284 err_exit("GetTapePosition", "Get Tape Pos test Failed", 1);
287 printf("%s: Cur Tape Block : %d \n", whoami
, offLow
);
293 PrintTapePos(usd_handle_t hTape
)
295 afs_int64 startOff
, stopOff
;
301 rcode
= USD_SEEK(hTape
, startOff
, SEEK_CUR
, &stopOff
);
302 err_exit("Seek", "Tape Seek Test Failed", rcode
);
305 printf("%s: Cur Tape Pos : %"AFS_INT64_FMT
" bytes\n", whoami
, stopOff
);