convert-from-bsd-to-posix-string-and-memory-functions-20010807
[openafs.git] / src / butm / butm_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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #include <sys/file.h>
17 #include <sys/stat.h>
18 #include <afs/com_err.h>
19 #include <stdio.h>
20 #include <sys/ioctl.h>
21 #include <netinet/in.h>
22 #include <lwp.h>
23 #include <afs/butm.h>
24 #include "AFS_component_version_number.c"
25
26 extern int errno;
27
28 static char *whoami = "TEST FAIL";
29
30 #define NULL (char *)0
31 #define PASS(str, err) printf("TEST: %s\n", str); \
32                        if (code != err) \
33                        { \
34                           printf("FAILURE: expected %u; got %u\n", err, code); \
35                           if (code) com_err(whoami, code, ""); \
36                        } \
37                        else printf("PASSED; got %u\n", err); \
38                        printf("\n");
39
40 #define PASSq(str, err) if (code != err) \
41                         { \
42                            printf("TEST: %s\n", str); \
43                            printf("FAILURE: expected %u; got %u\n", err, code); \
44                            if (code) com_err(whoami, code, ""); \
45                            printf("\n"); \
46                         }
47 #define NOREWIND 0
48 #define REWIND   1
49
50 char tapeBlock[16384];
51 char *dataBlock;
52 long dataSize;
53
54 main (argc, argv)
55   int   argc;
56   char *argv[];
57 {
58     char *config = 0;
59     char *tape = "testtape.0";
60     long  code;
61     char **files;
62     int   nFiles;
63     int   i;
64     int   past;
65
66     struct butm_tapeInfo  tapeInfo;
67     struct tapeConfig     tapeConfig;
68
69     struct butm_tapeLabel tapeLabelWrite, tapeLabelRead;
70
71     /* -------------
72      * General Setup 
73      * ------------- */
74     initialize_butm_error_table();
75
76     tapeInfo.structVersion = BUTM_MAJORVERSION;
77
78     strcpy(tapeConfig.device, "/dev/rmt0");
79     tapeConfig.capacity     = 100;
80     tapeConfig.fileMarkSize = 16384;
81     tapeConfig.portOffset   = 0;
82     tapeConfig.aixScsi      = 0;
83
84 goto start;
85
86     /* ------------- */
87     /* START TESTING */
88     /* ------------- */
89
90     /* butm_file_Instantiate tests */
91     /* --------------------------- */
92     code = butm_file_Instantiate (NULL, &tapeConfig);
93     PASS("Bad info paramater", BUTM_BADARGUMENT)
94
95     code = butm_file_Instantiate (&tapeInfo, NULL);
96     PASS("Bad config parameter", BUTM_BADCONFIG);
97
98     tapeInfo.structVersion = 0;
99     code = butm_file_Instantiate (&tapeInfo, &tapeConfig);
100     PASS ("Bad version number", BUTM_OLDINTERFACE);
101     tapeInfo.structVersion = BUTM_MAJORVERSION;
102
103     tapeConfig.capacity = 0;
104     code = butm_file_Instantiate (&tapeInfo, &tapeConfig);
105     PASS ("zero capacity tape", BUTM_BADCONFIG);
106     tapeConfig.capacity = 100;
107
108     tapeConfig.fileMarkSize = 0;
109     code = butm_file_Instantiate (&tapeInfo, &tapeConfig);
110     PASS ("zero length filemark", BUTM_BADCONFIG);
111     tapeConfig.fileMarkSize = 16384;
112
113     strcpy(tapeConfig.device, "");
114     code = butm_file_Instantiate (&tapeInfo, &tapeConfig);
115     PASS ("no tape device name", BUTM_BADCONFIG);
116     strcpy(tapeConfig.device, "/dev/rmt0");
117
118     /* file_Mount and file_Dismount tests */
119     /* ---------------------------------- */
120
121     strcpy(tapeConfig.device, "/dev/Bogus");
122     code = butm_file_Instantiate(&tapeInfo, &tapeConfig);
123     PASSq("file Instantiate", 0);
124     strcpy(tapeConfig.device, "/dev/rmt0");
125
126     code = tapeInfo.ops.mount(NULL, "TAPE_NAME");
127     PASS ("Null tapeInfo ptr for mount", BUTM_BADARGUMENT);
128
129     code = tapeInfo.ops.dismount(NULL);
130     PASS ("Null info ptr for dismount", BUTM_BADARGUMENT);
131
132     /* --------- */
133
134     code = tapeInfo.ops.mount(&tapeInfo, NULL);
135     PASS ("NULL virtual tape name", BUTM_BADARGUMENT);
136
137     code = tapeInfo.ops.mount(&tapeInfo, "-MORE_THAN_THIRTY_TWO_CHARACTERS-");
138     PASS (">32 character tape name", BUTM_BADARGUMENT);
139
140     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
141     PASS ("Bogus tape name", BUTM_MOUNTFAIL);
142
143     /* --------- */
144
145     code = butm_file_Instantiate(&tapeInfo, &tapeConfig);
146     PASSq("file Instantiate", 0);
147
148     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
149     PASS ("Open tape drive", 0);
150
151     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
152     PASS ("Open tape drive 2nd time", BUTM_PARALLELMOUNTS);
153
154     code = tapeInfo.ops.dismount(&tapeInfo);
155     PASSq ("Unmount the tape drive", 0);
156
157     code = tapeInfo.ops.dismount(&tapeInfo);
158     PASS ("Unmount the tape drive which is not mounted", 0);
159
160     /* file_writeLabel and file_readLabel tests */
161     /* ---------------------------------------- */
162
163     code = butm_file_Instantiate(&tapeInfo, &tapeConfig);
164     PASSq("file Instantiate", 0);
165
166     code = tapeInfo.ops.create(NULL, &tapeLabelWrite, REWIND);
167     PASS ("NULL info to Write label", BUTM_BADARGUMENT);
168
169     code = tapeInfo.ops.readLabel(NULL, &tapeLabelWrite, REWIND);
170     PASS ("NULL info to Read Label", BUTM_BADARGUMENT);
171
172     /* ---------- */
173
174     code = tapeInfo.ops.create(&tapeInfo, &tapeLabelWrite, REWIND);
175     PASS ("Write label to unmounted tape", BUTM_NOMOUNT);
176
177     code = tapeInfo.ops.readLabel(&tapeInfo, &tapeLabelRead, REWIND);
178     PASS ("Read label of unmounted tape", BUTM_NOMOUNT);
179
180     /* ---------- */
181
182     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
183     PASSq ("Mount tape", 0);
184
185     memset(tapeLabelWrite, 0, sizeof(tapeLabelWrite));
186     tapeLabelWrite.structVersion  = CUR_TAPE_VERSION;
187     tapeLabelWrite.creationTime   = time(0);
188     tapeLabelWrite.expirationDate = time(0);
189     strcpy(tapeLabelWrite.name, "TAPE_LABEL_NAME");
190     /* tapeLabelWrite.creator. */
191     strcpy(tapeLabelWrite.cell, "CELLNAME");
192     tapeLabelWrite.dumpid         = 999;
193     tapeLabelWrite.useCount       = 8888;
194     strcpy(tapeLabelWrite.comment, "THIS IS THE COMMENT FIELD");
195     tapeLabelWrite.size           = 77777;
196     strcpy(tapeLabelWrite.dumpPath, "/FULL/WEEK3/DAY4/HOUR7");
197
198     code = tapeInfo.ops.create(&tapeInfo, &tapeLabelWrite, REWIND);
199     PASS ("Write a label", 0);
200
201     code = tapeInfo.ops.readLabel(&tapeInfo, &tapeLabelRead, REWIND);
202     PASS ("Read a label", 0);
203
204     if ( memcmp(&tapeLabelWrite, &tapeLabelRead, sizeof(tapeLabelWrite)) )
205         printf("FAILURE: Label Read is not same as label Written\n");
206     else
207         printf("PASSED: Label Read is same as label Written\n");
208
209     code = tapeInfo.ops.dismount(&tapeInfo);
210     PASSq ("Unmount the tape drive", 0);
211
212     /* ---------- */
213
214     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
215     PASSq ("Mount tape", 0);
216
217     code = tapeInfo.ops.create(&tapeInfo, NULL, REWIND);
218     PASS ("Write a NULL label", BUTM_BADARGUMENT);
219
220     tapeLabelWrite.structVersion  = 0;
221     code = tapeInfo.ops.create(&tapeInfo, &tapeLabelWrite, REWIND);
222     PASS ("Write label with bad version in it", BUTM_OLDINTERFACE);
223     tapeLabelWrite.structVersion  = CUR_TAPE_VERSION;
224
225     code = tapeInfo.ops.dismount(&tapeInfo);
226     PASSq ("Unmount the tape drive", 0);
227
228     /* file_WriteFileBegin and file_ReadFileBegin tests */
229     /* file_WriteFileEnd   and file_ReadFileEnd   tests */
230     /* ------------------------------------------------ */
231
232 start:
233     code = butm_file_Instantiate(&tapeInfo, &tapeConfig);
234     PASSq("file Instantiate", 0);
235
236     code = tapeInfo.ops.writeFileBegin(NULL);
237     PASS ("Null info ptr for writeFileBegin", BUTM_BADARGUMENT);
238
239     code = tapeInfo.ops.readFileBegin(NULL);
240     PASS ("Null info ptr for readFileBegin", BUTM_BADARGUMENT);
241     
242     code = tapeInfo.ops.writeFileEnd(NULL);
243     PASS ("Null info ptr for writeFileEnd", BUTM_BADARGUMENT);
244
245     code = tapeInfo.ops.readFileEnd(NULL);
246     PASS ("Null info ptr for readFileEnd", BUTM_BADARGUMENT);
247     
248     /* ---------- */
249
250     code = tapeInfo.ops.writeFileBegin(&tapeInfo);
251     PASS ("Tape not mounted for writeFileBegin", BUTM_NOMOUNT);
252
253     code = tapeInfo.ops.readFileBegin(&tapeInfo);
254     PASS ("Null info ptr for writeFileBegin", BUTM_NOMOUNT);
255     
256     code = tapeInfo.ops.writeFileEnd(&tapeInfo);
257     PASS ("Tape not mounted for writeFileEnd", BUTM_NOMOUNT);
258
259     code = tapeInfo.ops.readFileEnd(&tapeInfo);
260     PASS ("Null info ptr for writeFileEnd", BUTM_NOMOUNT);
261     
262     /* ---------- */
263
264     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
265     PASSq ("Mount tape", 0);
266
267     code = tapeInfo.ops.writeFileEnd(&tapeInfo);
268     PASS ("Write a fileEnd as first entry on tape", BUTM_BADOP);
269
270     code = tapeInfo.ops.readFileEnd(&tapeInfo);
271     PASS ("Read a fileEnd as first entry on tape", BUTM_BADOP);
272
273     code = tapeInfo.ops.dismount(&tapeInfo);
274     PASSq ("Unmount the tape drive", 0);
275
276     /* ---------- */
277
278     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
279     PASSq ("Mount tape", 0);
280
281     code = tapeInfo.ops.writeFileBegin(&tapeInfo);
282     PASS ("Write a fileBegin", 0);
283
284     code = tapeInfo.ops.writeFileEnd(&tapeInfo);
285     PASS ("Write a fileEnd", 0);
286
287     code = tapeInfo.ops.dismount(&tapeInfo);
288     PASSq ("Unmount the tape drive", 0);
289
290     /* ---------- */
291
292     code = tapeInfo.ops.mount(&tapeInfo, "TAPE_NAME");
293     PASSq ("Mount tape", 0);
294
295     code = tapeInfo.ops.readFileBegin(&tapeInfo);
296     PASS ("Read a fileBegin", 0);
297
298     code = tapeInfo.ops.readFileEnd(&tapeInfo);
299     PASS ("Read a fileEnd", 0);
300
301     code = tapeInfo.ops.dismount(&tapeInfo);
302     PASSq ("Unmount the tape drive", 0);
303
304     /* file_WriteFileData and file_ReadFileData tests */
305     /* ---------------------------------------------- */
306
307     dataBlock = &tapeBlock[sizeof(struct blockMark)];
308     dataSize  = 16384 - sizeof(struct blockMark);
309
310
311     code = butm_file_Instantiate(&tapeInfo, &tapeConfig);
312     PASSq("file Instantiate", 0);
313
314     code = tapeInfo.ops.writeFileData(NULL, dataBlock, dataSize);
315     PASS ("Null info ptr for writeFileData", BUTM_BADARGUMENT);
316
317     code = tapeInfo.ops.readFileData(NULL, dataBlock, dataSize, nBytes);
318     PASS ("Null info ptr for readFileData", BUTM_BADARGUMENT);
319     
320     /* ---------- */
321
322     code = tapeInfo.ops.writeFileData(&tapeInfo, NULL, dataSize);
323     PASS ("Null data ptr for writeFileData", BUTM_BADARGUMENT);
324
325     code = tapeInfo.ops.readFileData(&tapeInfo, NULL, dataSize, nBytes);
326     PASS ("Null data ptr for readFileData", BUTM_BADARGUMENT);
327     
328     /* ---------- */
329
330     code = tapeInfo.ops.writeFileData(&tapeInfo, dataBlock, -1);
331     PASS ("Neg. data size for writeFileData", BUTM_BADARGUMENT);
332
333     code = tapeInfo.ops.readFileData(&tapeInfo, dataBlock, -1, nBytes);
334     PASS ("Neg. data size for readFileData", BUTM_BADARGUMENT);
335     
336     /* ---------- */
337
338     code = tapeInfo.ops.writeFileData(&tapeInfo, dataBlock, dataSize + 1);
339     PASS ("Large data size for writeFileData", BUTM_BADARGUMENT);
340
341     code = tapeInfo.ops.readFileData(&tapeInfo, dataBlock, dataSize + 1, nBytes);
342     PASS ("Large data size for readFileData", BUTM_BADARGUMENT);
343     
344     /* ---------- */
345     
346     code = tapeInfo.ops.readFileData(&tapeInfo, dataBlock, dataSize, NULL);
347     PASS ("Null nBytes ptr for readFileData", BUTM_BADARGUMENT);
348     
349     /* ---------- */
350     
351     code = tapeInfo.ops.writeFileData(&tapeInfo, dataBlock, dataSize);
352     PASS ("First write for WriteFileData", BUTM_BADOP);
353
354     code = tapeInfo.ops.readFileData(&tapeInfo, dataBlock, dataSize, nBytes);
355     PASS ("First read for readFileData", BUTM_BADOP);
356     
357 end: return;
358 }