public string SetUniqueApplicationName(string baseName = "")
        {
            // note: due to retries, we incorporate a GUID here to ensure that we have a fresh connection pool
            var applicationName = DistributedLockHelpers.ToSafeName(
                $"{(baseName.Length > 0 ? baseName + "_" : string.Empty)}{TestContext.CurrentContext.Test.FullName}_{TargetFramework.Current}_{Guid.NewGuid()}",
                maxNameLength: this.Db.MaxApplicationNameLength,
                s => s
                );

            this.Db.ConnectionStringBuilder["Application Name"] = applicationName;
            return(applicationName);
        }
        internal static string GetName(string name, bool exactName)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (exactName)
            {
                if (name.Length > MaxNameLength)
                {
                    throw new FormatException($"{nameof(name)}: must be at most {MaxNameLength} characters");
                }
                // Oracle treats NULL as the empty string. See https://stackoverflow.com/questions/13278773/null-vs-empty-string-in-oracle
                if (name.Length == 0)
                {
                    throw new FormatException($"{nameof(name)} must not be empty");
                }
                return(name);
            }

            return(DistributedLockHelpers.ToSafeName(name, MaxNameLength, s => s.Length == 0 ? "EMPTY" : s));
        }
Ejemplo n.º 3
0
        internal ZooKeeperPath GetChildNodePathWithSafeName(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var isRoot   = this == Root;
            var safeName = DistributedLockHelpers.ToSafeName(
                name,
                maxNameLength: int.MaxValue,     // no max
                convertToValidName: ConvertToValidNodeName
                )
                           // If ToSafeName adds a hash, it uses Base64 encoding which can include the separator character. We replace
                           // with '_' which is not in Base64 so that the output name remains safe without weakening the hash
                           .Replace(Separator, '_');

            return(new ZooKeeperPath((this == Root ? this._path : (this._path + Separator)) + safeName, checkPath: false));

            string ConvertToValidNodeName(string name)
            {
                // in order to be a valid node name:

                // must not be empty (special-case this because our generic conversion method will map empty to itself)
                if (name.Length == 0)
                {
                    return("EMPTY");
                }

                // must not be ., .., or (this this is root), the reserved path "zookeeper"
                // (see https://zookeeper.apache.org/doc/current/zookeeperProgrammers.html#ch_zkDataModel)
                switch (name)
                {
                case ".":
                case "..":
                case "zookeeper" when isRoot:
                    return(name + "_");

                default:
                    break;     // keep going
                }

                if (name.IndexOf(Separator) < 0 && // must not contain the path separator
                    !ValidatePath(Separator + name).HasValue)    // "/name" must be a valid path
                {
                    return(name);
                }

                var converted = name.ToCharArray();

                for (var i = 0; i < name.Length; ++i)
                {
                    switch (name[i])
                    {
                    // note: we don't have to replace '.' because it is only invalid if '.' or '..' is a full path
                    // segment. Since we'll be appending on a hash, that doesn't matter
                    case Separator:     // separator cannot appear in names, only in paths
                    case '\0':
                    case char @char when IsNonNullInvalidPathChar(@char):
                        converted[i] = '_';     // replace with placeholder

                        break;
                    }
                }

                return(new string(converted));
            }
        }
 static string ConvertToSafeSuffix(string suffix) => DistributedLockHelpers.ToSafeName(
     suffix,
     MaxNameLength - GlobalPrefix.Length,
     s => s.Length == 0 ? "EMPTY" : s.Replace('\\', '_')
     );
Ejemplo n.º 5
0
 internal static string GetSafeName(string name) =>
 DistributedLockHelpers.ToSafeName(name, MaxNameLength, s => s);