Remove.
[gsasl.git] / tests / old-cram-md5.c
blob62e16734af2514995321a107a0129df69436b5f5
1 /* cram-md5.c --- Test the CRAM-MD5 mechanism, using old callback API.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL 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 * GNU SASL 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 GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "utils.h"
33 #define PASSWORD "Open, Sesame"
34 #define USERNAME "Ali Baba"
35 /* "Ali " "\xC2\xAD" "Bab" "\xC2\xAA" */
36 /* "Al\xC2\xAA""dd\xC2\xAD""in\xC2\xAE" */
38 static int
39 server_cb_retrieve (Gsasl_session_ctx * xctx,
40 const char *authentication_id,
41 const char *authorization_id,
42 const char *realm, char *key, size_t * keylen)
44 size_t needlen = strlen (PASSWORD);
46 if (key && *keylen < needlen)
47 return GSASL_TOO_SMALL_BUFFER;
49 *keylen = needlen;
50 if (key)
51 memcpy (key, PASSWORD, *keylen);
53 return GSASL_OK;
56 static int
57 client_cb_authentication_id (Gsasl_session_ctx * xctx,
58 char *out, size_t * outlen)
60 size_t needlen = strlen (USERNAME);
62 if (out && *outlen < needlen)
63 return GSASL_TOO_SMALL_BUFFER;
65 *outlen = needlen;
66 if (out)
67 memcpy (out, USERNAME, *outlen);
69 return GSASL_OK;
72 static int
73 client_cb_password (Gsasl_session_ctx * xctx, char *out, size_t * outlen)
75 size_t needlen = strlen (PASSWORD);
77 if (out && *outlen < needlen)
78 return GSASL_TOO_SMALL_BUFFER;
80 *outlen = needlen;
81 if (out)
82 memcpy (out, PASSWORD, *outlen);
84 return GSASL_OK;
87 void
88 doit (void)
90 Gsasl_ctx *ctx = NULL;
91 Gsasl_session_ctx *server = NULL, *client = NULL;
92 char *s1, *s2;
93 size_t s1len, s2len;
94 size_t i;
95 int res;
97 res = gsasl_init (&ctx);
98 if (res != GSASL_OK)
100 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
101 return;
104 gsasl_server_callback_retrieve_set (ctx, server_cb_retrieve);
106 gsasl_client_callback_authentication_id_set (ctx,
107 client_cb_authentication_id);
108 gsasl_client_callback_password_set (ctx, client_cb_password);
111 for (i = 0; i < 5; i++)
113 res = gsasl_server_start (ctx, "CRAM-MD5", &server);
114 if (res != GSASL_OK)
116 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
117 return;
119 res = gsasl_client_start (ctx, "CRAM-MD5", &client);
120 if (res != GSASL_OK)
122 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
123 return;
126 res = gsasl_step (server, NULL, 0, &s1, &s1len);
127 if (res != GSASL_NEEDS_MORE)
129 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
130 return;
133 if (debug)
134 printf ("S: %.*s\n", s1len, s1);
136 res = gsasl_step (client, s1, s1len, &s2, &s2len);
137 free (s1);
138 if (res != GSASL_OK)
140 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
141 return;
144 if (debug)
145 printf ("C: %.*s\n", s2len, s2);
147 res = gsasl_step (server, s2, s2len, &s1, &s1len);
148 free (s2);
149 if (res != GSASL_OK)
151 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
152 return;
155 if (s1len != 0)
157 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
158 fail ("%s\n", s1);
159 return;
162 free (s1);
164 if (debug)
165 printf ("\n");
167 gsasl_client_finish (client);
168 gsasl_server_finish (server);
171 gsasl_done (ctx);