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