Add.
[shishi.git] / tests / safe.c
blob6dfd7d1f32efac308af7d9078dd60ec3a1f46c64
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 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 #include "utils.c"
24 void
25 test (Shishi * handle)
27 Shishi_safe *safe;
28 Shishi_key *key;
29 Shishi_asn1 asn1safe;
30 char *p, *q;
31 size_t l, m;
32 int32_t t;
33 int res;
35 /* shishi_safe() */
36 res = shishi_safe (handle, &safe);
37 if (debug)
38 printf ("shishi_safe () => `%p'.\n", safe);
39 if (res == SHISHI_OK)
40 success ("shishi_safe() OK\n");
41 else
42 fail ("shishi_safe() failed\n");
44 /* shishi_safe_key */
45 key = shishi_safe_key (safe);
46 if (key)
47 success ("shishi_safe_key() OK\n");
48 else
49 fail ("shishi_safe_key() failed\n");
51 /* shishi_safe_safe */
52 asn1safe = shishi_safe_safe (safe);
53 if (asn1safe)
54 success ("shishi_safe_safe() OK\n");
55 else
56 fail ("shishi_safe_safe() failed\n");
58 /* shishi_safe_set_user_data */
59 res = shishi_safe_set_user_data (handle, asn1safe, "foo", 3);
60 if (res == SHISHI_OK)
61 success ("shishi_safe_set_user_data() OK\n");
62 else
63 fail ("shishi_safe_set_user_data() failed (%d)\n", res);
65 /* shishi_safe_user_data */
66 res = shishi_safe_user_data (handle, asn1safe, &p, &l);
67 if (debug)
68 escapeprint (p, l);
69 if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
70 success ("shishi_safe_user_data() OK\n");
71 else
72 fail ("shishi_safe_user_data() failed (%d)\n", res);
73 free (p);
75 /* shishi_safe_set_cksum */
76 res = shishi_safe_set_cksum (handle, asn1safe, 42, "bar", 3);
77 if (res == SHISHI_OK)
78 success ("shishi_safe_set_cksum() OK\n");
79 else
80 fail ("shishi_safe_set_cksum() failed (%d)\n", res);
82 /* shishi_safe_cksum */
83 res = shishi_safe_cksum (handle, asn1safe, &t, &q, &m);
84 if (debug)
86 printf ("type=%d\n", t);
87 escapeprint (q, m);
89 if (res == SHISHI_OK && t == 42 && m == 3 && memcmp (q, "bar", 3) == 0)
90 success ("shishi_safe_cksum() OK\n");
91 else
92 fail ("shishi_safe_cksum() failed (%d)\n", res);
93 free (q);
95 /* shishi_safe_verify */
96 res = shishi_safe_verify (safe, key);
97 if (res == SHISHI_CRYPTO_ERROR) /* t==42 unsupported cksumtype */
98 success ("shishi_safe_verify() OK\n");
99 else
100 fail ("shishi_safe_verify() failed (%d)\n", res);
102 #if WITH_DES
103 /* shishi_safe_set_cksum */
104 res = shishi_safe_set_cksum (handle, asn1safe, SHISHI_RSA_MD5_DES,
105 "bar", 3);
106 if (res == SHISHI_OK)
107 success ("shishi_safe_set_cksum() OK\n");
108 else
109 fail ("shishi_safe_set_cksum() failed (%d)\n", res);
111 /* shishi_safe_verify */
112 res = shishi_safe_verify (safe, key);
113 if (res == SHISHI_VERIFY_FAILED)
114 success ("shishi_safe_verify() OK\n");
115 else
116 fail ("shishi_safe_verify() failed (%d)\n", res);
117 #endif
119 /* shishi_safe_safe_der() */
120 res = shishi_safe_safe_der (safe, &p, &l);
121 if (res == SHISHI_OK)
122 success ("shishi_safe_safe_der() OK\n");
123 else
124 fail ("shishi_safe_safe_der() failed\n");
126 /* shishi_safe_to_file() */
127 res = shishi_safe_to_file (handle, asn1safe, SHISHI_FILETYPE_TEXT,
128 "safe.tmp");
129 if (res == SHISHI_OK)
130 success ("shishi_safe_to_file() OK\n");
131 else
132 fail ("shishi_safe_to_file() failed\n");
134 /* shishi_safe_done() */
135 shishi_safe_done (safe);
136 success ("shishi_safe_done() OK\n");
138 /* shishi_authenticator_from_file() */
139 res = shishi_safe_from_file (handle, &asn1safe, SHISHI_FILETYPE_TEXT,
140 "safe.tmp");
141 if (res == SHISHI_OK)
142 success ("shishi_safe_from_file() OK\n");
143 else
144 fail ("shishi_safe_from_file() failed\n");
146 if (debug)
148 /* shishi_safe_print() */
149 res = shishi_safe_print (handle, stdout, asn1safe);
150 if (res == SHISHI_OK)
151 success ("shishi_safe_print() OK\n");
152 else
153 fail ("shishi_safe_print() failed\n");
156 /* shishi_asn1_to_der() */
157 res = shishi_asn1_to_der (handle, asn1safe, &q, &m);
158 if (res == SHISHI_OK)
159 success ("shishi_asn1_to_der() OK\n");
160 else
161 fail ("shishi_asn1_to_der() failed\n");
163 /* Compare DER encodings of authenticators */
164 if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
165 success ("DER comparison OK\n");
166 else
167 fail ("DER comparison failed\n");
169 shishi_asn1_done (handle, asn1safe);
170 free (q);
171 free (p);
173 /* unlink() */
174 res = unlink ("safe.tmp");
175 if (res == 0)
176 success ("unlink() OK\n");
177 else
178 fail ("unlink() failed\n");