2 * Copyright (c) 2000-2001 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, (long)expected.dt_mode, (long)found.dt_mode);
98 if (found.dt_uid != expected.dt_uid) {
100 "ERROR: get #%d, expected uid %ld, but found %ld\n",
101 i, (long)expected.dt_uid, (long)found.dt_uid);
104 if (found.dt_gid != expected.dt_gid) {
106 "ERROR: get #%d, expected gid %ld, but found %ld\n",
107 i, (long)expected.dt_gid, (long)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;
181 dm_fileattr_t fileattr;
187 size_t buflen = 16*sizeof(dm_stat_t);
196 char check_name[100];
200 if (Progname = strrchr(argv[0], '/')) {
207 while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
213 num_files = atoi(optarg);
222 if (optind + 2 != argc) {
225 ls_path = argv[optind];
226 pathname = argv[optind+1];
228 /* Seed the random number generator */
229 srand((unsigned int)time(NULL));
231 if (dm_init_service(&name) == -1) {
232 fprintf(stderr, "Can't initialize the DMAPI\n");
235 if (sid == DM_NO_SESSION)
236 find_test_session(&sid);
238 /* Dynamically allocate stat_arr; */
240 (dm_stat_t *)malloc(num_files * sizeof(dm_stat_t));
242 printf("Beginning file attribute tests...\n");
244 /* Fill in the dm_stat blocks with lots of junk...
246 for (i=0; i<num_files; i++) {
247 stat_arr[i].dt_atime=(time_t)(rand()+rand()*0x10000);
248 stat_arr[i].dt_mtime=(time_t)(rand()+rand()*0x10000);
249 stat_arr[i].dt_ctime=(time_t)(rand()+rand()*0x10000);
250 stat_arr[i].dt_dtime=(time_t)(rand()+rand()*0x10000);
251 stat_arr[i].dt_uid=(uid_t)(rand()+rand()*0x10000);
252 stat_arr[i].dt_gid=(gid_t)(rand()+rand()*0x10000);
253 stat_arr[i].dt_mode=(mode_t)((rand()%4096)+32768);
254 stat_arr[i].dt_size=(dm_off_t)(rand()+rand()*0x10000);
257 /*-----------------------------------------------------*\
258 |* File creation and set_fileattr loop *|
259 \*-----------------------------------------------------*/
260 if (Vflag) fprintf(stderr, "\nCreating/setting up test files.\n");
261 for (i=0; i < num_files; i++) {
262 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
264 sprintf(command, "cp %s %s \n", ls_path, test_file);
267 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
268 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
269 test_file, ERR_NAME);
272 fileattr.fa_mode = stat_arr[i].dt_mode;
273 fileattr.fa_uid = stat_arr[i].dt_uid;
274 fileattr.fa_gid = stat_arr[i].dt_gid;
275 fileattr.FA_ATIME = stat_arr[i].dt_atime;
276 fileattr.FA_MTIME = stat_arr[i].dt_mtime;
277 fileattr.FA_CTIME = stat_arr[i].dt_ctime;
278 fileattr.FA_DTIME = stat_arr[i].dt_dtime;
279 fileattr.fa_size = stat_arr[i].dt_size;
280 if (dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
281 SET_MASK, &fileattr)) {
282 fprintf(stderr, "ERROR: set_fileattr failed on pass #%d; %s.\n",
286 fprintf(stderr, "report: set #%d was successful.\n", i);
291 /*-----------------------------------------------------*\
292 |* Get_fileattr loop *|
293 \*-----------------------------------------------------*/
294 if (Vflag) fprintf(stderr, "\nRunning get_fileattr test\n");
295 for (i=0; i < num_files; i++) {
296 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
298 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
299 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
300 test_file, ERR_NAME);
302 if (dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
303 GET_MASK, &dmstat)) {
305 "ERROR: dm_get_fileattr failed on pass #%d, %s\n",
309 comp_stat(stat_arr[i], dmstat, i);
313 /*-----------------------------------------------------*\
314 |* Get_dirattrs loop *|
315 \*-----------------------------------------------------*/
316 if (Vflag) fprintf(stderr, "\nRunning get_dirattrs test\n");
317 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
318 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
322 if ((bufp = (void*)malloc(buflen)) == NULL) {
323 fprintf(stderr, "Can't allocate memory for buffer.\n");
326 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
328 "ERROR: dm_init_attrloc failed with %s.\n",
335 /* printf("About to call get_dirattrs;\tloops=%d\n", loops);
339 oops=dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
340 GET_MASK, &loc, buflen,
343 for (statbuf = bufp; statbuf != NULL;
344 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *)) {
345 chk_name_p = DM_GET_VALUE(statbuf, dt_compname, void *);
346 if (strncmp(chk_name_p, "DMAPI_fileattr_test.", 20)==0) {
347 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
348 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8) i++;
356 "ERROR: dm_get_dirattrs failed with %s.\n",
361 "ERROR: get_dirattrs found %d matching file%s "
362 "(expected %d).\n", i, (i==1)?"":"s", num_files);
365 fprintf(stderr, "report: get_dirattrs successfully "
366 "found %d files in %d loops.\n", i, loops);
369 /*-----------------------------------------------------*\
370 |* Get_bulkattr loop *|
371 \*-----------------------------------------------------*/
372 if (Vflag) fprintf(stderr, "\nRunning get_bulkattr test\n");
373 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
374 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
378 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
379 fprintf(stderr, "ERROR: can't get filesystem handle for %s; %s\n",
384 buflen = 16*sizeof(dm_stat_t); /* 100000000; */
385 if ((bufp = (void*)malloc(buflen)) == NULL) {
386 fprintf(stderr, "Can't allocate memory for buffer.\n");
389 if (dm_init_attrloc(sid, fs_hanp, fs_hlen,
392 "ERROR: dm_init_attrloc failed with %s.\n",
400 oops=dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
401 GET_MASK, &loc, buflen, bufp, &rlen);
403 for( statbuf = bufp; statbuf != NULL;
404 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *) ) {
405 targhanp = DM_GET_VALUE(statbuf, dt_handle, void *);
406 targhlen = DM_GET_LEN(statbuf, dt_handle);
407 if (dm_handle_to_path(hanp, hlen, targhanp, targhlen,
408 (size_t)100, check_name, &rlen)){
410 "Warning: Couldn't get name from handle. (%s)\n",
414 /* Put JUST name (not path) from check_name into chk_name_p */
415 if (chk_name_p = strrchr(check_name, '/')) chk_name_p++;
416 else chk_name_p = check_name;
417 /* Verify that check_name_p holds a testfile name */
418 if (strncmp(chk_name_p, "DMAPI_fileattr_test.",20)==0) {
419 /* Get the test file's number and compare. */
420 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
421 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8)i++;
430 "ERROR: dm_get_bulkattr failed with %s.\n",
433 /* printf("All_file_count: %d. BUFLEN: %d\n",
434 * all_file_count, buflen);
438 "ERROR: get_bulkattr found %d matching file%s "
439 "(expected %d) in %d loops.\n", i, (i==1)?"":"s",
443 fprintf(stderr, "report: get_bulkattr successfully "
444 "found %d files in %d loops.\n", i, loops);
447 /*------------------------*\
448 |* ## Errno subtests ## *|
449 \*------------------------*/
450 printf("\t(errno subtests beginning...)\n");
451 sprintf(test_file, "%s/DMAPI_fileattr_test.ERRNO",
453 sprintf(command, "cp %s %s\n", ls_path, test_file);
456 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
457 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
458 test_file, ERR_NAME);
462 /*------------------------------------*\
463 |* ## dm_set_fileattr() subtests ## *|
464 \*------------------------------------*/
465 /*---------------------------------------------------------*/
466 EXCLTEST("set", hanp, hlen, test_token,
467 dm_set_fileattr(sid, hanp, hlen, test_token,
468 SET_MASK, &fileattr))
469 /*---------------------------------------------------------*/
471 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
473 "Cannot create a test handle (%s); skipping EBADF test\n",
477 ((char *) test_vp)[hlen/2]++;
480 dm_set_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
481 SET_MASK, &fileattr))
482 dm_handle_free(test_vp, hlen);
485 /*---------------------------------------------------------*/
488 dm_set_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
489 SET_MASK, &fileattr))
490 /*---------------------------------------------------------*/
492 PROBLEM: 32 ones as a mask does not produce a "bad mask"
493 EINVAL. If it does not, I suspect nothing will.
496 "set (bad mask) [BROKEN]",
497 dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
498 0xFFFFFFFF, &fileattr))
500 /*---------------------------------------------------------*/
503 dm_set_fileattr(sid, hanp, hlen, 0,
504 SET_MASK, &fileattr))
505 /*---------------------------------------------------------*/
508 dm_set_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
509 SET_MASK, &fileattr))
510 /*---------------------------------------------------------*/
513 /*------------------------------------*\
514 |* ## dm_get_fileattr() subtests ## *|
515 \*------------------------------------*/
516 /*---------------------------------------------------------*/
517 SHAREDTEST("get", hanp, hlen, test_token,
518 dm_get_fileattr(sid, hanp, hlen, test_token,
520 /*---------------------------------------------------------*/
522 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
524 "Cannot create a test handle (%s); skipping EBADF test\n",
528 ((char *) test_vp)[hlen/2]++;
531 dm_get_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
533 dm_handle_free(test_vp, hlen);
536 /*---------------------------------------------------------*/
539 dm_get_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
543 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
544 GET_MASK, (dm_stat_t *)(-1000)))
545 /*---------------------------------------------------------*/
547 PROBLEM: 32 ones as a mask does not produce a "bad mask"
548 EINVAL. If it does not, I suspect nothing will.
551 "get (bad mask) [BROKEN]",
552 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
553 0xFFFFFFFF, &dmstat))
555 /*---------------------------------------------------------*/
558 dm_get_fileattr(sid, hanp, hlen, 0,
560 /*---------------------------------------------------------*/
563 dm_get_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
565 /*---------------------------------------------------------*/
568 dm_handle_free(hanp, hlen);
570 /*------------------------------------*\
571 |* ## dm_get_dirattrs() subtests ## *|
572 \*------------------------------------*/
573 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
574 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
577 else if ((bufp = (void*)malloc(buflen)) == NULL) {
578 fprintf(stderr, "Can't allocate memory for buffer.\n");
581 /*---------------------------------------------------------*/
582 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
584 "ERROR: dm_init_attrloc failed with %s.\n",
587 SHAREDTEST("get_dir", hanp, hlen, test_token,
588 dm_get_dirattrs(sid, hanp, hlen, test_token,
589 GET_MASK, &loc, buflen, bufp, &rlen))
591 /*---------------------------------------------------------*/
593 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
594 fprintf(stderr, "ERROR: dm_init_attrloc failed with %s.\n",
597 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
598 fprintf(stderr, "Cannot create a test handle (%s); "
599 "skipping EBADF test\n", ERR_NAME);
602 ((char *) test_vp)[hlen/2]++;
605 dm_get_dirattrs(sid, test_vp, hlen, DM_NO_TOKEN,
606 GET_MASK, &loc, buflen, bufp, &rlen))
607 dm_handle_free(test_vp, hlen);
610 /*---------------------------------------------------------*/
612 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
614 "ERROR: dm_init_attrloc failed with %s.\n",
619 This would test alignment. Right now, no error occurs
620 when the buffer is "out of sync" with struct size.
621 It makes it tough to read from the buffer, tho!
623 "get_dir (bad bufp)",
624 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
625 GET_MASK, &loc, buflen, p, &rlen))
628 "get_dir (bad locp)",
629 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
630 GET_MASK, (dm_attrloc_t*)(-1000),
631 buflen, bufp, &rlen))
633 "get_dir (bad bufp)",
634 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
635 GET_MASK, &loc, buflen,
636 (void*)(-1000), &rlen))
638 "get_dir (bad rlenp)",
639 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
640 GET_MASK, &loc, buflen, bufp,
644 /*---------------------------------------------------------*/
645 /*---------------------------------------------------------*/
648 /*------------------------------------*\
649 |* ## dm_get_bulkattr() subtests ## *|
650 \*------------------------------------*/
651 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
652 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
655 else if ((bufp = (void*)malloc(buflen)) == NULL) {
656 fprintf(stderr, "Can't allocate memory for buffer.\n");
659 /*---------------------------------------------------------*/
660 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
662 "ERROR: dm_init_attrloc failed with %s.\n",
666 SHAREDTEST("get_bulk", fs_hanp, fs_hlen, test_token,
667 dm_get_bulkattr(sid, fs_hanp, fs_hlen, test_token,
668 GET_MASK, &loc, buflen, bufp, &rlen))
670 /*---------------------------------------------------------*/
671 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
673 "ERROR: dm_init_attrloc failed with %s.\n",
677 void *p = (void *)(((char *)bufp)+1);
678 ERRTEST(EFAULT, "get_bulk (bad bufp)",
679 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
680 GET_MASK, &loc, buflen,
681 (void *)(-1000), &rlen))
682 ERRTEST(EFAULT, "get_bulk (bad locp)",
683 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
684 GET_MASK, (dm_attrloc_t *)(-1000),
685 buflen, bufp, &rlen))
686 ERRTEST(EFAULT, "get_bulk (bad rlenp)",
687 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
688 GET_MASK, &loc, buflen, bufp,
690 ERRTEST(EFAULT, "get_bulk (bad bufp)",
691 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
692 GET_MASK, &loc, buflen, p, &rlen))
694 /*---------------------------------------------------------*/
697 sprintf(command, "rm %s/DMAPI_fileattr_test.ERRNO\n", pathname);
699 printf("\t(errno subtests complete)\n");
700 /*---------------------*\
701 |* End of errno tests *|
702 \*---------------------*/
705 /* File deletion loop */
706 if (Vflag) printf("(Deleting test files...)\n");
707 for (i=0; i < num_files; i++) {
708 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
710 sprintf(command, "rm %s\n", test_file);
713 printf("File attribute tests complete.\n");