Microsoft.Protocols.TestSuites.Common.Common.IsDNMatchABNF C# (CSharp) Method

IsDNMatchABNF() public static method

To determine whether the DN matches regular expression or not. The regular expression is generated according to the ABNF in [MS-OXOABK] section 2.2.1.1.
public static IsDNMatchABNF ( string dn, DNFormat format ) : bool
dn string The distinguished name value to be matched.
format DNFormat The DN format needs to be matched.
return bool
        public static bool IsDNMatchABNF(string dn, DNFormat format)
        {
            // The regular expression specifies the non-space-teletex ABNF definition "!" / DQUOTE / "%" / "&" / "\" / "(" / ")" / 
            // "*" / "+" / "," / "-" / "." / "0" / "1" / 
            // "2" / "3" / "4" / "5" / "6" / "7" / "8" /
            // "9" / ":" / "<" / "=" / ">" / "?" / "@" /
            // "A" / "B" / "C" / "D" / "E" / "F" / "G" / 
            // "H" / "I" / "J" / "K" / "L" / "M" / "N" / 
            // "O" / "P" / "Q" / "R" / "S" / "T" / "U" / 
            // "V" / "W" / "X" / "Y" / "Z" / "[" / "]" /
            // "_" / "a" / "b" / "c" / "d" / "e" / "f" /
            // "g" / "h" / "i" / "j" / "k" / "l" / "m" /
            // "n" / "o" / "p" / "q" / "r" / "s" / "t" /
            // "u" / "v" / "w" / "x" / "y" / "z" / "|"
            string nonSpaceTeletex = "(" + @"[A-Za-z0-9!%&\()*+,-.:<=>?\[\]@_|]|" + "\"" + ")";

            string teletextChar = "(" + nonSpaceTeletex + @"|\s" + ")";

            string rdn = "(" + "(" + nonSpaceTeletex
                             + "(" + teletextChar + ")" + "{0,62}"
                             + nonSpaceTeletex + ")"
                             + "|"
                             + "(" + nonSpaceTeletex + ")"
                             + ")";

            string orgRdn = "(" + "/(o|O)=" + rdn + ")";

            string orgUnitRdn = "(" + "/(o|O)(u|U)=" + rdn + ")";

            string containerRdn = "(" + "/(c|C)(n|N)=" + rdn + ")";

            string objectRdn = "(" + "/(c|C)(n|N)=" + rdn + ")";

            string regexX500ContainerDn = "(" + orgRdn + orgUnitRdn + containerRdn + "{0,13}" + ")";

            string regexX500Dn = "(" + regexX500ContainerDn + objectRdn + ")";

            string regexX500DnWithNoContainerRdn = "(" + objectRdn + ")";

            string organizationDn = "(" + orgRdn + ")";

            string galAddrlistDn = "/";

            string addresslistDn = "(" + "/guid=" + "[a-fA-F0-9]{32}" + "|/" + ")";

            string[] regexStrs = null;

            switch (format)
            {
                case DNFormat.AddressListDn:
                    regexStrs = new string[] { addresslistDn };
                    break;

                case DNFormat.X500Dn:
                    regexStrs = new string[] { regexX500Dn };
                    break;

                case DNFormat.GalAddrlistDn:
                    regexStrs = new string[] { galAddrlistDn };
                    break;

                case DNFormat.X500DnWithNoContainerRdn:
                    regexStrs = new string[] { regexX500DnWithNoContainerRdn };
                    break;

                case DNFormat.Dn:
                    regexStrs = new string[] { regexX500Dn, addresslistDn, organizationDn };
                    break;

                default:
                    break;
            }

            foreach (string regexStr in regexStrs)
            {
                Regex regex = new Regex(regexStr);
                MatchCollection matchResult = regex.Matches(dn);

                if (matchResult.Count == 1)
                {
                    if (matchResult[0].Value.Equals(dn))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Common