improve parsing network files on WSL (#44680)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Sun, 15 Nov 2020 17:50:22 +0000 (09:50 -0800)
committerGitHub <noreply@github.com>
Sun, 15 Nov 2020 17:50:22 +0000 (12:50 -0500)
* improve parsing network files on WSL

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/StringParsingHelpers.Connections.cs
src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/ConnectionsParsingTests.cs
src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkFiles/empty [new file with mode: 0644]

index 5f2548e..f48a231 100644 (file)
@@ -42,8 +42,20 @@ namespace System.Net.NetworkInformation
             string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
             string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
 
+            // First line is header in each file. On WSL, this file may be empty.
+            int count = 0;
+            if (v4connections.Length > 0)
+            {
+                count += v4connections.Length - 1;
+            }
+
+            if (v6connections.Length > 0)
+            {
+                count += v6connections.Length - 1;
+            }
+
             // First line is header in each file.
-            TcpConnectionInformation[] connections = new TcpConnectionInformation[v4connections.Length + v6connections.Length - 2];
+            TcpConnectionInformation[] connections = new TcpConnectionInformation[count];
             int index = 0;
             int skip = 0;
 
@@ -98,8 +110,20 @@ namespace System.Net.NetworkInformation
             string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
             string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
 
+            // First line is header in each file. On WSL, this file may be empty.
+            int count = 0;
+            if (v4connections.Length > 0)
+            {
+                count += v4connections.Length - 1;
+            }
+
+            if (v6connections.Length > 0)
+            {
+                count += v6connections.Length - 1;
+            }
+
             // First line is header in each file.
-            IPEndPoint[] endPoints = new IPEndPoint[v4connections.Length + v6connections.Length - 2];
+            IPEndPoint[] endPoints = new IPEndPoint[count];
             int index = 0;
             int skip = 0;
 
@@ -154,8 +178,19 @@ namespace System.Net.NetworkInformation
             string udp6FileContents = File.ReadAllText(udp6File);
             string[] v6connections = udp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
 
-            // First line is header in each file.
-            IPEndPoint[] endPoints = new IPEndPoint[v4connections.Length + v6connections.Length - 2];
+            // First line is header in each file. On WSL, this file may be empty.
+            int count = 0;
+            if (v4connections.Length > 0)
+            {
+                count += v4connections.Length - 1;
+            }
+
+            if (v6connections.Length > 0)
+            {
+                count += v6connections.Length - 1;
+            }
+
+            IPEndPoint[] endPoints = new IPEndPoint[count];
             int index = 0;
 
             // UDP Connections
index ce5cc09..6a5764c 100644 (file)
@@ -128,6 +128,22 @@ namespace System.Net.NetworkInformation.Tests
             Assert.Equal(listeners[16], new IPEndPoint(IPAddress.Parse("fe80::215:5dff:fe00:402"), 123));
         }
 
+        [Fact]
+        public void WSLListenersParsing()
+        {
+            // WSL1 may have files empty
+            string emptyFile = GetTestFilePath();
+            FileUtil.NormalizeLineEndings("NetworkFiles/empty", emptyFile);
+
+            IPEndPoint[] tcpListeners = StringParsingHelpers.ParseActiveTcpListenersFromFiles(emptyFile, emptyFile);
+            IPEndPoint[] udpListeners = StringParsingHelpers.ParseActiveUdpListenersFromFiles(emptyFile, emptyFile);
+            TcpConnectionInformation[] tcpConnections = StringParsingHelpers.ParseActiveTcpConnectionsFromFiles(emptyFile, emptyFile);
+
+            Assert.Equal(0, tcpListeners.Length);
+            Assert.Equal(0, udpListeners.Length);
+            Assert.Equal(0, tcpConnections.Length);
+        }
+
         private static void ValidateInfo(TcpConnectionInformation tcpConnectionInformation, IPEndPoint localEP, IPEndPoint remoteEP, TcpState state)
         {
             Assert.Equal(localEP, tcpConnectionInformation.LocalEndPoint);
diff --git a/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkFiles/empty b/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkFiles/empty
new file mode 100644 (file)
index 0000000..e69de29