Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /* Internal tests for the OpenSSL library context */
     11      1.1  christos 
     12      1.1  christos #include "internal/cryptlib.h"
     13      1.1  christos #include "testutil.h"
     14      1.1  christos 
     15      1.1  christos static int test_set0_default(void)
     16      1.1  christos {
     17      1.1  christos     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
     18      1.1  christos     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
     19      1.1  christos     OSSL_LIB_CTX *prev;
     20      1.1  christos     int testresult = 0;
     21      1.1  christos 
     22      1.1  christos     if (!TEST_ptr(global)
     23  1.1.1.2  christos         || !TEST_ptr(local)
     24  1.1.1.2  christos         || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL)))
     25      1.1  christos         goto err;
     26      1.1  christos 
     27      1.1  christos     /* Check we can change the local default context */
     28      1.1  christos     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
     29  1.1.1.2  christos         || !TEST_ptr_eq(global, prev))
     30      1.1  christos         goto err;
     31      1.1  christos 
     32      1.1  christos     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
     33      1.1  christos     if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL)))
     34      1.1  christos         goto err;
     35      1.1  christos 
     36      1.1  christos     /* Global default should be unchanged */
     37      1.1  christos     if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
     38      1.1  christos         goto err;
     39      1.1  christos 
     40      1.1  christos     /* Check we can swap back to the global default */
     41      1.1  christos     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
     42  1.1.1.2  christos         || !TEST_ptr_eq(local, prev))
     43      1.1  christos         goto err;
     44      1.1  christos 
     45      1.1  christos     testresult = 1;
     46  1.1.1.2  christos err:
     47      1.1  christos     OSSL_LIB_CTX_free(local);
     48      1.1  christos     return testresult;
     49      1.1  christos }
     50      1.1  christos 
     51      1.1  christos static int test_set_get_conf_diagnostics(void)
     52      1.1  christos {
     53      1.1  christos     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
     54      1.1  christos     int res = 0;
     55      1.1  christos 
     56      1.1  christos     if (!TEST_ptr(ctx))
     57      1.1  christos         goto err;
     58      1.1  christos 
     59      1.1  christos     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     60      1.1  christos         goto err;
     61      1.1  christos 
     62      1.1  christos     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 1);
     63      1.1  christos 
     64      1.1  christos     if (!TEST_true(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     65      1.1  christos         goto err;
     66      1.1  christos 
     67      1.1  christos     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 0);
     68      1.1  christos 
     69      1.1  christos     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     70      1.1  christos         goto err;
     71      1.1  christos 
     72      1.1  christos     res = 1;
     73  1.1.1.2  christos err:
     74      1.1  christos     OSSL_LIB_CTX_free(ctx);
     75      1.1  christos     return res;
     76      1.1  christos }
     77      1.1  christos 
     78      1.1  christos int setup_tests(void)
     79      1.1  christos {
     80      1.1  christos     ADD_TEST(test_set0_default);
     81      1.1  christos     ADD_TEST(test_set_get_conf_diagnostics);
     82      1.1  christos     return 1;
     83      1.1  christos }
     84