Happy new year.
[idzebra-moved-to-github.git] / util / tstpass.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 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 #include <passwddb.h>
21 #include <yaz/test.h>
22 #include <yaz/snprintf.h>
23 #include <stdlib.h>
24
25 /* use env srcdir as base directory - or current directory if unset */
26 const char *get_srcdir(void)
27 {
28     const char *srcdir = getenv("srcdir");
29     if (!srcdir || ! *srcdir)
30         srcdir=".";
31     return srcdir;
32
33 }
34
35 static void tst(void)
36 {
37     char path[1024];
38     Passwd_db db;
39     
40     db = passwd_db_open();
41     YAZ_CHECK(db);
42     if (!db)
43         return;
44
45     yaz_snprintf(path, sizeof(path), "%s/no_such_file.txt", get_srcdir());
46     YAZ_CHECK_EQ(passwd_db_file_plain(db, path), -1);
47     YAZ_CHECK_EQ(passwd_db_file_crypt(db, path), -1);
48     yaz_snprintf(path, sizeof(path), "%s/tstpass.txt", get_srcdir());
49 #if HAVE_CRYPT_H
50     YAZ_CHECK_EQ(passwd_db_file_crypt(db, path), 0);
51     YAZ_CHECK_EQ(passwd_db_auth(db, "other", "x1234"), -1);
52     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "abcd"), -2);
53     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitbat"), 0);
54     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitbatx"), -2);
55     YAZ_CHECK_EQ(passwd_db_auth(db, "admin", "fruitba"), -2);
56 #else
57     YAZ_CHECK_EQ(passwd_db_file_plain(db, "passtest.txt"), -1);
58 #endif
59     passwd_db_close(db);
60 }
61
62 int main (int argc, char **argv)
63 {
64     YAZ_CHECK_INIT(argc, argv);
65     YAZ_CHECK_LOG();
66     tst();
67     YAZ_CHECK_TERM;
68 }
69 /*
70  * Local variables:
71  * c-basic-offset: 4
72  * c-file-style: "Stroustrup"
73  * indent-tabs-mode: nil
74  * End:
75  * vim: shiftwidth=4 tabstop=8 expandtab
76  */
77