Update gnulib files.
[gsasl.git] / tests / digest-md5.c
blob5838f59c14ae05d3543b44392a70b525a97ba741
1 /* digest-md5.c --- Test the DIGEST-MD5 mechanism.
2 * Copyright (C) 2002, 2003, 2004, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program 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.
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "utils.h"
32 #define PASSWORD "Open, Sesame"
33 #define USERNAME "Ali Baba"
34 /* "Ali " "\xC2\xAD" "Bab" "\xC2\xAA" */
35 /* "Al\xC2\xAA""dd\xC2\xAD""in\xC2\xAE" */
36 #define AUTHZID "joe"
37 #define SERVICE "imap"
38 #define HOSTNAME "hostname"
39 #define REALM "realm"
41 static int
42 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
44 static int flip = 0;
45 static int flip2 = 0;
46 int rc = GSASL_NO_CALLBACK;
48 /* Get user info from user. */
50 switch (prop)
52 case GSASL_PASSWORD:
53 gsasl_property_set (sctx, prop, PASSWORD);
54 rc = GSASL_OK;
55 break;
57 case GSASL_AUTHID:
58 gsasl_property_set (sctx, prop, USERNAME);
59 rc = GSASL_OK;
60 break;
62 case GSASL_AUTHZID:
63 if (flip)
64 gsasl_property_set (sctx, prop, AUTHZID);
65 else
66 gsasl_property_set (sctx, prop, NULL);
67 flip = !flip;
68 rc = GSASL_OK;
69 break;
71 case GSASL_SERVICE:
72 gsasl_property_set (sctx, prop, SERVICE);
73 rc = GSASL_OK;
74 break;
76 case GSASL_REALM:
77 if (flip2)
78 gsasl_property_set (sctx, prop, REALM);
79 else
80 gsasl_property_set (sctx, prop, NULL);
81 flip2++;
82 if (flip2 == 3)
83 flip2 = 0;
84 rc = GSASL_OK;
85 break;
87 case GSASL_HOSTNAME:
88 gsasl_property_set (sctx, prop, HOSTNAME);
89 rc = GSASL_OK;
90 break;
92 default:
93 fail ("Unknown callback property %d\n", prop);
94 break;
97 return rc;
100 void
101 doit (void)
103 Gsasl *ctx = NULL;
104 Gsasl_session *server = NULL, *client = NULL;
105 char *s1, *s2;
106 size_t s1len, s2len;
107 size_t i;
108 int res;
110 res = gsasl_init (&ctx);
111 if (res != GSASL_OK)
113 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
114 return;
117 gsasl_callback_set (ctx, callback);
119 for (i = 0; i < 5; i++)
121 res = gsasl_server_start (ctx, "DIGEST-MD5", &server);
122 if (res != GSASL_OK)
124 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
125 return;
127 res = gsasl_client_start (ctx, "DIGEST-MD5", &client);
128 if (res != GSASL_OK)
130 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
131 return;
134 /* Server begins... */
136 res = gsasl_step (server, NULL, 0, &s1, &s1len);
137 if (res != GSASL_NEEDS_MORE)
139 fail ("gsasl_step(1) failed (%d):\n%s\n", res,
140 gsasl_strerror (res));
141 return;
144 if (debug)
145 printf ("S: %.*s\n", s1len, s1);
147 /* Client respond... */
149 res = gsasl_step (client, s1, s1len, &s2, &s2len);
150 free (s1);
151 if (res != GSASL_NEEDS_MORE)
153 fail ("gsasl_step(2) failed (%d):\n%s\n", res,
154 gsasl_strerror (res));
155 return;
158 if (debug)
159 printf ("C: %.*s\n", s2len, s2);
161 /* Server finishes... */
163 res = gsasl_step (server, s2, s2len, &s1, &s1len);
164 free (s2);
165 if (res != GSASL_NEEDS_MORE)
167 fail ("gsasl_step(3) failed (%d):\n%s\n", res,
168 gsasl_strerror (res));
169 return;
172 if (debug)
173 printf ("S: %.*s\n", s1len, s1);
175 /* Client finishes... */
177 res = gsasl_step (client, s1, s1len, &s2, &s2len);
178 free (s1);
179 if (res != GSASL_OK)
181 fail ("gsasl_step(4) failed (%d):\n%s\n", res,
182 gsasl_strerror (res));
183 return;
186 if (debug)
188 /* Solaris x86 crashes here if s2 is NULL, even when s2len
189 is 0. */
190 if (s2len)
191 printf ("C: %.*s\n", s2len, s2);
192 else
193 printf ("C: \n");
196 /* Server is done. */
198 res = gsasl_step (server, s2, s2len, &s1, &s1len);
199 free (s2);
200 if (res != GSASL_OK)
202 fail ("gsasl_step(5) failed (%d):\n%s\n", res,
203 gsasl_strerror (res));
204 return;
207 if (s1len != 0)
209 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
210 fail ("%s\n", s1);
211 return;
214 free (s1);
216 if (debug)
217 printf ("\n");
219 gsasl_finish (client);
220 gsasl_finish (server);
223 gsasl_done (ctx);