cmd/xfsprogs/libdm/dmapi_tests/README 1.1 Renamed to cmd/xfstests/dmapi/README
[xfstests-dev.git] / dmapi / src / suite2 / src / test_efault.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 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39
40 #include <lib/dmport.h>
41 #include <lib/hsm.h>
42 #include <lib/errtest.h>
43
44 #ifdef linux
45 #include <string.h>
46 #endif
47
48 /*---------------------------------------------------------------------------
49 Automated search for EFAULT in the following DMAPI commands:
50    dm_get_allocinfo
51    dm_get_config
52    dm_get_config_events
53    dm_getall_dmattr
54    dm_init_attrloc
55
56 There are other EFAULT tests, in the programs that test individual 
57 DMAPI functions.  Those other tests are referenced in comments in this source.
58
59 The command line is:
60    test_efault [-s sid] [-v] ls_path pathname
61
62 where:
63    sid
64       is the session ID whose events you you are interested in.
65    ls_path
66       is the path to a copy of ls, which will be copied as a test file.
67    pathname
68       is the filesystem to use for the test.
69 ----------------------------------------------------------------------------*/
70
71 extern  int     optind;
72 extern  int     opterr;
73 extern  char    *optarg;
74
75 char    *Progname;
76 int     Vflag=0;
77
78 static void
79 usage(void)
80 {
81         fprintf(stderr, 
82                 "Usage: %s [-v] [-s sid] ls_path pathname\n",
83                 Progname);
84         exit(1);
85 }
86
87
88 int
89 main(int argc, char **argv) {
90      
91         dm_sessid_t     sid = DM_NO_SESSION;
92         dm_token_t      test_token = DM_NO_TOKEN;
93         void            *hanp;
94         size_t          hlen;
95         char            *name;
96         char            *ls_path;
97         char            *pathname;
98         char            test_file[100];
99         char            command[100];
100         int             opt;
101         int             i=0;
102
103         if (Progname = strrchr(argv[0], '/')) {
104                 Progname++;
105         } else {
106                 Progname = argv[0];
107         }
108
109         opterr = 0;
110         while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
111                 switch (opt) {
112                 case 'v':
113                          Vflag++;
114                          break;
115                 case 's': 
116                          sid = atol(optarg);
117                          break;
118                 case '?':
119                          usage();
120                 }
121         }
122         if (optind + 2 != argc) {
123                 usage();
124         }
125         ls_path = argv[optind];
126         pathname = argv[optind+1];
127
128         if (dm_init_service(&name) == -1)  {
129                 fprintf(stderr, "Can't inititalize the DMAPI\n");
130                 exit(1);
131         }
132         if (sid == DM_NO_SESSION)
133                 find_test_session(&sid);
134         
135         printf("Beginning EFAULT testing...\n");
136
137         /*----------------------------------*\
138         |*  ## Traditional errno tests  ##  *|
139         \*----------------------------------*/
140         sprintf(test_file, "%s/DMAPI_EFAULT_test_file", pathname);
141         sprintf(command, "cp %s %s\n", ls_path, test_file); 
142         system(command);
143         
144         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
145           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
146                   test_file, ERR_NAME);
147           goto abort_test;
148         }
149
150
151         /*---------------------------------------------------------
152          * get_allocinfo
153          *---------------------------------------------------------
154          */
155         { dm_off_t     off=0;
156           u_int        nelem=1;
157           dm_extent_t  extent;
158           dm_extent_t  *extentp;
159           u_int        nelem_ret;
160           
161           ERRTEST(EFAULT, "get_allocinfo (bad offp)",
162                   dm_get_allocinfo(sid, hanp, hlen, DM_NO_TOKEN,
163                                    (dm_off_t*)(-1000), 
164                                    nelem, &extent, &nelem_ret))
165           ERRTEST(EFAULT, "get_allocinfo (bad extentp)",
166                   dm_get_allocinfo(sid, hanp, hlen, DM_NO_TOKEN,
167                                    &off, nelem, (dm_extent_t*)(-1000),
168                                    &nelem_ret))
169           ERRTEST(EFAULT, "get_allocinfo (bad nelemp)",
170                   dm_get_allocinfo(sid, hanp, hlen, DM_NO_TOKEN,
171                                    &off, nelem, &extent,
172                                    (u_int*)(-1000)))
173         }
174    
175         /*------------------------------------------------------
176          * get_bulkattr: see test_fileattr.c
177          *------------------------------------------------------
178          * get_config
179          *------------------------------------------------------
180          */
181           ERRTEST(EFAULT, "get_config", 
182                   dm_get_config(hanp, hlen, DM_CONFIG_BULKALL,
183                                 (dm_size_t*)(-1000)))
184         /*---------------------------------------------------------
185          * get_config_events
186          *---------------------------------------------------------
187          */
188         { 
189           u_int         nelem=5;
190           u_int         *nelemp;
191           dm_eventset_t *eventsetp;
192           eventsetp = (dm_eventset_t *)malloc(nelem*sizeof(dm_eventset_t));
193           if (eventsetp == NULL) {
194             printf("Couldn't malloc for config_events tests: %s\n", ERR_NAME);
195           }
196           else {
197           ERRTEST(EFAULT, "get_config_events (bad eventsetp)",
198                 dm_get_config_events(hanp, hlen, nelem, 
199                                      (dm_eventset_t*)(-1000), nelemp))
200           ERRTEST(EFAULT, "get_config_events (bad nelemp)",
201                   dm_get_config_events(hanp, hlen, nelem, eventsetp, 
202                                        (u_int*)(-1000)))
203           }
204         }
205         /*---------------------------------------------------------
206          * get_dirattrs: see test_fileattr.c
207          *---------------------------------------------------------
208          * get_fileattr: see test_fileattr.c
209          *---------------------------------------------------------
210          * get_region: see test_region.c
211          *---------------------------------------------------------
212          * getall_dmattr
213          *---------------------------------------------------------
214          */
215         { 
216           size_t buflen = 5;
217           void   *bufp  = (void *)malloc(buflen*sizeof(dm_attrlist_t));
218           size_t *rlenp;
219           ERRTEST(EFAULT, "getall_dmattr (NULL handle)", 
220                   dm_getall_dmattr(sid, NULL, hlen, DM_NO_TOKEN,
221                                    buflen, bufp, rlenp))
222           ERRTEST(EFAULT, "getall_dmattr (incremented  bufp)", 
223                   dm_getall_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
224                                    buflen, (void*)((char*)(bufp)+1), rlenp))
225           ERRTEST(EFAULT, "getall_dmattr (bad bufp)", 
226                   dm_getall_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
227                                    buflen, (void*)(-1000), rlenp))
228           ERRTEST(EFAULT, "getall_dmattr (bad rlenp)", 
229                   dm_getall_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
230                                    buflen, bufp, (size_t*)(-1000)))
231         }
232         /*---------------------------------------------------------
233          * init_attrloc
234          *---------------------------------------------------------
235          */
236         ERRTEST(ENOTSUP, "init_attrloc", 
237                 dm_init_attrloc(sid, hanp, hlen, DM_NO_TOKEN,
238                                 (dm_attrloc_t*)(-1000)))
239         /*---------------------------------------------------------
240          * probe_hole: see test_hole.c
241          *---------------------------------------------------------
242          * remove_dmattr: see test_dmattr.c
243          *---------------------------------------------------------
244          * set_dmattr: see test_dmattr.c
245          *---------------------------------------------------------
246          * set_eventlist: see test_eventlist.c
247          *---------------------------------------------------------
248          * set_region: see test_region.c
249          *---------------------------------------------------------
250          */
251
252 abort_test:
253         sprintf(command, "rm %s\n", test_file); 
254         system(command);
255
256         printf("EFAULT testing complete.\n");
257
258 }
259