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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import static org.eclipse.jgit.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertEquals;
  46. import org.eclipse.jgit.junit.MockSystemReader;
  47. import org.eclipse.jgit.util.SystemReader;
  48. import org.junit.Test;
  49. public class ValidRefNameTest {
  50. private static void assertValid(final boolean exp, final String name) {
  51. SystemReader instance = SystemReader.getInstance();
  52. try {
  53. setUnixSystemReader();
  54. assertEquals("\"" + name + "\"", exp,
  55. Repository.isValidRefName(name));
  56. setWindowsSystemReader();
  57. assertEquals("\"" + name + "\"", exp,
  58. Repository.isValidRefName(name));
  59. } finally {
  60. SystemReader.setInstance(instance);
  61. }
  62. }
  63. private static void setWindowsSystemReader() {
  64. SystemReader.setInstance(new MockSystemReader() {
  65. {
  66. setWindows();
  67. }
  68. });
  69. }
  70. private static void setUnixSystemReader() {
  71. SystemReader.setInstance(new MockSystemReader() {
  72. {
  73. setUnix();
  74. }
  75. });
  76. }
  77. private static void assertInvalidOnWindows(final String name) {
  78. SystemReader instance = SystemReader.getInstance();
  79. try {
  80. setUnixSystemReader();
  81. assertEquals("\"" + name + "\"", true,
  82. Repository.isValidRefName(name));
  83. setWindowsSystemReader();
  84. assertEquals("\"" + name + "\"", false,
  85. Repository.isValidRefName(name));
  86. } finally {
  87. SystemReader.setInstance(instance);
  88. }
  89. }
  90. private static void assertNormalized(final String name,
  91. final String expected) {
  92. SystemReader instance = SystemReader.getInstance();
  93. try {
  94. setUnixSystemReader();
  95. String normalized = Repository.normalizeBranchName(name);
  96. assertEquals("Normalization of " + name, expected, normalized);
  97. assertEquals("\"" + normalized + "\"", true,
  98. Repository.isValidRefName(Constants.R_HEADS + normalized));
  99. setWindowsSystemReader();
  100. normalized = Repository.normalizeBranchName(name);
  101. assertEquals("Normalization of " + name, expected, normalized);
  102. assertEquals("\"" + normalized + "\"", true,
  103. Repository.isValidRefName(Constants.R_HEADS + normalized));
  104. } finally {
  105. SystemReader.setInstance(instance);
  106. }
  107. }
  108. @Test
  109. public void testEmptyString() {
  110. assertValid(false, "");
  111. assertValid(false, "/");
  112. }
  113. @Test
  114. public void testMustHaveTwoComponents() {
  115. assertValid(false, "master");
  116. assertValid(true, "heads/master");
  117. }
  118. @Test
  119. public void testValidHead() {
  120. assertValid(true, "refs/heads/master");
  121. assertValid(true, "refs/heads/pu");
  122. assertValid(true, "refs/heads/z");
  123. assertValid(true, "refs/heads/FoO");
  124. }
  125. @Test
  126. public void testValidTag() {
  127. assertValid(true, "refs/tags/v1.0");
  128. }
  129. @Test
  130. public void testNoLockSuffix() {
  131. assertValid(false, "refs/heads/master.lock");
  132. }
  133. @Test
  134. public void testNoDirectorySuffix() {
  135. assertValid(false, "refs/heads/master/");
  136. }
  137. @Test
  138. public void testNoSpace() {
  139. assertValid(false, "refs/heads/i haz space");
  140. }
  141. @Test
  142. public void testNoAsciiControlCharacters() {
  143. for (char c = '\0'; c < ' '; c++)
  144. assertValid(false, "refs/heads/mast" + c + "er");
  145. }
  146. @Test
  147. public void testNoBareDot() {
  148. assertValid(false, "refs/heads/.");
  149. assertValid(false, "refs/heads/..");
  150. assertValid(false, "refs/heads/./master");
  151. assertValid(false, "refs/heads/../master");
  152. }
  153. @Test
  154. public void testNoLeadingOrTrailingDot() {
  155. assertValid(false, ".");
  156. assertValid(false, "refs/heads/.bar");
  157. assertValid(false, "refs/heads/..bar");
  158. assertValid(false, "refs/heads/bar.");
  159. }
  160. @Test
  161. public void testContainsDot() {
  162. assertValid(true, "refs/heads/m.a.s.t.e.r");
  163. assertValid(false, "refs/heads/master..pu");
  164. }
  165. @Test
  166. public void testNoMagicRefCharacters() {
  167. assertValid(false, "refs/heads/master^");
  168. assertValid(false, "refs/heads/^master");
  169. assertValid(false, "^refs/heads/master");
  170. assertValid(false, "refs/heads/master~");
  171. assertValid(false, "refs/heads/~master");
  172. assertValid(false, "~refs/heads/master");
  173. assertValid(false, "refs/heads/master:");
  174. assertValid(false, "refs/heads/:master");
  175. assertValid(false, ":refs/heads/master");
  176. }
  177. @Test
  178. public void testShellGlob() {
  179. assertValid(false, "refs/heads/master?");
  180. assertValid(false, "refs/heads/?master");
  181. assertValid(false, "?refs/heads/master");
  182. assertValid(false, "refs/heads/master[");
  183. assertValid(false, "refs/heads/[master");
  184. assertValid(false, "[refs/heads/master");
  185. assertValid(false, "refs/heads/master*");
  186. assertValid(false, "refs/heads/*master");
  187. assertValid(false, "*refs/heads/master");
  188. }
  189. @Test
  190. public void testValidSpecialCharacterUnixs() {
  191. assertValid(true, "refs/heads/!");
  192. assertValid(true, "refs/heads/#");
  193. assertValid(true, "refs/heads/$");
  194. assertValid(true, "refs/heads/%");
  195. assertValid(true, "refs/heads/&");
  196. assertValid(true, "refs/heads/'");
  197. assertValid(true, "refs/heads/(");
  198. assertValid(true, "refs/heads/)");
  199. assertValid(true, "refs/heads/+");
  200. assertValid(true, "refs/heads/,");
  201. assertValid(true, "refs/heads/-");
  202. assertValid(true, "refs/heads/;");
  203. assertValid(true, "refs/heads/=");
  204. assertValid(true, "refs/heads/@");
  205. assertValid(true, "refs/heads/]");
  206. assertValid(true, "refs/heads/_");
  207. assertValid(true, "refs/heads/`");
  208. assertValid(true, "refs/heads/{");
  209. assertValid(true, "refs/heads/}");
  210. // This is valid on UNIX, but not on Windows
  211. // hence we make in invalid due to non-portability
  212. //
  213. assertValid(false, "refs/heads/\\");
  214. // More invalid characters on Windows, but we allow them
  215. assertInvalidOnWindows("refs/heads/\"");
  216. assertInvalidOnWindows("refs/heads/<");
  217. assertInvalidOnWindows("refs/heads/>");
  218. assertInvalidOnWindows("refs/heads/|");
  219. }
  220. @Test
  221. public void testUnicodeNames() {
  222. assertValid(true, "refs/heads/\u00e5ngstr\u00f6m");
  223. }
  224. @Test
  225. public void testRefLogQueryIsValidRef() {
  226. assertValid(false, "refs/heads/master@{1}");
  227. assertValid(false, "refs/heads/master@{1.hour.ago}");
  228. }
  229. @Test
  230. public void testWindowsReservedNames() {
  231. // re-using code from DirCacheCheckoutTest, hence
  232. // only testing for one of the special names.
  233. assertInvalidOnWindows("refs/heads/con");
  234. assertInvalidOnWindows("refs/con/x");
  235. assertInvalidOnWindows("con/heads/x");
  236. assertValid(true, "refs/heads/conx");
  237. assertValid(true, "refs/heads/xcon");
  238. }
  239. @Test
  240. public void testNormalizeBranchName() {
  241. assertEquals(true, Repository.normalizeBranchName(null) == "");
  242. assertEquals(true, Repository.normalizeBranchName("").equals(""));
  243. assertNormalized("Bug 12345::::Hello World", "Bug_12345-Hello_World");
  244. assertNormalized("Bug 12345 :::: Hello World", "Bug_12345_Hello_World");
  245. assertNormalized("Bug 12345 :::: Hello::: World",
  246. "Bug_12345_Hello-World");
  247. assertNormalized(":::Bug 12345 - Hello World", "Bug_12345_Hello_World");
  248. assertNormalized("---Bug 12345 - Hello World", "Bug_12345_Hello_World");
  249. assertNormalized("Bug 12345 ---- Hello --- World",
  250. "Bug_12345_Hello_World");
  251. assertNormalized("Bug 12345 - Hello World!", "Bug_12345_Hello_World!");
  252. assertNormalized("Bug 12345 : Hello World!", "Bug_12345_Hello_World!");
  253. assertNormalized("Bug 12345 _ Hello World!", "Bug_12345_Hello_World!");
  254. assertNormalized("Bug 12345 - Hello World!",
  255. "Bug_12345_Hello_World!");
  256. assertNormalized(" Bug 12345 - Hello World! ",
  257. "Bug_12345_Hello_World!");
  258. assertNormalized(" Bug 12345 - Hello World! ",
  259. "Bug_12345_Hello_World!");
  260. assertNormalized("Bug 12345 - Hello______ World!",
  261. "Bug_12345_Hello_World!");
  262. assertNormalized("_Bug 12345 - Hello World!", "Bug_12345_Hello_World!");
  263. }
  264. @Test
  265. public void testNormalizeWithSlashes() {
  266. assertNormalized("foo/bar/baz", "foo/bar/baz");
  267. assertNormalized("foo/bar.lock/baz.lock", "foo/bar_lock/baz_lock");
  268. assertNormalized("foo/.git/.git~1/bar", "foo/git/git-1/bar");
  269. assertNormalized(".foo/aux/con/com3.txt/com0/prn/lpt1",
  270. "foo/+aux/+con/+com3.txt/com0/+prn/+lpt1");
  271. assertNormalized("foo/../bar///.--ba...z", "foo/bar/ba.z");
  272. }
  273. @Test
  274. public void testNormalizeWithUnicode() {
  275. assertNormalized("f\u00f6\u00f6/.b\u00e0r/*[<>|^~/b\u00e9\\z",
  276. "f\u00f6\u00f6/b\u00e0r/b\u00e9-z");
  277. assertNormalized("\u5165\u53e3 entrance;/.\u51fa\u53e3_*ex*it*/",
  278. "\u5165\u53e3_entrance;/\u51fa\u53e3_ex-it");
  279. }
  280. @Test
  281. public void testNormalizeAlreadyValidRefName() {
  282. assertNormalized("refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
  283. assertNormalized("refs/tags/v1.0-20170223", "refs/tags/v1.0-20170223");
  284. }
  285. @Test
  286. public void testNormalizeTrimmedUnicodeAlreadyValidRefName() {
  287. assertNormalized(" \u00e5ngstr\u00f6m\t", "\u00e5ngstr\u00f6m");
  288. }
  289. }