1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.IO;
6 using Xunit;
7 
8 namespace System.Net.NetworkInformation.Tests
9 {
10     public class ConnectionsParsingTests : FileCleanupTestBase
11     {
12         [Fact]
NumSocketConnectionsParsing()13         public void NumSocketConnectionsParsing()
14         {
15             string sockstatFile = GetTestFilePath();
16             string sockstat6File = GetTestFilePath();
17             FileUtil.NormalizeLineEndings("sockstat", sockstatFile);
18             FileUtil.NormalizeLineEndings("sockstat6", sockstat6File);
19 
20             int numTcp = StringParsingHelpers.ParseNumSocketConnections(sockstatFile, "TCP");
21             Assert.Equal(4, numTcp);
22 
23             int numTcp6 = StringParsingHelpers.ParseNumSocketConnections(sockstat6File, "TCP6");
24             Assert.Equal(6, numTcp6);
25 
26             int numUdp = StringParsingHelpers.ParseNumSocketConnections(sockstatFile, "UDP");
27             Assert.Equal(12, numUdp);
28 
29             int numUdp6 = StringParsingHelpers.ParseNumSocketConnections(sockstat6File, "UDP6");
30             Assert.Equal(3, numUdp6);
31         }
32 
33         [Fact]
ActiveTcpConnectionsParsing()34         public void ActiveTcpConnectionsParsing()
35         {
36             string tcpFile = GetTestFilePath();
37             string tcp6File = GetTestFilePath();
38             FileUtil.NormalizeLineEndings("tcp", tcpFile);
39             FileUtil.NormalizeLineEndings("tcp6", tcp6File);
40 
41             TcpConnectionInformation[] infos = StringParsingHelpers.ParseActiveTcpConnectionsFromFiles(tcpFile, tcp6File);
42             Assert.Equal(11, infos.Length);
43             ValidateInfo(infos[0], new IPEndPoint(0xFFFFFF01L, 0x01BD), new IPEndPoint(0L, 0), TcpState.Established);
44             ValidateInfo(infos[1], new IPEndPoint(0x12345678L, 0x008B), new IPEndPoint(0L, 0), TcpState.SynSent);
45             ValidateInfo(infos[2], new IPEndPoint(0x0101007FL, 0x0035), new IPEndPoint(0L, 0), TcpState.SynReceived);
46             ValidateInfo(infos[3], new IPEndPoint(0x0100007FL, 0x0277), new IPEndPoint(0L, 0), TcpState.FinWait1);
47             ValidateInfo(infos[4], new IPEndPoint(0x0100007FL, 0x0277), new IPEndPoint(0x00000001L, 0), TcpState.SynReceived);
48 
49             ValidateInfo(
50                 infos[5],
51                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x01BD),
52                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x0000),
53                 TcpState.FinWait2);
54 
55             ValidateInfo(
56                 infos[6],
57                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x008B),
58                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x0000),
59                 TcpState.TimeWait);
60 
61             ValidateInfo(
62                 infos[7],
63                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0x0277),
64                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x0000),
65                 TcpState.Closing);
66 
67             ValidateInfo(
68                 infos[8],
69                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA696),
70                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0x0277),
71                 TcpState.CloseWait);
72 
73             ValidateInfo(
74                 infos[9],
75                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA69B),
76                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0x0277),
77                 TcpState.LastAck);
78 
79             ValidateInfo(
80                 infos[10],
81                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA697),
82                 new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0x0277),
83                 TcpState.Listen);
84         }
85 
86         [Fact]
TcpListenersParsing()87         public void TcpListenersParsing()
88         {
89             string tcpFile = GetTestFilePath();
90             string tcp6File = GetTestFilePath();
91             FileUtil.NormalizeLineEndings("tcp", tcpFile);
92             FileUtil.NormalizeLineEndings("tcp6", tcp6File);
93 
94             IPEndPoint[] listeners = StringParsingHelpers.ParseActiveTcpListenersFromFiles(tcpFile, tcp6File);
95             Assert.Equal(11, listeners.Length);
96 
97             Assert.Equal(new IPEndPoint(0xFFFFFF01, 0x01Bd), listeners[0]);
98             Assert.Equal(new IPEndPoint(0x12345678, 0x008B), listeners[1]);
99             Assert.Equal(new IPEndPoint(0x0101007F, 0x0035), listeners[2]);
100             Assert.Equal(new IPEndPoint(0x0100007F, 0x0277), listeners[3]);
101             Assert.Equal(new IPEndPoint(0x0100007F, 0x0277), listeners[4]);
102 
103             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x01BD), listeners[5]);
104             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x008B), listeners[6]);
105             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0x0277), listeners[7]);
106             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA696), listeners[8]);
107             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA69B), listeners[9]);
108             Assert.Equal(new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"), 0xA697), listeners[10]);
109         }
110 
111         [Fact]
UdpListenersParsing()112         public void UdpListenersParsing()
113         {
114             string udpFile = GetTestFilePath();
115             string udp6File = GetTestFilePath();
116             FileUtil.NormalizeLineEndings("udp", udpFile);
117             FileUtil.NormalizeLineEndings("udp6", udp6File);
118 
119             IPEndPoint[] listeners = StringParsingHelpers.ParseActiveUdpListenersFromFiles(udpFile, udp6File);
120             Assert.Equal(15, listeners.Length);
121 
122             Assert.Equal(listeners[0], new IPEndPoint(0x00000000, 0x8E15));
123             Assert.Equal(listeners[1], new IPEndPoint(0x00000000, 0x14E9));
124             Assert.Equal(listeners[2], new IPEndPoint(0x00000000, 0xB50F));
125             Assert.Equal(listeners[3], new IPEndPoint(0x0101007F, 0x0035));
126             Assert.Equal(listeners[4], new IPEndPoint(0x00000000, 0x0044));
127             Assert.Equal(listeners[5], new IPEndPoint(0xFF83690A, 0x0089));
128             Assert.Equal(listeners[6], new IPEndPoint(0x3B80690A, 0x0089));
129             Assert.Equal(listeners[7], new IPEndPoint(0x00000000, 0x0089));
130             Assert.Equal(listeners[8], new IPEndPoint(0xFF83690A, 0x008A));
131             Assert.Equal(listeners[9], new IPEndPoint(0x3B80690A, 0x008A));
132             Assert.Equal(listeners[10], new IPEndPoint(0x00000000, 0x008A));
133             Assert.Equal(listeners[11], new IPEndPoint(0x00000000, 0x0277));
134 
135             Assert.Equal(listeners[12], new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x14E9));
136             Assert.Equal(listeners[13], new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x96D3));
137             Assert.Equal(listeners[14], new IPEndPoint(StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"), 0x8B58));
138         }
139 
ValidateInfo(TcpConnectionInformation tcpConnectionInformation, IPEndPoint localEP, IPEndPoint remoteEP, TcpState state)140         private static void ValidateInfo(TcpConnectionInformation tcpConnectionInformation, IPEndPoint localEP, IPEndPoint remoteEP, TcpState state)
141         {
142             Assert.Equal(localEP, tcpConnectionInformation.LocalEndPoint);
143             Assert.Equal(remoteEP, tcpConnectionInformation.RemoteEndPoint);
144             Assert.Equal(state, tcpConnectionInformation.State);
145         }
146     }
147 }
148