Fix.
[shishi.git] / tests / safe.c
blob0d9b3822faf1c779fa8ae8c28333f9b30439b347
1 /* safe.c Shishi SAFE self tests.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "utils.c"
23 #include <shishi.h>
25 int
26 main (int argc, char *argv[])
28 Shishi *handle;
29 Shishi_safe *safe;
30 Shishi_key *key;
31 Shishi_asn1 asn1safe;
32 char *p, *q;
33 size_t l, m;
34 int32_t t;
35 int res;
38 if (strcmp (argv[argc - 1], "-v") == 0 ||
39 strcmp (argv[argc - 1], "--verbose") == 0)
40 verbose = 1;
41 else if (strcmp (argv[argc - 1], "-d") == 0 ||
42 strcmp (argv[argc - 1], "--debug") == 0)
43 debug = 1;
44 else if (strcmp (argv[argc - 1], "-b") == 0 ||
45 strcmp (argv[argc - 1], "--break-on-error") == 0)
46 break_on_error = 1;
47 else if (strcmp (argv[argc - 1], "-h") == 0 ||
48 strcmp (argv[argc - 1], "-?") == 0 ||
49 strcmp (argv[argc - 1], "--help") == 0)
51 printf ("Usage: %s [-vdbh?] [--verbose] [--debug] "
52 "[--break-on-error] [--help]\n", argv[0]);
53 return 1;
55 while (argc-- > 1);
57 handle = shishi ();
58 if (handle == NULL)
60 fail ("Could not initialize shishi\n");
61 return 1;
64 if (debug)
65 shishi_cfg (handle, strdup ("verbose"));
67 escapeprint (NULL, 0);
68 hexprint (NULL, 0);
69 binprint (NULL, 0);
71 /* shishi_safe() */
72 res = shishi_safe (handle, &safe);
73 if (debug)
74 printf ("shishi_safe () => `%p'.\n", safe);
75 if (res == SHISHI_OK)
76 success ("shishi_safe() OK\n");
77 else
78 fail ("shishi_safe() failed\n");
80 /* shishi_safe_key */
81 key = shishi_safe_key (safe);
82 if (key)
83 success ("shishi_safe_key() OK\n");
84 else
85 fail ("shishi_safe_key() failed\n");
87 /* shishi_safe_safe */
88 asn1safe = shishi_safe_safe (safe);
89 if (asn1safe)
90 success ("shishi_safe_safe() OK\n");
91 else
92 fail ("shishi_safe_safe() failed\n");
94 /* shishi_safe_set_user_data */
95 res = shishi_safe_set_user_data (handle, asn1safe, "foo", 3);
96 if (res == SHISHI_OK)
97 success ("shishi_safe_set_user_data() OK\n");
98 else
99 fail ("shishi_safe_set_user_data() failed (%d)\n", res);
101 /* shishi_safe_user_data */
102 res = shishi_safe_user_data (handle, asn1safe, &p, &l);
103 if (debug)
104 escapeprint (p, l);
105 if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
106 success ("shishi_safe_user_data() OK\n");
107 else
108 fail ("shishi_safe_user_data() failed (%d)\n", res);
110 /* shishi_safe_set_cksum */
111 res = shishi_safe_set_cksum (handle, asn1safe, 42, "bar", 3);
112 if (res == SHISHI_OK)
113 success ("shishi_safe_set_cksum() OK\n");
114 else
115 fail ("shishi_safe_set_cksum() failed (%d)\n", res);
117 /* shishi_safe_cksum */
118 res = shishi_safe_cksum (handle, asn1safe, &t, &q, &m);
119 if (debug)
121 printf ("type=%d\n", t);
122 escapeprint (q, m);
124 if (res == SHISHI_OK && t == 42 && m == 3 && memcmp (q, "bar", 3) == 0)
125 success ("shishi_safe_cksum() OK\n");
126 else
127 fail ("shishi_safe_cksum() failed (%d)\n", res);
129 /* shishi_safe_verify */
130 res = shishi_safe_verify (safe, key);
131 if (res == SHISHI_CRYPTO_ERROR) /* t==42 unsupported cksumtype */
132 success ("shishi_safe_verify() OK\n");
133 else
134 fail ("shishi_safe_verify() failed (%d)\n", res);
136 #if WITH_DES
137 /* shishi_safe_set_cksum */
138 res = shishi_safe_set_cksum (handle, asn1safe, SHISHI_RSA_MD5_DES,
139 "bar", 3);
140 if (res == SHISHI_OK)
141 success ("shishi_safe_set_cksum() OK\n");
142 else
143 fail ("shishi_safe_set_cksum() failed (%d)\n", res);
145 /* shishi_safe_verify */
146 res = shishi_safe_verify (safe, key);
147 if (res == SHISHI_VERIFY_FAILED)
148 success ("shishi_safe_verify() OK\n");
149 else
150 fail ("shishi_safe_verify() failed (%d)\n", res);
151 #endif
153 /* shishi_safe_safe_der() */
154 res = shishi_safe_safe_der (safe, &p, &l);
155 if (res == SHISHI_OK)
156 success ("shishi_safe_safe_der() OK\n");
157 else
158 fail ("shishi_safe_safe_der() failed\n");
160 /* shishi_safe_to_file() */
161 res = shishi_safe_to_file (handle, asn1safe, SHISHI_FILETYPE_TEXT,
162 "safe.tmp");
163 if (res == SHISHI_OK)
164 success ("shishi_safe_to_file() OK\n");
165 else
166 fail ("shishi_safe_to_file() failed\n");
168 /* shishi_safe_done() */
169 shishi_safe_done (safe);
170 success ("shishi_safe_done() OK\n");
172 /* shishi_authenticator_from_file() */
173 res = shishi_safe_from_file (handle, &asn1safe, SHISHI_FILETYPE_TEXT,
174 "safe.tmp");
175 if (res == SHISHI_OK)
176 success ("shishi_safe_from_file() OK\n");
177 else
178 fail ("shishi_safe_from_file() failed\n");
180 if (debug)
182 /* shishi_safe_print() */
183 res = shishi_safe_print (handle, stdout, asn1safe);
184 if (res == SHISHI_OK)
185 success ("shishi_safe_print() OK\n");
186 else
187 fail ("shishi_safe_print() failed\n");
190 /* shishi_a2d() */
191 res = shishi_new_a2d (handle, asn1safe, &q, &m);
192 if (res == SHISHI_OK)
193 success ("shishi_a2d() OK\n");
194 else
195 fail ("shishi_a2d() failed\n");
197 /* Compare DER encodings of authenticators */
198 if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
199 success ("DER comparison OK\n");
200 else
201 fail ("DER comparison failed\n");
203 free (q);
204 free (p);
206 /* unlink() */
207 res = unlink ("safe.tmp");
208 if (res == 0)
209 success ("unlink() OK\n");
210 else
211 fail ("unlink() failed\n");
213 shishi_done (handle);
215 if (verbose)
216 printf ("SAFE self tests done with %d errors\n", error_count);
218 return error_count ? 1 : 0;