context_internal_test.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2019-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. /* Internal tests for the OpenSSL library context */
  10. #include "internal/cryptlib.h"
  11. #include "testutil.h"
  12. /*
  13. * Everything between BEGIN EXAMPLE and END EXAMPLE is copied from
  14. * doc/internal/man3/ossl_lib_ctx_get_data.pod
  15. */
  16. /*
  17. * ======================================================================
  18. * BEGIN EXAMPLE
  19. */
  20. typedef struct foo_st {
  21. int i;
  22. void *data;
  23. } FOO;
  24. static void *foo_new(OSSL_LIB_CTX *ctx)
  25. {
  26. FOO *ptr = OPENSSL_zalloc(sizeof(*ptr));
  27. if (ptr != NULL)
  28. ptr->i = 42;
  29. return ptr;
  30. }
  31. static void foo_free(void *ptr)
  32. {
  33. OPENSSL_free(ptr);
  34. }
  35. static const OSSL_LIB_CTX_METHOD foo_method = {
  36. foo_new,
  37. foo_free
  38. };
  39. /*
  40. * END EXAMPLE
  41. * ======================================================================
  42. */
  43. static int test_context(OSSL_LIB_CTX *ctx)
  44. {
  45. FOO *data = NULL;
  46. return TEST_ptr(data = ossl_lib_ctx_get_data(ctx, 0, &foo_method))
  47. /* OPENSSL_zalloc in foo_new() initialized it to zero */
  48. && TEST_int_eq(data->i, 42);
  49. }
  50. static int test_app_context(void)
  51. {
  52. OSSL_LIB_CTX *ctx = NULL;
  53. int result =
  54. TEST_ptr(ctx = OSSL_LIB_CTX_new())
  55. && test_context(ctx);
  56. OSSL_LIB_CTX_free(ctx);
  57. return result;
  58. }
  59. static int test_def_context(void)
  60. {
  61. return test_context(NULL);
  62. }
  63. int setup_tests(void)
  64. {
  65. ADD_TEST(test_app_context);
  66. ADD_TEST(test_def_context);
  67. return 1;
  68. }