Fix.
[gsasl.git] / tests / old-md5file.c
blobdcd2b3e52ba6ca341c386f57c5480d0b377e2097
1 /* md5file.c --- Test the MD5 file password function.
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>
30 #include <gsasl.h>
32 #include "utils.h"
34 /* Should match values from cram-md5.pwd. */
35 #define BILL "bill"
36 #define BILL_PASSWD "hubba-hubba"
38 void
39 doit (void)
41 char *md5file;
42 char key[BUFSIZ];
43 size_t keylen = BUFSIZ - 1;
44 int res;
46 md5file = getenv ("MD5FILE");
47 if (md5file)
49 char *p;
50 if ((p = strchr (md5file, '=')))
51 md5file = p;
54 if (!md5file)
55 md5file = "cram-md5.pwd";
57 keylen = sizeof (key) - 1;
58 res = gsasl_md5pwd_get_password ("non-existing-file", "user", key, &keylen);
59 if (res == GSASL_FOPEN_ERROR)
60 success ("non-existing-file OK\n");
61 else
62 fail ("non-existing-file FAIL (%d): %s\n", res, gsasl_strerror (res));
64 keylen = sizeof (key) - 1;
65 res = gsasl_md5pwd_get_password (md5file, BILL, key, &keylen);
66 if (res == GSASL_OK)
67 success ("user-found OK\n");
68 else
69 fail ("user-found FAIL (%d): %s\n", res, gsasl_strerror (res));
70 if (keylen != strlen (BILL_PASSWD)
71 || memcmp (key, BILL_PASSWD, keylen) != 0)
72 fail ("user-password FAIL (%d): %.*s\n", keylen, keylen, key);
73 else
74 success ("user-password OK\n");
76 keylen = 5;
77 res = gsasl_md5pwd_get_password (md5file, BILL, key, &keylen);
78 if (res == GSASL_TOO_SMALL_BUFFER)
79 success ("too-small-buffer OK\n");
80 else
81 fail ("too-small-buffer FAIL (%d): %s\n", res, gsasl_strerror (res));
83 keylen = sizeof (key) - 1;
84 res = gsasl_md5pwd_get_password (md5file, "user", key, &keylen);
85 if (res == GSASL_AUTHENTICATION_ERROR)
86 success ("no-such-user OK\n");
87 else
88 fail ("no-such-user FAIL (%d): %s\n", res, gsasl_strerror (res));