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