From: Marcos Henrich Date: Thu, 26 Nov 2015 15:32:51 +0000 (+0000) Subject: Updated SocketTest.UdpDoubleBind X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=91a6e35c9b5230894a5320d3eac9172e5066e683;p=mono.git Updated SocketTest.UdpDoubleBind --- diff --git a/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs b/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs index 14a4490fa4b..4bf3ec27d71 100755 --- a/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs +++ b/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs @@ -3483,21 +3483,31 @@ namespace MonoTests.System.Net.Sockets [Test] public void UdpDoubleBind () { - Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); - - var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()); - s.Bind (ep); - - Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); - - ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port)); + using (Socket s = new Socket (AddressFamily.InterNetwork, + SocketType.Dgram, ProtocolType.Udp)) + using (Socket ss = new Socket (AddressFamily.InterNetwork, + SocketType.Dgram, ProtocolType.Udp)) { + var supportsReuseAddress = true; + try { + s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + } catch (SocketException e) { + // Exception is thrown when ReuseAddress is not supported + supportsReuseAddress = false; + } - // If we make it this far, we succeeded. - - ss.Close (); - s.Close (); + var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()); + s.Bind (ep); + + if (supportsReuseAddress) + ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + + try { + ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port)); + if (!supportsReuseAddress) + Assert.Fail ("Reusing address is not supported, exception was expected on second bind."); + } catch (SocketException e) { + } + } } // Test case for bug #31557