+ if (!TEST_ptr(q))
+ goto end;
+ /* r = non-secure 20, p = secure 20, q = non-secure 20, s = non-secure 20 */
+ if (!TEST_false(CRYPTO_secure_allocated(q)))
+ goto end;
+ OPENSSL_secure_clear_free(s, 20);
+ s = OPENSSL_secure_malloc(20);
+ if (!TEST_ptr(s)
+ /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
+ || !TEST_true(CRYPTO_secure_allocated(s))
+ /* 2 * 20 secure -> 64 bytes allocated */
+ || !TEST_size_t_eq(CRYPTO_secure_used(), 64))
+ goto end;
+ OPENSSL_secure_clear_free(p, 20);
+ p = NULL;
+ /* 20 secure -> 32 bytes allocated */
+ if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
+ goto end;
+ OPENSSL_free(q);
+ q = NULL;
+ /* should not complete, as secure memory is still allocated */
+ if (!TEST_false(CRYPTO_secure_malloc_done())
+ || !TEST_true(CRYPTO_secure_malloc_initialized()))
+ goto end;
+ OPENSSL_secure_free(s);
+ s = NULL;
+ /* secure memory should now be 0, so done should complete */
+ if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
+ || !TEST_true(CRYPTO_secure_malloc_done())
+ || !TEST_false(CRYPTO_secure_malloc_initialized()))
+ goto end;
+
+ TEST_info("Possible infinite loop: allocate more than available");
+ if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16)))
+ goto end;
+ TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1));
+ TEST_true(CRYPTO_secure_malloc_done());
+
+ /*
+ * If init fails, then initialized should be false, if not, this
+ * could cause an infinite loop secure_malloc, but we don't test it
+ */
+ if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) &&
+ !TEST_false(CRYPTO_secure_malloc_initialized())) {
+ TEST_true(CRYPTO_secure_malloc_done());
+ goto end;