]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/uitest.c
Add a test "uitest"
[thirdparty/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/err.h>
13 #include <openssl/ui.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 /* Old style PEM password callback */
24 static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
25 {
26 OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
27 return 1;
28 }
29
30 /*
31 * Test wrapping old style PEM password callback in a UI method through the
32 * use of UI utility functions
33 */
34 static int test_old()
35 {
36 UI_METHOD *ui_method = NULL;
37 UI *ui = NULL;
38 char defpass[] = "password";
39 char pass[16];
40 int ok = 0;
41
42 if ((ui_method =
43 UI_UTIL_wrap_read_pem_callback(test_pem_password_cb, 0)) == NULL
44 || (ui = UI_new_method(ui_method)) == NULL)
45 goto err;
46
47 /* The wrapper passes the UI userdata as the callback userdata param */
48 UI_add_user_data(ui, defpass);
49
50 if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
51 pass, 0, sizeof(pass) - 1))
52 goto err;
53
54 switch (UI_process(ui)) {
55 case -2:
56 BIO_printf(bio_err, "test_old: UI process interrupted or cancelled\n");
57 /* fall through */
58 case -1:
59 goto err;
60 default:
61 break;
62 }
63
64 if (strcmp(pass, defpass) == 0)
65 ok = 1;
66 else
67 BIO_printf(bio_err, "test_old: password failure\n");
68
69 err:
70 if (!ok)
71 ERR_print_errors_fp(stderr);
72 UI_free(ui);
73 UI_destroy_method(ui_method);
74
75 return ok;
76 }
77
78 /* Test of UI. This uses the UI method defined in apps/apps.c */
79 static int test_new_ui()
80 {
81 PW_CB_DATA cb_data = {
82 "password",
83 "prompt"
84 };
85 char pass[16];
86 int ok = 0;
87
88 setup_ui_method();
89 if (password_callback(pass, sizeof(pass), 0, &cb_data) > 0
90 && strcmp(pass, cb_data.password) == 0)
91 ok = 1;
92 else
93 BIO_printf(bio_err, "test_new: password failure\n");
94
95 if (!ok)
96 ERR_print_errors_fp(stderr);
97
98 destroy_ui_method();
99 return ok;
100 }
101
102 int test_main(int argc, char *argv[])
103 {
104 int ret;
105
106 bio_err = dup_bio_err(FORMAT_TEXT);
107
108 ADD_TEST(test_old);
109 ADD_TEST(test_new_ui);
110
111 ret = run_tests(argv[0]);
112
113 (void)BIO_flush(bio_err);
114 BIO_free(bio_err);
115
116 return ret;
117 }