Undoes mod: xfs-cmds:slinx:120772a
[xfstests-dev.git] / dmapi / src / suite2 / src / test_dmattr.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
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.
7  * 
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.
11  * 
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.
18  * 
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.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <limits.h>
37
38 #include <lib/hsm.h>
39 #include <lib/errtest.h>
40
41 #include <getopt.h>
42 #include <string.h>
43
44
45 /*---------------------------------------------------------------------------
46 Automated test of the DMAPI functions: 
47      dm_set_dmattr()
48      dm_get_dmattr()
49      dm_remove_dmattr()
50
51 The command line is:
52
53         test_dmattr [-v] [-n num] [-l length] [-s sid] directory 
54
55 where 
56    ls_path 
57      is the path to a specific copy of ls, important only for its size
58    directory 
59      is the pathname to a DMAPI filesystem
60    num
61      is the number of files to create for the test.
62    length
63      is the length of the attribute value for the test.
64    sid 
65      is the session ID whose attributes you are interested in.
66
67 ----------------------------------------------------------------------------*/
68
69 #define VALUE_LENGTH 22
70 #define NUM_ITERATIONS 50
71 #ifndef linux
72 extern  char    *sys_errlist[];
73 #endif
74 extern  int     optind;
75 extern  char    *optarg;
76
77
78 char    *Progname;
79
80 static void
81 usage(void)
82 {
83         fprintf(stderr, "usage:\t%s [-v] [-n number] [-l length] "
84                 "[-s sid] ls_path pathname\n", Progname);
85         exit(1);
86 }
87
88
89 int
90 main(
91         int     argc, 
92         char    **argv)
93 {
94         dm_sessid_t     sid = DM_NO_SESSION;
95         char            *dir_name;
96         char            *ls_path;
97         dm_attrname_t   *attrnamep;
98         size_t          buflen=VALUE_LENGTH;
99         char            *bufp;
100         int             setdtime = 0;
101         size_t          rlenp;
102         void            *hanp;
103         size_t          hlen;
104         char            *name;
105         int             opt;
106         int             i=0;
107         int             j=0;
108         int             Vflag=0;
109         int             num_iter = NUM_ITERATIONS;
110         char            test_file[128];
111         char            command[128];
112         char            **test_array;
113         dm_size_t       config_retval;
114         dm_token_t      test_token;
115         struct stat    *statbuf;
116         struct stat    *checkbuf;
117
118         if (Progname = strrchr(argv[0], '/')) {
119                 Progname++;
120         } else {
121                 Progname = argv[0];
122         }
123
124         /* Crack and validate the command line options. */
125
126         while ((opt = getopt(argc, argv, "vn:l:s:")) != EOF) {
127                 switch (opt) {
128                 case 'v':
129                         Vflag++;
130                         break;
131                 case 'n':
132                         num_iter = atoi(optarg);
133                         break;
134                 case 'l':
135                         buflen = atoi(optarg);
136                         break;
137                 case 's':
138                         sid = atol(optarg);
139                         break;
140                 case '?':
141                         usage();
142                 }
143         }
144         if (optind + 2 != argc)
145                 usage();
146         ls_path = argv[optind];
147         dir_name = argv[optind+1];
148
149         bufp =
150           (char *)(malloc (buflen * sizeof(char)));  
151         statbuf = 
152           (struct stat *)(malloc (num_iter * sizeof(struct stat)));
153         checkbuf = 
154           (struct stat *)(malloc (num_iter * sizeof(struct stat)));
155         test_array = 
156           (char **)(malloc (num_iter * sizeof(char *)));
157         if (bufp==NULL || test_array==NULL || 
158             statbuf==NULL || checkbuf==NULL) {
159           printf("Malloc failed\n");
160           exit(1);
161         }
162         for (i=0; i<num_iter; i++) {
163           test_array[i] =
164             (char*)(malloc (buflen * sizeof(char)));
165           if (test_array[i] == NULL) {
166             printf("Malloc failed\n");
167             exit(1);
168           }
169         }
170
171         if (dm_init_service(&name) == -1)  {
172                 fprintf(stderr, "Can't initialize the DMAPI\n");
173                 exit(1);
174         }
175         if (sid == DM_NO_SESSION)
176                 find_test_session(&sid);
177         
178         printf("Attribute tests beginning...\n");
179         
180         attrnamep = (dm_attrname_t *)("DMATTR");
181
182         /* File creation loop*/
183         for (i=0; i < num_iter; i++) {
184           sprintf(test_file, "%s/DMAPI_attribute_test_file.%d", 
185                   dir_name, i);
186           sprintf(command, "cp %s %s \n", ls_path, test_file); 
187           system(command);
188         }
189
190         /* SET loop */
191         for (i=0; i < num_iter; i++) {
192           sprintf(test_file, "%s/DMAPI_attribute_test_file.%d", 
193                   dir_name, i);
194
195           if (stat(test_file, &(statbuf[i]))){
196             fprintf(stdout, 
197                     "Error: unable to stat the test file; %s (before set)\n", 
198                     test_file);
199           }
200           if (dm_path_to_handle(test_file, &hanp, &hlen)) {
201             fprintf(stderr, "can't get handle for %s; bypassing test\n",
202                     test_file);
203           }
204           else {
205             for (j=0; j < VALUE_LENGTH; j++) {
206               test_array[i][j]=(char)(rand()/128);;
207             } 
208             /* buflen is already set (to VALUE_LENGTH) */
209             if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, 
210                               (i<num_iter/2)?0:1, buflen, test_array[i])) {
211               fprintf(stderr, "dm_set_dmattr failed on test %d, %s\n",
212                       i, ERR_NAME);
213             }
214             else { 
215               if (Vflag){
216                 printf("Report: success with set #%d.\n", i);
217               }
218             }
219           }
220         }
221                 
222         /* GET loop */
223         for (i=0; i < num_iter; i++) {
224           sprintf(test_file, "%s/DMAPI_attribute_test_file.%d", 
225                   dir_name, i);
226
227           if (stat(test_file, &(checkbuf[i]))){
228             fprintf(stdout, 
229                     "Error: unable to stat the test file; %s (before get)\n", 
230                     test_file);
231           }
232           if (dm_path_to_handle(test_file, &hanp, &hlen)) {
233             fprintf(stderr, "can't get handle for %s; bypassing test\n",
234                     test_file);
235           }
236           else {
237             if (dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, buflen,
238                               bufp, &rlenp)) {
239               if (errno == E2BIG) {
240                 fprintf(stderr, "dm_get_dmattr buffer too small, "
241                         "should be %d bytes\n", rlenp);
242               } else {
243                 fprintf(stderr, "dm_get_dmattr failed (%s) for test file %d\n",
244                         ERR_NAME, i);
245               }
246             }
247             else {
248               /* Compare bufp with test_array[i]: */
249               if (strncmp(test_array[i], bufp, buflen)){
250                 printf("ERROR: failure on get test #%d.\n", i);
251               }
252               else if (Vflag) {
253                 printf("Report: success with get #%d. "
254                        "(output matches expectation)\n",i);
255               }
256             }
257           }
258         }
259         
260         /* It's time for timestamp checking! */
261         for (i=0; i < num_iter; i++) {
262 #ifdef linux
263           if ((statbuf[i].st_atime == checkbuf[i].st_atime) &&
264               (statbuf[i].st_mtime == checkbuf[i].st_mtime) &&
265               (statbuf[i].st_ctime == checkbuf[i].st_ctime))
266 #else
267           if ((statbuf[i].st_atim.tv_sec == checkbuf[i].st_atim.tv_sec) &&
268               (statbuf[i].st_atim.tv_nsec == checkbuf[i].st_atim.tv_nsec) &&
269               (statbuf[i].st_mtim.tv_sec == checkbuf[i].st_mtim.tv_sec) &&
270               (statbuf[i].st_mtim.tv_nsec == checkbuf[i].st_mtim.tv_nsec) &&
271               (statbuf[i].st_ctim.tv_sec == checkbuf[i].st_ctim.tv_sec) &&
272               (statbuf[i].st_ctim.tv_nsec == checkbuf[i].st_ctim.tv_nsec))
273 #endif
274           {
275             if (i < num_iter/2) {
276               /* Time stamp did not change, correctly */
277                 if (Vflag) {
278                 fprintf(stdout, "Report: Time stamp was correctly "
279                         "unchanged by test %d.\n", i);
280                 }
281             }
282             else {
283               /* Time stamp did not change, but should have */
284               fprintf(stdout, "Error: the time stamp should have "
285                       "changed in test file %d\n", i);
286             }
287           }
288           else {
289             /* Time stamp changed, but should not have. */
290             if (i < num_iter/2) {
291               fprintf(stdout, "Error: the time stamp should not"
292                       "change in test file %d\n", i);
293             }
294             else {
295             /* Time stamp changed, and should  have. */
296               if (Vflag) {
297                 fprintf(stdout, "Report: Time stamp was correctly "
298                         "changed by test %d.\n", i);
299               }
300             }
301           }
302         }
303
304         
305         /* REMOVE loop */
306         for (i=0; i < num_iter; i++) {
307           sprintf(test_file, "%s/DMAPI_attribute_test_file.%d", 
308                   dir_name, i);
309
310           if (dm_path_to_handle(test_file, &hanp, &hlen)) {
311             fprintf(stderr, "can't get handle for %s; bypassing test\n",
312                     test_file);
313           }
314           else {
315             if (dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN, setdtime,
316                                  attrnamep)) {
317               fprintf(stderr, "dm_remove_dmattr failed (%s) on test #%d\n",
318                       ERR_NAME, i);
319             }
320             else {
321               if (Vflag) {
322                 printf("Report: success with remove test #%d.\n",i);
323               }
324             }
325           }
326         }
327
328         for (i=0; i < num_iter; i++) {
329           sprintf(test_file, "%s/DMAPI_attribute_test_file.%d", 
330                   dir_name, i);
331           sprintf(command, "rm %s \n", test_file); 
332           system(command);
333         }
334
335         /*************************************\
336         |* Correct-input testing complete.   *|
337         |* Beginning improper-input testing. *|
338         \*************************************/
339         sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO", 
340                 dir_name);
341         sprintf(command, "cp %s %s\n", ls_path, test_file); 
342         system(command);
343         
344         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
345           fprintf(stderr, "can't get handle for %s; bypassing errno tests\n",
346                   test_file);
347         }
348         else {
349           
350           printf("\t(errno subtests beginning...)\n");
351           /**** SET tests ****/
352           /*---------------------------------------------------------*/
353           dm_get_config(hanp, hlen, DM_CONFIG_MAX_ATTRIBUTE_SIZE, 
354                         &config_retval);
355           
356           ERRTEST(E2BIG,
357                   "set", 
358                   dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
359                                  attrnamep, setdtime, (config_retval+1), 
360                                 "foofoofoo"))
361           /*---------------------------------------------------------*/
362           EXCLTEST("set", hanp, hlen, test_token, 
363                    dm_set_dmattr(sid, hanp, hlen, test_token,
364                                  attrnamep, 0, buflen, "no right"))
365           /*---------------------------------------------------------*/
366           { void* test_vp;
367             if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
368               fprintf(stderr, "Cannot create a test handle (%s); "
369                       "skipping EBADF test\n", ERR_NAME);
370             }
371             else {
372               ((char *) test_vp)[hlen/2]++;
373               ERRTEST(EBADF,
374                       "set",
375                       dm_set_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
376                                     attrnamep, 0, buflen, "EBADF"))
377                 dm_handle_free(test_vp, hlen);
378             }
379           }
380           /*---------------------------------------------------------*/
381           ERRTEST(EBADF,
382                   "set",
383                   dm_set_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
384                                 attrnamep, 0, buflen, "EBADF"))
385           /*---------------------------------------------------------*/
386           ERRTEST(EFAULT,
387                   "set",
388                   dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
389                                 (dm_attrname_t*)(-1000), 0,
390                                 buflen, "EFAULT_test" ))
391           ERRTEST(EFAULT,
392                   "set",
393                   dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
394                                 attrnamep, 0, buflen, (void*)(-1000)))
395           /*---------------------------------------------------------*/
396           ERRTEST(EINVAL, 
397                   "set (bad token)",
398                   dm_set_dmattr(sid, hanp, hlen, (dm_token_t)(-1000), 
399                                 attrnamep, 0, buflen, 
400                                 "EINVAL_bad_token"))
401           /*---------------------------------------------------------*/
402           ERRTEST(EINVAL, 
403                   "set (bad session id)",
404                   dm_set_dmattr((dm_sessid_t)(-1000), hanp, hlen, 
405                                 DM_NO_TOKEN, attrnamep, 0, buflen, 
406                                 "EINVAL_bad_session_id"))
407             
408           /**** GET tests ****/
409           /*---------------------------------------------------------*/
410           dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
411                         attrnamep, 0, buflen,
412                         "ERRNO for GET_DMATTR");
413           /*---------------------------------------------------------*/
414           ERRTEST(E2BIG,
415                   "get",
416                   dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
417                                 attrnamep, 0, bufp, &rlenp))
418           /*---------------------------------------------------------*/
419           SHAREDTEST("get", hanp, hlen, test_token, 
420                      dm_get_dmattr(sid, hanp, hlen, test_token,
421                                    attrnamep, buflen, bufp, &rlenp))
422           /*---------------------------------------------------------*/
423           { void* test_vp;
424             if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
425               fprintf(stderr, "Cannot create a test handle (%s); "
426                       "skipping EBADF test\n", ERR_NAME);
427             }
428             else {
429               ((char *) test_vp)[hlen/2]++;
430               ERRTEST(EBADF,
431                       "get",
432                       dm_get_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
433                                     attrnamep, buflen, bufp, &rlenp))
434                 dm_handle_free(test_vp, hlen);
435             }
436           }
437           /*---------------------------------------------------------*/
438           ERRTEST(EBADF,
439                   "get",
440                   dm_get_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
441                                 attrnamep, buflen, bufp, &rlenp))
442           /*---------------------------------------------------------*/
443           ERRTEST(EINVAL,
444                   "get (invalid session)",
445                   dm_get_dmattr((dm_sessid_t)(-1000), hanp, hlen, DM_NO_TOKEN,
446                                 attrnamep, buflen, bufp, &rlenp))
447           /*---------------------------------------------------------*/
448           ERRTEST(EINVAL,
449                   "get (invalid token)",
450                   dm_get_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
451                                 attrnamep, buflen, bufp, &rlenp))
452           /*---------------------------------------------------------*/
453           ERRTEST(ENOENT,
454                   "get",
455                   dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
456                                 (dm_attrname_t *)("NO_SUCH_ENTRY"),
457                                 buflen, bufp, &rlenp))
458           /*---------------------------------------------------------*/
459         
460           /**** REMOVE tests ****/
461           /*---------------------------------------------------------*/
462           dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
463                         attrnamep, 0, buflen,
464                         "ERRNO for DMATTR");
465           EXCLTEST("remove", hanp, hlen, test_token,
466                    dm_remove_dmattr(sid, hanp, hlen, test_token, 
467                                     0, attrnamep))
468           /*---------------------------------------------------------*/
469           { void* test_vp;
470             if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, 
471                               0, buflen, "ERRNO for DMATTR")) {
472               printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
473                      ERR_NAME);
474             } 
475             else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
476               fprintf(stderr, "Cannot create a test handle (%s); "
477                       "skipping EBADF test\n", ERR_NAME);
478             }
479             else {
480               ((char *) test_vp)[hlen/2]++;
481               ERRTEST(EBADF,
482                       "remove",
483                       dm_remove_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
484                                        0, attrnamep))
485                 dm_handle_free(test_vp, hlen);
486             }
487           }
488           /*---------------------------------------------------------*/
489           if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, 0, 
490                             buflen, "ERRNO for DMATTR")) {
491             printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
492                    ERR_NAME);
493           } 
494           else {
495             ERRTEST(EBADF,
496                     "remove",
497                     dm_remove_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
498                                      0, attrnamep))
499           }
500           /*---------------------------------------------------------*/
501           ERRTEST(EFAULT,
502                   "remove",
503                   dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
504                                  0, (void*)(-1000)))
505           /*---------------------------------------------------------*/
506           ERRTEST(EINVAL,
507                   "remove (bad token)",
508                   dm_remove_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
509                                  0, attrnamep))
510           /*---------------------------------------------------------*/
511           ERRTEST(EINVAL,
512                   "remove (bad session)",
513                   dm_remove_dmattr(-1, hanp, hlen, DM_NO_TOKEN,
514                                  0, attrnamep))
515           /*---------------------------------------------------------*/
516
517
518          sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO", 
519                  dir_name);
520          sprintf(command, "rm %s\n", test_file); 
521          system(command);
522          printf("\t(errno subtests complete)\n");
523         }
524         /**********************************\
525         |* End of improper-input testing. *|
526         \**********************************/
527
528
529         printf("Attribute tests complete!\n");
530
531         dm_handle_free(hanp, hlen);
532         exit(0);
533 }