File 7731-kernel-test-Fixed-the-fdconnect-test-case.patch of Package erlang

From 9d0ae5933301a337c0f3875b25684d9bf61ed095 Mon Sep 17 00:00:00 2001
From: Micael Karlberg <bmk@erlang.org>
Date: Thu, 13 Oct 2022 18:52:57 +0200
Subject: [PATCH] [kernel|test] Fixed the fdconnect test case

Creating a socket (via gen_tcp:connect) using an already
existing file descriptor in principle requires descriptor to be
bound (its assumed to be, but the inet-driver attempt to verify).
The inet-driver does some checks (calls sockname) to ensure that
this is the case (and that its actually a 'socket' descriptor).
The problem is that for unix this function works even if the
descriptor is *not* bound. *But* this is not the case for Windows
(requires it to be bound).
The test case only created the descriptor but did not bind it,
so the test case worked on unix but not on windows.
The has now been fixed by providing the ifaddr-option to the
connect function causing an explicit bind.
---
 lib/kernel/test/gen_tcp_api_SUITE.erl | 26 +++++++++++++++++++++-----
 lib/kernel/test/kernel_test_lib.erl   |  9 +++++++++
 lib/kernel/test/kernel_test_lib.hrl   |  4 ++++
 3 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/lib/kernel/test/gen_tcp_api_SUITE.erl b/lib/kernel/test/gen_tcp_api_SUITE.erl
index cd2da3f1c9..593a784bcc 100644
--- a/lib/kernel/test/gen_tcp_api_SUITE.erl
+++ b/lib/kernel/test/gen_tcp_api_SUITE.erl
@@ -588,9 +588,23 @@ t_fdopen(Config) when is_list(Config) ->
 
 
 t_fdconnect(Config) when is_list(Config) ->
-    ?TC_TRY(t_fdconnect, fun() -> do_t_fdconnect(Config) end).
+    Cond = fun() ->
+                   ?P("Try verify if IPv4 is supported"),
+                   ?HAS_SUPPORT_IPV4()
+           end,
+    Pre  = fun() ->
+                   {ok, Addr} = ?WHICH_LOCAL_ADDR(inet),
+                   ?P("Use (local) address: ~p", [Addr]),
+                   #{local_addr => Addr}
+           end,
+    Case = fun(#{local_addr := Addr}) ->
+                   do_t_fdconnect(Addr, Config)
+           end,
+    Post = fun(_) -> ok end,
+    ?TC_TRY(?FUNCTION_NAME,
+            Cond, Pre, Case, Post).
 
-do_t_fdconnect(Config) ->
+do_t_fdconnect(Addr, Config) ->
     Question  = "Aaaa... Long time ago in a small town in Germany,",
     Question1 = list_to_binary(Question),
     Question2 = [<<"Aaaa">>, "... ", $L, <<>>, $o, "ng time ago ",
@@ -613,7 +627,7 @@ do_t_fdconnect(Config) ->
             ?SKIPT("failed loading util nif lib")
     end,
     ?P("try create listen socket"),
-    LOpts = ?INET_BACKEND_OPTS(Config) ++ [{active, false}],
+    LOpts = ?INET_BACKEND_OPTS(Config) ++ [{ifaddr, Addr}, {active, false}],
     L = try gen_tcp:listen(0, LOpts) of
             {ok, LSock} ->
                 LSock;
@@ -637,9 +651,11 @@ do_t_fdconnect(Config) ->
     ?P("try create file descriptor"),
     FD = gen_tcp_api_SUITE:getsockfd(),
     ?P("try connect (to port ~w) using file descriptor ~w", [LPort, FD]),
-    COpts = ?INET_BACKEND_OPTS(Config) ++ [{fd, FD}, {active, false}],
+    COpts = ?INET_BACKEND_OPTS(Config) ++ [{fd,     FD},
+                                           {ifaddr, Addr},
+                                           {active, false}],
     %% The debug is just to "see" that it (debug) "works"...
-    Client = try gen_tcp:connect(localhost, LPort, COpts ++ [{debug, true}]) of
+    Client = try gen_tcp:connect(Addr, LPort, COpts ++ [{debug, true}]) of
                  {ok, CSock} ->
                      ok = inet:setopts(CSock, [{debug, false}]),
                      CSock;
diff --git a/lib/kernel/test/kernel_test_lib.erl b/lib/kernel/test/kernel_test_lib.erl
index b68c93a935..c6ffee88b6 100644
--- a/lib/kernel/test/kernel_test_lib.erl
+++ b/lib/kernel/test/kernel_test_lib.erl
@@ -44,6 +44,7 @@
          proxy_call/3,
 
          %% Generic 'has support' test function(s)
+         has_support_ipv4/0,
          has_support_ipv6/0,
 
          which_local_host_info/1, which_local_host_info/2,
@@ -2175,6 +2176,14 @@ proxy_call(F, Timeout, Default)
 
 
 %% This is an extremely simple check...
+has_support_ipv4() ->
+    case which_local_addr(inet) of
+        {ok, _Addr} ->
+            ok;
+        {error, _R1} ->
+            skip("IPv4 Not Supported")
+    end.
+
 has_support_ipv6() ->
     case which_local_addr(inet6) of
         {ok, _Addr} ->
diff --git a/lib/kernel/test/kernel_test_lib.hrl b/lib/kernel/test/kernel_test_lib.hrl
index 41fc2a2cfa..2ab3facac3 100644
--- a/lib/kernel/test/kernel_test_lib.hrl
+++ b/lib/kernel/test/kernel_test_lib.hrl
@@ -56,6 +56,10 @@
 -define(WHICH_INET_BACKEND(C),   ?LIB:which_inet_backend(C)).
 -define(IS_SOCKET_BACKEND(C),    ?LIB:is_socket_backend(C)).
 
+-define(HAS_SUPPORT_IPV4(),      ?LIB:has_support_ipv4()).
+-define(HAS_SUPPORT_IPV6(),      ?LIB:has_support_ipv6()).
+-define(WHICH_LOCAL_ADDR(__D__), ?LIB:which_local_addr(__D__)).
+
 -define(START_NODE(__N__, __A__),
         ?LIB:start_node(__N__, __A__)).
 -define(START_NODE(__N__, __A__, __O__),
-- 
2.35.3

openSUSE Build Service is sponsored by