context_internal_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2019-2021 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. static int test_set0_default(void)
  64. {
  65. OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
  66. OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
  67. OSSL_LIB_CTX *prev;
  68. int testresult = 0;
  69. FOO *data = NULL;
  70. if (!TEST_ptr(global)
  71. || !TEST_ptr(local)
  72. || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))
  73. || !TEST_ptr(data = ossl_lib_ctx_get_data(local, 0, &foo_method)))
  74. goto err;
  75. /* Set local "i" value to 43. Global "i" should be 42 */
  76. data->i++;
  77. if (!TEST_int_eq(data->i, 43))
  78. goto err;
  79. /* The default context should still be the "global" default */
  80. if (!TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
  81. || !TEST_int_eq(data->i, 42))
  82. goto err;
  83. /* Check we can change the local default context */
  84. if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
  85. || !TEST_ptr_eq(global, prev)
  86. || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
  87. || !TEST_int_eq(data->i, 43))
  88. goto err;
  89. /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
  90. if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))
  91. || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
  92. || !TEST_int_eq(data->i, 43))
  93. goto err;
  94. /* Global default should be unchanged */
  95. if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
  96. goto err;
  97. /* Check we can swap back to the global default */
  98. if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
  99. || !TEST_ptr_eq(local, prev)
  100. || !TEST_ptr(data = ossl_lib_ctx_get_data(NULL, 0, &foo_method))
  101. || !TEST_int_eq(data->i, 42))
  102. goto err;
  103. testresult = 1;
  104. err:
  105. OSSL_LIB_CTX_free(local);
  106. return testresult;
  107. }
  108. int setup_tests(void)
  109. {
  110. ADD_TEST(test_app_context);
  111. ADD_TEST(test_def_context);
  112. ADD_TEST(test_set0_default);
  113. return 1;
  114. }