/src/Storage/Commands.Storage.Test/Common/FileNamingGenerator.cs

https://gitlab.com/jslee1/azure-powershell · C# · 163 lines · 139 code · 9 blank · 15 comment · 0 complexity · 95aaefd1a6aa1d9994de69bbb30d9aa6 MD5 · raw file

  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. namespace Microsoft.WindowsAzure.Management.Storage.Test.Common
  19. {
  20. /// <summary>
  21. /// Provides utilities to generate different names for file service.
  22. /// </summary>
  23. internal static class FileNamingGenerator
  24. {
  25. public const int MaxShareNameLength = 63;
  26. public const int MaxFileNameLength = 255;
  27. public const int MaxPathLength = 1024;
  28. public const int MinShareNameLength = 3;
  29. private const char Dash = '-';
  30. private const double DashRate = 0.2;
  31. private static readonly Tuple<int, int> ValidASCIIRange = new Tuple<int, int>(0x0020, 0x07E);
  32. private static readonly char[] InvalidFileNameCharacters = new char[] { '\\', '/', ':', '|', '<', '>', '*', '?', '"' };
  33. private static readonly char[] ValidShareNameCharactersExceptDash =
  34. Enumerable.Range(0, 26).Select(x => (char)('a' + x)).Concat(
  35. Enumerable.Range(0, 10).Select(x => (char)('0' + x))).ToArray();
  36. public static string GenerateValidASCIIName(int length)
  37. {
  38. return GenerateNameFromRange(length, ValidASCIIRange);
  39. }
  40. public static string GenerateValidShareName(int length)
  41. {
  42. return GenerateShareNameInternal(length);
  43. }
  44. public static string GenerateInvalidShareName_DoubleDash(int length)
  45. {
  46. return GenerateShareNameInternal(length, ensureDoubleDash: true);
  47. }
  48. public static string GenerateInvalidShareName_StartsWithDash(int length)
  49. {
  50. return GenerateShareNameInternal(length, startsWithDash: true);
  51. }
  52. public static string GenerateInvalidShareName_EndsWithDash(int length)
  53. {
  54. return GenerateShareNameInternal(length, endsWithDash: true);
  55. }
  56. public static string GenerateInvalidShareName_UpperCase(int length)
  57. {
  58. return GenerateShareNameInternal(length, ensureUpperCase: true);
  59. }
  60. public static string GenerateNameFromRange(int length, Tuple<int, int> range)
  61. {
  62. Random r = new Random();
  63. StringBuilder sb = new StringBuilder(length);
  64. for (int i = 0; i < length; i++)
  65. {
  66. char ch;
  67. do
  68. {
  69. ch = Convert.ToChar(r.Next(range.Item1, range.Item2));
  70. }
  71. while (InvalidFileNameCharacters.Contains(ch));
  72. sb.Append(ch);
  73. }
  74. return sb.ToString();
  75. }
  76. private static string GenerateShareNameInternal(int length, bool ensureDoubleDash = false, bool startsWithDash = false, bool endsWithDash = false, bool ensureUpperCase = false)
  77. {
  78. Random r = new Random();
  79. StringBuilder sb = new StringBuilder(length);
  80. if (startsWithDash)
  81. {
  82. sb.Append(Dash);
  83. }
  84. else
  85. {
  86. sb.Append(ValidShareNameCharactersExceptDash[r.Next(ValidShareNameCharactersExceptDash.Length)]);
  87. }
  88. bool lastIsDash = false;
  89. bool doubleDashEnsured = false;
  90. for (int i = 0; i < length - 2; i++)
  91. {
  92. if (ensureDoubleDash && lastIsDash)
  93. {
  94. sb.Append(Dash);
  95. doubleDashEnsured = true;
  96. }
  97. if (!lastIsDash && r.NextDouble() < DashRate)
  98. {
  99. sb.Append(Dash);
  100. lastIsDash = true;
  101. }
  102. else
  103. {
  104. sb.Append(ValidShareNameCharactersExceptDash[r.Next(ValidShareNameCharactersExceptDash.Length)]);
  105. }
  106. }
  107. if (endsWithDash)
  108. {
  109. sb.Append(Dash);
  110. }
  111. else
  112. {
  113. sb.Append(ValidShareNameCharactersExceptDash[r.Next(ValidShareNameCharactersExceptDash.Length)]);
  114. }
  115. if (ensureDoubleDash && !doubleDashEnsured)
  116. {
  117. int pos = r.Next(1, length - 2);
  118. sb[pos] = Dash;
  119. sb[pos + 1] = Dash;
  120. }
  121. if (ensureUpperCase)
  122. {
  123. var allLettersPosition = new List<int>(sb.Length);
  124. for (int i = 0; i < sb.Length; i++)
  125. {
  126. if (sb[i] >= 'a' && sb[i] <= 'z')
  127. {
  128. allLettersPosition.Add(i);
  129. }
  130. }
  131. int index = allLettersPosition[r.Next(allLettersPosition.Count)];
  132. sb[index] = Char.ToUpperInvariant(sb[index]);
  133. }
  134. return sb.ToString();
  135. }
  136. }
  137. }