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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 org.eclipse.jgit.junit.MockSystemReader;
  46. import org.eclipse.jgit.util.SystemReader;
  47. import org.junit.Test;
  48. public class ValidRefNameTest {
  49. private static void assertValid(final boolean exp, final String name) {
  50. SystemReader instance = SystemReader.getInstance();
  51. try {
  52. setUnixSystemReader();
  53. assertEquals("\"" + name + "\"", exp,
  54. Repository.isValidRefName(name));
  55. setWindowsSystemReader();
  56. assertEquals("\"" + name + "\"", exp,
  57. Repository.isValidRefName(name));
  58. } finally {
  59. SystemReader.setInstance(instance);
  60. }
  61. }
  62. private static void setWindowsSystemReader() {
  63. SystemReader.setInstance(new MockSystemReader() {
  64. {
  65. setWindows();
  66. }
  67. });
  68. }
  69. private static void setUnixSystemReader() {
  70. SystemReader.setInstance(new MockSystemReader() {
  71. {
  72. setUnix();
  73. }
  74. });
  75. }
  76. private static void assertInvalidOnWindows(final String name) {
  77. SystemReader instance = SystemReader.getInstance();
  78. try {
  79. setUnixSystemReader();
  80. assertEquals("\"" + name + "\"", true,
  81. Repository.isValidRefName(name));
  82. setWindowsSystemReader();
  83. assertEquals("\"" + name + "\"", false,
  84. Repository.isValidRefName(name));
  85. } finally {
  86. SystemReader.setInstance(instance);
  87. }
  88. }
  89. @Test
  90. public void testEmptyString() {
  91. assertValid(false, "");
  92. assertValid(false, "/");
  93. }
  94. @Test
  95. public void testMustHaveTwoComponents() {
  96. assertValid(false, "master");
  97. assertValid(true, "heads/master");
  98. }
  99. @Test
  100. public void testValidHead() {
  101. assertValid(true, "refs/heads/master");
  102. assertValid(true, "refs/heads/pu");
  103. assertValid(true, "refs/heads/z");
  104. assertValid(true, "refs/heads/FoO");
  105. }
  106. @Test
  107. public void testValidTag() {
  108. assertValid(true, "refs/tags/v1.0");
  109. }
  110. @Test
  111. public void testNoLockSuffix() {
  112. assertValid(false, "refs/heads/master.lock");
  113. }
  114. @Test
  115. public void testNoDirectorySuffix() {
  116. assertValid(false, "refs/heads/master/");
  117. }
  118. @Test
  119. public void testNoSpace() {
  120. assertValid(false, "refs/heads/i haz space");
  121. }
  122. @Test
  123. public void testNoAsciiControlCharacters() {
  124. for (char c = '\0'; c < ' '; c++)
  125. assertValid(false, "refs/heads/mast" + c + "er");
  126. }
  127. @Test
  128. public void testNoBareDot() {
  129. assertValid(false, "refs/heads/.");
  130. assertValid(false, "refs/heads/..");
  131. assertValid(false, "refs/heads/./master");
  132. assertValid(false, "refs/heads/../master");
  133. }
  134. @Test
  135. public void testNoLeadingOrTrailingDot() {
  136. assertValid(false, ".");
  137. assertValid(false, "refs/heads/.bar");
  138. assertValid(false, "refs/heads/..bar");
  139. assertValid(false, "refs/heads/bar.");
  140. }
  141. @Test
  142. public void testContainsDot() {
  143. assertValid(true, "refs/heads/m.a.s.t.e.r");
  144. assertValid(false, "refs/heads/master..pu");
  145. }
  146. @Test
  147. public void testNoMagicRefCharacters() {
  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. assertValid(false, "refs/heads/:master");
  156. assertValid(false, ":refs/heads/master");
  157. }
  158. @Test
  159. public void testShellGlob() {
  160. assertValid(false, "refs/heads/master?");
  161. assertValid(false, "refs/heads/?master");
  162. assertValid(false, "?refs/heads/master");
  163. assertValid(false, "refs/heads/master[");
  164. assertValid(false, "refs/heads/[master");
  165. assertValid(false, "[refs/heads/master");
  166. assertValid(false, "refs/heads/master*");
  167. assertValid(false, "refs/heads/*master");
  168. assertValid(false, "*refs/heads/master");
  169. }
  170. @Test
  171. public void testValidSpecialCharacterUnixs() {
  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. assertValid(true, "refs/heads/'");
  178. assertValid(true, "refs/heads/(");
  179. assertValid(true, "refs/heads/)");
  180. assertValid(true, "refs/heads/+");
  181. assertValid(true, "refs/heads/,");
  182. assertValid(true, "refs/heads/-");
  183. assertValid(true, "refs/heads/;");
  184. assertValid(true, "refs/heads/=");
  185. assertValid(true, "refs/heads/@");
  186. assertValid(true, "refs/heads/]");
  187. assertValid(true, "refs/heads/_");
  188. assertValid(true, "refs/heads/`");
  189. assertValid(true, "refs/heads/{");
  190. assertValid(true, "refs/heads/}");
  191. // This is valid on UNIX, but not on Windows
  192. // hence we make in invalid due to non-portability
  193. //
  194. assertValid(false, "refs/heads/\\");
  195. // More invalid characters on Windows, but we allow them
  196. assertInvalidOnWindows("refs/heads/\"");
  197. assertInvalidOnWindows("refs/heads/<");
  198. assertInvalidOnWindows("refs/heads/>");
  199. assertInvalidOnWindows("refs/heads/|");
  200. }
  201. @Test
  202. public void testUnicodeNames() {
  203. assertValid(true, "refs/heads/\u00e5ngstr\u00f6m");
  204. }
  205. @Test
  206. public void testRefLogQueryIsValidRef() {
  207. assertValid(false, "refs/heads/master@{1}");
  208. assertValid(false, "refs/heads/master@{1.hour.ago}");
  209. }
  210. @Test
  211. public void testWindowsReservedNames() {
  212. // re-using code from DirCacheCheckoutTest, hence
  213. // only testing for one of the special names.
  214. assertInvalidOnWindows("refs/heads/con");
  215. assertInvalidOnWindows("refs/con/x");
  216. assertInvalidOnWindows("con/heads/x");
  217. assertValid(true, "refs/heads/conx");
  218. assertValid(true, "refs/heads/xcon");
  219. }
  220. }