| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-05-03 18:52:38 +08:00
										 |  |  |  * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Internal tests for the OpenSSL library context */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "internal/cryptlib.h"
 | 
					
						
							|  |  |  | #include "testutil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  | static int test_set0_default(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_LIB_CTX *global = OSSL_LIB_CTX_get0_global_default(); | 
					
						
							|  |  |  |     OSSL_LIB_CTX *local = OSSL_LIB_CTX_new(); | 
					
						
							|  |  |  |     OSSL_LIB_CTX *prev; | 
					
						
							|  |  |  |     int testresult = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(global) | 
					
						
							|  |  |  |             || !TEST_ptr(local) | 
					
						
							| 
									
										
										
										
											2022-03-14 16:13:12 +08:00
										 |  |  |             || !TEST_ptr_eq(global, OSSL_LIB_CTX_set0_default(NULL))) | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check we can change the local default context */ | 
					
						
							|  |  |  |     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(local)) | 
					
						
							| 
									
										
										
										
											2022-03-14 16:13:12 +08:00
										 |  |  |             || !TEST_ptr_eq(global, prev)) | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Calling OSSL_LIB_CTX_set0_default() with a NULL should be a no-op */ | 
					
						
							| 
									
										
										
										
											2022-03-14 16:13:12 +08:00
										 |  |  |     if (!TEST_ptr_eq(local, OSSL_LIB_CTX_set0_default(NULL))) | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Global default should be unchanged */ | 
					
						
							|  |  |  |     if (!TEST_ptr_eq(global, OSSL_LIB_CTX_get0_global_default())) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Check we can swap back to the global default */ | 
					
						
							| 
									
										
										
										
											2022-03-14 16:13:12 +08:00
										 |  |  |     if (!TEST_ptr(prev = OSSL_LIB_CTX_set0_default(global)) | 
					
						
							|  |  |  |             || !TEST_ptr_eq(local, prev)) | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     testresult = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     OSSL_LIB_CTX_free(local); | 
					
						
							|  |  |  |     return testresult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 21:58:11 +08:00
										 |  |  | static int test_set_get_conf_diagnostics(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new(); | 
					
						
							|  |  |  |     int res = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(ctx)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(OSSL_LIB_CTX_get_conf_diagnostics(ctx))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OSSL_LIB_CTX_set_conf_diagnostics(ctx, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_false(OSSL_LIB_CTX_get_conf_diagnostics(ctx))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     OSSL_LIB_CTX_free(ctx); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-16 19:21:50 +08:00
										 |  |  |     ADD_TEST(test_set0_default); | 
					
						
							| 
									
										
										
										
											2024-05-02 21:58:11 +08:00
										 |  |  |     ADD_TEST(test_set_get_conf_diagnostics); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:42:50 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } |