From 9aa8c6f3c4842c4b848d847d49b2c167823ea58f Mon Sep 17 00:00:00 2001 From: Mojtaba Tajik Date: Sat, 20 Apr 2024 20:32:37 +0200 Subject: [PATCH] Changed GetNetworkInterfaces method to to retrieve information related to all interfaces (#100824) * Add IncludeAllInterfaces as falg to reterive all avaliable interfaces including disabled ones * Update tests to accommodate new adapters. Ensure adapters reflect proper speeds; non-positive speeds indicates absence of physical address. * Removed unnecessary assertion * Add assertion over physical address bytes --- .../System/Net/NetworkInformation/SystemNetworkInterface.cs | 3 +-- .../tests/FunctionalTests/NetworkInterfaceBasicTest.cs | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs index de2197321e5355..8a99feaeec369f 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/SystemNetworkInterface.cs @@ -85,8 +85,7 @@ internal static unsafe NetworkInterface[] GetNetworkInterfaces() List interfaceList = new List(); Interop.IpHlpApi.GetAdaptersAddressesFlags flags = - Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeGateways - | Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeWins; + Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeAllInterfaces; // Figure out the right buffer size for the adapter information. uint result = Interop.IpHlpApi.GetAdaptersAddresses( diff --git a/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkInterfaceBasicTest.cs b/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkInterfaceBasicTest.cs index dcb12e8b7f87b0..5d5167be739f8e 100644 --- a/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkInterfaceBasicTest.cs +++ b/src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/NetworkInterfaceBasicTest.cs @@ -22,6 +22,7 @@ public NetworkInterfaceBasicTest(ITestOutputHelper output) [Fact] public void BasicTest_GetNetworkInterfaces_AtLeastOne() { + Assert.NotEqual(0, NetworkInterface.GetAllNetworkInterfaces().Length); } @@ -50,7 +51,8 @@ public void BasicTest_AccessInstanceProperties_NoExceptions() if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { - Assert.Equal(6, nic.GetPhysicalAddress().GetAddressBytes().Length); + var physicalAddressLength = nic.GetPhysicalAddress().GetAddressBytes().Length; + Assert.True(physicalAddressLength == 0 || physicalAddressLength == 6); } } }