-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathNetworkInterfaceBasicTest.cs
333 lines (293 loc) · 15.5 KB
/
NetworkInterfaceBasicTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.NetworkInformation.Tests
{
public class NetworkInterfaceBasicTest
{
private readonly ITestOutputHelper _log;
public NetworkInterfaceBasicTest(ITestOutputHelper output)
{
_log = output;
}
[Fact]
public void BasicTest_GetNetworkInterfaces_AtLeastOne()
{
Assert.NotEqual<int>(0, NetworkInterface.GetAllNetworkInterfaces().Length);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Not all APIs are supported on Linux and OSX
public void BasicTest_AccessInstanceProperties_NoExceptions()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
_log.WriteLine("- NetworkInterface -");
_log.WriteLine("Name: " + nic.Name);
_log.WriteLine("Description: " + nic.Description);
_log.WriteLine("ID: " + nic.Id);
_log.WriteLine("IsReceiveOnly: " + nic.IsReceiveOnly);
_log.WriteLine("Type: " + nic.NetworkInterfaceType);
_log.WriteLine("Status: " + nic.OperationalStatus);
_log.WriteLine("Speed: " + nic.Speed);
// Validate NIC speed overflow.
// We've found that certain WiFi adapters will return speed of -1 when not connected.
// We've found that Wi-Fi Direct Virtual Adapters return speed of -1 even when up.
Assert.InRange(nic.Speed, -1, long.MaxValue);
_log.WriteLine("SupportsMulticast: " + nic.SupportsMulticast);
_log.WriteLine("GetPhysicalAddress(): " + nic.GetPhysicalAddress());
// We verify that the NIC is an Ethernet type with a positive speed, indicating the presence of a physical address.
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet && nic.Speed > 0)
{
Assert.Equal(6, nic.GetPhysicalAddress().GetAddressBytes().Length);
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux|TestPlatforms.Android)] // Some APIs are not supported on Linux and Android
public void BasicTest_AccessInstanceProperties_NoExceptions_Linux()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
_log.WriteLine("- NetworkInterface -");
_log.WriteLine("Name: " + nic.Name);
string description = nic.Description;
Assert.False(string.IsNullOrEmpty(description), "NetworkInterface.Description should not be null or empty.");
_log.WriteLine("Description: " + description);
string id = nic.Id;
Assert.False(string.IsNullOrEmpty(id), "NetworkInterface.Id should not be null or empty.");
_log.WriteLine("ID: " + id);
Assert.False(nic.IsReceiveOnly);
_log.WriteLine("Type: " + nic.NetworkInterfaceType);
_log.WriteLine("Status: " + nic.OperationalStatus);
try
{
_log.WriteLine("Speed: " + nic.Speed);
Assert.InRange(nic.Speed, -1, long.MaxValue);
}
// We cannot guarantee this works on all devices.
catch (PlatformNotSupportedException pnse)
{
_log.WriteLine(pnse.ToString());
}
_log.WriteLine("SupportsMulticast: " + nic.SupportsMulticast);
_log.WriteLine("GetPhysicalAddress(): " + nic.GetPhysicalAddress());
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Assert.Equal(6, nic.GetPhysicalAddress().GetAddressBytes().Length);
}
}
}
[Fact]
[PlatformSpecific(TestPlatforms.OSX|TestPlatforms.FreeBSD)]
public void BasicTest_AccessInstanceProperties_NoExceptions_Bsd()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
_log.WriteLine("- NetworkInterface -");
_log.WriteLine("Name: " + nic.Name);
string description = nic.Description;
Assert.False(string.IsNullOrEmpty(description), "NetworkInterface.Description should not be null or empty.");
_log.WriteLine("Description: " + description);
string id = nic.Id;
Assert.False(string.IsNullOrEmpty(id), "NetworkInterface.Id should not be null or empty.");
_log.WriteLine("ID: " + id);
Assert.False(nic.IsReceiveOnly);
_log.WriteLine("Type: " + nic.NetworkInterfaceType);
_log.WriteLine("Status: " + nic.OperationalStatus);
_log.WriteLine("Speed: " + nic.Speed);
Assert.InRange(nic.Speed, 0, long.MaxValue);
_log.WriteLine("SupportsMulticast: " + nic.SupportsMulticast);
_log.WriteLine("GetPhysicalAddress(): " + nic.GetPhysicalAddress());
if (nic.Name.StartsWith("en") || nic.Name == "lo0")
{
// Ethernet, WIFI and loopback should have known status.
Assert.True((nic.OperationalStatus == OperationalStatus.Up) || (nic.OperationalStatus == OperationalStatus.Down));
}
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Assert.Equal(6, nic.GetPhysicalAddress().GetAddressBytes().Length);
}
}
}
[Fact]
[Trait("IPv4", "true")]
public void BasicTest_StaticLoopbackIndex_MatchesLoopbackNetworkInterface()
{
Assert.True(Capability.IPv4Support());
_log.WriteLine("Loopback IPv4 index: " + NetworkInterface.LoopbackInterfaceIndex);
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation unicast in nic.GetIPProperties().UnicastAddresses)
{
if (unicast.Address.Equals(IPAddress.Loopback))
{
Assert.Equal<int>(nic.GetIPProperties().GetIPv4Properties().Index,
NetworkInterface.LoopbackInterfaceIndex);
Assert.True(nic.NetworkInterfaceType == NetworkInterfaceType.Loopback);
return; // Only check IPv4 loopback
}
}
}
}
[Fact]
[Trait("IPv4", "true")]
public void BasicTest_StaticLoopbackIndex_ExceptionIfV4NotSupported()
{
Assert.True(Capability.IPv4Support());
_log.WriteLine("Loopback IPv4 index: " + NetworkInterface.LoopbackInterfaceIndex);
}
[Trait("IPv6", "true")]
[ConditionalFact(typeof(Socket), nameof(Socket.OSSupportsIPv6))]
public void BasicTest_StaticIPv6LoopbackIndex_MatchesLoopbackNetworkInterface()
{
Assert.True(Capability.IPv6Support());
_log.WriteLine("Loopback IPv6 index: " + NetworkInterface.IPv6LoopbackInterfaceIndex);
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation unicast in nic.GetIPProperties().UnicastAddresses)
{
if (unicast.Address.Equals(IPAddress.IPv6Loopback))
{
Assert.Equal<int>(
nic.GetIPProperties().GetIPv6Properties().Index,
NetworkInterface.IPv6LoopbackInterfaceIndex);
return; // Only check IPv6 loopback.
}
}
}
}
[Trait("IPv6", "true")]
[ConditionalFact(typeof(Socket), nameof(Socket.OSSupportsIPv6))]
public void BasicTest_StaticIPv6LoopbackIndex_ExceptionIfV6NotSupported()
{
Assert.True(Capability.IPv6Support());
_log.WriteLine("Loopback IPv6 index: " + NetworkInterface.IPv6LoopbackInterfaceIndex);
}
[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Not all APIs are supported on Linux and OSX
public void BasicTest_GetIPInterfaceStatistics_Success()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceStatistics stats = nic.GetIPStatistics();
_log.WriteLine("- Stats for : " + nic.Name);
_log.WriteLine("BytesReceived: " + stats.BytesReceived);
_log.WriteLine("BytesSent: " + stats.BytesSent);
_log.WriteLine("IncomingPacketsDiscarded: " + stats.IncomingPacketsDiscarded);
_log.WriteLine("IncomingPacketsWithErrors: " + stats.IncomingPacketsWithErrors);
_log.WriteLine("IncomingUnknownProtocolPackets: " + stats.IncomingUnknownProtocolPackets);
_log.WriteLine("NonUnicastPacketsReceived: " + stats.NonUnicastPacketsReceived);
_log.WriteLine("NonUnicastPacketsSent: " + stats.NonUnicastPacketsSent);
_log.WriteLine("OutgoingPacketsDiscarded: " + stats.OutgoingPacketsDiscarded);
_log.WriteLine("OutgoingPacketsWithErrors: " + stats.OutgoingPacketsWithErrors);
_log.WriteLine("OutputQueueLength: " + stats.OutputQueueLength);
_log.WriteLine("UnicastPacketsReceived: " + stats.UnicastPacketsReceived);
_log.WriteLine("UnicastPacketsSent: " + stats.UnicastPacketsSent);
}
}
[Fact]
[PlatformSpecific(TestPlatforms.Linux)] // Some APIs are not supported on Linux
[SkipOnPlatform(TestPlatforms.LinuxBionic, "Bionic is not normal Linux, has no normal /proc")]
public void BasicTest_GetIPInterfaceStatistics_Success_Linux()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceStatistics stats = nic.GetIPStatistics();
_log.WriteLine("- Stats for : " + nic.Name);
_log.WriteLine("BytesReceived: " + stats.BytesReceived);
_log.WriteLine("BytesSent: " + stats.BytesSent);
_log.WriteLine("IncomingPacketsDiscarded: " + stats.IncomingPacketsDiscarded);
_log.WriteLine("IncomingPacketsWithErrors: " + stats.IncomingPacketsWithErrors);
Assert.Throws<PlatformNotSupportedException>(() => stats.IncomingUnknownProtocolPackets);
_log.WriteLine("NonUnicastPacketsReceived: " + stats.NonUnicastPacketsReceived);
Assert.Throws<PlatformNotSupportedException>(() => stats.NonUnicastPacketsSent);
_log.WriteLine("OutgoingPacketsDiscarded: " + stats.OutgoingPacketsDiscarded);
_log.WriteLine("OutgoingPacketsWithErrors: " + stats.OutgoingPacketsWithErrors);
_log.WriteLine("OutputQueueLength: " + stats.OutputQueueLength);
_log.WriteLine("UnicastPacketsReceived: " + stats.UnicastPacketsReceived);
_log.WriteLine("UnicastPacketsSent: " + stats.UnicastPacketsSent);
}
}
[Fact]
[PlatformSpecific(TestPlatforms.OSX|TestPlatforms.FreeBSD)]
public void BasicTest_GetIPInterfaceStatistics_Success_Bsd()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceStatistics stats = nic.GetIPStatistics();
_log.WriteLine("- Stats for : " + nic.Name);
_log.WriteLine("BytesReceived: " + stats.BytesReceived);
_log.WriteLine("BytesSent: " + stats.BytesSent);
_log.WriteLine("IncomingPacketsDiscarded: " + stats.IncomingPacketsDiscarded);
_log.WriteLine("IncomingPacketsWithErrors: " + stats.IncomingPacketsWithErrors);
_log.WriteLine("IncomingUnknownProtocolPackets: " + stats.IncomingUnknownProtocolPackets);
_log.WriteLine("NonUnicastPacketsReceived: " + stats.NonUnicastPacketsReceived);
_log.WriteLine("NonUnicastPacketsSent: " + stats.NonUnicastPacketsSent);
Assert.Throws<PlatformNotSupportedException>(() => stats.OutgoingPacketsDiscarded);
_log.WriteLine("OutgoingPacketsWithErrors: " + stats.OutgoingPacketsWithErrors);
_log.WriteLine("OutputQueueLength: " + stats.OutputQueueLength);
_log.WriteLine("UnicastPacketsReceived: " + stats.UnicastPacketsReceived);
_log.WriteLine("UnicastPacketsSent: " + stats.UnicastPacketsSent);
}
}
[Fact]
public void BasicTest_GetIsNetworkAvailable_Success()
{
Assert.True(NetworkInterface.GetIsNetworkAvailable());
}
[ConditionalTheory]
[SkipOnPlatform(TestPlatforms.OSX | TestPlatforms.FreeBSD, "Expected behavior is different on OSX or FreeBSD")]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
[InlineData(false)]
[InlineData(true)]
public async Task NetworkInterface_LoopbackInterfaceIndex_MatchesReceivedPackets(bool ipv6)
{
if (ipv6 && !Socket.OSSupportsIPv6)
{
throw new SkipTestException("IPv6 is not supported");
}
using (var client = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (var server = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
server.Bind(new IPEndPoint(ipv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback, 0));
var serverEndPoint = (IPEndPoint)server.LocalEndPoint;
Task<SocketReceiveMessageFromResult> receivedTask =
server.ReceiveMessageFromAsync(new ArraySegment<byte>(new byte[1]), SocketFlags.None, serverEndPoint);
while (!receivedTask.IsCompleted)
{
client.SendTo(new byte[] { 42 }, serverEndPoint);
await Task.Delay(1);
}
Assert.Equal(
(await receivedTask).PacketInformation.Interface,
ipv6 ? NetworkInterface.IPv6LoopbackInterfaceIndex : NetworkInterface.LoopbackInterfaceIndex);
}
}
[ConditionalFact]
public void NetworkInterface_UnicastLLA_ScopeIdSet()
{
bool foundLla = false;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties prop = nic.GetIPProperties();
foreach (UnicastIPAddressInformation info in prop.UnicastAddresses)
{
if (info.Address.IsIPv6LinkLocal)
{
foundLla = true;
Assert.NotEqual(0, info.Address.ScopeId);
}
}
}
if (!foundLla)
{
throw new SkipTestException("Did not find any LLA");
}
}
}
}