2
0

uitest.c 2.2 KB

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