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