1 /* $NetBSD: pcnfsd_test.c,v 1.4 2018/01/23 21:06:25 sevan Exp $ */
2
3 /* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_test.c 1.2 92/01/27 18:00:39 SMI */
4 #include <stdio.h>
5 #include <rpc/rpc.h>
6 #include <malloc.h>
7 #include "pcnfsd.h"
8
9 CLIENT *cl;
10 CLIENT *cl2;
11 char *server;
12 char spooldirbuff[256];
13 char filenamebuff[256];
14 char last_id[32] = "";
15
16 void free_pr_list_item();
17 void free_pr_queue_item();
18 void good();
19 void bad();
20
21 int
main(int argc,char * argv[])22 main(int argc, char *argv[])
23 {
24
25 char *host_name;
26 char *printer;
27 char *user_name;
28 char *passwd;
29 char *transport = "udp";
30
31 if((argc < 6) || (argc > 7)) {
32 fprintf(stderr, "usage: %s server host printer user password [transport]\n",
33 argv[0]);
34 exit(1);
35 }
36
37 server = argv[1];
38 host_name = argv[2];
39 printer = argv[3];
40 user_name = argv[4];
41 passwd = argv[5];
42 if (argc == 7)
43 transport = argv[6];
44
45 cl = clnt_create(server, PCNFSDPROG, PCNFSDVERS, transport);
46 if(cl == NULL) {
47 clnt_pcreateerror(server);
48 exit(1);
49 }
50 cl2 = clnt_create(server, PCNFSDPROG, PCNFSDV2, transport);
51 if(cl2 == NULL) {
52 clnt_pcreateerror(server);
53 exit(1);
54 }
55 good();
56 test_v2_info();
57 good();
58 test_v2_auth(host_name, user_name, passwd);
59 bad("Invalid password");
60 test_v2_auth(host_name, user_name, "bogus");
61 good();
62 test_v2_list();
63 good();
64 test_v2_init(host_name, printer);
65 good();
66 test_v2_start(host_name, printer, user_name, "foo", "foo");
67 good();
68 test_v2_start(host_name, printer, user_name, "bar", "bar");
69 bad("No such file to print");
70 test_v2_start(host_name, printer, user_name, "bletch", "gack");
71 good();
72 test_v2_queue(printer, user_name, FALSE);
73 if(strlen(last_id)) {
74 bad("Cancelling job with bad username");
75 test_v2_cancel(host_name, printer, "nosuchuser", last_id);
76 good();
77 test_v2_cancel(host_name, printer, user_name, last_id);
78 }
79 bad("Cancelling unknown job");
80 test_v2_cancel(host_name, printer, user_name, "99999");
81 bad("Cancelling job on invalid printer");
82 test_v2_cancel(host_name, "nosuchprinter", user_name, last_id);
83 good();
84 test_v2_queue(printer, user_name, TRUE);
85 bad("Checking queue on invalid printer");
86 test_v2_queue("nosuchprinter", user_name, TRUE);
87 good();
88 test_v2_stat(printer);
89 bad("Checking status of invalid printer");
90 test_v2_stat("nosuchprinter");
91 good();
92 test_v2_map();
93 exit(0);
94 /*NOTREACHED*/
95 }
96
97 #define zchar 0x5b
98
99 void
scramble(char * s1,char * s2)100 scramble(char *s1, char *s2)
101 {
102 while (*s1)
103 {
104 *s2++ = (*s1 ^ zchar) & 0x7f;
105 s1++;
106 }
107 *s2 = 0;
108 }
109
110
111
test_v2_info()112 test_v2_info()
113 {
114 v2_info_args a;
115 v2_info_results *rp;
116 int *gp;
117 int i;
118
119 a.vers = "Sun Microsystems PCNFSD test subsystem V1";
120 a.cm = "-";
121 printf("\ninvoking pr_info_2\n");
122
123 rp = pcnfsd2_info_2(&a, cl2);
124
125 if(rp == NULL) {
126 clnt_perror(cl2, server);
127 return(1);
128 }
129
130 printf("results: vers = '%s', cm = '%s'\n",
131 rp->vers, rp->cm);
132 printf("facilities_len = %d\n", rp->facilities.facilities_len);
133 if (rp->facilities.facilities_len) {
134 gp = rp->facilities.facilities_val;
135 for(i = 0; i < rp->facilities.facilities_len; i++)
136 printf(" procedure %2d: %6d\n", i, *gp++);
137 printf("\n");
138 }
139 /* free up allocated strings */
140 if(rp->cm)
141 free(rp->cm);
142 if(rp->facilities.facilities_val)
143 free(rp->facilities.facilities_val);
144 if(rp->vers)
145 free(rp->vers);
146
147 return(0);
148 }
149
test_v2_auth(char * host_name,char * user_name,char * pwrd)150 test_v2_auth(char *host_name, char *user_name , char *pwrd)
151 {
152 v2_auth_args a;
153 v2_auth_results *rp;
154 char uname[32];
155 char pw[64];
156 u_int *gp;
157 int i;
158
159 scramble(user_name, uname);
160 scramble(pwrd, pw);
161 a.system = host_name;
162 a.id = uname;
163 a.pw = pw;
164 a.cm = "-";
165 printf("\ninvoking pr_auth_2\n");
166
167 rp = pcnfsd2_auth_2(&a, cl2);
168
169 if(rp == NULL) {
170 clnt_perror(cl2, server);
171 return(1);
172 }
173
174 if(rp->stat == AUTH_RES_FAIL)
175 printf("results: stat = AUTH_RES_FAIL\n");
176 else {
177 printf("results: stat = %d, uid = %u, gid = %u,\n homedir= '%s', cm = '%s'\n",
178 rp->stat, rp->uid, rp->gid, rp->home, rp->cm);
179 printf("gids_len = %d", rp->gids.gids_len);
180 if (rp->gids.gids_len) {
181 gp = rp->gids.gids_val;
182 for(i = 0; i < rp->gids.gids_len; i++)
183 printf(" %u", *gp++);
184 printf("\n");
185 }
186 }
187 /* free up allocated strings */
188 if(rp->cm)
189 free(rp->cm);
190 if(rp->gids.gids_val)
191 free(rp->gids.gids_val);
192 if(rp->home)
193 free(rp->home);
194
195 return(0);
196 }
197
test_v2_init(char * host_name,char * printer)198 test_v2_init(char *host_name, char *printer)
199 {
200 v2_pr_init_args a;
201 v2_pr_init_results *rp;
202
203 a.system = host_name;
204 a.pn = printer;
205 a.cm = "-";
206 printf("\ninvoking pr_init_2\n");
207
208 rp = pcnfsd2_pr_init_2(&a, cl2);
209
210 if(rp == NULL) {
211 clnt_perror(cl2, server);
212 return(1);
213 }
214 printf("results: stat = %d, dir = '%s', cm = '%s'\n",
215 rp->stat, rp->dir, rp->cm);
216 strlcpy(spooldirbuff, rp->dir, sizeof(spooldirbuff));
217 /* free up allocated strings */
218 if(rp->cm)
219 free(rp->cm);
220 if(rp->dir)
221 free(rp->dir);
222 return(0);
223 }
224
225
test_v2_start(char * host_name,char * printer,char * user_name,char * tag1,char * tag2)226 test_v2_start(char *host_name, char *printer, char *user_name, char *tag1, char *tag2)
227 {
228 v2_pr_start_args a;
229 v2_pr_start_results *rp;
230 FILE *fp;
231 printf("\ntesting start print v2\n");
232
233 if(strcmp(server, "localhost")) {
234 printf("sorry - can only test start print on 'localhost'\n");
235 return(1);
236 }
237
238 snprintf(filenamebuff, sizeof(filenamebuff), "%s/%s",
239 spooldirbuff, tag1);
240
241 fp = fopen(filenamebuff, "w");
242 if(fp == NULL) {
243 perror("creating test file");
244 return(1);
245 }
246 (void)fputs("foo bar bletch\n", fp);
247 (void)fclose(fp);
248
249 a.system = host_name;
250 a.pn = printer;
251 a.user = user_name;
252 a.file = tag2;
253 a.opts = "xxxx";
254 a.copies = 1;
255 a.cm = "-";
256
257 printf("\ninvoking pr_start_2\n");
258
259 rp = pcnfsd2_pr_start_2(&a, cl2);
260
261 if(rp == NULL) {
262 clnt_perror(cl2, server);
263 return(1);
264 }
265 printf("results: stat = %d, jobid = '%s', cm = '%s'\n",
266 rp->stat, rp->id, rp->cm);
267 if(rp->stat == PS_RES_OK)
268 strlcpy(last_id, rp->id, sizeof(last_id));
269 /* free up allocated strings */
270 if(rp->cm)
271 free(rp->cm);
272 if(rp->id)
273 free(rp->id);
274 return(0);
275 }
276
277
test_v2_cancel(char * host_name,char * printer,char * user_name,char * id)278 test_v2_cancel(char *host_name, char *printer, char *user_name, char *id)
279 {
280 v2_pr_cancel_args a;
281 v2_pr_cancel_results *rp;
282 printf("\ntesting cancel print v2\n");
283
284 a.system = host_name;
285 a.pn = printer;
286 a.user = user_name;
287 a.id = id;
288 a.cm = "-";
289
290 printf("\ninvoking pr_cancel_2 for job %s on printer %s\n",
291 id, printer);
292
293 rp = pcnfsd2_pr_cancel_2(&a, cl2);
294
295 if(rp == NULL) {
296 clnt_perror(cl2, server);
297 return(1);
298 }
299 printf("results: stat = %d, cm = '%s'\n",
300 rp->stat, rp->cm);
301 /* free up allocated strings */
302 if(rp->cm)
303 free(rp->cm);
304 return(0);
305 }
test_v2_list()306 test_v2_list()
307 {
308 char a;
309 v2_pr_list_results *rp;
310 pr_list curr;
311
312
313 printf("\ninvoking pr_list_2\n");
314
315 rp = pcnfsd2_pr_list_2(&a, cl2);
316
317 if(rp == NULL) {
318 clnt_perror(cl2, server);
319 return(1);
320 }
321 printf("results: cm = '%s', printerlist:\n", rp->cm);
322 curr = rp->printers;
323 while(curr) {
324 printf(" name '%s' ", curr->pn);
325 if(strlen(curr->remhost))
326 printf("remote: srvr '%s', name '%s'",
327 curr->remhost,
328 curr->device);
329 else
330 printf("local device = '%s'", curr->device);
331 printf(", cm = '%s'\n", curr->cm);
332 curr = curr->pr_next;
333 }
334 printf("end of list\n");
335 /* free up allocated strings */
336 if(rp->cm)
337 free(rp->cm);
338 if(rp->printers) {
339 printf("freeing results\n");
340 free_pr_list_item(rp->printers);
341 }
342 return(0);
343 }
344
345
346 void
free_pr_list_item(pr_list curr)347 free_pr_list_item(pr_list curr)
348 {
349 if(curr->pn)
350 free(curr->pn);
351 if(curr->remhost)
352 free(curr->remhost);
353 if(curr->device)
354 free(curr->device);
355 if(curr->cm)
356 free(curr->cm);
357 if(curr->pr_next)
358 free_pr_list_item(curr->pr_next); /* recurse */
359 free(curr);
360 }
361
362
363
test_v2_queue(char * printer,char * user_name,int private)364 test_v2_queue(char *printer, char *user_name, int private)
365 {
366 struct v2_pr_queue_args a;
367 v2_pr_queue_results *rp;
368 pr_queue curr;
369
370 a.pn = printer;
371 a.system = "foo";
372 a.user = user_name;
373 a.just_mine = private;
374 a.cm = "no";
375
376 printf("\ninvoking pr_queue_2 (just_mine = %d)\n", private);
377
378 rp = pcnfsd2_pr_queue_2(&a, cl2);
379
380 if(rp == NULL) {
381 clnt_perror(cl2, server);
382 return(1);
383 }
384 printf("results: stat = %d, qlen = %d, qshown = %d cm = '%s', queue:\n",
385 rp->stat, rp->qlen, rp->qshown, rp->cm);
386 curr = rp->jobs;
387 while(curr) {
388 printf("rank = %2d, id = '%s', size = '%s', status = '%s'\n",
389 curr->position,
390 curr->id,
391 curr->size,
392 curr->status);
393 printf(" user = '%s', file = '%s', cm = '%s'\n",
394 curr->user,
395 curr->file,
396 curr->cm);
397 curr = curr->pr_next;
398 }
399 printf("end of list\n");
400 /* free up allocated strings */
401 if(rp->cm)
402 free(rp->cm);
403 if(rp->jobs) {
404 printf("freeing results\n");
405 free_pr_queue_item(rp->jobs);
406 }
407 return(0);
408 }
409
410
411
412 void
free_pr_queue_item(pr_queue curr)413 free_pr_queue_item(pr_queue curr)
414 {
415 if(curr->id)
416 free(curr->id);
417 if(curr->size)
418 free(curr->size);
419 if(curr->status)
420 free(curr->status);
421 if(curr->system)
422 free(curr->system);
423 if(curr->user)
424 free(curr->user);
425 if(curr->file)
426 free(curr->file);
427 if(curr->cm)
428 free(curr->cm);
429 if(curr->pr_next)
430 free_pr_queue_item(curr->pr_next); /* recurse */
431 free(curr);
432 }
433
434
435
test_v2_stat(char * printer)436 test_v2_stat(char *printer)
437 {
438 v2_pr_status_args a;
439 v2_pr_status_results *rp;
440
441 printf("\ntesting status print v2\n");
442
443 a.pn = printer;
444 a.cm = "-";
445
446 printf("\ninvoking pr_status_2\n");
447
448 rp = pcnfsd2_pr_status_2(&a, cl2);
449
450 if(rp == NULL) {
451 clnt_perror(cl2, server);
452 return(1);
453 }
454 printf("results: stat = %d, cm = '%s'\n",
455 rp->stat, rp->cm);
456 if(rp->stat == PI_RES_OK) {
457 printf("avail = %s, ", (rp->avail ? "YES" : "NO"));
458 printf("printing = %s, ", (rp->printing ? "YES" : "NO"));
459 printf("needs_operator = %s, ", (rp->needs_operator ? "YES" : "NO"));
460 printf("qlen = %d, status = '%s'\n", rp->qlen, rp->status);
461 }
462 /* free up allocated strings */
463 if(rp->cm)
464 free(rp->cm);
465 if(rp->status)
466 free(rp->status);
467 return(0);
468 }
469
make_mapreq_entry(mapreq t,int i,char * n,struct mapreq_arg_item * next)470 struct mapreq_arg_item * make_mapreq_entry(mapreq t, int i, char *n, struct mapreq_arg_item *next)
471 {
472 struct mapreq_arg_item *x;
473 x = (struct mapreq_arg_item *)malloc(sizeof(struct mapreq_arg_item));
474 if(x == NULL) {
475 fprintf(stderr, "out of memory\n");
476 exit(123);
477 }
478 x->req = t;
479 x->id = i;
480 x->name = (n ? n : "");
481 x->mapreq_next = next;
482 return(x);
483 }
484
test_v2_map()485 test_v2_map()
486 {
487 v2_mapid_args a;
488 v2_mapid_results *rp;
489 struct mapreq_res_item *rip;
490
491 a.cm = "-";
492 a.req_list = make_mapreq_entry(MAP_REQ_UID, 906, NULL,
493 make_mapreq_entry(MAP_REQ_GID, 1, NULL,
494 make_mapreq_entry(MAP_REQ_UNAME, 0, "root",
495 make_mapreq_entry(MAP_REQ_GNAME, 0, "wheel",
496 make_mapreq_entry(MAP_REQ_UNAME, 0, "bogus", NULL)))));
497
498 printf("\ninvoking pr_mapid_2\n");
499 rp = pcnfsd2_mapid_2(&a, cl2);
500
501 if(rp == NULL) {
502 clnt_perror(cl2, server);
503 return(1);
504 }
505 printf("results: cm = '%s', result list %s\n",
506 rp->cm, rp->res_list ? "follows" : "omitted");
507 rip = rp->res_list;
508 while(rip) {
509 printf("request type = %d, status = %d, id = %d, name = '%s'\n",
510 rip->req, rip->stat, rip->id,
511 (rip->name ? rip->name : "(NULL)"));
512 rip = rip->mapreq_next;
513 }
514 /* XXX should free up results */
515
516
517
518 return(0);
519 }
520
521
522 void
good()523 good()
524 {
525 printf("\n");
526 printf("********************************************************\n");
527 printf("********************************************************\n");
528 printf("** The following test is expected to SUCCEED **\n");
529 printf("********************************************************\n");
530 printf("********************************************************\n");
531 }
532
533 void
bad(char * reason)534 bad(char *reason)
535 {
536 printf("\n");
537 printf("********************************************************\n");
538 printf("********************************************************\n");
539 printf("** The following test is expected to FAIL **\n");
540 printf("** Reason: **\n");
541 printf("** %50s **\n", reason);
542 printf("********************************************************\n");
543 printf("********************************************************\n");
544 }
545