Add.
[gsasl.git] / tests / cram-md5.c
blobc47ab0dba14302f527e6d405721769ed0c1e48e5
1 /* cram-md5.c --- Test the CRAM-MD5 mechanism.
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" */
39 static int
40 callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
42 int rc = GSASL_NO_CALLBACK;
44 /* Get user info from user. */
46 switch (prop)
48 case GSASL_PASSWORD:
49 gsasl_property_set (sctx, GSASL_PASSWORD, PASSWORD);
50 rc = GSASL_OK;
51 break;
53 case GSASL_AUTHID:
54 gsasl_property_set (sctx, GSASL_AUTHID, USERNAME);
55 rc = GSASL_OK;
56 break;
58 default:
59 fail ("Unknown callback property %d\n", prop);
60 break;
63 return rc;
66 void
67 doit (void)
69 Gsasl *ctx = NULL;
70 Gsasl_session *server = NULL, *client = NULL;
71 char *s1, *s2;
72 size_t s1len, s2len;
73 size_t i;
74 int res;
76 res = gsasl_init (&ctx);
77 if (res != GSASL_OK)
79 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
80 return;
83 gsasl_callback_set (ctx, callback);
85 for (i = 0; i < 5; i++)
87 res = gsasl_server_start (ctx, "CRAM-MD5", &server);
88 if (res != GSASL_OK)
90 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
91 return;
93 res = gsasl_client_start (ctx, "CRAM-MD5", &client);
94 if (res != GSASL_OK)
96 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
97 return;
100 res = gsasl_step (server, NULL, 0, &s1, &s1len);
101 if (res != GSASL_NEEDS_MORE)
103 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
104 return;
107 if (debug)
108 printf ("S: %.*s\n", s1len, s1);
110 res = gsasl_step (client, s1, s1len, &s2, &s2len);
111 free (s1);
112 if (res != GSASL_OK)
114 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
115 return;
118 if (debug)
119 printf ("C: %.*s\n", s2len, s2);
121 res = gsasl_step (server, s2, s2len, &s1, &s1len);
122 free (s2);
123 if (res != GSASL_OK)
125 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
126 return;
129 if (s1len != 0)
131 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
132 fail ("%s\n", s1);
133 return;
136 free (s1);
138 if (debug)
139 printf ("\n");
141 gsasl_finish (client);
142 gsasl_finish (server);
145 gsasl_done (ctx);