Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / tests / pkcs11 / benchmarks / session.c
blobe782f26cedbc29737448990f566d852cb7acfb0b
1 /* $NetBSD: session.c,v 1.1.1.3 2014/12/10 03:34:28 christos Exp $ */
3 /*
4 * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
20 * Portions copyright (c) 2008 Nominet UK. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 /* Id */
45 /* session [-m module] [-s $slot] [-n count] */
47 /*! \file */
49 #include <config.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
57 #include <isc/commandline.h>
58 #include <isc/result.h>
59 #include <isc/types.h>
61 #include <pk11/pk11.h>
62 #include <pk11/internal.h>
64 #ifndef HAVE_CLOCK_GETTIME
65 #ifndef CLOCK_REALTIME
66 #define CLOCK_REALTIME 0
67 #endif
69 int
70 clock_gettime(int32_t id, struct timespec *tp)
72 struct timeval tv;
73 int result;
75 result = gettimeofday(&tv, NULL);
76 if (result)
77 return (result);
78 tp->tv_sec = tv.tv_sec;
79 tp->tv_nsec = (long) tv.tv_usec * 1000;
80 return (result);
82 #endif
84 int
85 main(int argc, char *argv[]) {
86 CK_RV rv;
87 CK_SLOT_ID slot = 0;
88 CK_SESSION_HANDLE *hSession;
89 char *lib_name = NULL;
90 int error = 0;
91 int c, errflg = 0;
92 unsigned int count = 1000;
93 unsigned int i;
94 struct timespec starttime;
95 struct timespec endtime;
97 while ((c = isc_commandline_parse(argc, argv, ":m:s:n:")) != -1) {
98 switch (c) {
99 case 'm':
100 lib_name = isc_commandline_argument;
101 break;
102 case 's':
103 slot = atoi(isc_commandline_argument);
104 break;
105 case 'n':
106 count = atoi(isc_commandline_argument);
107 break;
108 case ':':
109 fprintf(stderr,
110 "Option -%c requires an operand\n",
111 isc_commandline_option);
112 errflg++;
113 break;
114 case '?':
115 default:
116 fprintf(stderr, "Unrecognised option: -%c\n",
117 isc_commandline_option);
118 errflg++;
122 if (errflg) {
123 fprintf(stderr, "Usage:\n");
124 fprintf(stderr,
125 "\tsession [-m module] [-s slot] [-n count]\n");
126 exit(1);
129 /* Allocate sessions */
130 hSession = (CK_SESSION_HANDLE *)
131 malloc(count * sizeof(CK_SESSION_HANDLE));
132 if (hSession == NULL) {
133 perror("malloc");
134 exit(1);
136 for (i = 0; i < count; i++)
137 hSession[i] = CK_INVALID_HANDLE;
139 /* Initialize the CRYPTOKI library */
140 if (lib_name != NULL)
141 pk11_set_lib_name(lib_name);
143 rv = pkcs_C_Initialize(NULL_PTR);
144 if (rv != CKR_OK) {
145 if (rv == 0xfe)
146 fprintf(stderr,
147 "Can't load or link module \"%s\"\n",
148 pk11_get_lib_name());
149 else
150 fprintf(stderr, "C_Initialize: Error = 0x%.8lX\n", rv);
151 free(hSession);
152 exit(1);
155 if (clock_gettime(CLOCK_REALTIME, &starttime) < 0) {
156 perror("clock_gettime(start)");
157 goto exit_program;
160 /* loop */
161 for (i = 0; i < count; i++) {
162 /* Open sessions */
163 rv = pkcs_C_OpenSession(slot, CKF_SERIAL_SESSION,
164 NULL_PTR, NULL_PTR, &hSession[i]);
165 if (rv != CKR_OK) {
166 fprintf(stderr,
167 "C_OpenSession[%u]: Error = 0x%.8lX\n",
168 i, rv);
169 error = 1;
170 if (i == 0)
171 goto exit_program;
172 break;
176 if (clock_gettime(CLOCK_REALTIME, &endtime) < 0) {
177 perror("clock_gettime(end)");
178 goto exit_program;
181 endtime.tv_sec -= starttime.tv_sec;
182 endtime.tv_nsec -= starttime.tv_nsec;
183 while (endtime.tv_nsec < 0) {
184 endtime.tv_sec -= 1;
185 endtime.tv_nsec += 1000000000;
187 printf("%u sessions in %ld.%09lds\n", i,
188 endtime.tv_sec, endtime.tv_nsec);
189 if (i > 0)
190 printf("%g sessions/s\n",
191 i / ((double) endtime.tv_sec +
192 (double) endtime.tv_nsec / 1000000000.));
194 for (i = 0; i < count; i++) {
195 /* Close sessions */
196 if (hSession[i] == CK_INVALID_HANDLE)
197 continue;
198 rv = pkcs_C_CloseSession(hSession[i]);
199 if ((rv != CKR_OK) && !errflg) {
200 fprintf(stderr,
201 "C_CloseSession[%u]: Error = 0x%.8lX\n",
202 i, rv);
203 errflg = 1;
207 exit_program:
208 free(hSession);
210 rv = pkcs_C_Finalize(NULL_PTR);
211 if (rv != CKR_OK)
212 fprintf(stderr, "C_Finalize: Error = 0x%.8lX\n", rv);
214 exit(error);