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