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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
24 #include <netinet/in.h>
26 #include <afs/afsutil.h>
32 #include <afs/com_err.h>
34 #include "error_macros.h"
36 int isafile = 0, debugLevel = 1;
38 /* Format of the config file is now
39 * <capacity devicename portno isafile>
40 * capacity - size of tape or file in bytes
41 * devicename - tape device or file name to write data into
42 * portno - tape coordinator port no. (irrelevant)
43 * isafile - Is this entry a file or a tape?
45 * test_ftm goes through the lines in the config file and selects the
46 * entry that has a port no. of 0 and tries to open the device.
49 static char *whoami = "test_TM";
52 typedef struct TestInfo {
53 char *tapeName; /* tape name */
54 struct tapeConfig *tc_Infop; /* tape config info (size, devname etc..) */
55 int nFiles; /* no. of files to be dumped */
56 char **files; /* names of files to be dumped */
57 char dumpDone; /* event that signals the completion of
58 * the dump test thread.
60 int appended; /* do an appended test? */
64 char mark[sizeof(struct blockMark)];
65 char data[BUTM_BLKSIZE];
68 struct tapeConfig confInfo;
70 int PerformDumpTest(TestInfo * tip);
73 GetDeviceInfo(filename, config)
75 struct tapeConfig *config;
80 afs_int32 aport, capacity, notfound;
84 devFile = fopen(filename, "r");
86 fprintf(stderr, "failed to open %s\n", filename);
91 if (fgets(line, 100, devFile) == NULL)
93 sscanf(line, "%u %s %u %u\n", &capacity, devName, &aport, &isafile);
94 if (aport == portOffset) {
95 config->capacity = capacity;
96 config->portOffset = aport;
97 strncpy(config->device, devName, 100);
107 #include "AFS_component_version_number.c"
114 char *tape = "testtape.0", *parent_pid, *pid;
121 initialize_BUTM_error_table();
125 files = (char **)malloc(argc * sizeof(char *));
127 for (i = 1; i < argc; i++) {
128 if (argv[i][0] == '-') {
129 if (strncmp(argv[i], "-configuration", strlen(argv[i])) == 0)
131 else if (strncmp(argv[i], "-tapename", strlen(argv[i])) == 0)
134 com_err(whoami, 0, "unexpected command argument: '%s'",
140 if (stat(argv[i], &buf)) {
141 com_err(whoami, errno, "can't stat filename parameter %s\n",
144 if ((buf.st_mode & S_IFREG) && (buf.st_mode & 0444))
145 files[nFiles++] = argv[i];
147 printf("skipping non-file '%s'\n", argv[i]);
154 if ((strlen(tape) == 0) || (strlen(tape) >= BU_MAXTAPELEN)) {
155 com_err(whoami, 0, "bogus tape name");
158 code = GetDeviceInfo(config, &confInfo);
160 com_err(whoami, 0, "cant find tape config info");
165 ti.tc_Infop = &confInfo;
169 printf("%s: Beginning Dump Tests\n", whoami);
170 code = LWP_InitializeProcessSupport(1, &parent_pid);
172 com_err(whoami, code, "Can't init LWP lib");
175 code = IOMGR_Initialize();
177 com_err(whoami, code, "Can't init LWP IOMGR lib");
180 /* Perform normal test */
182 LWP_CreateProcess(PerformDumpTest, 100000, 0, (void *)&ti,
183 "BUTM Tester", &pid);
185 com_err(whoami, code, "libbutm.a: Normal Tests failed!. :-( ");
188 LWP_WaitProcess(&ti.dumpDone);
189 LWP_DestroyProcess(pid);
191 /* Perform Appended Test, on tapes */
195 LWP_CreateProcess(PerformDumpTest, 100000, 0, (void *)&ti,
196 "BUTM Tester", &pid);
198 com_err(whoami, code, "libbutm.a: Appended Tests failed!. :-( ");
202 LWP_WaitProcess(&ti.dumpDone);
203 LWP_DestroyProcess(pid);
207 LWP_TerminateProcessSupport();
213 ("usage is: %s [-configuration <config file>] [-tapename <name>] <file>*\n",
220 PerformDumpTest(TestInfo * tip)
221 { /* Dump Files into target tape/file */
222 struct butm_tapeInfo info;
223 struct butm_tapeLabel label;
227 bufferBlock = (struct BufferBlock *)malloc(sizeof(struct BufferBlock));
229 info.structVersion = BUTM_MAJORVERSION;
230 if (code = butm_file_Instantiate(&info, tip->tc_Infop)) {
231 com_err(whoami, code, "instantiating file tape");
235 memset(&label, 0, sizeof(label));
236 gettimeofday(&tp, 0);
237 label.structVersion = CUR_TAPE_VERSION;
238 label.creationTime = tp.tv_sec;
239 label.size = info.tapeSize;
241 #define T_NAME "AFS 3.5"
243 #define T_CELL "cellname"
244 #define T_REALM "cellname"
245 #define T_COMMENT "Testing the butm library.."
246 strcpy(label.AFSName, tip->tapeName);
247 strcpy(label.creator.name, T_NAME);
248 strcpy(label.creator.instance, T_INST);
249 strcpy(label.creator.cell, T_CELL);
250 strcpy(label.cell, T_REALM);
251 strcpy(label.comment, T_COMMENT);
253 if (code = butm_Mount(&info, tip->tapeName)) {
254 com_err(whoami, code, "setting up tape");
257 if (tip->appended) { /* This is to be an appended dump */
258 code = butm_SeekEODump(&info, tip->nFiles + 1);
260 com_err(whoami, code,
261 "Can't append: Can't position to end of dump on tape\n");
265 if ((code = butm_Create(&info, &label, !tip->appended /*Rewind */ ))) {
266 com_err(whoami, code, "Writing tape label");
271 for (i = 0; i < tip->nFiles; i++) {
273 int fid = open(tip->files[i], O_RDONLY, 0);
275 com_err(whoami, errno, "opening file to write on tape");
278 if (code = butm_WriteFileBegin(&info)) {
279 com_err(whoami, code, "beginning butm write file");
282 while ((len = read(fid, bufferBlock->data, BUTM_BLKSIZE)) > 0) {
283 if (code = butm_WriteFileData(&info, bufferBlock->data, 1, len)) {
284 com_err(whoami, code, "butm writing file data");
289 com_err(whoami, errno, "reading file data");
292 if (code = butm_WriteFileEnd(&info)) {
293 com_err(whoami, code, "ending butm write file");
296 if (close(fid) < 0) {
297 com_err(whoami, errno, "closing file");
301 if ((code = butm_WriteEOT(&info)) || (code = butm_Dismount(&info))) {
302 com_err(whoami, code, "finishing up tape");
306 /* now read the tape back in and make sure everything is OK */
308 label.structVersion = BUTM_MAJORVERSION;
309 if (code = butm_Mount(&info, tip->tapeName)) {
310 com_err(whoami, code, "setting up tape");
313 if (tip->appended) { /* This is to be an appended dump */
314 code = butm_SeekEODump(&info, tip->nFiles + 1);
316 com_err(whoami, code,
317 "Can't append: Can't position to end of dump on tape\n");
321 if (code = butm_ReadLabel(&info, &label, !tip->appended /*rewind */ )) {
322 com_err(whoami, code, "reading tape label");
325 past = time(0) - label.creationTime;
326 if ((past < 0) || (past > 5 * 60)) {
327 time_t t = label.creationTime;
328 printf("label creation time is long ago: %s\n", ctime(&t));
331 if (strcmp(label.AFSName, tip->tapeName) != 0) {
332 printf("label tape name is bogus: %s, should be %s\n", label.AFSName,
336 if ((strcmp(label.creator.name, T_NAME) != 0)
337 || (strcmp(label.creator.instance, T_INST) != 0)
338 || (strcmp(label.creator.cell, T_CELL) != 0)
339 || (strcmp(label.cell, T_REALM) != 0)
340 || (strcmp(label.comment, T_COMMENT) != 0)) {
341 printf("label strings are bad: creator %s.%s@%s from realm %s (%s)\n",
342 label.creator.name, label.creator.instance, label.creator.cell,
343 label.cell, label.comment);
346 for (i = 0; i < sizeof(label.spare); i++)
347 if (*(char *)label.spare) {
348 printf("Label's spare fields not zero\n");
352 for (i = 0; i < tip->nFiles; i++) {
353 char *tbuffer = bufferBlock->data;
355 char fbuffer[BUTM_BLKSIZE];
357 int tprogress, fprogress;
360 fid = open(tip->files[i], O_RDONLY, 0);
362 com_err(whoami, errno, "Opening %dth file to compare with tape",
366 if (code = butm_ReadFileBegin(&info)) {
367 com_err(whoami, code, "Beginning butm %dth read file", i + 1);
371 tprogress = tlen = fprogress = flen = 0;
373 memset(tbuffer, 0, BUTM_BLKSIZE);
374 code = butm_ReadFileData(&info, tbuffer, BUTM_BLKSIZE, &tlen);
376 if (code && code != BUTM_STATUS_EOF) {
377 com_err(whoami, code, "Reading %dth tape data", i + 1);
380 memset(fbuffer, 0, BUTM_BLKSIZE);
381 flen = read(fid, fbuffer, sizeof(fbuffer));
383 com_err(whoami, errno, "Reading %dth file data", i + 1);
386 if (code == BUTM_STATUS_EOF)
388 if ((tlen == 0) && (flen == 0))
389 break; /* correct termination */
391 printf("File length mismatch for %dth file (%s)\n", i,
395 if (tbuffer[tprogress++] != fbuffer[fprogress++]) {
396 printf("Data mismatch for %dth file (%s)\n", i + 1,
400 if (tlen < BUTM_BLKSIZE)
404 if (code = butm_ReadFileEnd(&info)) {
405 com_err(whoami, code, "Ending butm %dth read file", i + 1);
408 if (close(fid) < 0) {
409 com_err(whoami, errno, "Closing %dth file", i + 1);
414 if ((info.status & BUTM_STATUS_EOD) == 0) {
415 code = butm_ReadFileBegin(&info);
416 if (code && (code != BUTM_EOD)) {
417 com_err(whoami, code, "Should have encountered an 'End Of Tape'");
421 if (code = butm_Dismount(&info)) {
422 com_err(whoami, code, "Finishing up tape");
427 printf("%d files Appended: All OK\n", tip->nFiles);
429 printf("%d files written: All OK\n", tip->nFiles);
432 LWP_SignalProcess(&tip->dumpDone);