File: passwdpromptcb.c

package info (click to toggle)
shishi 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 22,144 kB
  • ctags: 7,865
  • sloc: ansic: 61,153; sh: 12,212; yacc: 1,219; makefile: 1,164; perl: 624; sed: 16
file content (83 lines) | stat: -rw-r--r-- 2,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* passwdpromptcb.c --- Self test the password prompt callback stuff.
 * Copyright (C) 2008  Simon Josefsson
 *
 * This file is part of Shishi.
 *
 * Shishi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Shishi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Shishi; if not, see http://www.gnu.org/licenses or write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA
 *
 */

#include "utils.c"

int cb_ret = 0;

static int
prompt_password (Shishi * handle, char **s, const char *format, va_list ap)
{
  if (cb_ret == 0)
    *s = strdup ("pencil");
  return cb_ret;
}


void
test (Shishi * handle)
{
  shishi_prompt_password_func cb;
  char *passwd, *save;
  int ret;

  cb = shishi_prompt_password_callback_get (handle);
  if (cb)
    fail ("callback not null: %p\n", cb);
  else
    success ("callback is null.\n");

  shishi_prompt_password_callback_set (handle, prompt_password);

  cb = shishi_prompt_password_callback_get (handle);
  if (cb != prompt_password)
    fail ("callback not equal: %p != %p\n", cb, prompt_password);
  else
    success ("callback equal to our function.\n");

  cb_ret = SHISHI_CRYPTO_ERROR;
  save = passwd = strdup ("foo");
  ret = shishi_prompt_password (handle, &passwd, "Enter %s: ", "password");
  if (ret != cb_ret)
    fail ("callback return mismatch: %d != %d\n", ret, cb_ret);
  else
    success ("invoke callback successfully with non-zero return\n");

  if (passwd != save)
    fail ("callback messed with password: %s != %s\n", passwd, save);

  free (save);

  cb_ret = 0;
  ret = shishi_prompt_password (handle, &passwd, "Enter %s: ", "password");
  if (ret != cb_ret)
    fail ("callback return mismatch: %d != %d\n", ret, cb_ret);
  else
    success ("invoke callback successfully with zero return code\n");

  if (strcmp (passwd, "pencil") != 0)
    fail ("callback returned bad password: %s\n", passwd);
  else
    success ("callback returned correct password: %s\n", passwd);

  free (passwd);
}