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