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