windows-64-bit-type-safety-20051105
[openafs.git] / src / rx / test / kctest.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include "afs/param.h"
11 #include <afsconfig.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <sys/types.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include "xdr.h"
22 #include "rx.h"
23 #include "rx_globals.h"
24 #include "rx_null.h"
25 #if RX_VAB_EXISTS
26 #include "rx_vab.h"
27 #endif
28
29 static long host;
30 static short port;
31 static short count;
32 static short secLevel = 0;
33 static short stats = 0;
34
35 #if RX_VAB_EXISTS
36 static
37 MakeVTest(akey, aticket, asession)
38      struct rxvab_EncryptionKey *akey, *asession;
39      struct rxvab_Ticket *aticket;
40 {
41     aticket->ViceId = htonl(71);
42     memcpy(&aticket->HandShakeKey, "testkeyx", 8);
43     memcpy(asession, "testkeyx", 8);
44     bcrypt_encrypt(aticket, aticket, sizeof(struct rxvab_Ticket), akey);
45     return 0;
46 }
47 #else
48 #define MakeVTest(a,b,c) (printf ("rx_vab support removed\n"), exit (-1))
49 #endif
50
51 void
52 SigInt(int ignore)
53 {
54     if (rx_debugFile) {
55         rx_PrintStats(rx_debugFile);
56         fflush(rx_debugFile);
57     }
58     if (stats)
59         rx_PrintStats(stdout);
60     rx_Finalize();
61     exit(1);
62 }
63
64 static
65 ParseCmd(argc, argv)
66      int argc;
67      char **argv;
68 {
69     register int i;
70     register struct hostent *th;
71     for (i = 1; i < argc; i++) {
72         if (!strcmp(argv[i], "-port")) {
73             port = atoi(argv[i + 1]);
74             i++;
75         } else if (!strcmp(argv[i], "-host")) {
76             th = gethostbyname(argv[i + 1]);
77             if (!th) {
78                 printf("could not find host '%s' in host table\n",
79                        argv[i + 1]);
80                 return -1;
81             }
82             memcpy(&host, th->h_addr, sizeof(long));
83             i++;
84         } else if (!strcmp(argv[i], "-count")) {
85             count = atoi(argv[i + 1]);
86             i++;
87         } else if (!strcmp(argv[i], "-security")) {
88             secLevel = atoi(argv[i + 1]);
89             i++;
90         } else if (!strcmp(argv[i], "-log")) {
91             rx_debugFile = fopen("kctest.log", "w");
92             if (rx_debugFile == NULL)
93                 printf("Couldn't open rx_stest.db");
94             signal(SIGINT, SigInt);
95         } else if (!strcmp(argv[i], "-stats"))
96             stats = 1;
97         else {
98             printf("unrecognized switch '%s'\n", argv[i]);
99             return -1;
100         }
101     }
102     return 0;
103 }
104
105 nowms()
106 {
107     struct timeval tv;
108     long temp;
109
110     gettimeofday(&tv, 0);
111     temp = ((tv.tv_sec & 0xffff) * 1000) + (tv.tv_usec / 1000);
112     return temp;
113 }
114
115 main(argc, argv)
116      int argc;
117      char **argv;
118 {
119     struct rx_securityClass *so;
120     struct rx_connection *tconn;
121     struct rx_call *tcall;
122     XDR xdr;
123     int i, startms, endms;
124     long temp;
125 #if RX_VAB_EXISTS
126     struct rxvab_Ticket ticket;
127     struct rxvab_EncryptionKey session;
128 #endif
129
130     host = htonl(0x7f000001);
131     port = htons(10000);
132     count = 1;
133     if (ParseCmd(argc, argv) != 0) {
134         printf("error parsing commands\n");
135         exit(1);
136     }
137     rx_Init(0);
138     if (secLevel == 0)
139         so = rxnull_NewClientSecurityObject();
140     else if (secLevel == 1) {
141         MakeVTest((struct rxvab_EncryptionKey *)"applexxx", &ticket,
142                   &session);
143 #if RX_VAB_EXISTS
144         so = rxvab_NewClientSecurityObject(&session, &ticket, 0);
145 #endif
146     } else if (secLevel == 2) {
147         MakeVTest((struct rxvab_EncryptionKey *)"applexxx", &ticket,
148                   &session);
149 #if RX_VAB_EXISTS
150         so = rxvab_NewClientSecurityObject(&session, &ticket, 1);
151 #endif
152     } else {
153         printf("bad security index\n");
154         exit(1);
155     }
156     if (!so) {
157         printf("failed to create security obj\n");
158         exit(1);
159     }
160     tconn = rx_NewConnection(host, port, 1, so, secLevel);
161     printf("conn is %x\n", tconn);
162
163     startms = nowms();
164     for (i = 0; i < count; i++) {
165         tcall = rx_NewCall(tconn);
166         /* fill in data */
167         xdrrx_create(&xdr, tcall, XDR_ENCODE);
168         temp = 1988;
169         xdr_long(&xdr, &temp);
170         xdr.x_op = XDR_DECODE;
171         xdr_long(&xdr, &temp);
172         if (temp != 1989)
173             printf("wrong value returned (%d)\n", temp);
174         rx_EndCall(tcall, 0);
175     }
176     endms = nowms();
177     printf("That was %d ms per call.\n", (endms - startms) / count);
178     printf("Done.\n");
179 #ifdef RXDEBUG
180     if (stats)
181         rx_PrintStats(stdout);
182 #endif
183     SigInt(0);
184 }