Branch data Line data Source code
1 : : /* passwdpromptcb.c --- Self test the password prompt callback stuff.
2 : : * Copyright (C) 2008 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
6 : : * Shishi 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.
10 : : *
11 : : * Shishi 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.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #include "utils.c"
24 : :
25 : : int cb_ret = 0;
26 : :
27 : : static int
28 : 2 : prompt_password (Shishi * handle, char **s, const char *format, va_list ap)
29 : : {
30 [ + + ]: 2 : if (cb_ret == 0)
31 : 1 : *s = strdup ("pencil");
32 : 2 : return cb_ret;
33 : : }
34 : :
35 : :
36 : : void
37 : 1 : test (Shishi * handle)
38 : : {
39 : : shishi_prompt_password_func cb;
40 : : char *passwd, *save;
41 : : int ret;
42 : :
43 : 1 : cb = shishi_prompt_password_callback_get (handle);
44 [ - + ]: 1 : if (cb)
45 : 0 : fail ("callback not null: %p\n", cb);
46 : : else
47 : 1 : success ("callback is null.\n");
48 : :
49 : 1 : shishi_prompt_password_callback_set (handle, prompt_password);
50 : :
51 : 1 : cb = shishi_prompt_password_callback_get (handle);
52 [ - + ]: 1 : if (cb != prompt_password)
53 : 0 : fail ("callback not equal: %p != %p\n", cb, prompt_password);
54 : : else
55 : 1 : success ("callback equal to our function.\n");
56 : :
57 : 1 : cb_ret = SHISHI_CRYPTO_ERROR;
58 : 1 : save = passwd = strdup ("foo");
59 : 1 : ret = shishi_prompt_password (handle, &passwd, "Enter %s: ", "password");
60 [ - + ]: 1 : if (ret != cb_ret)
61 : 0 : fail ("callback return mismatch: %d != %d\n", ret, cb_ret);
62 : : else
63 : 1 : success ("invoke callback successfully with non-zero return\n");
64 : :
65 [ - + ]: 1 : if (passwd != save)
66 : 0 : fail ("callback messed with password: %s != %s\n", passwd, save);
67 : :
68 : 1 : free (save);
69 : :
70 : 1 : cb_ret = 0;
71 : 1 : ret = shishi_prompt_password (handle, &passwd, "Enter %s: ", "password");
72 [ - + ]: 1 : if (ret != cb_ret)
73 : 0 : fail ("callback return mismatch: %d != %d\n", ret, cb_ret);
74 : : else
75 : 1 : success ("invoke callback successfully with zero return code\n");
76 : :
77 [ - + ]: 1 : if (strcmp (passwd, "pencil") != 0)
78 : 0 : fail ("callback returned bad password: %s\n", passwd);
79 : : else
80 : 1 : success ("callback returned correct password: %s\n", passwd);
81 : :
82 : 1 : free (passwd);
83 : 1 : }
|