2 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/types.h>
27 #include <lib/dmport.h>
29 #include <lib/errtest.h>
37 /*---------------------------------------------------------------------------
38 Automated test of the DMAPI functions:
45 test_fileattr [-s sid] [-n num_files] [-v] ls_path pathname
49 is the session ID whose events you you are interested in.
51 is the number of test files to create.
53 is the path to a copy of ls, which will be copied as a test file.
55 is the filesystem to use for the test.
56 ----------------------------------------------------------------------------*/
58 #define SET_MASK DM_AT_ATIME|DM_AT_MTIME|DM_AT_CTIME|DM_AT_DTIME|\
59 DM_AT_UID|DM_AT_GID|DM_AT_MODE|DM_AT_SIZE
61 #define GET_MASK DM_AT_EMASK|DM_AT_PMANR|DM_AT_PATTR|\
62 DM_AT_DTIME|DM_AT_CFLAG|DM_AT_STAT
73 comp_stat ( dm_stat_t expected,
78 if (found.dt_mode != expected.dt_mode) {
80 "ERROR: get #%d, expected mode %ld, but found %ld\n",
81 i, (long)expected.dt_mode, (long)found.dt_mode);
84 if (found.dt_uid != expected.dt_uid) {
86 "ERROR: get #%d, expected uid %ld, but found %ld\n",
87 i, (long)expected.dt_uid, (long)found.dt_uid);
90 if (found.dt_gid != expected.dt_gid) {
92 "ERROR: get #%d, expected gid %ld, but found %ld\n",
93 i, (long)expected.dt_gid, (long)found.dt_gid);
96 if (found.dt_atime != expected.dt_atime) {
98 "ERROR: get #%d, expected atime %ld, but found %ld\n",
99 i, expected.dt_atime, found.dt_atime);
102 if (found.dt_mtime != expected.dt_mtime) {
104 "ERROR: get #%d, expected mtime %ld, but found %ld\n",
105 i, expected.dt_mtime, found.dt_mtime);
108 if (found.dt_ctime != expected.dt_ctime) {
110 "ERROR: get #%d, expected ctime %ld, but found %ld\n",
111 i, expected.dt_ctime, found.dt_ctime);
115 /* NOTE: dtime gets set to ctime */
117 if (found.dt_dtime != expected.dt_ctime) {
119 "ERROR: get #%d, expected dtime %ld, but found %ld\n",
120 i, expected.dt_ctime, found.dt_dtime);
123 if (found.dt_size != expected.dt_size) {
125 "ERROR: get #%d, expected size %lld, but found %lld\n",
126 i, expected.dt_size, found.dt_size);
131 fprintf(stderr, "report: get #%d had no errors.\n",i);
133 fprintf(stderr, "report: %d tests correct for get #%d.\n",
145 "Usage: %s [-v] [-s sid] [-n num_files] ls_path pathname\n",
156 dm_sessid_t sid = DM_NO_SESSION;
157 dm_token_t test_token = DM_NO_TOKEN;
167 dm_fileattr_t fileattr;
173 size_t buflen = 16*sizeof(dm_stat_t);
182 char check_name[100];
186 if (Progname = strrchr(argv[0], '/')) {
193 while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
199 num_files = atoi(optarg);
208 if (optind + 2 != argc) {
211 ls_path = argv[optind];
212 pathname = argv[optind+1];
214 /* Seed the random number generator */
215 srand((unsigned int)time(NULL));
217 if (dm_init_service(&name) == -1) {
218 fprintf(stderr, "Can't initialize the DMAPI\n");
221 if (sid == DM_NO_SESSION)
222 find_test_session(&sid);
224 /* Dynamically allocate stat_arr; */
226 (dm_stat_t *)malloc(num_files * sizeof(dm_stat_t));
228 printf("Beginning file attribute tests...\n");
230 /* Fill in the dm_stat blocks with lots of junk...
232 for (i=0; i<num_files; i++) {
233 stat_arr[i].dt_atime=(time_t)(rand()+rand()*0x10000);
234 stat_arr[i].dt_mtime=(time_t)(rand()+rand()*0x10000);
235 stat_arr[i].dt_ctime=(time_t)(rand()+rand()*0x10000);
236 stat_arr[i].dt_dtime=(time_t)(rand()+rand()*0x10000);
237 stat_arr[i].dt_uid=(uid_t)(rand()+rand()*0x10000);
238 stat_arr[i].dt_gid=(gid_t)(rand()+rand()*0x10000);
239 stat_arr[i].dt_mode=(mode_t)((rand()%4096)+32768);
240 stat_arr[i].dt_size=((dm_off_t)(rand()+rand()*0x10000)) & 0x3FFFFFFFFFFFF; /* 1 TB max */
243 /*-----------------------------------------------------*\
244 |* File creation and set_fileattr loop *|
245 \*-----------------------------------------------------*/
246 if (Vflag) fprintf(stderr, "\nCreating/setting up test files.\n");
247 for (i=0; i < num_files; i++) {
248 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
250 sprintf(command, "cp %s %s \n", ls_path, test_file);
253 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
254 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
255 test_file, ERR_NAME);
258 fileattr.fa_mode = stat_arr[i].dt_mode;
259 fileattr.fa_uid = stat_arr[i].dt_uid;
260 fileattr.fa_gid = stat_arr[i].dt_gid;
261 fileattr.FA_ATIME = stat_arr[i].dt_atime;
262 fileattr.FA_MTIME = stat_arr[i].dt_mtime;
263 fileattr.FA_CTIME = stat_arr[i].dt_ctime;
264 fileattr.FA_DTIME = stat_arr[i].dt_dtime;
265 fileattr.fa_size = stat_arr[i].dt_size;
266 if (dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
267 SET_MASK, &fileattr)) {
268 fprintf(stderr, "ERROR: set_fileattr failed on pass #%d; %s.\n",
272 fprintf(stderr, "report: set #%d was successful.\n", i);
277 /*-----------------------------------------------------*\
278 |* Get_fileattr loop *|
279 \*-----------------------------------------------------*/
280 if (Vflag) fprintf(stderr, "\nRunning get_fileattr test\n");
281 for (i=0; i < num_files; i++) {
282 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
284 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
285 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
286 test_file, ERR_NAME);
288 if (dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
289 GET_MASK, &dmstat)) {
291 "ERROR: dm_get_fileattr failed on pass #%d, %s\n",
295 comp_stat(stat_arr[i], dmstat, i);
299 /*-----------------------------------------------------*\
300 |* Get_dirattrs loop *|
301 \*-----------------------------------------------------*/
302 if (Vflag) fprintf(stderr, "\nRunning get_dirattrs test\n");
303 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
304 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
308 if ((bufp = (void*)malloc(buflen)) == NULL) {
309 fprintf(stderr, "Can't allocate memory for buffer.\n");
312 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
314 "ERROR: dm_init_attrloc failed with %s.\n",
321 /* printf("About to call get_dirattrs;\tloops=%d\n", loops);
325 oops=dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
326 GET_MASK, &loc, buflen,
329 for (statbuf = bufp; statbuf != NULL;
330 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *)) {
331 chk_name_p = DM_GET_VALUE(statbuf, dt_compname, void *);
332 if (strncmp(chk_name_p, "DMAPI_fileattr_test.", 20)==0) {
333 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
334 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8) i++;
342 "ERROR: dm_get_dirattrs failed with %s.\n",
347 "ERROR: get_dirattrs found %d matching file%s "
348 "(expected %d).\n", i, (i==1)?"":"s", num_files);
351 fprintf(stderr, "report: get_dirattrs successfully "
352 "found %d files in %d loops.\n", i, loops);
355 /*-----------------------------------------------------*\
356 |* Get_bulkattr loop *|
357 \*-----------------------------------------------------*/
358 if (Vflag) fprintf(stderr, "\nRunning get_bulkattr test\n");
359 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
360 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
364 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
365 fprintf(stderr, "ERROR: can't get filesystem handle for %s; %s\n",
370 buflen = 16*sizeof(dm_stat_t); /* 100000000; */
371 if ((bufp = (void*)malloc(buflen)) == NULL) {
372 fprintf(stderr, "Can't allocate memory for buffer.\n");
375 if (dm_init_attrloc(sid, fs_hanp, fs_hlen,
378 "ERROR: dm_init_attrloc failed with %s.\n",
386 oops=dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
387 GET_MASK, &loc, buflen, bufp, &rlen);
389 for( statbuf = bufp; statbuf != NULL;
390 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *) ) {
391 targhanp = DM_GET_VALUE(statbuf, dt_handle, void *);
392 targhlen = DM_GET_LEN(statbuf, dt_handle);
393 if (dm_handle_to_path(hanp, hlen, targhanp, targhlen,
394 (size_t)100, check_name, &rlen)){
396 "Warning: Couldn't get name from handle. (%s)\n",
400 /* Put JUST name (not path) from check_name into chk_name_p */
401 if (chk_name_p = strrchr(check_name, '/')) chk_name_p++;
402 else chk_name_p = check_name;
403 /* Verify that check_name_p holds a testfile name */
404 if (strncmp(chk_name_p, "DMAPI_fileattr_test.",20)==0) {
405 /* Get the test file's number and compare. */
406 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
407 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8)i++;
416 "ERROR: dm_get_bulkattr failed with %s.\n",
419 /* printf("All_file_count: %d. BUFLEN: %d\n",
420 * all_file_count, buflen);
424 "ERROR: get_bulkattr found %d matching file%s "
425 "(expected %d) in %d loops.\n", i, (i==1)?"":"s",
429 fprintf(stderr, "report: get_bulkattr successfully "
430 "found %d files in %d loops.\n", i, loops);
433 /*------------------------*\
434 |* ## Errno subtests ## *|
435 \*------------------------*/
436 printf("\t(errno subtests beginning...)\n");
437 sprintf(test_file, "%s/DMAPI_fileattr_test.ERRNO",
439 sprintf(command, "cp %s %s\n", ls_path, test_file);
442 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
443 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
444 test_file, ERR_NAME);
448 /*------------------------------------*\
449 |* ## dm_set_fileattr() subtests ## *|
450 \*------------------------------------*/
451 /*---------------------------------------------------------*/
452 EXCLTEST("set", hanp, hlen, test_token,
453 dm_set_fileattr(sid, hanp, hlen, test_token,
454 SET_MASK, &fileattr))
455 /*---------------------------------------------------------*/
457 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
459 "Cannot create a test handle (%s); skipping EBADF test\n",
463 ((char *) test_vp)[hlen/2]++;
466 dm_set_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
467 SET_MASK, &fileattr))
468 dm_handle_free(test_vp, hlen);
471 /*---------------------------------------------------------*/
474 dm_set_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
475 SET_MASK, &fileattr))
476 /*---------------------------------------------------------*/
478 PROBLEM: 32 ones as a mask does not produce a "bad mask"
479 EINVAL. If it does not, I suspect nothing will.
482 "set (bad mask) [BROKEN]",
483 dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
484 0xFFFFFFFF, &fileattr))
486 /*---------------------------------------------------------*/
489 dm_set_fileattr(sid, hanp, hlen, 0,
490 SET_MASK, &fileattr))
491 /*---------------------------------------------------------*/
494 dm_set_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
495 SET_MASK, &fileattr))
496 /*---------------------------------------------------------*/
499 /*------------------------------------*\
500 |* ## dm_get_fileattr() subtests ## *|
501 \*------------------------------------*/
502 /*---------------------------------------------------------*/
503 SHAREDTEST("get", hanp, hlen, test_token,
504 dm_get_fileattr(sid, hanp, hlen, test_token,
506 /*---------------------------------------------------------*/
508 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
510 "Cannot create a test handle (%s); skipping EBADF test\n",
514 ((char *) test_vp)[hlen/2]++;
517 dm_get_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
519 dm_handle_free(test_vp, hlen);
522 /*---------------------------------------------------------*/
525 dm_get_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
529 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
530 GET_MASK, (dm_stat_t *)(-1000)))
531 /*---------------------------------------------------------*/
533 PROBLEM: 32 ones as a mask does not produce a "bad mask"
534 EINVAL. If it does not, I suspect nothing will.
537 "get (bad mask) [BROKEN]",
538 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
539 0xFFFFFFFF, &dmstat))
541 /*---------------------------------------------------------*/
544 dm_get_fileattr(sid, hanp, hlen, 0,
546 /*---------------------------------------------------------*/
549 dm_get_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
551 /*---------------------------------------------------------*/
554 dm_handle_free(hanp, hlen);
556 /*------------------------------------*\
557 |* ## dm_get_dirattrs() subtests ## *|
558 \*------------------------------------*/
559 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
560 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
563 else if ((bufp = (void*)malloc(buflen)) == NULL) {
564 fprintf(stderr, "Can't allocate memory for buffer.\n");
567 /*---------------------------------------------------------*/
568 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
570 "ERROR: dm_init_attrloc failed with %s.\n",
573 SHAREDTEST("get_dir", hanp, hlen, test_token,
574 dm_get_dirattrs(sid, hanp, hlen, test_token,
575 GET_MASK, &loc, buflen, bufp, &rlen))
577 /*---------------------------------------------------------*/
579 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
580 fprintf(stderr, "ERROR: dm_init_attrloc failed with %s.\n",
583 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
584 fprintf(stderr, "Cannot create a test handle (%s); "
585 "skipping EBADF test\n", ERR_NAME);
588 ((char *) test_vp)[hlen/2]++;
591 dm_get_dirattrs(sid, test_vp, hlen, DM_NO_TOKEN,
592 GET_MASK, &loc, buflen, bufp, &rlen))
593 dm_handle_free(test_vp, hlen);
596 /*---------------------------------------------------------*/
598 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
600 "ERROR: dm_init_attrloc failed with %s.\n",
605 This would test alignment. Right now, no error occurs
606 when the buffer is "out of sync" with struct size.
607 It makes it tough to read from the buffer, tho!
609 "get_dir (bad bufp)",
610 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
611 GET_MASK, &loc, buflen, p, &rlen))
614 "get_dir (bad locp)",
615 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
616 GET_MASK, (dm_attrloc_t*)(-1000),
617 buflen, bufp, &rlen))
619 "get_dir (bad bufp)",
620 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
621 GET_MASK, &loc, buflen,
622 (void*)(-1000), &rlen))
624 "get_dir (bad rlenp)",
625 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
626 GET_MASK, &loc, buflen, bufp,
630 /*---------------------------------------------------------*/
631 /*---------------------------------------------------------*/
634 /*------------------------------------*\
635 |* ## dm_get_bulkattr() subtests ## *|
636 \*------------------------------------*/
637 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
638 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
641 else if ((bufp = (void*)malloc(buflen)) == NULL) {
642 fprintf(stderr, "Can't allocate memory for buffer.\n");
645 /*---------------------------------------------------------*/
646 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
648 "ERROR: dm_init_attrloc failed with %s.\n",
652 SHAREDTEST("get_bulk", fs_hanp, fs_hlen, test_token,
653 dm_get_bulkattr(sid, fs_hanp, fs_hlen, test_token,
654 GET_MASK, &loc, buflen, bufp, &rlen))
656 /*---------------------------------------------------------*/
657 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
659 "ERROR: dm_init_attrloc failed with %s.\n",
663 void *p = (void *)(((char *)bufp)+1);
664 ERRTEST(EFAULT, "get_bulk (bad bufp)",
665 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
666 GET_MASK, &loc, buflen,
667 (void *)(-1000), &rlen))
668 ERRTEST(EFAULT, "get_bulk (bad locp)",
669 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
670 GET_MASK, (dm_attrloc_t *)(-1000),
671 buflen, bufp, &rlen))
672 ERRTEST(EFAULT, "get_bulk (bad rlenp)",
673 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
674 GET_MASK, &loc, buflen, bufp,
676 ERRTEST(EFAULT, "get_bulk (bad bufp)",
677 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
678 GET_MASK, &loc, buflen, p, &rlen))
680 /*---------------------------------------------------------*/
683 sprintf(command, "rm %s/DMAPI_fileattr_test.ERRNO\n", pathname);
685 printf("\t(errno subtests complete)\n");
686 /*---------------------*\
687 |* End of errno tests *|
688 \*---------------------*/
691 /* File deletion loop */
692 if (Vflag) printf("(Deleting test files...)\n");
693 for (i=0; i < num_files; i++) {
694 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
696 sprintf(command, "rm %s\n", test_file);
699 printf("File attribute tests complete.\n");