16d83b9528d6664c53a3592f6e7023146ac88681
[openssl.git] / test / uitest.c
1 /*
2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <openssl/opensslconf.h>
13 #include <openssl/err.h>
14 #include "../apps/apps.h"
15
16 #include "testutil.h"
17 #include "test_main_custom.h"
18
19 /* apps/apps.c depend on these */
20 char *default_config_file = NULL;
21 BIO *bio_err = NULL;
22
23 #ifndef OPENSSL_NO_UI
24 # include <openssl/ui.h>
25
26 /* Old style PEM password callback */
27 static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
28 {
29     OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
30     return 1;
31 }
32
33 /*
34  * Test wrapping old style PEM password callback in a UI method through the
35  * use of UI utility functions
36  */
37 static int test_old()
38 {
39     UI_METHOD *ui_method = NULL;
40     UI *ui = NULL;
41     char defpass[] = "password";
42     char pass[16];
43     int ok = 0;
44
45     if ((ui_method =
46          UI_UTIL_wrap_read_pem_callback(test_pem_password_cb, 0)) == NULL
47         || (ui = UI_new_method(ui_method)) == NULL)
48         goto err;
49
50     /* The wrapper passes the UI userdata as the callback userdata param */
51     UI_add_user_data(ui, defpass);
52
53     if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
54                              pass, 0, sizeof(pass) - 1))
55         goto err;
56
57     switch (UI_process(ui)) {
58     case -2:
59         BIO_printf(bio_err, "test_old: UI process interrupted or cancelled\n");
60         /* fall through */
61     case -1:
62         goto err;
63     default:
64         break;
65     }
66
67     if (strcmp(pass, defpass) == 0)
68         ok = 1;
69     else
70         BIO_printf(bio_err, "test_old: password failure\n");
71
72  err:
73     if (!ok)
74         ERR_print_errors_fp(stderr);
75     UI_free(ui);
76     UI_destroy_method(ui_method);
77
78     return ok;
79 }
80
81 /* Test of UI.  This uses the UI method defined in apps/apps.c */
82 static int test_new_ui()
83 {
84     PW_CB_DATA cb_data = {
85         "password",
86         "prompt"
87     };
88     char pass[16];
89     int ok = 0;
90
91     setup_ui_method();
92     if (password_callback(pass, sizeof(pass), 0, &cb_data) > 0
93         && strcmp(pass, cb_data.password) == 0)
94         ok = 1;
95     else
96         BIO_printf(bio_err, "test_new: password failure\n");
97
98     if (!ok)
99         ERR_print_errors_fp(stderr);
100
101     destroy_ui_method();
102     return ok;
103 }
104
105 #endif
106
107 int test_main(int argc, char *argv[])
108 {
109     int ret;
110
111     bio_err = dup_bio_err(FORMAT_TEXT);
112
113 #ifndef OPENSSL_NO_UI
114     ADD_TEST(test_old);
115     ADD_TEST(test_new_ui);
116 #endif
117
118     ret = run_tests(argv[0]);
119
120     (void)BIO_flush(bio_err);
121     BIO_free(bio_err);
122
123     return ret;
124 }