uitest.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2002-2017 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. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/err.h>
  13. #include "apps.h"
  14. #include "testutil.h"
  15. /* apps/apps.c depend on these */
  16. char *default_config_file = NULL;
  17. #include <openssl/ui.h>
  18. /* Old style PEM password callback */
  19. static int test_pem_password_cb(char *buf, int size, int rwflag, void *userdata)
  20. {
  21. OPENSSL_strlcpy(buf, (char *)userdata, (size_t)size);
  22. return strlen(buf);
  23. }
  24. /*
  25. * Test wrapping old style PEM password callback in a UI method through the
  26. * use of UI utility functions
  27. */
  28. static int test_old(void)
  29. {
  30. UI_METHOD *ui_method = NULL;
  31. UI *ui = NULL;
  32. char defpass[] = "password";
  33. char pass[16];
  34. int ok = 0;
  35. if (!TEST_ptr(ui_method =
  36. UI_UTIL_wrap_read_pem_callback( test_pem_password_cb, 0))
  37. || !TEST_ptr(ui = UI_new_method(ui_method)))
  38. goto err;
  39. /* The wrapper passes the UI userdata as the callback userdata param */
  40. UI_add_user_data(ui, defpass);
  41. if (!UI_add_input_string(ui, "prompt", UI_INPUT_FLAG_DEFAULT_PWD,
  42. pass, 0, sizeof(pass) - 1))
  43. goto err;
  44. switch (UI_process(ui)) {
  45. case -2:
  46. TEST_info("test_old: UI process interrupted or cancelled");
  47. /* fall through */
  48. case -1:
  49. goto err;
  50. default:
  51. break;
  52. }
  53. if (TEST_str_eq(pass, defpass))
  54. ok = 1;
  55. err:
  56. UI_free(ui);
  57. UI_destroy_method(ui_method);
  58. return ok;
  59. }
  60. /* Test of UI. This uses the UI method defined in apps/apps.c */
  61. static int test_new_ui(void)
  62. {
  63. PW_CB_DATA cb_data = {
  64. "password",
  65. "prompt"
  66. };
  67. char pass[16];
  68. int ok = 0;
  69. setup_ui_method();
  70. if (TEST_int_gt(password_callback(pass, sizeof(pass), 0, &cb_data), 0)
  71. && TEST_str_eq(pass, cb_data.password))
  72. ok = 1;
  73. destroy_ui_method();
  74. return ok;
  75. }
  76. int setup_tests(void)
  77. {
  78. ADD_TEST(test_old);
  79. ADD_TEST(test_new_ui);
  80. return 1;
  81. }