Add LIBTASN1_CFLAGS.
[shishi.git] / tests / priv.c
blobf75c9bc147ad05cab1f590d1f39d0512a0bbca8a
1 /* priv.c Shishi PRIV 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_priv *priv;
30 Shishi_key *key;
31 Shishi_asn1 asn1priv;
32 Shishi_asn1 asn1encprivpart;
33 char *p, *q;
34 size_t l, m;
35 int32_t t;
36 int res;
39 if (strcmp (argv[argc - 1], "-v") == 0 ||
40 strcmp (argv[argc - 1], "--verbose") == 0)
41 verbose = 1;
42 else if (strcmp (argv[argc - 1], "-d") == 0 ||
43 strcmp (argv[argc - 1], "--debug") == 0)
44 debug = 1;
45 else if (strcmp (argv[argc - 1], "-b") == 0 ||
46 strcmp (argv[argc - 1], "--break-on-error") == 0)
47 break_on_error = 1;
48 else if (strcmp (argv[argc - 1], "-h") == 0 ||
49 strcmp (argv[argc - 1], "-?") == 0 ||
50 strcmp (argv[argc - 1], "--help") == 0)
52 printf ("Usage: %s [-vdbh?] [--verbose] [--debug] "
53 "[--break-on-error] [--help]\n", argv[0]);
54 return 1;
56 while (argc-- > 1);
58 handle = shishi ();
59 if (handle == NULL)
61 fail ("Could not initialize shishi\n");
62 return 1;
65 if (debug)
67 shishi_cfg (handle, strdup ("verbose"));
68 shishi_cfg (handle, strdup ("verbose-crypto"));
71 escapeprint (NULL, 0);
72 hexprint (NULL, 0);
73 binprint (NULL, 0);
75 /* shishi_priv() */
76 res = shishi_priv (handle, &priv);
77 if (debug)
78 printf ("shishi_priv () => `%p'.\n", priv);
79 if (res == SHISHI_OK)
80 success ("shishi_priv() OK\n");
81 else
82 fail ("shishi_priv() failed\n");
84 /* shishi_priv_key */
85 key = shishi_priv_key (priv);
86 if (key)
87 success ("shishi_priv_key() OK\n");
88 else
89 fail ("shishi_priv_key() failed\n");
91 /* shishi_priv_priv */
92 asn1priv = shishi_priv_priv (priv);
93 if (asn1priv)
94 success ("shishi_priv_priv() OK\n");
95 else
96 fail ("shishi_priv_priv() failed\n");
98 /* shishi_priv_encprivpart */
99 asn1encprivpart = shishi_priv_encprivpart (priv);
100 if (asn1encprivpart)
101 success ("shishi_priv_encprivpart() OK\n");
102 else
103 fail ("shishi_priv_encprivpart() failed\n");
105 /* shishi_encprivpart_set_user_data */
106 res = shishi_encprivpart_set_user_data (handle, asn1encprivpart, "foo", 3);
107 if (res == SHISHI_OK)
108 success ("shishi_encprivpart_set_user_data() OK\n");
109 else
110 fail ("shishi_encprivpart_set_user_data() failed (%d)\n", res);
112 /* shishi_encprivpart_user_data */
113 res = shishi_encprivpart_user_data (handle, asn1encprivpart, &p, &l);
114 if (debug)
115 escapeprint (p, l);
116 if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
117 success ("shishi_encprivpart_user_data() OK\n");
118 else
119 fail ("shishi_encprivpart_user_data() failed (%d)\n", res);
121 /* shishi_priv_set_cksum */
122 res = shishi_priv_set_enc_part (handle, asn1priv, 42, "bar", 3);
123 if (res == SHISHI_OK)
124 success ("shishi_priv_set_enc_part() OK\n");
125 else
126 fail ("shishi_priv_set_enc_part() failed (%d)\n", res);
128 /* shishi_priv_enc_part_etype */
129 res = shishi_priv_enc_part_etype (handle, asn1priv, &t);
130 if (debug)
131 printf ("type=%d\n", t);
132 if (res == SHISHI_OK && t == 42)
133 success ("shishi_priv_enc_part_etype() OK\n");
134 else
135 fail ("shishi_priv_enc_part_etype() failed (%d)\n", res);
137 /* shishi_priv_process */
138 res = shishi_priv_process (priv, key);
139 if (res == SHISHI_PRIV_BAD_KEYTYPE) /* t==42 unsupported etype */
140 success ("shishi_priv_proces() OK\n");
141 else
142 fail ("shishi_priv_process() failed (%d)\n", res);
144 /* shishi_priv_priv_der() */
145 res = shishi_priv_priv_der (priv, &p, &l);
146 if (res == SHISHI_OK)
147 success ("shishi_priv_priv_der() OK\n");
148 else
149 fail ("shishi_priv_priv_der() failed\n");
151 /* shishi_priv_to_file() */
152 res = shishi_priv_to_file (handle, asn1priv, SHISHI_FILETYPE_TEXT,
153 "priv.tmp");
154 if (res == SHISHI_OK)
155 success ("shishi_priv_to_file() OK\n");
156 else
157 fail ("shishi_priv_to_file() failed\n");
159 /* shishi_priv_done() */
160 shishi_priv_done (priv);
161 success ("shishi_priv_done() OK\n");
163 /* shishi_authenticator_from_file() */
164 res = shishi_priv_from_file (handle, &asn1priv, SHISHI_FILETYPE_TEXT,
165 "priv.tmp");
166 if (res == SHISHI_OK)
167 success ("shishi_priv_from_file() OK\n");
168 else
169 fail ("shishi_priv_from_file() failed\n");
171 if (debug)
173 /* shishi_priv_print() */
174 res = shishi_priv_print (handle, stdout, asn1priv);
175 if (res == SHISHI_OK)
176 success ("shishi_priv_print() OK\n");
177 else
178 fail ("shishi_priv_print() failed\n");
181 /* shishi_a2d() */
182 res = shishi_new_a2d (handle, asn1priv, &q, &m);
183 if (res == SHISHI_OK)
184 success ("shishi_a2d() OK\n");
185 else
186 fail ("shishi_a2d() failed\n");
188 /* Compare DER encodings of authenticators */
189 if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
190 success ("DER comparison OK\n");
191 else
192 fail ("DER comparison failed\n");
194 free (q);
195 free (p);
197 /* unlink() */
198 res = unlink ("priv.tmp");
199 if (res == 0)
200 success ("unlink() OK\n");
201 else
202 fail ("unlink() failed\n");
204 shishi_done (handle);
206 if (verbose)
207 printf ("PRIV self tests done with %d errors\n", error_count);
209 return error_count ? 1 : 0;