Converted the bio_enc tests to use new test framework.
[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
15 /*
16  * The VMS build does stdio via a socketpair.
17  */
18 #ifdef __VMS
19 # include "../apps/vms_term_sock.h"
20 #endif
21
22 #include "../apps/apps.h"
23
24 #include "testutil.h"
25 #include "test_main_custom.h"
26
27 /* apps/apps.c depend on these */
28 char *default_config_file = NULL;
29 BIO *bio_err = NULL;
30
31 #ifndef OPENSSL_NO_UI
32 # include <openssl/ui.h>
33
34 /* Old style PEM password callback */
35 static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
36 {
37     OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
38     return 1;
39 }
40
41 /*
42  * Test wrapping old style PEM password callback in a UI method through the
43  * use of UI utility functions
44  */
45 static int test_old()
46 {
47     UI_METHOD *ui_method = NULL;
48     UI *ui = NULL;
49     char defpass[] = "password";
50     char pass[16];
51     int ok = 0;
52
53     if (!TEST_ptr(ui_method =
54                   UI_UTIL_wrap_read_pem_callback( test_pem_password_cb, 0))
55             || !TEST_ptr(ui = UI_new_method(ui_method)))
56         goto err;
57
58     /* The wrapper passes the UI userdata as the callback userdata param */
59     UI_add_user_data(ui, defpass);
60
61     if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
62                              pass, 0, sizeof(pass) - 1))
63         goto err;
64
65     switch (UI_process(ui)) {
66     case -2:
67         TEST_info("test_old: UI process interrupted or cancelled");
68         /* fall through */
69     case -1:
70         goto err;
71     default:
72         break;
73     }
74
75     if (TEST_str_eq(pass, defpass))
76         ok = 1;
77
78  err:
79     UI_free(ui);
80     UI_destroy_method(ui_method);
81
82     return ok;
83 }
84
85 /* Test of UI.  This uses the UI method defined in apps/apps.c */
86 static int test_new_ui()
87 {
88     PW_CB_DATA cb_data = {
89         "password",
90         "prompt"
91     };
92     char pass[16];
93     int ok = 0;
94
95     setup_ui_method();
96     if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0)
97             && TEST_str_eq(pass, cb_data.password))
98         ok = 1;
99     destroy_ui_method();
100     return ok;
101 }
102
103 #endif
104
105 int test_main(int argc, char *argv[])
106 {
107     int ret;
108
109     bio_err = dup_bio_err(FORMAT_TEXT);
110
111 #ifndef OPENSSL_NO_UI
112     ADD_TEST(test_old);
113     ADD_TEST(test_new_ui);
114 #endif
115
116     ret = run_tests(argv[0]);
117
118     (void)BIO_flush(bio_err);
119     BIO_free(bio_err);
120
121     return ret;
122 }