home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / LINUX_PC.TAR / pcnfsd_linux2 / pcnfsd_test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  11.5 KB  |  571 lines

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