You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidRefNameTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2009, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import static org.eclipse.jgit.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import org.eclipse.jgit.junit.MockSystemReader;
  14. import org.eclipse.jgit.util.SystemReader;
  15. import org.junit.Test;
  16. public class ValidRefNameTest {
  17. private static void assertValid(boolean exp, String name) {
  18. SystemReader instance = SystemReader.getInstance();
  19. try {
  20. setUnixSystemReader();
  21. assertEquals("\"" + name + "\"", exp,
  22. Repository.isValidRefName(name));
  23. setWindowsSystemReader();
  24. assertEquals("\"" + name + "\"", exp,
  25. Repository.isValidRefName(name));
  26. } finally {
  27. SystemReader.setInstance(instance);
  28. }
  29. }
  30. private static void setWindowsSystemReader() {
  31. SystemReader.setInstance(new MockSystemReader() {
  32. {
  33. setWindows();
  34. }
  35. });
  36. }
  37. private static void setUnixSystemReader() {
  38. SystemReader.setInstance(new MockSystemReader() {
  39. {
  40. setUnix();
  41. }
  42. });
  43. }
  44. private static void assertInvalidOnWindows(String name) {
  45. SystemReader instance = SystemReader.getInstance();
  46. try {
  47. setUnixSystemReader();
  48. assertEquals("\"" + name + "\"", true,
  49. Repository.isValidRefName(name));
  50. setWindowsSystemReader();
  51. assertEquals("\"" + name + "\"", false,
  52. Repository.isValidRefName(name));
  53. } finally {
  54. SystemReader.setInstance(instance);
  55. }
  56. }
  57. private static void assertNormalized(final String name,
  58. final String expected) {
  59. SystemReader instance = SystemReader.getInstance();
  60. try {
  61. setUnixSystemReader();
  62. String normalized = Repository.normalizeBranchName(name);
  63. assertEquals("Normalization of " + name, expected, normalized);
  64. assertEquals("\"" + normalized + "\"", true,
  65. Repository.isValidRefName(Constants.R_HEADS + normalized));
  66. setWindowsSystemReader();
  67. normalized = Repository.normalizeBranchName(name);
  68. assertEquals("Normalization of " + name, expected, normalized);
  69. assertEquals("\"" + normalized + "\"", true,
  70. Repository.isValidRefName(Constants.R_HEADS + normalized));
  71. } finally {
  72. SystemReader.setInstance(instance);
  73. }
  74. }
  75. @Test
  76. public void testEmptyString() {
  77. assertValid(false, "");
  78. assertValid(false, "/");
  79. }
  80. @Test
  81. public void testMustHaveTwoComponents() {
  82. assertValid(false, "master");
  83. assertValid(true, "heads/master");
  84. }
  85. @Test
  86. public void testValidHead() {
  87. assertValid(true, "refs/heads/master");
  88. assertValid(true, "refs/heads/pu");
  89. assertValid(true, "refs/heads/z");
  90. assertValid(true, "refs/heads/FoO");
  91. }
  92. @Test
  93. public void testValidTag() {
  94. assertValid(true, "refs/tags/v1.0");
  95. }
  96. @Test
  97. public void testNoLockSuffix() {
  98. assertValid(false, "refs/heads/master.lock");
  99. }
  100. @Test
  101. public void testNoDirectorySuffix() {
  102. assertValid(false, "refs/heads/master/");
  103. }
  104. @Test
  105. public void testNoSpace() {
  106. assertValid(false, "refs/heads/i haz space");
  107. }
  108. @Test
  109. public void testNoAsciiControlCharacters() {
  110. for (char c = '\0'; c < ' '; c++)
  111. assertValid(false, "refs/heads/mast" + c + "er");
  112. }
  113. @Test
  114. public void testNoBareDot() {
  115. assertValid(false, "refs/heads/.");
  116. assertValid(false, "refs/heads/..");
  117. assertValid(false, "refs/heads/./master");
  118. assertValid(false, "refs/heads/../master");
  119. }
  120. @Test
  121. public void testNoLeadingOrTrailingDot() {
  122. assertValid(false, ".");
  123. assertValid(false, "refs/heads/.bar");
  124. assertValid(false, "refs/heads/..bar");
  125. assertValid(false, "refs/heads/bar.");
  126. }
  127. @Test
  128. public void testContainsDot() {
  129. assertValid(true, "refs/heads/m.a.s.t.e.r");
  130. assertValid(false, "refs/heads/master..pu");
  131. }
  132. @Test
  133. public void testNoMagicRefCharacters() {
  134. assertValid(false, "refs/heads/master^");
  135. assertValid(false, "refs/heads/^master");
  136. assertValid(false, "^refs/heads/master");
  137. assertValid(false, "refs/heads/master~");
  138. assertValid(false, "refs/heads/~master");
  139. assertValid(false, "~refs/heads/master");
  140. assertValid(false, "refs/heads/master:");
  141. assertValid(false, "refs/heads/:master");
  142. assertValid(false, ":refs/heads/master");
  143. }
  144. @Test
  145. public void testShellGlob() {
  146. assertValid(false, "refs/heads/master?");
  147. assertValid(false, "refs/heads/?master");
  148. assertValid(false, "?refs/heads/master");
  149. assertValid(false, "refs/heads/master[");
  150. assertValid(false, "refs/heads/[master");
  151. assertValid(false, "[refs/heads/master");
  152. assertValid(false, "refs/heads/master*");
  153. assertValid(false, "refs/heads/*master");
  154. assertValid(false, "*refs/heads/master");
  155. }
  156. @Test
  157. public void testValidSpecialCharacterUnixs() {
  158. assertValid(true, "refs/heads/!");
  159. assertValid(true, "refs/heads/#");
  160. assertValid(true, "refs/heads/$");
  161. assertValid(true, "refs/heads/%");
  162. assertValid(true, "refs/heads/&");
  163. assertValid(true, "refs/heads/'");
  164. assertValid(true, "refs/heads/(");
  165. assertValid(true, "refs/heads/)");
  166. assertValid(true, "refs/heads/+");
  167. assertValid(true, "refs/heads/,");
  168. assertValid(true, "refs/heads/-");
  169. assertValid(true, "refs/heads/;");
  170. assertValid(true, "refs/heads/=");
  171. assertValid(true, "refs/heads/@");
  172. assertValid(true, "refs/heads/]");
  173. assertValid(true, "refs/heads/_");
  174. assertValid(true, "refs/heads/`");
  175. assertValid(true, "refs/heads/{");
  176. assertValid(true, "refs/heads/}");
  177. // This is valid on UNIX, but not on Windows
  178. // hence we make in invalid due to non-portability
  179. //
  180. assertValid(false, "refs/heads/\\");
  181. // More invalid characters on Windows, but we allow them
  182. assertInvalidOnWindows("refs/heads/\"");
  183. assertInvalidOnWindows("refs/heads/<");
  184. assertInvalidOnWindows("refs/heads/>");
  185. assertInvalidOnWindows("refs/heads/|");
  186. }
  187. @Test
  188. public void testUnicodeNames() {
  189. assertValid(true, "refs/heads/\u00e5ngstr\u00f6m");
  190. }
  191. @Test
  192. public void testRefLogQueryIsValidRef() {
  193. assertValid(false, "refs/heads/master@{1}");
  194. assertValid(false, "refs/heads/master@{1.hour.ago}");
  195. }
  196. @Test
  197. public void testWindowsReservedNames() {
  198. // re-using code from DirCacheCheckoutTest, hence
  199. // only testing for one of the special names.
  200. assertInvalidOnWindows("refs/heads/con");
  201. assertInvalidOnWindows("refs/con/x");
  202. assertInvalidOnWindows("con/heads/x");
  203. assertValid(true, "refs/heads/conx");
  204. assertValid(true, "refs/heads/xcon");
  205. }
  206. @Test
  207. public void testNormalizeBranchName() {
  208. assertEquals("", Repository.normalizeBranchName(null));
  209. assertEquals("", Repository.normalizeBranchName(""));
  210. assertNormalized("Bug 12345::::Hello World", "Bug_12345-Hello_World");
  211. assertNormalized("Bug 12345 :::: Hello World", "Bug_12345_Hello_World");
  212. assertNormalized("Bug 12345 :::: Hello::: World",
  213. "Bug_12345_Hello-World");
  214. assertNormalized(":::Bug 12345 - Hello World", "Bug_12345_Hello_World");
  215. assertNormalized("---Bug 12345 - Hello World", "Bug_12345_Hello_World");
  216. assertNormalized("Bug 12345 ---- Hello --- World",
  217. "Bug_12345_Hello_World");
  218. assertNormalized("Bug 12345 - Hello World!", "Bug_12345_Hello_World!");
  219. assertNormalized("Bug 12345 : Hello World!", "Bug_12345_Hello_World!");
  220. assertNormalized("Bug 12345 _ Hello World!", "Bug_12345_Hello_World!");
  221. assertNormalized("Bug 12345 - Hello World!",
  222. "Bug_12345_Hello_World!");
  223. assertNormalized(" Bug 12345 - Hello World! ",
  224. "Bug_12345_Hello_World!");
  225. assertNormalized(" Bug 12345 - Hello World! ",
  226. "Bug_12345_Hello_World!");
  227. assertNormalized("Bug 12345 - Hello______ World!",
  228. "Bug_12345_Hello_World!");
  229. assertNormalized("_Bug 12345 - Hello World!", "Bug_12345_Hello_World!");
  230. }
  231. @Test
  232. public void testNormalizeWithSlashes() {
  233. assertNormalized("foo/bar/baz", "foo/bar/baz");
  234. assertNormalized("foo/bar.lock/baz.lock", "foo/bar_lock/baz_lock");
  235. assertNormalized("foo/.git/.git~1/bar", "foo/git/git-1/bar");
  236. assertNormalized(".foo/aux/con/com3.txt/com0/prn/lpt1",
  237. "foo/+aux/+con/+com3.txt/com0/+prn/+lpt1");
  238. assertNormalized("foo/../bar///.--ba...z", "foo/bar/ba.z");
  239. }
  240. @Test
  241. public void testNormalizeWithUnicode() {
  242. assertNormalized("f\u00f6\u00f6/.b\u00e0r/*[<>|^~/b\u00e9\\z",
  243. "f\u00f6\u00f6/b\u00e0r/b\u00e9-z");
  244. assertNormalized("\u5165\u53e3 entrance;/.\u51fa\u53e3_*ex*it*/",
  245. "\u5165\u53e3_entrance;/\u51fa\u53e3_ex-it");
  246. }
  247. @Test
  248. public void testNormalizeAlreadyValidRefName() {
  249. assertNormalized("refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
  250. assertNormalized("refs/tags/v1.0-20170223", "refs/tags/v1.0-20170223");
  251. }
  252. @Test
  253. public void testNormalizeTrimmedUnicodeAlreadyValidRefName() {
  254. assertNormalized(" \u00e5ngstr\u00f6m\t", "\u00e5ngstr\u00f6m");
  255. }
  256. }