uitest.c revision 1.1 1 1.1 christos /*
2 1.1 christos * Copyright 2002-2017 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 christos *
4 1.1 christos * Licensed under the OpenSSL license (the "License"). You may not use
5 1.1 christos * this file except in compliance with the License. You can obtain a copy
6 1.1 christos * in the file LICENSE in the source distribution or at
7 1.1 christos * https://www.openssl.org/source/license.html
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include <stdio.h>
11 1.1 christos #include <string.h>
12 1.1 christos #include <openssl/opensslconf.h>
13 1.1 christos #include <openssl/err.h>
14 1.1 christos #include "apps.h"
15 1.1 christos #include "testutil.h"
16 1.1 christos
17 1.1 christos /* apps/apps.c depend on these */
18 1.1 christos char *default_config_file = NULL;
19 1.1 christos
20 1.1 christos #include <openssl/ui.h>
21 1.1 christos
22 1.1 christos /* Old style PEM password callback */
23 1.1 christos static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
24 1.1 christos {
25 1.1 christos OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
26 1.1 christos return strlen(buf);
27 1.1 christos }
28 1.1 christos
29 1.1 christos /*
30 1.1 christos * Test wrapping old style PEM password callback in a UI method through the
31 1.1 christos * use of UI utility functions
32 1.1 christos */
33 1.1 christos static int test_old(void)
34 1.1 christos {
35 1.1 christos UI_METHOD *ui_method = NULL;
36 1.1 christos UI *ui = NULL;
37 1.1 christos char defpass[] = "password";
38 1.1 christos char pass[16];
39 1.1 christos int ok = 0;
40 1.1 christos
41 1.1 christos if (!TEST_ptr(ui_method =
42 1.1 christos UI_UTIL_wrap_read_pem_callback( test_pem_password_cb, 0))
43 1.1 christos || !TEST_ptr(ui = UI_new_method(ui_method)))
44 1.1 christos goto err;
45 1.1 christos
46 1.1 christos /* The wrapper passes the UI userdata as the callback userdata param */
47 1.1 christos UI_add_user_data(ui, defpass);
48 1.1 christos
49 1.1 christos if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
50 1.1 christos pass, 0, sizeof(pass) - 1))
51 1.1 christos goto err;
52 1.1 christos
53 1.1 christos switch (UI_process(ui)) {
54 1.1 christos case -2:
55 1.1 christos TEST_info("test_old: UI process interrupted or cancelled");
56 1.1 christos /* fall through */
57 1.1 christos case -1:
58 1.1 christos goto err;
59 1.1 christos default:
60 1.1 christos break;
61 1.1 christos }
62 1.1 christos
63 1.1 christos if (TEST_str_eq(pass, defpass))
64 1.1 christos ok = 1;
65 1.1 christos
66 1.1 christos err:
67 1.1 christos UI_free(ui);
68 1.1 christos UI_destroy_method(ui_method);
69 1.1 christos
70 1.1 christos return ok;
71 1.1 christos }
72 1.1 christos
73 1.1 christos /* Test of UI. This uses the UI method defined in apps/apps.c */
74 1.1 christos static int test_new_ui(void)
75 1.1 christos {
76 1.1 christos PW_CB_DATA cb_data = {
77 1.1 christos "password",
78 1.1 christos "prompt"
79 1.1 christos };
80 1.1 christos char pass[16];
81 1.1 christos int ok = 0;
82 1.1 christos
83 1.1 christos setup_ui_method();
84 1.1 christos if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0)
85 1.1 christos && TEST_str_eq(pass, cb_data.password))
86 1.1 christos ok = 1;
87 1.1 christos destroy_ui_method();
88 1.1 christos return ok;
89 1.1 christos }
90 1.1 christos
91 1.1 christos int setup_tests(void)
92 1.1 christos {
93 1.1 christos ADD_TEST(test_old);
94 1.1 christos ADD_TEST(test_new_ui);
95 1.1 christos return 1;
96 1.1 christos }
97