test.c (1903B)
1 #include <u.h> 2 #include <libc.h> 3 #include <auth.h> 4 5 typedef struct Test Test; 6 7 struct Test 8 { 9 char *name; 10 int (*server)(Test*, AuthRpc*, int); 11 int (*client)(Test*, int); 12 }; 13 14 int 15 ai2status(AuthInfo *ai) 16 { 17 if(ai == nil) 18 return -1; 19 auth_freeAI(ai); 20 return 0; 21 } 22 23 int 24 proxyserver(Test *t, AuthRpc *rpc, int fd) 25 { 26 char buf[1024]; 27 28 sprint(buf, "proto=%q role=server", t->name); 29 return ai2status(fauth_proxy(fd, rpc, nil, buf)); 30 } 31 32 int 33 proxyclient(Test *t, int fd) 34 { 35 return ai2status(auth_proxy(fd, auth_getkey, "proto=%q role=client", t->name)); 36 } 37 38 Test test[] = 39 { 40 "apop", proxyserver, proxyclient, 41 "cram", proxyserver, proxyclient, 42 "p9sk1", proxyserver, proxyclient, 43 "p9sk2", proxyserver, proxyclient, 44 "p9any", proxyserver, proxyclient 45 }; 46 47 void 48 usage(void) 49 { 50 fprint(2, "usage: test [name]...\n"); 51 exits("usage"); 52 } 53 54 void 55 runtest(AuthRpc *srpc, Test *t) 56 { 57 int p[2], bad; 58 Waitmsg *w; 59 60 if(pipe(p) < 0) 61 sysfatal("pipe: %r"); 62 63 print("%s...", t->name); 64 65 switch(fork()){ 66 case -1: 67 sysfatal("fork: %r"); 68 69 case 0: 70 close(p[0]); 71 if((*t->server)(t, srpc, p[1]) < 0){ 72 print("\n\tserver: %r"); 73 _exits("oops"); 74 } 75 close(p[1]); 76 _exits(nil); 77 default: 78 close(p[1]); 79 if((*t->client)(t, p[0]) < 0){ 80 print("\n\tclient: %r"); 81 bad = 1; 82 } 83 close(p[0]); 84 break; 85 } 86 w = wait(); 87 if(w->msg[0]) 88 bad = 1; 89 print("\n"); 90 } 91 92 void 93 main(int argc, char **argv) 94 { 95 int i, j; 96 int afd; 97 AuthRpc *srpc; 98 99 ARGBEGIN{ 100 default: 101 usage(); 102 }ARGEND 103 104 quotefmtinstall(); 105 afd = open("/n/kremvax/factotum/rpc", ORDWR); 106 if(afd < 0) 107 sysfatal("open /n/kremvax/factotum/rpc: %r"); 108 srpc = auth_allocrpc(afd); 109 if(srpc == nil) 110 sysfatal("auth_allocrpc: %r"); 111 112 if(argc == 0) 113 for(i=0; i<nelem(test); i++) 114 runtest(srpc, &test[i]); 115 else 116 for(i=0; i<argc; i++) 117 for(j=0; j<nelem(test); j++) 118 if(strcmp(argv[i], test[j].name) == 0) 119 runtest(srpc, &test[j]); 120 exits(nil); 121 }