Use new xreadlink interface.
[shishi.git] / tests / priv.c
blob2cb724911f496ed93ded751fec81a47f9267f473
1 /* priv.c Shishi PRIV self tests.
2 * Copyright (C) 2002, 2003, 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 #include "utils.c"
24 void
25 test (Shishi * handle)
27 Shishi_priv *priv;
28 Shishi_key *key;
29 Shishi_asn1 asn1priv;
30 Shishi_asn1 asn1encprivpart;
31 char *p, *q;
32 size_t l, m;
33 int32_t t;
34 int res;
36 if (debug)
37 shishi_cfg (handle, strdup ("verbose-crypto"));
39 /* shishi_priv() */
40 res = shishi_priv (handle, &priv);
41 if (debug)
42 printf ("shishi_priv () => `%p'.\n", priv);
43 if (res == SHISHI_OK)
44 success ("shishi_priv() OK\n");
45 else
46 fail ("shishi_priv() failed\n");
48 /* shishi_priv_key */
49 key = shishi_priv_key (priv);
50 if (key)
51 success ("shishi_priv_key() OK\n");
52 else
53 fail ("shishi_priv_key() failed\n");
55 /* shishi_priv_priv */
56 asn1priv = shishi_priv_priv (priv);
57 if (asn1priv)
58 success ("shishi_priv_priv() OK\n");
59 else
60 fail ("shishi_priv_priv() failed\n");
62 /* shishi_priv_encprivpart */
63 asn1encprivpart = shishi_priv_encprivpart (priv);
64 if (asn1encprivpart)
65 success ("shishi_priv_encprivpart() OK\n");
66 else
67 fail ("shishi_priv_encprivpart() failed\n");
69 /* shishi_encprivpart_set_user_data */
70 res = shishi_encprivpart_set_user_data (handle, asn1encprivpart, "foo", 3);
71 if (res == SHISHI_OK)
72 success ("shishi_encprivpart_set_user_data() OK\n");
73 else
74 fail ("shishi_encprivpart_set_user_data() failed (%d)\n", res);
76 /* shishi_encprivpart_user_data */
77 res = shishi_encprivpart_user_data (handle, asn1encprivpart, &p, &l);
78 if (debug)
79 escapeprint (p, l);
80 if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
81 success ("shishi_encprivpart_user_data() OK\n");
82 else
83 fail ("shishi_encprivpart_user_data() failed (%d)\n", res);
84 free (p);
86 /* shishi_priv_set_cksum */
87 res = shishi_priv_set_enc_part (handle, asn1priv, 42, "bar", 3);
88 if (res == SHISHI_OK)
89 success ("shishi_priv_set_enc_part() OK\n");
90 else
91 fail ("shishi_priv_set_enc_part() failed (%d)\n", res);
93 /* shishi_priv_enc_part_etype */
94 res = shishi_priv_enc_part_etype (handle, asn1priv, &t);
95 if (debug)
96 printf ("type=%d\n", t);
97 if (res == SHISHI_OK && t == 42)
98 success ("shishi_priv_enc_part_etype() OK\n");
99 else
100 fail ("shishi_priv_enc_part_etype() failed (%d)\n", res);
102 /* shishi_priv_process */
103 res = shishi_priv_process (priv, key);
104 if (res == SHISHI_PRIV_BAD_KEYTYPE) /* t==42 unsupported etype */
105 success ("shishi_priv_proces() OK\n");
106 else
107 fail ("shishi_priv_process() failed (%d)\n", res);
109 /* shishi_priv_priv_der() */
110 res = shishi_priv_priv_der (priv, &p, &l);
111 if (res == SHISHI_OK)
112 success ("shishi_priv_priv_der() OK\n");
113 else
114 fail ("shishi_priv_priv_der() failed\n");
116 /* shishi_priv_to_file() */
117 res = shishi_priv_to_file (handle, asn1priv, SHISHI_FILETYPE_TEXT,
118 "priv.tmp");
119 if (res == SHISHI_OK)
120 success ("shishi_priv_to_file() OK\n");
121 else
122 fail ("shishi_priv_to_file() failed\n");
124 /* shishi_priv_done() */
125 shishi_priv_done (priv);
126 success ("shishi_priv_done() OK\n");
128 /* shishi_authenticator_from_file() */
129 res = shishi_priv_from_file (handle, &asn1priv, SHISHI_FILETYPE_TEXT,
130 "priv.tmp");
131 if (res == SHISHI_OK)
132 success ("shishi_priv_from_file() OK\n");
133 else
134 fail ("shishi_priv_from_file() failed\n");
136 if (debug)
138 /* shishi_priv_print() */
139 res = shishi_priv_print (handle, stdout, asn1priv);
140 if (res == SHISHI_OK)
141 success ("shishi_priv_print() OK\n");
142 else
143 fail ("shishi_priv_print() failed\n");
146 /* shishi_asn1_to_der() */
147 res = shishi_asn1_to_der (handle, asn1priv, &q, &m);
148 if (res == SHISHI_OK)
149 success ("shishi_asn1_to_der() OK\n");
150 else
151 fail ("shishi_asn1_to_der() failed\n");
153 shishi_asn1_done (handle, asn1priv);
155 /* Compare DER encodings of authenticators */
156 if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
157 success ("DER comparison OK\n");
158 else
159 fail ("DER comparison failed\n");
161 free (q);
162 free (p);
164 /* unlink() */
165 res = unlink ("priv.tmp");
166 if (res == 0)
167 success ("unlink() OK\n");
168 else
169 fail ("unlink() failed\n");