2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <sys/types.h>
41 #include <lib/dmport.h>
43 #include <lib/errtest.h>
51 /*---------------------------------------------------------------------------
52 Automated test of the DMAPI functions:
59 test_fileattr [-s sid] [-n num_files] [-v] ls_path pathname
63 is the session ID whose events you you are interested in.
65 is the number of test files to create.
67 is the path to a copy of ls, which will be copied as a test file.
69 is the filesystem to use for the test.
70 ----------------------------------------------------------------------------*/
72 #define SET_MASK DM_AT_ATIME|DM_AT_MTIME|DM_AT_CTIME|DM_AT_DTIME|\
73 DM_AT_UID|DM_AT_GID|DM_AT_MODE|DM_AT_SIZE
75 #define GET_MASK DM_AT_EMASK|DM_AT_PMANR|DM_AT_PATTR|\
76 DM_AT_DTIME|DM_AT_CFLAG|DM_AT_STAT
87 comp_stat ( dm_stat_t expected,
92 if (found.dt_mode != expected.dt_mode) {
94 "ERROR: get #%d, expected mode %ld, but found %ld\n",
95 i, expected.dt_mode, found.dt_mode);
98 if (found.dt_uid != expected.dt_uid) {
100 "ERROR: get #%d, expected uid %ld, but found %ld\n",
101 i, expected.dt_uid, found.dt_uid);
104 if (found.dt_gid != expected.dt_gid) {
106 "ERROR: get #%d, expected gid %ld, but found %ld\n",
107 i, expected.dt_gid, found.dt_gid);
110 if (found.dt_atime != expected.dt_atime) {
112 "ERROR: get #%d, expected atime %ld, but found %ld\n",
113 i, expected.dt_atime, found.dt_atime);
116 if (found.dt_mtime != expected.dt_mtime) {
118 "ERROR: get #%d, expected mtime %ld, but found %ld\n",
119 i, expected.dt_mtime, found.dt_mtime);
122 if (found.dt_ctime != expected.dt_ctime) {
124 "ERROR: get #%d, expected ctime %ld, but found %ld\n",
125 i, expected.dt_ctime, found.dt_ctime);
129 /* NOTE: dtime gets set to ctime */
131 if (found.dt_dtime != expected.dt_ctime) {
133 "ERROR: get #%d, expected dtime %ld, but found %ld\n",
134 i, expected.dt_ctime, found.dt_dtime);
137 if (found.dt_size != expected.dt_size) {
139 "ERROR: get #%d, expected size %lld, but found %lld\n",
140 i, expected.dt_size, found.dt_size);
145 fprintf(stderr, "report: get #%d had no errors.\n",i);
147 fprintf(stderr, "report: %d tests correct for get #%d.\n",
159 "Usage: %s [-v] [-s sid] [-n num_files] ls_path pathname\n",
170 dm_sessid_t sid = DM_NO_SESSION;
171 dm_token_t test_token = DM_NO_TOKEN;
182 dm_fileattr_t fileattr;
188 size_t buflen = 16*sizeof(dm_stat_t);
193 int all_file_count=0;
198 char check_name[100];
202 if (Progname = strrchr(argv[0], '/')) {
209 while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
215 num_files = atoi(optarg);
224 if (optind + 2 != argc) {
227 ls_path = argv[optind];
228 pathname = argv[optind+1];
230 /* Seed the random number generator */
231 srand((unsigned int)time(NULL));
233 if (dm_init_service(&name) == -1) {
234 fprintf(stderr, "Can't inititalize the DMAPI\n");
237 if (sid == DM_NO_SESSION)
238 find_test_session(&sid);
240 /* Dynamically allocate stat_arr; */
242 (dm_stat_t *)malloc(num_files * sizeof(dm_stat_t));
244 printf("Beginning file attribute tests...\n");
246 /* Fill in the dm_stat blocks with lots of junk...
248 for (i=0; i<num_files; i++) {
249 stat_arr[i].dt_atime=(time_t)(rand()+rand()*0x10000);
250 stat_arr[i].dt_mtime=(time_t)(rand()+rand()*0x10000);
251 stat_arr[i].dt_ctime=(time_t)(rand()+rand()*0x10000);
252 stat_arr[i].dt_dtime=(time_t)(rand()+rand()*0x10000);
253 stat_arr[i].dt_uid=(uid_t)(rand()+rand()*0x10000);
254 stat_arr[i].dt_gid=(gid_t)(rand()+rand()*0x10000);
255 stat_arr[i].dt_mode=(mode_t)((rand()%4096)+32768);
256 stat_arr[i].dt_size=(dm_off_t)(rand()+rand()*0x10000);
259 /*-----------------------------------------------------*\
260 |* File creation and set_fileattr loop *|
261 \*-----------------------------------------------------*/
262 if (Vflag) fprintf(stderr, "\nCreating/setting up test files.\n");
263 for (i=0; i < num_files; i++) {
264 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
266 sprintf(command, "cp %s %s \n", ls_path, test_file);
269 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
270 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
271 test_file, ERR_NAME);
274 fileattr.fa_mode = stat_arr[i].dt_mode;
275 fileattr.fa_uid = stat_arr[i].dt_uid;
276 fileattr.fa_gid = stat_arr[i].dt_gid;
277 fileattr.FA_ATIME = stat_arr[i].dt_atime;
278 fileattr.FA_MTIME = stat_arr[i].dt_mtime;
279 fileattr.FA_CTIME = stat_arr[i].dt_ctime;
280 fileattr.FA_DTIME = stat_arr[i].dt_dtime;
281 fileattr.fa_size = stat_arr[i].dt_size;
282 if (dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
283 SET_MASK, &fileattr)) {
284 fprintf(stderr, "ERROR: set_fileattr failed on pass #%d; %s.\n",
288 fprintf(stderr, "report: set #%d was successful.\n", i);
293 /*-----------------------------------------------------*\
294 |* Get_fileattr loop *|
295 \*-----------------------------------------------------*/
296 if (Vflag) fprintf(stderr, "\nRunning get_fileattr test\n");
297 for (i=0; i < num_files; i++) {
298 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
300 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
301 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
302 test_file, ERR_NAME);
304 if (dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
305 GET_MASK, &dmstat)) {
307 "ERROR: dm_get_fileattr failed on pass #%d, %s\n",
311 comp_stat(stat_arr[i], dmstat, i);
315 /*-----------------------------------------------------*\
316 |* Get_dirattrs loop *|
317 \*-----------------------------------------------------*/
318 if (Vflag) fprintf(stderr, "\nRunning get_dirattrs test\n");
319 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
320 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
324 if ((bufp = (void*)malloc(buflen)) == NULL) {
325 fprintf(stderr, "Can't allocate memory for buffer.\n");
328 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
330 "ERROR: dm_init_attrloc failed with %s.\n",
337 /* printf("About to call get_dirattrs;\tloops=%d\n", loops);
341 oops=dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
342 GET_MASK, &loc, buflen,
345 for (statbuf = bufp; statbuf != NULL;
346 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *)) {
347 chk_name_p = DM_GET_VALUE(statbuf, dt_compname, void *);
348 if (strncmp(chk_name_p, "DMAPI_fileattr_test.", 20)==0) {
349 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
350 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8) i++;
358 "ERROR: dm_get_dirattrs failed with %s.\n",
363 "ERROR: get_dirattrs found %d matching file%s "
364 "(expected %d).\n", i, (i==1)?"":"s", num_files);
367 fprintf(stderr, "report: get_dirattrs successfully "
368 "found %d files in %d loops.\n", i, loops);
371 /*-----------------------------------------------------*\
372 |* Get_bulkattr loop *|
373 \*-----------------------------------------------------*/
374 if (Vflag) fprintf(stderr, "\nRunning get_bulkattr test\n");
375 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
376 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
380 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
381 fprintf(stderr, "ERROR: can't get filesystem handle for %s; %s\n",
386 buflen = 16*sizeof(dm_stat_t); /* 100000000; */
387 if ((bufp = (void*)malloc(buflen)) == NULL) {
388 fprintf(stderr, "Can't allocate memory for buffer.\n");
391 if (dm_init_attrloc(sid, fs_hanp, fs_hlen,
394 "ERROR: dm_init_attrloc failed with %s.\n",
402 oops=dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
403 GET_MASK, &loc, buflen, bufp, &rlen);
405 for( statbuf = bufp; statbuf != NULL;
406 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *) ) {
407 targhanp = DM_GET_VALUE(statbuf, dt_handle, void *);
408 targhlen = DM_GET_LEN(statbuf, dt_handle);
409 if (dm_handle_to_path(hanp, hlen, targhanp, targhlen,
410 (size_t)100, check_name, &rlen)){
412 "Warning: Couldn't get name from handle. (%s)\n",
416 /* Put JUST name (not path) from check_name into chk_name_p */
417 if (chk_name_p = strrchr(check_name, '/')) chk_name_p++;
418 else chk_name_p = check_name;
419 /* Verify that check_name_p holds a testfile name */
420 if (strncmp(chk_name_p, "DMAPI_fileattr_test.",20)==0) {
421 /* Get the test file's number and compare. */
422 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
423 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8)i++;
432 "ERROR: dm_get_bulkattr failed with %s.\n",
435 /* printf("All_file_count: %d. BUFLEN: %d\n",
436 * all_file_count, buflen);
440 "ERROR: get_bulkattr found %d matching file%s "
441 "(expected %d) in %d loops.\n", i, (i==1)?"":"s",
445 fprintf(stderr, "report: get_bulkattr successfully "
446 "found %d files in %d loops.\n", i, loops);
449 /*------------------------*\
450 |* ## Errno subtests ## *|
451 \*------------------------*/
452 printf("\t(errno subtests beginning...)\n");
453 sprintf(test_file, "%s/DMAPI_fileattr_test.ERRNO",
455 sprintf(command, "cp %s %s\n", ls_path, test_file);
458 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
459 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
460 test_file, ERR_NAME);
464 /*------------------------------------*\
465 |* ## dm_set_fileattr() subtests ## *|
466 \*------------------------------------*/
467 /*---------------------------------------------------------*/
468 EXCLTEST("set", hanp, hlen, test_token,
469 dm_set_fileattr(sid, hanp, hlen, test_token,
470 SET_MASK, &fileattr))
471 /*---------------------------------------------------------*/
473 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
475 "Cannot create a test handle (%s); skipping EBADF test\n",
479 ((char *) test_vp)[hlen/2]++;
482 dm_set_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
483 SET_MASK, &fileattr))
484 dm_handle_free(test_vp, hlen);
487 /*---------------------------------------------------------*/
490 dm_set_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
491 SET_MASK, &fileattr))
492 /*---------------------------------------------------------*/
494 PROBLEM: 32 ones as a mask does not produce a "bad mask"
495 EINVAL. If it does not, I suspect nothing will.
498 "set (bad mask) [BROKEN]",
499 dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
500 0xFFFFFFFF, &fileattr))
502 /*---------------------------------------------------------*/
505 dm_set_fileattr(sid, hanp, hlen, 0,
506 SET_MASK, &fileattr))
507 /*---------------------------------------------------------*/
510 dm_set_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
511 SET_MASK, &fileattr))
512 /*---------------------------------------------------------*/
515 /*------------------------------------*\
516 |* ## dm_get_fileattr() subtests ## *|
517 \*------------------------------------*/
518 /*---------------------------------------------------------*/
519 SHAREDTEST("get", hanp, hlen, test_token,
520 dm_get_fileattr(sid, hanp, hlen, test_token,
522 /*---------------------------------------------------------*/
524 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
526 "Cannot create a test handle (%s); skipping EBADF test\n",
530 ((char *) test_vp)[hlen/2]++;
533 dm_get_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
535 dm_handle_free(test_vp, hlen);
538 /*---------------------------------------------------------*/
541 dm_get_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
545 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
546 GET_MASK, (dm_stat_t *)(-1000)))
547 /*---------------------------------------------------------*/
549 PROBLEM: 32 ones as a mask does not produce a "bad mask"
550 EINVAL. If it does not, I suspect nothing will.
553 "get (bad mask) [BROKEN]",
554 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
555 0xFFFFFFFF, &dmstat))
557 /*---------------------------------------------------------*/
560 dm_get_fileattr(sid, hanp, hlen, 0,
562 /*---------------------------------------------------------*/
565 dm_get_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
567 /*---------------------------------------------------------*/
570 dm_handle_free(hanp, hlen);
572 /*------------------------------------*\
573 |* ## dm_get_dirattrs() subtests ## *|
574 \*------------------------------------*/
575 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
576 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
579 else if ((bufp = (void*)malloc(buflen)) == NULL) {
580 fprintf(stderr, "Can't allocate memory for buffer.\n");
583 /*---------------------------------------------------------*/
584 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
586 "ERROR: dm_init_attrloc failed with %s.\n",
589 SHAREDTEST("get_dir", hanp, hlen, test_token,
590 dm_get_dirattrs(sid, hanp, hlen, test_token,
591 GET_MASK, &loc, buflen, bufp, &rlen))
593 /*---------------------------------------------------------*/
595 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
596 fprintf(stderr, "ERROR: dm_init_attrloc failed with %s.\n",
599 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
600 fprintf(stderr, "Cannot create a test handle (%s); "
601 "skipping EBADF test\n", ERR_NAME);
604 ((char *) test_vp)[hlen/2]++;
607 dm_get_dirattrs(sid, test_vp, hlen, DM_NO_TOKEN,
608 GET_MASK, &loc, buflen, bufp, &rlen))
609 dm_handle_free(test_vp, hlen);
612 /*---------------------------------------------------------*/
613 { void *p = (void *)(((char *)bufp)+1);
614 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
616 "ERROR: dm_init_attrloc failed with %s.\n",
621 This would test alignment. Right now, no error occurs
622 when the buffer is "out of sync" with struct size.
623 It makes it tough to read from the buffer, tho!
625 "get_dir (bad bufp)",
626 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
627 GET_MASK, &loc, buflen, p, &rlen))
630 "get_dir (bad locp)",
631 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
632 GET_MASK, (dm_attrloc_t*)(-1000),
633 buflen, bufp, &rlen))
635 "get_dir (bad bufp)",
636 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
637 GET_MASK, &loc, buflen,
638 (void*)(-1000), &rlen))
640 "get_dir (bad rlenp)",
641 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
642 GET_MASK, &loc, buflen, bufp,
646 /*---------------------------------------------------------*/
647 /*---------------------------------------------------------*/
650 /*------------------------------------*\
651 |* ## dm_get_bulkattr() subtests ## *|
652 \*------------------------------------*/
653 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
654 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
657 else if ((bufp = (void*)malloc(buflen)) == NULL) {
658 fprintf(stderr, "Can't allocate memory for buffer.\n");
661 /*---------------------------------------------------------*/
662 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
664 "ERROR: dm_init_attrloc failed with %s.\n",
668 SHAREDTEST("get_bulk", fs_hanp, fs_hlen, test_token,
669 dm_get_bulkattr(sid, fs_hanp, fs_hlen, test_token,
670 GET_MASK, &loc, buflen, bufp, &rlen))
672 /*---------------------------------------------------------*/
673 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
675 "ERROR: dm_init_attrloc failed with %s.\n",
679 void *p = (void *)(((char *)bufp)+1);
680 ERRTEST(EFAULT, "get_bulk (bad bufp)",
681 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
682 GET_MASK, &loc, buflen,
683 (void *)(-1000), &rlen))
684 ERRTEST(EFAULT, "get_bulk (bad locp)",
685 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
686 GET_MASK, (dm_attrloc_t *)(-1000),
687 buflen, bufp, &rlen))
688 ERRTEST(EFAULT, "get_bulk (bad rlenp)",
689 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
690 GET_MASK, &loc, buflen, bufp,
692 ERRTEST(EFAULT, "get_bulk (bad bufp)",
693 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
694 GET_MASK, &loc, buflen, p, &rlen))
696 /*---------------------------------------------------------*/
699 sprintf(command, "rm %s/DMAPI_fileattr_test.ERRNO\n", pathname);
701 printf("\t(errno subtests complete)\n");
702 /*---------------------*\
703 |* End of errno tests *|
704 \*---------------------*/
707 /* File deletion loop */
708 if (Vflag) printf("(Deleting test files...)\n");
709 for (i=0; i < num_files; i++) {
710 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
712 sprintf(command, "rm %s\n", test_file);
715 printf("File attribute tests complete.\n");