Home | History | Annotate | Line # | Download | only in test
      1 /*
      2  * Copyright 2019-2024 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 
     10 /* Internal tests for the OpenSSL library context */
     11 
     12 #include "internal/cryptlib.h"
     13 #include "testutil.h"
     14 
     15 static int test_set0_default(void)
     16 {
     17     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default();
     18     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new();
     19     OSSL_LIB_CTX *prev;
     20     int testresult = 0;
     21 
     22     if (!TEST_ptr(global)
     23         || !TEST_ptr(local)
     24         || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL)))
     25         goto err;
     26 
     27     /* Check we can change the local default context */
     28     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local))
     29         || !TEST_ptr_eq(global, prev))
     30         goto err;
     31 
     32     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */
     33     if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL)))
     34         goto err;
     35 
     36     /* Global default should be unchanged */
     37     if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default()))
     38         goto err;
     39 
     40     /* Check we can swap back to the global default */
     41     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global))
     42         || !TEST_ptr_eq(local, prev))
     43         goto err;
     44 
     45     testresult = 1;
     46 err:
     47     OSSL_LIB_CTX_free(local);
     48     return testresult;
     49 }
     50 
     51 static int test_set_get_conf_diagnostics(void)
     52 {
     53     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
     54     int res = 0;
     55 
     56     if (!TEST_ptr(ctx))
     57         goto err;
     58 
     59     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     60         goto err;
     61 
     62     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 1);
     63 
     64     if (!TEST_true(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     65         goto err;
     66 
     67     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 0);
     68 
     69     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx)))
     70         goto err;
     71 
     72     res = 1;
     73 err:
     74     OSSL_LIB_CTX_free(ctx);
     75     return res;
     76 }
     77 
     78 int setup_tests(void)
     79 {
     80     ADD_TEST(test_set0_default);
     81     ADD_TEST(test_set_get_conf_diagnostics);
     82     return 1;
     83 }
     84