Happy new year
[idzebra-moved-to-github.git] / util / tstpass.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <passwddb.h>
24 #include <yaz/test.h>
25 #include <yaz/snprintf.h>
26 #include <stdlib.h>
27
28 /* use env srcdir as base directory - or current directory if unset */
29 const char *get_srcdir(void)
30 {
31     const char *srcdir = getenv("srcdir");
32     if (!srcdir || ! *srcdir)
33         srcdir=".";
34     return srcdir;
35
36 }
37
38 static void tst(void)
39 {
40     char path[1024];
41     Passwd_db db;
42
43     db = passwd_db_open();
44     YAZ_CHECK(db);
45     if (!db)
46         return;
47
48     yaz_snprintf(path, sizeof(path), "%s/no_such_file.txt", get_srcdir());
49     YAZ_CHECK_EQ(passwd_db_file_plain(db, path), -1);
50     YAZ_CHECK_EQ(passwd_db_file_crypt(db, path), -1);
51     yaz_snprintf(path, sizeof(path), "%s/tstpass.txt", get_srcdir());
52 #if HAVE_CRYPT_H
53     YAZ_CHECK_EQ(passwd_db_file_crypt(db, path), 0);
54     YAZ_CHECK_EQ(passwd_db_auth(db, "other", "x1234"), -1);
55     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "abcd"), -2);
56     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitbat"), 0);
57     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitbatx"), -2);
58     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitba"), -2);
59 #else
60     YAZ_CHECK_EQ(passwd_db_file_plain(db, "passtest.txt"), -1);
61 #endif
62     passwd_db_close(db);
63 }
64
65 int main (int argc, char **argv)
66 {
67     YAZ_CHECK_INIT(argc, argv);
68     YAZ_CHECK_LOG();
69     tst();
70     YAZ_CHECK_TERM;
71 }
72 /*
73  * Local variables:
74  * c-basic-offset: 4
75  * c-file-style: "Stroustrup"
76  * indent-tabs-mode: nil
77  * End:
78  * vim: shiftwidth=4 tabstop=8 expandtab
79  */
80