< Summary

Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 82
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
InterfaceNameToIndex(...)95%2020100%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Net/NetworkInformation/InterfaceInfoPal.Windows.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Numerics;
 6using System.Runtime.InteropServices;
 7using System.Text;
 8
 9namespace System.Net.NetworkInformation
 10{
 11    internal static class InterfaceInfoPal
 12    {
 13        // Measured in characters (the native data type for this platform's API.)
 14        private const int StackAllocationThreshold = 256;
 15
 16        public static unsafe uint InterfaceNameToIndex<TChar>(ReadOnlySpan<TChar> interfaceName)
 17            where TChar : unmanaged, IBinaryNumber<TChar>
 26818        {
 26819            Debug.Assert(typeof(TChar) == typeof(char) ||  typeof(TChar) == typeof(byte));
 20
 21            // Measured in characters, including null terminator.
 26822            int bufferSize = 0;
 26823            char* nativeMemory = null;
 26824            ulong interfaceLuid = 0;
 26825            uint interfaceIndex = 0;
 26
 27            // The underlying API for this method accepts a null-terminated Unicode string containing the interface name
 28            // If TChar is char, the only work required is a byte copy. If TChar is byte, there's a transcoding step fro
 29            // to Unicode.
 30            // Notably: the encoding passed to the underlying API is different between Linux and Windows.
 26831            if (typeof(TChar) == typeof(char))
 13432            {
 13433                bufferSize = interfaceName.Length + 1;
 13434            }
 13435            else if (typeof(TChar) == typeof(byte))
 13436            {
 13437                ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName);
 38
 13439                bufferSize = Encoding.UTF8.GetCharCount(castInterfaceName) + 1;
 13440            }
 26841            Debug.Assert(bufferSize <= int.MaxValue - 1);
 42
 43            try
 26844            {
 26845                nativeMemory = (uint)bufferSize <= StackAllocationThreshold ? null : (char*)NativeMemory.Alloc((nuint)(b
 46
 26847                Span<char> buffer = nativeMemory == null ? stackalloc char[StackAllocationThreshold].Slice(0, bufferSize
 48
 26849                if (typeof(TChar) == typeof(char))
 13450                {
 13451                    ReadOnlySpan<char> castInterfaceName = MemoryMarshal.Cast<TChar, char>(interfaceName);
 52
 13453                    castInterfaceName.CopyTo(buffer);
 13454                }
 13455                else if (typeof(TChar) == typeof(byte))
 13456                {
 13457                    ReadOnlySpan<byte> castInterfaceName = MemoryMarshal.Cast<TChar, byte>(interfaceName);
 58
 13459                    Encoding.UTF8.GetChars(castInterfaceName, buffer);
 13460                }
 26861                buffer[buffer.Length - 1] = '\0';
 62
 26863                if (Interop.IpHlpApi.ConvertInterfaceNameToLuid(buffer, ref interfaceLuid) != 0)
 26264                {
 26265                    return 0;
 66                }
 667            }
 68            finally
 26869            {
 26870                if (nativeMemory != null)
 1671                {
 1672                    NativeMemory.Free(nativeMemory);
 1673                }
 26874            }
 75
 676            return Interop.IpHlpApi.ConvertInterfaceLuidToIndex(interfaceLuid, ref interfaceIndex) == 0
 677                ? interfaceIndex
 678                : 0;
 26879        }
 80    }
 81}
 82