dmapi: spdx license conversion
[xfstests-dev.git] / dmapi / src / suite2 / src / test_region.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 <sys/types.h>
8
9 #include <lib/hsm.h>
10 #include <lib/errtest.h>
11
12 #include <getopt.h>
13 #include <string.h>
14
15
16 /*---------------------------------------------------------------------------
17
18 Automated test of the DMAPI functions dm_set_region() and dm_get_region().
19
20 The command line is:
21
22         test_region [-s sid] ls_path directory 
23
24 where 
25    pathname
26       is the name of a file
27    ls_path
28       is the path to a specific copy of ls, important only for its size
29    sid
30       is the session ID whose events you you are interested in.
31 ----------------------------------------------------------------------------*/
32
33 #define NELEM   1
34
35 #ifndef linux
36 extern  char    *sys_errlist[];
37 #endif
38 extern  int     optind;
39 extern  char    *optarg;
40
41
42 char    *Progname;
43
44
45 u_int reg_flags[8] = {DM_REGION_NOEVENT,
46                       DM_REGION_READ,
47                       DM_REGION_WRITE,
48                       DM_REGION_TRUNCATE,
49                       DM_REGION_READ | DM_REGION_WRITE,
50                       DM_REGION_READ | DM_REGION_TRUNCATE,
51                       DM_REGION_WRITE | DM_REGION_TRUNCATE,
52                       DM_REGION_READ | DM_REGION_WRITE | DM_REGION_TRUNCATE};
53
54
55
56 static void
57 usage(void)
58 {
59         fprintf(stderr, "usage:\t%s [-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_region_t     region = { 0, 0, 0 };
70         dm_region_t     checkregion = { 0, 0, 0 };
71         dm_sessid_t     sid = DM_NO_SESSION;
72         char            *dir_name = NULL;
73         char            *ls_path = NULL;
74         char            object[128];
75         char            command[128];
76         u_int           exactflag;
77         u_int           nelem_read = 0;
78         void            *hanp;
79         size_t           hlen;
80         char            *name;
81         int             opt;
82         int             i;
83         int             Vflag = 0;
84         dm_token_t      test_token = DM_NO_TOKEN;
85
86         Progname = strrchr(argv[0], '/');
87         if (Progname) {
88                 Progname++;
89         } else {
90                 Progname = argv[0];
91         }
92
93         /* Crack and validate the command line options. */
94
95         while ((opt = getopt(argc, argv, "vo:l:s:")) != EOF) {
96                 switch (opt) {
97                 case 'v':
98                         Vflag++;
99                         break;
100                 case 'o':
101                         region.rg_offset = atol(optarg);
102                         break;
103                 case 'l':
104                         region.rg_size = atol(optarg);
105                         break;
106                 case 's':
107                         sid = atol(optarg);
108                         break;
109                 case '?':
110                         usage();
111                 }
112         }
113         if (optind + 2 > argc)
114                 usage();
115         ls_path = argv[optind];
116         dir_name = argv[optind+1];
117
118         if (dm_init_service(&name) == -1)  {
119                 fprintf(stderr, "Can't initialize the DMAPI\n");
120                 exit(1);
121         }
122         if (sid == DM_NO_SESSION)
123                 find_test_session(&sid);
124
125         /***********************************************\
126         |* Beginning the testing of set/get_region...  *|
127         \***********************************************/
128
129         printf("Region test beginning...\n");
130         sprintf(object, "%s/VeryLongUnlikelyFilename.REGIONTEST", dir_name);
131         sprintf(command, "cp %s %s \n", ls_path, object); 
132         system(command);
133         
134         /* Get the test file's handle. */
135         if (dm_path_to_handle(object, &hanp, &hlen)) {
136                 fprintf(stderr, "can't get handle for file %s\n", object);
137                 exit(1);
138         }
139         /* Loop over all possible region flag combinations, 
140          * setting and getting. See what works!  
141          */
142         for (i = 0; i < 8; i++) {
143                 region.rg_flags = reg_flags[i];
144                 if (dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, NELEM, 
145                                   &region, &exactflag)) {
146                   fprintf(stderr, "dm_set_region failed, %s\n",
147                           ERR_NAME);
148                   continue;
149                 }
150                 if (exactflag != DM_TRUE){
151                   fprintf(stdout, "oops...exactflag was false!\n");
152                 }
153                 if (dm_get_region(sid, hanp, hlen, DM_NO_TOKEN, NELEM,
154                                   &checkregion, &nelem_read)) {
155                   fprintf(stderr, "dm_get_region failed, %s\n",
156                           ERR_NAME);
157                   continue;
158                 }
159                 if (region.rg_flags != checkregion.rg_flags) {
160                   fprintf(stdout, "set region flags %d, but found %d\n", 
161                           region.rg_flags, checkregion.rg_flags);
162                 }
163                 else if (Vflag) {
164                   fprintf(stdout, "Test #%d okay\n", i);
165                 }
166         }
167
168         /*************************************\
169         |* Correct-input testing complete.   *|
170         |* Beginning improper-input testing. *|
171         \*************************************/
172         printf("\t(errno subtests beginning...)\n");
173         region.rg_flags = 7;
174         
175         /**** SET tests ****/
176         /*---------------------------------------------------------*/
177         ERRTEST(E2BIG,
178                 "set", 
179                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, 
180                               2, &region, &exactflag))
181         ERRTEST(E2BIG,
182                 "set", 
183                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, 
184                               -1, &region, &exactflag))
185         /*---------------------------------------------------------*/
186         EXCLTEST("set", hanp, hlen, test_token, 
187                  dm_set_region(sid, hanp, hlen, test_token,
188                                NELEM, &region, &exactflag)) 
189         /*---------------------------------------------------------*/
190         ERRTEST(EFAULT,
191                 "set",
192                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN,
193                              NELEM, (dm_region_t*)(-1000), &exactflag))
194         ERRTEST(EFAULT,
195                 "set",
196                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN,
197                              NELEM, &region, (dm_boolean_t*)(-1000)))
198         /*---------------------------------------------------------*/
199         ERRTEST(EINVAL, 
200                 "set (bad session id)",
201                 dm_set_region(-100, hanp, hlen, DM_NO_TOKEN, 
202                               NELEM, &region, &exactflag))
203         /*---------------------------------------------------------*/
204         
205         /**** GET tests ****/
206         /*---------------------------------------------------------*/
207           ERRTEST (E2BIG,
208                  "get", 
209                  dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
210                                0, &checkregion, &nelem_read))
211         /*---------------------------------------------------------*/
212         ERRTEST(EFAULT,
213                 "get (bad handle)",
214                 dm_get_region(sid, NULL, hlen, DM_NO_TOKEN,
215                               NELEM, &checkregion, &nelem_read)) 
216         /*---------------------------------------------------------*/
217         ERRTEST(EFAULT,
218                 "get (bad regbufp)",
219                 dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
220                               NELEM, (dm_region_t *)(-1000), &nelem_read)) 
221         /*---------------------------------------------------------*/
222         ERRTEST(EFAULT,
223                 "get (bad nelemp)",
224                 dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
225                               NELEM, &checkregion, (u_int *)(-1000))) 
226         /*---------------------------------------------------------*/
227         SHAREDTEST("get", hanp, hlen, test_token, 
228                    dm_get_region(sid, hanp, hlen, test_token,
229                                  NELEM, &checkregion, &nelem_read))
230         /*---------------------------------------------------------*/
231         ERRTEST(EINVAL, 
232                 "get", 
233                 dm_get_region(-100, hanp, hlen, DM_NO_TOKEN,
234                               NELEM, &checkregion, &nelem_read))
235         /*---------------------------------------------------------*/
236           printf("\t(errno subtests complete)\n");
237         /**********************************\
238         |* End of improper-input testing. *|
239         \**********************************/
240
241         sprintf(command, "rm %s \n", object); 
242         system(command);
243         printf("Region test complete.\n");
244         
245         /***********************************\
246         |* End of set/get_region testing.  *|
247         \***********************************/
248
249         dm_handle_free(hanp, hlen);
250         exit(0);
251 }