Add.
[shishi.git] / tests / utils.c
blob1c3dff3cc9eb2bd9498d42badd27d86bc6410853
1 /* utils.c Shishi self tests utilities.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #if HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <stdint.h>
31 #include <sys/types.h>
32 #include <sys/select.h>
33 #include <sys/socket.h>
34 #include <unistd.h>
35 #include <string.h>
37 #ifdef HAVE_NETDB_H
38 #include <netdb.h>
39 #endif
41 #if defined HAVE_DECL_H_ERRNO && !HAVE_DECL_H_ERRNO
42 /* extern int h_errno; */
43 #endif
45 #ifdef HAVE_PWD_H
46 #include <pwd.h>
47 #endif
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
51 #endif
53 #ifdef HAVE_ERRNO_H
54 #include <errno.h>
55 #endif
57 #if TIME_WITH_SYS_TIME
58 # include <sys/time.h>
59 # include <time.h>
60 #else
61 # if HAVE_SYS_TIME_H
62 # include <sys/time.h>
63 # else
64 # include <time.h>
65 # endif
66 #endif
68 #ifdef HAVE_SIGNAL_H
69 #include <signal.h>
70 #endif
72 #ifdef HAVE_NETINET_IN_H
73 #include <netinet/in.h>
74 #endif
75 #ifdef HAVE_NETINET_IN6_H
76 #include <netinet/in6.h>
77 #endif
79 #include "base64.h"
81 #include <shishi.h>
83 const char *program_name = PACKAGE;
85 static int verbose = 0;
86 static int debug = 0;
87 static int error_count = 0;
88 static int break_on_error = 0;
90 static void
91 fail (const char *format, ...)
93 va_list arg_ptr;
95 va_start (arg_ptr, format);
96 vfprintf (stderr, format, arg_ptr);
97 va_end (arg_ptr);
98 error_count++;
99 if (break_on_error)
100 exit (1);
103 static void
104 success (const char *format, ...)
106 va_list arg_ptr;
108 va_start (arg_ptr, format);
109 if (verbose)
110 vfprintf (stdout, format, arg_ptr);
111 va_end (arg_ptr);
114 static void
115 escapeprint (const char *str, int len)
117 int i;
119 if (!str || !len)
120 return;
122 printf ("\t ;; `");
123 for (i = 0; i < len; i++)
124 if ((str[i] >= 'A' && str[i] <= 'Z') ||
125 (str[i] >= 'a' && str[i] <= 'z') ||
126 (str[i] >= '0' && str[i] <= '9') || str[i] == '.')
127 printf ("%c", str[i]);
128 else
129 printf ("\\x%02x", str[i]);
130 printf ("' (length %d bytes)\n", len);
133 static void
134 hexprint (const char *str, int len)
136 int i;
138 if (!str || !len)
139 return;
141 printf ("\t ;; ");
142 for (i = 0; i < len; i++)
144 printf ("%02x ", str[i]);
145 if ((i + 1) % 8 == 0)
146 printf (" ");
147 if ((i + 1) % 16 == 0 && i + 1 < len)
148 printf ("\n\t ;; ");
150 puts ("");
153 static void
154 binprint (const char *str, int len)
156 int i;
158 if (!str || !len)
159 return;
161 printf ("\t ;; ");
162 for (i = 0; i < len; i++)
164 printf ("%d%d%d%d%d%d%d%d ",
165 str[i] & 0x80 ? 1 : 0,
166 str[i] & 0x40 ? 1 : 0,
167 str[i] & 0x20 ? 1 : 0,
168 str[i] & 0x10 ? 1 : 0,
169 str[i] & 0x08 ? 1 : 0,
170 str[i] & 0x04 ? 1 : 0,
171 str[i] & 0x02 ? 1 : 0, str[i] & 0x01 ? 1 : 0);
172 if ((i + 1) % 3 == 0)
173 printf (" ");
174 if ((i + 1) % 6 == 0 && i + 1 < len)
175 printf ("\n\t ;; ");
177 puts ("");
180 void test (Shishi * handle);
183 main (int argc, char *argv[])
185 Shishi *handle;
188 if (strcmp (argv[argc - 1], "-v") == 0 ||
189 strcmp (argv[argc - 1], "--verbose") == 0)
190 verbose = 1;
191 else if (strcmp (argv[argc - 1], "-d") == 0 ||
192 strcmp (argv[argc - 1], "--debug") == 0)
193 debug = 1;
194 else if (strcmp (argv[argc - 1], "-b") == 0 ||
195 strcmp (argv[argc - 1], "--break-on-error") == 0)
196 break_on_error = 1;
197 else if (strcmp (argv[argc - 1], "-h") == 0 ||
198 strcmp (argv[argc - 1], "-?") == 0 ||
199 strcmp (argv[argc - 1], "--help") == 0)
201 printf ("Usage: %s [-vdbh?] [--verbose] [--debug] "
202 "[--break-on-error] [--help]\n", argv[0]);
203 return 1;
205 while (argc-- > 1);
207 handle = shishi ();
208 if (handle == NULL)
210 fail ("Could not initialize shishi\n");
211 return 1;
214 if (debug)
216 shishi_cfg (handle, strdup ("verbose"));
217 shishi_cfg (handle, strdup ("verbose-noise"));
218 shishi_cfg (handle, strdup ("verbose-asn1"));
219 shishi_cfg (handle, strdup ("verbose-crypto"));
220 shishi_cfg (handle, strdup ("verbose-crypto-noise"));
223 test (handle);
225 shishi_done (handle);
227 if (verbose)
228 printf ("Self test `%s' done with %d errors\n", argv[0], error_count);
230 return error_count ? 1 : 0;