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