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