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 %ld, but found %ld\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);
316 /*-----------------------------------------------------*\
317 |* Get_dirattrs loop *|
318 \*-----------------------------------------------------*/
319 if (Vflag) fprintf(stderr, "\nRunning get_dirattrs test\n");
320 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
321 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
325 if ((bufp = (void*)malloc(buflen)) == NULL) {
326 fprintf(stderr, "Can't allocate memory for buffer.\n");
329 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
331 "ERROR: dm_init_attrloc failed with %s.\n",
338 /* printf("About to call get_dirattrs;\tloops=%d\n", loops);
342 oops=dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
343 GET_MASK, &loc, buflen,
346 for (statbuf = bufp; statbuf != NULL;
347 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *)) {
348 chk_name_p = DM_GET_VALUE(statbuf, dt_compname, void *);
349 if (strncmp(chk_name_p, "DMAPI_fileattr_test.", 20)==0) {
350 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
351 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8) i++;
359 "ERROR: dm_get_dirattrs failed with %s.\n",
364 "ERROR: get_dirattrs found %d matching file%s "
365 "(expected %d).\n", i, (i==1)?"":"s", num_files);
368 fprintf(stderr, "report: get_dirattrs successfully "
369 "found %d files in %d loops.\n", i, loops);
373 /*-----------------------------------------------------*\
374 |* Get_bulkattr loop *|
375 \*-----------------------------------------------------*/
376 if (Vflag) fprintf(stderr, "\nRunning get_bulkattr test\n");
377 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
378 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
382 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
383 fprintf(stderr, "ERROR: can't get filesystem handle for %s; %s\n",
388 buflen = 16*sizeof(dm_stat_t); /* 100000000; */
389 if ((bufp = (void*)malloc(buflen)) == NULL) {
390 fprintf(stderr, "Can't allocate memory for buffer.\n");
393 if (dm_init_attrloc(sid, fs_hanp, fs_hlen,
396 "ERROR: dm_init_attrloc failed with %s.\n",
404 oops=dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
405 GET_MASK, &loc, buflen, bufp, &rlen);
407 for( statbuf = bufp; statbuf != NULL;
408 statbuf = DM_STEP_TO_NEXT(statbuf, dm_stat_t *) ) {
409 targhanp = DM_GET_VALUE(statbuf, dt_handle, void *);
410 targhlen = DM_GET_LEN(statbuf, dt_handle);
411 if (dm_handle_to_path(hanp, hlen, targhanp, targhlen,
412 (size_t)100, check_name, &rlen)){
414 "Warning: Couldn't get name from handle. (%s)\n",
418 /* Put JUST name (not path) from check_name into chk_name_p */
419 if (chk_name_p = strrchr(check_name, '/')) chk_name_p++;
420 else chk_name_p = check_name;
421 /* Verify that check_name_p holds a testfile name */
422 if (strncmp(chk_name_p, "DMAPI_fileattr_test.",20)==0) {
423 /* Get the test file's number and compare. */
424 sscanf(chk_name_p, "DMAPI_fileattr_test.%d", &chk_num);
425 if (comp_stat(stat_arr[chk_num], *statbuf, chk_num)==8)i++;
434 "ERROR: dm_get_bulkattr failed with %s.\n",
437 /* printf("All_file_count: %d. BUFLEN: %d\n",
438 * all_file_count, buflen);
442 "ERROR: get_bulkattr found %d matching file%s "
443 "(expected %d) in %d loops.\n", i, (i==1)?"":"s",
447 fprintf(stderr, "report: get_bulkattr successfully "
448 "found %d files in %d loops.\n", i, loops);
451 /*------------------------*\
452 |* ## Errno subtests ## *|
453 \*------------------------*/
454 printf("\t(errno subtests beginning...)\n");
455 sprintf(test_file, "%s/DMAPI_fileattr_test.ERRNO",
457 sprintf(command, "cp %s %s\n", ls_path, test_file);
460 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
461 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
462 test_file, ERR_NAME);
466 /*------------------------------------*\
467 |* ## dm_set_fileattr() subtests ## *|
468 \*------------------------------------*/
469 /*---------------------------------------------------------*/
470 EXCLTEST("set", hanp, hlen, test_token,
471 dm_set_fileattr(sid, hanp, hlen, test_token,
472 SET_MASK, &fileattr))
473 /*---------------------------------------------------------*/
475 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
477 "Cannot create a test handle (%s); skipping EBADF test\n",
481 ((char *) test_vp)[hlen/2]++;
484 dm_set_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
485 SET_MASK, &fileattr))
486 dm_handle_free(test_vp, hlen);
489 /*---------------------------------------------------------*/
492 dm_set_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
493 SET_MASK, &fileattr))
494 /*---------------------------------------------------------*/
495 /* PROBLEM: 32 ones as a mask does not produce a "bad mask"
496 /* EINVAL. If it doesn't, I suspect nothing will.
498 "set (bad mask) [BROKEN]",
499 dm_set_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
500 0xFFFFFFFF, &fileattr))
501 /*---------------------------------------------------------*/
504 dm_set_fileattr(sid, hanp, hlen, 0,
505 SET_MASK, &fileattr))
506 /*---------------------------------------------------------*/
509 dm_set_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
510 SET_MASK, &fileattr))
511 /*---------------------------------------------------------*/
514 /*------------------------------------*\
515 |* ## dm_get_fileattr() subtests ## *|
516 \*------------------------------------*/
517 /*---------------------------------------------------------*/
518 SHAREDTEST("get", hanp, hlen, test_token,
519 dm_get_fileattr(sid, hanp, hlen, test_token,
521 /*---------------------------------------------------------*/
523 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
525 "Cannot create a test handle (%s); skipping EBADF test\n",
529 ((char *) test_vp)[hlen/2]++;
532 dm_get_fileattr(sid, test_vp, hlen, DM_NO_TOKEN,
534 dm_handle_free(test_vp, hlen);
537 /*---------------------------------------------------------*/
540 dm_get_fileattr(sid, NULL, hlen, DM_NO_TOKEN,
544 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
545 GET_MASK, (dm_stat_t *)(-1000)))
546 /*---------------------------------------------------------*/
547 /* PROBLEM: 32 ones as a mask does not produce a "bad mask"
548 /* EINVAL. If it doesn't, I suspect nothing will.
550 "get (bad mask) [BROKEN]",
551 dm_get_fileattr(sid, hanp, hlen, DM_NO_TOKEN,
552 0xFFFFFFFF, &dmstat))
553 /*---------------------------------------------------------*/
556 dm_get_fileattr(sid, hanp, hlen, 0,
558 /*---------------------------------------------------------*/
561 dm_get_fileattr(-100, hanp, hlen, DM_NO_TOKEN,
563 /*---------------------------------------------------------*/
566 dm_handle_free(hanp, hlen);
568 /*------------------------------------*\
569 |* ## dm_get_dirattrs() subtests ## *|
570 \*------------------------------------*/
571 if (dm_path_to_handle(pathname, &hanp, &hlen)) {
572 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
575 else if ((bufp = (void*)malloc(buflen)) == NULL) {
576 fprintf(stderr, "Can't allocate memory for buffer.\n");
579 /*---------------------------------------------------------*/
580 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
582 "ERROR: dm_init_attrloc failed with %s.\n",
585 SHAREDTEST("get_dir", hanp, hlen, test_token,
586 dm_get_dirattrs(sid, hanp, hlen, test_token,
587 GET_MASK, &loc, buflen, bufp, &rlen))
589 /*---------------------------------------------------------*/
591 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
592 fprintf(stderr, "ERROR: dm_init_attrloc failed with %s.\n",
595 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
596 fprintf(stderr, "Cannot create a test handle (%s); "
597 "skipping EBADF test\n", ERR_NAME);
600 ((char *) test_vp)[hlen/2]++;
603 dm_get_dirattrs(sid, test_vp, hlen, DM_NO_TOKEN,
604 GET_MASK, &loc, buflen, bufp, &rlen))
605 dm_handle_free(test_vp, hlen);
608 /*---------------------------------------------------------*/
609 { void *p = (void *)(((char *)bufp)+1);
610 if (dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN, &loc)){
612 "ERROR: dm_init_attrloc failed with %s.\n",
617 This would test alignment. Right now, no error occurs
618 when the buffer is "out of sync" with struct size.
619 It makes it tough to read from the buffer, tho!
621 "get_dir (bad bufp)",
622 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
623 GET_MASK, &loc, buflen, p, &rlen))
626 "get_dir (bad locp)",
627 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
628 GET_MASK, (dm_attrloc_t*)(-1000),
629 buflen, bufp, &rlen))
631 "get_dir (bad bufp)",
632 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
633 GET_MASK, &loc, buflen,
634 (void*)(-1000), &rlen))
636 "get_dir (bad rlenp)",
637 dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN,
638 GET_MASK, &loc, buflen, bufp,
642 /*---------------------------------------------------------*/
643 /*---------------------------------------------------------*/
647 /*------------------------------------*\
648 |* ## dm_get_bulkattr() subtests ## *|
649 \*------------------------------------*/
650 if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
651 fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
654 else if ((bufp = (void*)malloc(buflen)) == NULL) {
655 fprintf(stderr, "Can't allocate memory for buffer.\n");
658 /*---------------------------------------------------------*/
659 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
661 "ERROR: dm_init_attrloc failed with %s.\n",
665 SHAREDTEST("get_bulk", fs_hanp, fs_hlen, test_token,
666 dm_get_bulkattr(sid, fs_hanp, fs_hlen, test_token,
667 GET_MASK, &loc, buflen, bufp, &rlen))
669 /*---------------------------------------------------------*/
670 if (dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &loc)){
672 "ERROR: dm_init_attrloc failed with %s.\n",
676 void *p = (void *)(((char *)bufp)+1);
677 ERRTEST(EFAULT, "get_bulk (bad bufp)",
678 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
679 GET_MASK, &loc, buflen,
680 (void *)(-1000), &rlen))
681 ERRTEST(EFAULT, "get_bulk (bad locp)",
682 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
683 GET_MASK, (dm_attrloc_t *)(-1000),
684 buflen, bufp, &rlen))
685 ERRTEST(EFAULT, "get_bulk (bad rlenp)",
686 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
687 GET_MASK, &loc, buflen, bufp,
689 ERRTEST(EFAULT, "get_bulk (bad bufp)",
690 dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
691 GET_MASK, &loc, buflen, p, &rlen))
693 /*---------------------------------------------------------*/
696 sprintf(command, "rm %s/DMAPI_fileattr_test.ERRNO\n", pathname);
698 printf("\t(errno subtests complete)\n");
699 /*---------------------*\
700 |* End of errno tests *|
701 \*---------------------*/
704 /* File deletion loop */
705 if (Vflag) printf("(Deleting test files...)\n");
706 for (i=0; i < num_files; i++) {
707 sprintf(test_file, "%s/DMAPI_fileattr_test.%d",
709 sprintf(command, "rm %s\n", test_file);
712 printf("File attribute tests complete.\n");