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