< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 58
Coverable lines: 58
Total lines: 115
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 62
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ParseCanonicalName(...)100%110%
IsValid(...)0%62620%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.Uri/src/System/UncNameHelper.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
 4namespace System
 5{
 6    // The class designed as to keep minimal the working set of Uri class.
 7    // The idea is to stay with static helper methods and strings
 8    internal static class UncNameHelper
 9    {
 10        public const int MaximumInternetNameLength = 256;
 11
 12        public static string ParseCanonicalName(string str, int start, int end, ref bool loopback)
 013        {
 014            return DomainNameHelper.ParseCanonicalName(str, start, end, ref loopback);
 015        }
 16
 17        //
 18        // IsValid
 19        //
 20        //
 21        //   ATTN: This class has been re-designed as to conform to XP+ UNC hostname format
 22        //         It is now similar to DNS name but can contain Unicode characters as well
 23        //         This class will be removed and replaced by IDN specification later,
 24        //         but for now we violate URI RFC cause we never escape Unicode characters on the wire
 25        //         For the same reason we never unescape UNC host names since we never accept
 26        //         them in escaped format.
 27        //
 28        //
 29        //      Valid UNC server name chars:
 30        //          a Unicode Letter    (not allowed as the only in a segment)
 31        //          a Latin-1 digit
 32        //          '-'    45 0x2D
 33        //          '.'    46 0x2E    (only as a host domain delimiter)
 34        //          '_'    95 0x5F
 35        //
 36        //
 37        // Assumption is the caller will check on the resulting name length
 38        // Remarks:  MUST NOT be used unless all input indexes are verified and trusted.
 39        public static bool IsValid(ReadOnlySpan<char> name, bool notImplicitFile, out int nameLength)
 040        {
 041            nameLength = 0;
 42
 43            // First segment could consist of only '_' or '-' but it cannot be all digits or empty
 044            bool validShortName = false;
 045            int i = 0;
 046            for (; i < name.Length; i++)
 047            {
 048                if (char.IsLetter(name[i]) || name[i] == '-' || name[i] == '_')
 049                {
 050                    validShortName = true;
 051                }
 052                else if (name[i] == '/' || name[i] == '\\' || (notImplicitFile && (name[i] == ':' || name[i] == '?' || n
 053                {
 054                    break;
 55                }
 056                else if (name[i] == '.')
 057                {
 058                    i++;
 059                    break;
 60                }
 061                else if (!char.IsAsciiDigit(name[i]))
 062                {
 063                    return false;
 64                }
 065            }
 66
 067            if (!validShortName)
 068                return false;
 69
 70            // Subsequent segments must start with a letter or a digit
 71
 072            for (; (uint)i < (uint)name.Length; i++)
 073            {
 074                if (name[i] == '/' || name[i] == '\\' || (notImplicitFile && (name[i] == ':' || name[i] == '?' || name[i
 075                {
 076                    break;
 77                }
 078                else if (name[i] == '.')
 079                {
 080                    if (!validShortName || name[i - 1] == '.')
 081                        return false;
 82
 083                    validShortName = false;
 084                }
 085                else if (name[i] == '-' || name[i] == '_')
 086                {
 087                    if (!validShortName)
 088                        return false;
 089                }
 090                else if (char.IsLetter(name[i]) || char.IsAsciiDigit(name[i]))
 091                {
 092                    validShortName = true;
 093                }
 94                else
 095                {
 096                    return false;
 97                }
 098            }
 99
 0100            if (!validShortName)
 0101            {
 102                // last segment can end with the dot
 0103                if ((uint)(i - 1) >= (uint)name.Length || name[i - 1] != '.')
 0104                {
 0105                    return false;
 106                }
 0107            }
 108
 109            // Caller must check that (nameLength <= MaximumInternetNameLength)
 0110            nameLength = i;
 0111            return true;
 0112        }
 113    }
 114}
 115