9300f6aa9d0d64c9b54678f433d1cee1f5150684
[xfstests-dev.git] / dmapi / src / suite2 / src / test_rights.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 test of access rights, involving many DMAPI functions
50
51 The command line is:
52    test_rights [-s sid] [-v] ls_path pathname
53
54 where:
55    sid
56       is the session ID whose events you you are interested in.
57    ls_path
58       is the path to a copy of ls, which will be copied as a test file.
59    pathname
60       is the filesystem to use for the test.
61 ----------------------------------------------------------------------------*/
62
63 #define NUM_TOKENS 4
64
65 extern  int     optind;
66 extern  int     opterr;
67 extern  char    *optarg;
68
69 char    *Progname;
70 int     Vflag=0;
71
72 static void
73 usage(void)
74 {
75         fprintf(stderr, 
76                 "Usage: %s [-v] [-s sid] ls_path pathname\n",
77                 Progname);
78         exit(1);
79 }
80
81
82 int
83 main(int argc, char **argv) {
84      
85         dm_sessid_t     sid = DM_NO_SESSION;
86         dm_token_t      token[NUM_TOKENS];
87         dm_token_t      test_token;
88         void            *fs_hanp;
89         size_t          fs_hlen;
90         void            *dir_hanp;
91         size_t          dir_hlen;
92         void            *ap;
93         size_t          alen;
94         void            *bp;
95         size_t          blen;
96         void            *cp;
97         size_t          clen;
98         char            *name;
99         char            *ls_path;
100         char            *pathname;
101         char            fname_a[100];
102         char            fname_b[100];
103         char            fname_c[100];
104         char            command[150];
105         int             opt;
106         int             i=0;
107         int             error_count=0;
108
109         if (Progname = strrchr(argv[0], '/')) {
110                 Progname++;
111         } else {
112                 Progname = argv[0];
113         }
114
115         opterr = 0;
116         while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
117                 switch (opt) {
118                 case 'v':
119                          Vflag++;
120                          break;
121                 case 's': 
122                          sid = atol(optarg);
123                          break;
124                 case '?':
125                          usage();
126                 }
127         }
128         if (optind + 2 != argc) {
129                 usage();
130         }
131         ls_path = argv[optind];
132         pathname = argv[optind+1];
133
134         if (dm_init_service(&name) == -1)  {
135                 fprintf(stderr, "Can't inititalize the DMAPI\n");
136                 exit(1);
137         }
138         if (sid == DM_NO_SESSION)
139                 find_test_session(&sid);
140         
141         printf("Beginning access rights testing...\n");
142
143         sprintf(fname_a, "%s/DMAPI_rights_test_file_a", pathname);
144         sprintf(command, "cp %s %s\n", ls_path, fname_a); 
145         system(command);
146
147         if (dm_path_to_handle(fname_a, &ap, &alen)) {
148           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
149                   fname_a, ERR_NAME);
150           goto abort_test;
151         }
152
153         sprintf(fname_b, "%s/DMAPI_rights_test_file_b", pathname);
154         sprintf(command, "cp %s %s\n", ls_path, fname_b); 
155         system(command);
156         
157         if (dm_path_to_handle(fname_b, &bp, &blen)) {
158           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
159                   fname_b, ERR_NAME);
160           goto abort_test;
161         }
162
163         sprintf(fname_c, "%s/DMAPI_rights_test_file_c", pathname);
164         sprintf(command, "cp %s %s\n", ls_path, fname_c); 
165         system(command);
166         
167         if (dm_path_to_handle(fname_c, &cp, &clen)) {
168           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
169                   fname_c, ERR_NAME);
170           goto abort_test;
171         }
172
173         if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
174           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
175                   pathname, ERR_NAME);
176           goto abort_test;
177         }
178
179         sprintf(pathname, "%s/DMAPI_rights_test_dir", pathname); 
180         sprintf(command, "mkdir %s\n", pathname); 
181         system(command);
182
183         if (dm_path_to_handle(pathname, &dir_hanp, &dir_hlen)) {
184           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
185                   pathname, ERR_NAME);
186           goto abort_test;
187         }
188
189         /* Test remaining functions for appropriate 
190          * right requirements...
191          *------------------------------------------------------------*/
192         { 
193           dm_off_t off = (dm_off_t)0;
194           dm_extent_t extent;
195           u_int nelem_ret;
196           SHAREDTEST("get_allocinfo", ap, alen, test_token,
197                    dm_get_allocinfo(sid, ap, alen, test_token, 
198                                     &off, 1, &extent, &nelem_ret))
199         }
200         /*------------------------------------------------------------*/
201         { 
202           void *bufp=(void*)malloc(5*sizeof(dm_attrlist_t));
203           size_t rlen;
204           SHAREDTEST("getall_dmattr", ap, alen, test_token,
205                    dm_getall_dmattr(sid, ap, alen, test_token, 
206                                     5, bufp, &rlen))
207         }
208         /*------------------------------------------------------------*/
209         { 
210           dm_attrloc_t loc;
211           SHAREDTEST("init_attrloc", dir_hanp, dir_hlen, test_token,
212                      dm_init_attrloc(sid, dir_hanp, dir_hlen, test_token,
213                                      &loc))
214         }
215         /*------------------------------------------------------------*/
216 #if 0
217         mkdir_by_handle is NOT SUPPORTED in current SGI DMAPI 
218
219         { 
220           SHAREDTEST("mkdir_by_handle", fs_hanp, fs_hlen, test_token,
221                      dm_mkdir_by_handle(sid, fs_hanp, fs_hlen, test_token,
222                                         dir_hanp, dir_hlen, "FUBAR_DIR"))
223         }
224 #endif
225         /*------------------------------------------------------------*/
226         { dm_eventset_t eventset;
227           DMEV_ZERO(eventset);
228           EXCLTEST("set_disp", fs_hanp, fs_hlen, test_token,
229                    dm_set_disp(sid, fs_hanp, fs_hlen, test_token,
230                                &eventset, DM_EVENT_MAX))
231         }
232         /*------------------------------------------------------------*/
233         { dm_attrname_t attrname={"TEST"};
234           EXCLTEST("set_return...", fs_hanp, fs_hlen, test_token,
235                    dm_set_return_on_destroy(sid, fs_hanp, fs_hlen, test_token,
236                                &attrname, DM_TRUE))
237         }
238         /*------------------------------------------------------------*/
239
240         /* Create the tokens */
241         for (i=1; i<NUM_TOKENS; i++){
242           if (dm_create_userevent(sid, 0, 0, &token[i])==-1) {
243             fprintf(stderr, "Couldn't create token %d.\n", i);
244             goto abort_test;
245           }
246         }
247
248         ERRTEST(EINVAL,
249                 "rights-on-NO_TOKEN",
250                 dm_request_right(sid, ap, alen, DM_NO_TOKEN,
251                                  0, DM_RIGHT_SHARED))
252         ERRTEST(EINVAL,
253                 "rights-on-NO_TOKEN",
254                 dm_request_right(sid, ap, alen, DM_NO_TOKEN,
255                                  0, DM_RIGHT_EXCL))
256
257         if (dm_request_right(sid, ap, alen, token[1], 0, DM_RIGHT_SHARED))
258           printf("ERROR: Request for SHARED failed on handle a, token 1");
259         if (dm_request_right(sid, ap, alen, token[2], 0, DM_RIGHT_SHARED))
260           printf("ERROR: Request for SHARED failed on handle a, token 2");
261         
262         /* ---  These WOULD be correct tests, 
263            ---  if rights were fully implemented.
264
265         ERRTEST(EAGAIN, "EXCL request",
266                 dm_request_right(sid, ap, alen, token[1], 0, DM_RIGHT_EXCL))
267         ERRTEST(EAGAIN, "EXCL request",
268                 dm_request_right(sid, ap, alen, token[2], 0, DM_RIGHT_EXCL))
269         ERRTEST(EAGAIN, "upgrade",
270                 dm_upgrade_right(sid, ap, alen, token[1]))
271        */
272         
273 abort_test:     
274         
275         for (i=1; i<NUM_TOKENS; i++)
276           dm_respond_event(sid, token[i], DM_RESP_CONTINUE, 0, 0, 0); 
277
278         sprintf(command, "rm %s\n", fname_a); 
279         system(command);
280
281         sprintf(command, "rm %s\n", fname_b); 
282         system(command);
283         
284         sprintf(command, "rm %s\n", fname_c); 
285         system(command);
286
287         sprintf(command, "rmdir %s\n", pathname); 
288         system(command);
289
290         dm_handle_free(ap, alen);
291         dm_handle_free(bp, blen);
292         dm_handle_free(cp, clen);
293
294         printf("Access rights testing complete.\n");
295
296 }
297
298
299
300
301
302
303