context_internal_test.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2019-2022 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. static int test_set0_default(void)
  13. {
  14. OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
  15. OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
  16. OSSL_LIB_CTX *prev;
  17. int testresult = 0;
  18. if (!TEST_ptr(global)
  19. || !TEST_ptr(local)
  20. || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL)))
  21. goto err;
  22. /* Check we can change the local default context */
  23. if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
  24. || !TEST_ptr_eq(global, prev))
  25. goto err;
  26. /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
  27. if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL)))
  28. goto err;
  29. /* Global default should be unchanged */
  30. if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
  31. goto err;
  32. /* Check we can swap back to the global default */
  33. if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
  34. || !TEST_ptr_eq(local, prev))
  35. goto err;
  36. testresult = 1;
  37. err:
  38. OSSL_LIB_CTX_free(local);
  39. return testresult;
  40. }
  41. int setup_tests(void)
  42. {
  43. ADD_TEST(test_set0_default);
  44. return 1;
  45. }