Undoes mod: xfs-cmds:slinx:120772a
[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 #include <getopt.h>
39 #include <string.h>
40
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
71 u_int reg_flags[8] = {DM_REGION_NOEVENT,
72                       DM_REGION_READ,
73                       DM_REGION_WRITE,
74                       DM_REGION_TRUNCATE,
75                       DM_REGION_READ | DM_REGION_WRITE,
76                       DM_REGION_READ | DM_REGION_TRUNCATE,
77                       DM_REGION_WRITE | DM_REGION_TRUNCATE,
78                       DM_REGION_READ | DM_REGION_WRITE | DM_REGION_TRUNCATE};
79
80
81
82 static void
83 usage(void)
84 {
85         fprintf(stderr, "usage:\t%s [-s sid] ls_path pathname\n", Progname);
86         exit(1);
87 }
88
89
90 int
91 main(
92         int     argc, 
93         char    **argv)
94 {
95         dm_region_t     region = { 0, 0, 0 };
96         dm_region_t     checkregion = { 0, 0, 0 };
97         dm_sessid_t     sid = DM_NO_SESSION;
98         char            *dir_name = NULL;
99         char            *ls_path = NULL;
100         char            object[128];
101         char            command[128];
102         u_int           exactflag;
103         u_int           nelem_read = 0;
104         void            *hanp;
105         size_t           hlen;
106         char            *name;
107         int             opt;
108         int             i;
109         int             Vflag = 0;
110         dm_token_t      test_token = DM_NO_TOKEN;
111
112         if (Progname = strrchr(argv[0], '/')) {
113                 Progname++;
114         } else {
115                 Progname = argv[0];
116         }
117
118         /* Crack and validate the command line options. */
119
120         while ((opt = getopt(argc, argv, "vo:l:s:")) != EOF) {
121                 switch (opt) {
122                 case 'v':
123                         Vflag++;
124                         break;
125                 case 'o':
126                         region.rg_offset = atol(optarg);
127                         break;
128                 case 'l':
129                         region.rg_size = atol(optarg);
130                         break;
131                 case 's':
132                         sid = atol(optarg);
133                         break;
134                 case '?':
135                         usage();
136                 }
137         }
138         if (optind + 2 > argc)
139                 usage();
140         ls_path = argv[optind];
141         dir_name = argv[optind+1];
142
143         if (dm_init_service(&name) == -1)  {
144                 fprintf(stderr, "Can't initialize the DMAPI\n");
145                 exit(1);
146         }
147         if (sid == DM_NO_SESSION)
148                 find_test_session(&sid);
149
150         /***********************************************\
151         |* Beginning the testing of set/get_region...  *|
152         \***********************************************/
153
154         printf("Region test beginning...\n");
155         sprintf(object, "%s/VeryLongUnlikelyFilename.REGIONTEST", dir_name);
156         sprintf(command, "cp %s %s \n", ls_path, object); 
157         system(command);
158         
159         /* Get the test file's handle. */
160         if (dm_path_to_handle(object, &hanp, &hlen)) {
161                 fprintf(stderr, "can't get handle for file %s\n", object);
162                 exit(1);
163         }
164         /* Loop over all possible region flag combinations, 
165          * setting and getting. See what works!  
166          */
167         for (i = 0; i < 8; i++) {
168                 region.rg_flags = reg_flags[i];
169                 if (dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, NELEM, 
170                                   &region, &exactflag)) {
171                   fprintf(stderr, "dm_set_region failed, %s\n",
172                           ERR_NAME);
173                   continue;
174                 }
175                 if (exactflag != DM_TRUE){
176                   fprintf(stdout, "oops...exactflag was false!\n");
177                 }
178                 if (dm_get_region(sid, hanp, hlen, DM_NO_TOKEN, NELEM,
179                                   &checkregion, &nelem_read)) {
180                   fprintf(stderr, "dm_get_region failed, %s\n",
181                           ERR_NAME);
182                   continue;
183                 }
184                 if (region.rg_flags != checkregion.rg_flags) {
185                   fprintf(stdout, "set region flags %d, but found %d\n", 
186                           region.rg_flags, checkregion.rg_flags);
187                 }
188                 else if (Vflag) {
189                   fprintf(stdout, "Test #%d okay\n", i);
190                 }
191         }
192
193         /*************************************\
194         |* Correct-input testing complete.   *|
195         |* Beginning improper-input testing. *|
196         \*************************************/
197         printf("\t(errno subtests beginning...)\n");
198         region.rg_flags = 7;
199         
200         /**** SET tests ****/
201         /*---------------------------------------------------------*/
202         ERRTEST(E2BIG,
203                 "set", 
204                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, 
205                               2, &region, &exactflag))
206         ERRTEST(E2BIG,
207                 "set", 
208                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, 
209                               -1, &region, &exactflag))
210         /*---------------------------------------------------------*/
211         EXCLTEST("set", hanp, hlen, test_token, 
212                  dm_set_region(sid, hanp, hlen, test_token,
213                                NELEM, &region, &exactflag)) 
214         /*---------------------------------------------------------*/
215         ERRTEST(EFAULT,
216                 "set",
217                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN,
218                              NELEM, (dm_region_t*)(-1000), &exactflag))
219         ERRTEST(EFAULT,
220                 "set",
221                 dm_set_region(sid, hanp, hlen, DM_NO_TOKEN,
222                              NELEM, &region, (dm_boolean_t*)(-1000)))
223         /*---------------------------------------------------------*/
224         ERRTEST(EINVAL, 
225                 "set (bad session id)",
226                 dm_set_region(-100, hanp, hlen, DM_NO_TOKEN, 
227                               NELEM, &region, &exactflag))
228         /*---------------------------------------------------------*/
229         
230         /**** GET tests ****/
231         /*---------------------------------------------------------*/
232           ERRTEST (E2BIG,
233                  "get", 
234                  dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
235                                0, &checkregion, &nelem_read))
236         /*---------------------------------------------------------*/
237         ERRTEST(EFAULT,
238                 "get (bad handle)",
239                 dm_get_region(sid, NULL, hlen, DM_NO_TOKEN,
240                               NELEM, &checkregion, &nelem_read)) 
241         /*---------------------------------------------------------*/
242         ERRTEST(EFAULT,
243                 "get (bad regbufp)",
244                 dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
245                               NELEM, (dm_region_t *)(-1000), &nelem_read)) 
246         /*---------------------------------------------------------*/
247         ERRTEST(EFAULT,
248                 "get (bad nelemp)",
249                 dm_get_region(sid, hanp, hlen, DM_NO_TOKEN,
250                               NELEM, &checkregion, (u_int *)(-1000))) 
251         /*---------------------------------------------------------*/
252         SHAREDTEST("get", hanp, hlen, test_token, 
253                    dm_get_region(sid, hanp, hlen, test_token,
254                                  NELEM, &checkregion, &nelem_read))
255         /*---------------------------------------------------------*/
256         ERRTEST(EINVAL, 
257                 "get", 
258                 dm_get_region(-100, hanp, hlen, DM_NO_TOKEN,
259                               NELEM, &checkregion, &nelem_read))
260         /*---------------------------------------------------------*/
261           printf("\t(errno subtests complete)\n");
262         /**********************************\
263         |* End of improper-input testing. *|
264         \**********************************/
265
266         sprintf(command, "rm %s \n", object); 
267         system(command);
268         printf("Region test complete.\n");
269         
270         /***********************************\
271         |* End of set/get_region testing.  *|
272         \***********************************/
273
274         dm_handle_free(hanp, hlen);
275         exit(0);
276 }