Bump versions.
[gsasl.git] / tests / cram-md5.c
blobe3ef093ab48004a58fd49c743c3fdaa1e22af061
1 /* cram-md5.c --- Test the CRAM-MD5 mechanism.
2 * Copyright (C) 2002, 2003, 2004, 2005, 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" */
38 static int
39 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
41 int rc = GSASL_NO_CALLBACK;
43 /* Get user info from user. */
45 switch (prop)
47 case GSASL_PASSWORD:
48 gsasl_property_set (sctx, GSASL_PASSWORD, PASSWORD);
49 rc = GSASL_OK;
50 break;
52 case GSASL_AUTHID:
53 gsasl_property_set (sctx, GSASL_AUTHID, USERNAME);
54 rc = GSASL_OK;
55 break;
57 default:
58 fail ("Unknown callback property %d\n", prop);
59 break;
62 return rc;
65 void
66 doit (void)
68 Gsasl *ctx = NULL;
69 Gsasl_session *server = NULL, *client = NULL;
70 char *s1, *s2;
71 size_t s1len, s2len;
72 size_t i;
73 int res;
75 res = gsasl_init (&ctx);
76 if (res != GSASL_OK)
78 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
79 return;
82 gsasl_callback_set (ctx, callback);
84 for (i = 0; i < 5; i++)
86 res = gsasl_server_start (ctx, "CRAM-MD5", &server);
87 if (res != GSASL_OK)
89 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
90 return;
92 res = gsasl_client_start (ctx, "CRAM-MD5", &client);
93 if (res != GSASL_OK)
95 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
96 return;
99 res = gsasl_step (server, NULL, 0, &s1, &s1len);
100 if (res != GSASL_NEEDS_MORE)
102 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
103 return;
106 if (debug)
107 printf ("S: %.*s\n", s1len, s1);
109 res = gsasl_step (client, s1, s1len, &s2, &s2len);
110 free (s1);
111 if (res != GSASL_OK)
113 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
114 return;
117 if (debug)
118 printf ("C: %.*s\n", s2len, s2);
120 res = gsasl_step (server, s2, s2len, &s1, &s1len);
121 free (s2);
122 if (res != GSASL_OK)
124 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
125 return;
128 if (s1len != 0)
130 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
131 fail ("%s\n", s1);
132 return;
135 free (s1);
137 if (debug)
138 printf ("\n");
140 gsasl_finish (client);
141 gsasl_finish (server);
144 gsasl_done (ctx);