< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 41
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
IsSafeDnsString(...)100%110%
NormalizeHostName(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Net/Security/TargetHostNameHelper.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.Buffers;
 5using System.Globalization;
 6
 7namespace System.Net.Security
 8{
 9    internal static class TargetHostNameHelper
 10    {
 011        private static readonly IdnMapping s_idnMapping = new IdnMapping();
 012        private static readonly SearchValues<char> s_safeDnsChars =
 013            SearchValues.Create("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz");
 14
 15        private static bool IsSafeDnsString(ReadOnlySpan<char> name) =>
 016            !name.ContainsAnyExcept(s_safeDnsChars);
 17
 18        internal static string NormalizeHostName(string? targetHost)
 019        {
 020            if (string.IsNullOrEmpty(targetHost))
 021            {
 022                return string.Empty;
 23            }
 24
 25            // RFC 6066 section 3 says to exclude trailing dot from fully qualified DNS hostname
 026            targetHost = targetHost.TrimEnd('.');
 27
 28            try
 029            {
 030                return s_idnMapping.GetAscii(targetHost);
 31            }
 032            catch (ArgumentException) when (IsSafeDnsString(targetHost))
 033            {
 34                // Seems like name that does not conform to IDN but appears somewhat valid according to original DNS rfc
 035            }
 36
 037            return targetHost;
 038        }
 39    }
 40}
 41