Branch data Line data Source code
1 : : /* utils.c --- Shishi self tests utilities.
2 : : * Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2010 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
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 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
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.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #ifdef HAVE_CONFIG_H
24 : : # include "config.h"
25 : : #endif
26 : :
27 : : #include <stdio.h>
28 : : #include <stdlib.h>
29 : :
30 : : #include "utils.h"
31 : :
32 : : const char *program_name = PACKAGE;
33 : :
34 : : int debug = 0;
35 : : int error_count = 0;
36 : : int break_on_error = 0;
37 : :
38 : : void
39 : 0 : fail (const char *format, ...)
40 : : {
41 : : va_list arg_ptr;
42 : :
43 : 0 : va_start (arg_ptr, format);
44 : 0 : vfprintf (stderr, format, arg_ptr);
45 : 0 : va_end (arg_ptr);
46 : 0 : error_count++;
47 [ # # ]: 0 : if (break_on_error)
48 : 0 : exit (EXIT_FAILURE);
49 : 0 : }
50 : :
51 : : void
52 : 520 : success (const char *format, ...)
53 : : {
54 : : va_list arg_ptr;
55 : :
56 : 520 : va_start (arg_ptr, format);
57 [ - + ]: 520 : if (debug)
58 : 0 : vfprintf (stdout, format, arg_ptr);
59 : 520 : va_end (arg_ptr);
60 : 520 : }
61 : :
62 : : void
63 : 0 : escapeprint (const char *str, size_t len)
64 : : {
65 : : size_t i;
66 : :
67 : 0 : printf (" (length %d bytes):\n\t", len);
68 [ # # ]: 0 : for (i = 0; i < len; i++)
69 : : {
70 [ # # ][ # # ]: 0 : if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
71 : 0 : ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
72 : 0 : ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
73 : 0 : || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
74 : 0 : printf ("%c", (str[i] & 0xFF));
75 : : else
76 : 0 : printf ("\\x%02X", (str[i] & 0xFF));
77 [ # # ][ # # ]: 0 : if ((i + 1) % 16 == 0 && (i + 1) < len)
78 : 0 : printf ("'\n\t'");
79 : : }
80 : 0 : printf ("\n");
81 : 0 : }
82 : :
83 : : void
84 : 0 : hexprint (const char *str, size_t len)
85 : : {
86 : : size_t i;
87 : :
88 : 0 : printf ("\t;; ");
89 [ # # ]: 0 : for (i = 0; i < len; i++)
90 : : {
91 : 0 : printf ("%02x ", (str[i] & 0xFF));
92 [ # # ]: 0 : if ((i + 1) % 8 == 0)
93 : 0 : printf (" ");
94 [ # # ][ # # ]: 0 : if ((i + 1) % 16 == 0 && i + 1 < len)
95 : 0 : printf ("\n\t;; ");
96 : : }
97 : 0 : printf ("\n");
98 : 0 : }
99 : :
100 : : void
101 : 0 : binprint (const char *str, size_t len)
102 : : {
103 : : size_t i;
104 : :
105 : 0 : printf ("\t;; ");
106 [ # # ]: 0 : for (i = 0; i < len; i++)
107 : : {
108 : 0 : printf ("%d%d%d%d%d%d%d%d ",
109 : 0 : (str[i] & 0xFF) & 0x80 ? 1 : 0,
110 : 0 : (str[i] & 0xFF) & 0x40 ? 1 : 0,
111 : 0 : (str[i] & 0xFF) & 0x20 ? 1 : 0,
112 : 0 : (str[i] & 0xFF) & 0x10 ? 1 : 0,
113 : 0 : (str[i] & 0xFF) & 0x08 ? 1 : 0,
114 : 0 : (str[i] & 0xFF) & 0x04 ? 1 : 0,
115 : 0 : (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
116 [ # # ]: 0 : if ((i + 1) % 3 == 0)
117 : 0 : printf (" ");
118 [ # # ][ # # ]: 0 : if ((i + 1) % 6 == 0 && i + 1 < len)
119 : 0 : printf ("\n\t;; ");
120 : : }
121 : 0 : printf ("\n");
122 : 0 : }
123 : :
124 : : int
125 : 14 : main (int argc, char *argv[])
126 : : {
127 : : Shishi *handle;
128 : :
129 : : do
130 [ + - ][ - + ]: 14 : if (strcmp (argv[argc - 1], "-v") == 0 ||
131 : 14 : strcmp (argv[argc - 1], "--verbose") == 0)
132 : 0 : debug = 1;
133 [ + - ][ - + ]: 14 : else if (strcmp (argv[argc - 1], "-b") == 0 ||
134 : 14 : strcmp (argv[argc - 1], "--break-on-error") == 0)
135 : 0 : break_on_error = 1;
136 [ + - ][ + - ]: 14 : else if (strcmp (argv[argc - 1], "-h") == 0 ||
[ - + ]
137 : 14 : strcmp (argv[argc - 1], "-?") == 0 ||
138 : 14 : strcmp (argv[argc - 1], "--help") == 0)
139 : : {
140 : 0 : printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
141 : : argv[0]);
142 : 0 : return 1;
143 : : }
144 [ - + ]: 14 : while (argc-- > 1);
145 : :
146 : 14 : handle = shishi ();
147 [ - + ]: 14 : if (handle == NULL)
148 : : {
149 : 0 : fail ("Could not initialize shishi\n");
150 : 0 : return 1;
151 : : }
152 : :
153 [ - + ]: 14 : if (debug)
154 : : {
155 : 0 : shishi_cfg (handle, strdup ("verbose"));
156 : 0 : shishi_cfg (handle, strdup ("verbose-noise"));
157 : 0 : shishi_cfg (handle, strdup ("verbose-asn1"));
158 : 0 : shishi_cfg (handle, strdup ("verbose-crypto"));
159 : 0 : shishi_cfg (handle, strdup ("verbose-crypto-noise"));
160 : : }
161 : :
162 : 14 : test (handle);
163 : :
164 : 14 : shishi_done (handle);
165 : :
166 [ - + ]: 14 : if (debug)
167 : 0 : printf ("Self test `%s' finished with %d errors\n", argv[0], error_count);
168 : :
169 : 14 : return error_count ? 1 : 0;
170 : : }
|