File 2322-crypto-Fix-ensure_engine_unloaded.patch of Package erlang
From bf0d3a9e7a7d073a8905bc4fede69427b3ea5f2c Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Fri, 13 Aug 2021 14:45:54 +0200
Subject: [PATCH 2/2] crypto: Fix ensure_engine_unloaded
to work even if engine has not been added.
---
 lib/crypto/src/crypto.erl        | 18 ++++++++++++-----
 lib/crypto/test/engine_SUITE.erl | 33 +++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 8 deletions(-)
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index a30b6b095c..6a64774f71 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -1998,13 +1998,21 @@ ensure_engine_unloaded(Engine) ->
                                                 EngineMethods :: [engine_method_type()],
                                                 Result :: ok | {error, Reason::term()}.
 ensure_engine_unloaded(Engine, EngineMethods) ->
-    case engine_remove(Engine) of
-        ok ->
-            engine_unload(Engine, EngineMethods);
-        {error, E} ->
-            {error, E}
+    List = crypto:engine_list(),
+    EngineId = crypto:engine_get_id(Engine),
+    case lists:member(EngineId, List) of
+        true ->
+            case engine_remove(Engine) of
+                ok ->
+                    engine_unload(Engine, EngineMethods);
+                {error, Error} ->
+                    {error, Error}
+            end;
+        false ->
+            engine_unload(Engine, EngineMethods)
     end.
 
+
 %%--------------------------------------------------------------------
 %%% On load
 %%--------------------------------------------------------------------
diff --git a/lib/crypto/test/engine_SUITE.erl b/lib/crypto/test/engine_SUITE.erl
index de902ca3f0..e4bba9c3a8 100644
--- a/lib/crypto/test/engine_SUITE.erl
+++ b/lib/crypto/test/engine_SUITE.erl
@@ -715,8 +715,8 @@ ensure_load(Config) when is_list(Config) ->
                 Md5Hash1 = crypto:hash(md5, "Don't panic"),
                 Md5Hash2 =  <<0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15>>,
                 case crypto:ensure_engine_loaded(<<"MD5">>, Engine) of
-                    {ok, E} ->
-                        {ok, _E1} = crypto:ensure_engine_loaded(<<"MD5">>, Engine),
+                    {ok, E1} ->
+                        {ok, E2} = crypto:ensure_engine_loaded(<<"MD5">>, Engine),
                         case crypto:hash(md5, "Don't panic") of
                             Md5Hash1 ->
                                 ct:fail(fail_to_load_still_original_engine);
@@ -725,11 +725,37 @@ ensure_load(Config) when is_list(Config) ->
                             _ ->
                                 ct:fail(fail_to_load_engine)
                         end,
-                        ok = crypto:ensure_engine_unloaded(E),
+
+                        {ok, E3} = crypto:engine_by_id(<<"MD5">>),
+
+                        ok = crypto:ensure_engine_unloaded(E3),
+                        case crypto:hash(md5, "Don't panic") of
+                            Md5Hash1 ->
+                                ok;
+                            Md5Hash2 ->
+                                ct:fail(fail_to_unload_still_test_engine);
+                            _ ->
+                                ct:fail(load_engine)
+                        end,
+
+                        %% ToDo: Why doesn't this work?
+                        %% {ok, E4} = crypto:ensure_engine_loaded(<<"MD5">>, Engine),
+                        %% case crypto:hash(md5, "Don't panic") of
+                        %%     Md5Hash1 ->
+                        %%         ct:fail(fail_to_load_still_original_engine);
+                        %%     Md5Hash2 ->
+                        %%         ok;
+                        %%     _ ->
+                        %%         ct:fail(fail_to_load_engine)
+                        %% end,
+
+                        ok = crypto:ensure_engine_unloaded(E1),
                         case crypto:hash(md5, "Don't panic") of
                             Md5Hash2 ->
                                 ct:fail(fail_to_unload_still_test_engine);
                             Md5Hash1 ->
+                                ok = crypto:ensure_engine_unloaded(E2),
+                                %% ok = crypto:ensure_engine_unloaded(E4);
                                 ok;
                             _ ->
                                 ct:fail(fail_to_unload_engine)
@@ -743,6 +769,7 @@ ensure_load(Config) when is_list(Config) ->
            end
     end.
 
+
 %%%----------------------------------------------------------------
 %%% Pub/priv key storage tests.  Those are for testing the crypto.erl
 %%% support for using priv/pub keys stored in an engine.
-- 
2.31.1