Skip to content

Commit

Permalink
improve parsing network files on WSL (#44680)
Browse files Browse the repository at this point in the history
* improve parsing network files on WSL

* Apply suggestions from code review

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
wfurt and stephentoub committed Nov 15, 2020
1 parent 25260d1 commit 45f8a37
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,20 @@ internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(st
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;

Expand Down Expand Up @@ -98,8 +110,20 @@ internal static IPEndPoint[] ParseActiveTcpListenersFromFiles(string tcp4Connect
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;

Expand Down Expand Up @@ -154,8 +178,19 @@ public static IPEndPoint[] ParseActiveUdpListenersFromFiles(string udp4File, str
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ public void UdpListenersParsing()
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);
Expand Down
Empty file.

0 comments on commit 45f8a37

Please sign in to comment.