Updated SocketTest.UdpDoubleBind
authorMarcos Henrich <marcos.henrich@xamarin.com>
Thu, 26 Nov 2015 15:32:51 +0000 (15:32 +0000)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Wed, 17 Feb 2016 09:56:15 +0000 (09:56 +0000)
mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

index 14a4490fa4bd4b71116477d8b57676193c167576..4bf3ec27d717fac423c8f19fac4d9225d0d142f2 100755 (executable)
@@ -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