6eecfb165cf39812409dd8bf1ed5e76927b9a58d
[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 #include <getopt.h>
45 #include <string.h>
46
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
108         if (Progname = strrchr(argv[0], '/')) {
109                 Progname++;
110         } else {
111                 Progname = argv[0];
112         }
113
114         opterr = 0;
115         while ((opt = getopt(argc, argv, "vn:s:")) != EOF) {
116                 switch (opt) {
117                 case 'v':
118                          Vflag++;
119                          break;
120                 case 's': 
121                          sid = atol(optarg);
122                          break;
123                 case '?':
124                          usage();
125                 }
126         }
127         if (optind + 2 != argc) {
128                 usage();
129         }
130         ls_path = argv[optind];
131         pathname = argv[optind+1];
132
133         if (dm_init_service(&name) == -1)  {
134                 fprintf(stderr, "Can't initialize the DMAPI\n");
135                 exit(1);
136         }
137         if (sid == DM_NO_SESSION)
138                 find_test_session(&sid);
139         
140         printf("Beginning access rights testing...\n");
141
142         sprintf(fname_a, "%s/DMAPI_rights_test_file_a", pathname);
143         sprintf(command, "cp %s %s\n", ls_path, fname_a); 
144         system(command);
145
146         if (dm_path_to_handle(fname_a, &ap, &alen)) {
147           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
148                   fname_a, ERR_NAME);
149           goto abort_test;
150         }
151
152         sprintf(fname_b, "%s/DMAPI_rights_test_file_b", pathname);
153         sprintf(command, "cp %s %s\n", ls_path, fname_b); 
154         system(command);
155         
156         if (dm_path_to_handle(fname_b, &bp, &blen)) {
157           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
158                   fname_b, ERR_NAME);
159           goto abort_test;
160         }
161
162         sprintf(fname_c, "%s/DMAPI_rights_test_file_c", pathname);
163         sprintf(command, "cp %s %s\n", ls_path, fname_c); 
164         system(command);
165         
166         if (dm_path_to_handle(fname_c, &cp, &clen)) {
167           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
168                   fname_c, ERR_NAME);
169           goto abort_test;
170         }
171
172         if (dm_path_to_fshandle(pathname, &fs_hanp, &fs_hlen)) {
173           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
174                   pathname, ERR_NAME);
175           goto abort_test;
176         }
177
178         sprintf(pathname, "%s/DMAPI_rights_test_dir", pathname); 
179         sprintf(command, "mkdir %s\n", pathname); 
180         system(command);
181
182         if (dm_path_to_handle(pathname, &dir_hanp, &dir_hlen)) {
183           fprintf(stderr, "ERROR: can't get handle for %s; %s\n",
184                   pathname, ERR_NAME);
185           goto abort_test;
186         }
187
188         /* Test remaining functions for appropriate 
189          * right requirements...
190          *------------------------------------------------------------*/
191         { 
192           dm_off_t off = (dm_off_t)0;
193           dm_extent_t extent;
194           u_int nelem_ret;
195           SHAREDTEST("get_allocinfo", ap, alen, test_token,
196                    dm_get_allocinfo(sid, ap, alen, test_token, 
197                                     &off, 1, &extent, &nelem_ret))
198         }
199         /*------------------------------------------------------------*/
200         { 
201           void *bufp=(void*)malloc(5*sizeof(dm_attrlist_t));
202           size_t rlen;
203           SHAREDTEST("getall_dmattr", ap, alen, test_token,
204                    dm_getall_dmattr(sid, ap, alen, test_token, 
205                                     5, bufp, &rlen))
206         }
207         /*------------------------------------------------------------*/
208         { 
209           dm_attrloc_t loc;
210           SHAREDTEST("init_attrloc", dir_hanp, dir_hlen, test_token,
211                      dm_init_attrloc(sid, dir_hanp, dir_hlen, test_token,
212                                      &loc))
213         }
214         /*------------------------------------------------------------*/
215 #if 0
216         mkdir_by_handle is NOT SUPPORTED in current SGI DMAPI 
217
218         { 
219           SHAREDTEST("mkdir_by_handle", fs_hanp, fs_hlen, test_token,
220                      dm_mkdir_by_handle(sid, fs_hanp, fs_hlen, test_token,
221                                         dir_hanp, dir_hlen, "FUBAR_DIR"))
222         }
223 #endif
224         /*------------------------------------------------------------*/
225         { dm_eventset_t eventset;
226           DMEV_ZERO(eventset);
227           EXCLTEST("set_disp", fs_hanp, fs_hlen, test_token,
228                    dm_set_disp(sid, fs_hanp, fs_hlen, test_token,
229                                &eventset, DM_EVENT_MAX))
230         }
231         /*------------------------------------------------------------*/
232         { dm_attrname_t attrname={"TEST"};
233           EXCLTEST("set_return...", fs_hanp, fs_hlen, test_token,
234                    dm_set_return_on_destroy(sid, fs_hanp, fs_hlen, test_token,
235                                &attrname, DM_TRUE))
236         }
237         /*------------------------------------------------------------*/
238
239         /* Create the tokens */
240         for (i=1; i<NUM_TOKENS; i++){
241           if (dm_create_userevent(sid, 0, 0, &token[i])==-1) {
242             fprintf(stderr, "Couldn't create token %d.\n", i);
243             goto abort_test;
244           }
245         }
246
247         ERRTEST(EINVAL,
248                 "rights-on-NO_TOKEN",
249                 dm_request_right(sid, ap, alen, DM_NO_TOKEN,
250                                  0, DM_RIGHT_SHARED))
251         ERRTEST(EINVAL,
252                 "rights-on-NO_TOKEN",
253                 dm_request_right(sid, ap, alen, DM_NO_TOKEN,
254                                  0, DM_RIGHT_EXCL))
255
256         if (dm_request_right(sid, ap, alen, token[1], 0, DM_RIGHT_SHARED))
257           printf("ERROR: Request for SHARED failed on handle a, token 1");
258         if (dm_request_right(sid, ap, alen, token[2], 0, DM_RIGHT_SHARED))
259           printf("ERROR: Request for SHARED failed on handle a, token 2");
260         
261         /* ---  These WOULD be correct tests, 
262            ---  if rights were fully implemented.
263
264         ERRTEST(EAGAIN, "EXCL request",
265                 dm_request_right(sid, ap, alen, token[1], 0, DM_RIGHT_EXCL))
266         ERRTEST(EAGAIN, "EXCL request",
267                 dm_request_right(sid, ap, alen, token[2], 0, DM_RIGHT_EXCL))
268         ERRTEST(EAGAIN, "upgrade",
269                 dm_upgrade_right(sid, ap, alen, token[1]))
270        */
271         
272 abort_test:     
273         
274         for (i=1; i<NUM_TOKENS; i++)
275           dm_respond_event(sid, token[i], DM_RESP_CONTINUE, 0, 0, 0); 
276
277         sprintf(command, "rm %s\n", fname_a); 
278         system(command);
279
280         sprintf(command, "rm %s\n", fname_b); 
281         system(command);
282         
283         sprintf(command, "rm %s\n", fname_c); 
284         system(command);
285
286         sprintf(command, "rmdir %s\n", pathname); 
287         system(command);
288
289         dm_handle_free(ap, alen);
290         dm_handle_free(bp, blen);
291         dm_handle_free(cp, clen);
292
293         printf("Access rights testing complete.\n");
294
295         exit(0);
296 }
297
298
299
300
301
302
303