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.

266 lines
5.8 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "testing"
  7. "gitea.com/macaron/binding"
  8. )
  9. var gitRefNameValidationTestCases = []validationTestCase{
  10. {
  11. description: "Referece contains only characters",
  12. data: TestForm{
  13. BranchName: "test",
  14. },
  15. expectedErrors: binding.Errors{},
  16. },
  17. {
  18. description: "Reference name contains single slash",
  19. data: TestForm{
  20. BranchName: "feature/test",
  21. },
  22. expectedErrors: binding.Errors{},
  23. },
  24. {
  25. description: "Reference name has allowed special characters",
  26. data: TestForm{
  27. BranchName: "debian/1%1.6.0-2",
  28. },
  29. expectedErrors: binding.Errors{},
  30. },
  31. {
  32. description: "Reference name contains backslash",
  33. data: TestForm{
  34. BranchName: "feature\\test",
  35. },
  36. expectedErrors: binding.Errors{
  37. binding.Error{
  38. FieldNames: []string{"BranchName"},
  39. Classification: ErrGitRefName,
  40. Message: "GitRefName",
  41. },
  42. },
  43. },
  44. {
  45. description: "Reference name starts with dot",
  46. data: TestForm{
  47. BranchName: ".test",
  48. },
  49. expectedErrors: binding.Errors{
  50. binding.Error{
  51. FieldNames: []string{"BranchName"},
  52. Classification: ErrGitRefName,
  53. Message: "GitRefName",
  54. },
  55. },
  56. },
  57. {
  58. description: "Reference name ends with dot",
  59. data: TestForm{
  60. BranchName: "test.",
  61. },
  62. expectedErrors: binding.Errors{
  63. binding.Error{
  64. FieldNames: []string{"BranchName"},
  65. Classification: ErrGitRefName,
  66. Message: "GitRefName",
  67. },
  68. },
  69. },
  70. {
  71. description: "Reference name starts with slash",
  72. data: TestForm{
  73. BranchName: "/test",
  74. },
  75. expectedErrors: binding.Errors{
  76. binding.Error{
  77. FieldNames: []string{"BranchName"},
  78. Classification: ErrGitRefName,
  79. Message: "GitRefName",
  80. },
  81. },
  82. },
  83. {
  84. description: "Reference name ends with slash",
  85. data: TestForm{
  86. BranchName: "test/",
  87. },
  88. expectedErrors: binding.Errors{
  89. binding.Error{
  90. FieldNames: []string{"BranchName"},
  91. Classification: ErrGitRefName,
  92. Message: "GitRefName",
  93. },
  94. },
  95. },
  96. {
  97. description: "Reference name ends with .lock",
  98. data: TestForm{
  99. BranchName: "test.lock",
  100. },
  101. expectedErrors: binding.Errors{
  102. binding.Error{
  103. FieldNames: []string{"BranchName"},
  104. Classification: ErrGitRefName,
  105. Message: "GitRefName",
  106. },
  107. },
  108. },
  109. {
  110. description: "Reference name contains multiple consecutive dots",
  111. data: TestForm{
  112. BranchName: "te..st",
  113. },
  114. expectedErrors: binding.Errors{
  115. binding.Error{
  116. FieldNames: []string{"BranchName"},
  117. Classification: ErrGitRefName,
  118. Message: "GitRefName",
  119. },
  120. },
  121. },
  122. {
  123. description: "Reference name contains multiple consecutive slashes",
  124. data: TestForm{
  125. BranchName: "te//st",
  126. },
  127. expectedErrors: binding.Errors{
  128. binding.Error{
  129. FieldNames: []string{"BranchName"},
  130. Classification: ErrGitRefName,
  131. Message: "GitRefName",
  132. },
  133. },
  134. },
  135. {
  136. description: "Reference name is single @",
  137. data: TestForm{
  138. BranchName: "@",
  139. },
  140. expectedErrors: binding.Errors{
  141. binding.Error{
  142. FieldNames: []string{"BranchName"},
  143. Classification: ErrGitRefName,
  144. Message: "GitRefName",
  145. },
  146. },
  147. },
  148. {
  149. description: "Reference name has @{",
  150. data: TestForm{
  151. BranchName: "branch@{",
  152. },
  153. expectedErrors: binding.Errors{
  154. binding.Error{
  155. FieldNames: []string{"BranchName"},
  156. Classification: ErrGitRefName,
  157. Message: "GitRefName",
  158. },
  159. },
  160. },
  161. {
  162. description: "Reference name has unallowed special character ~",
  163. data: TestForm{
  164. BranchName: "~debian/1%1.6.0-2",
  165. },
  166. expectedErrors: binding.Errors{
  167. binding.Error{
  168. FieldNames: []string{"BranchName"},
  169. Classification: ErrGitRefName,
  170. Message: "GitRefName",
  171. },
  172. },
  173. },
  174. {
  175. description: "Reference name has unallowed special character *",
  176. data: TestForm{
  177. BranchName: "*debian/1%1.6.0-2",
  178. },
  179. expectedErrors: binding.Errors{
  180. binding.Error{
  181. FieldNames: []string{"BranchName"},
  182. Classification: ErrGitRefName,
  183. Message: "GitRefName",
  184. },
  185. },
  186. },
  187. {
  188. description: "Reference name has unallowed special character ?",
  189. data: TestForm{
  190. BranchName: "?debian/1%1.6.0-2",
  191. },
  192. expectedErrors: binding.Errors{
  193. binding.Error{
  194. FieldNames: []string{"BranchName"},
  195. Classification: ErrGitRefName,
  196. Message: "GitRefName",
  197. },
  198. },
  199. },
  200. {
  201. description: "Reference name has unallowed special character ^",
  202. data: TestForm{
  203. BranchName: "^debian/1%1.6.0-2",
  204. },
  205. expectedErrors: binding.Errors{
  206. binding.Error{
  207. FieldNames: []string{"BranchName"},
  208. Classification: ErrGitRefName,
  209. Message: "GitRefName",
  210. },
  211. },
  212. },
  213. {
  214. description: "Reference name has unallowed special character :",
  215. data: TestForm{
  216. BranchName: "debian:jessie",
  217. },
  218. expectedErrors: binding.Errors{
  219. binding.Error{
  220. FieldNames: []string{"BranchName"},
  221. Classification: ErrGitRefName,
  222. Message: "GitRefName",
  223. },
  224. },
  225. },
  226. {
  227. description: "Reference name has unallowed special character (whitespace)",
  228. data: TestForm{
  229. BranchName: "debian jessie",
  230. },
  231. expectedErrors: binding.Errors{
  232. binding.Error{
  233. FieldNames: []string{"BranchName"},
  234. Classification: ErrGitRefName,
  235. Message: "GitRefName",
  236. },
  237. },
  238. },
  239. {
  240. description: "Reference name has unallowed special character [",
  241. data: TestForm{
  242. BranchName: "debian[jessie",
  243. },
  244. expectedErrors: binding.Errors{
  245. binding.Error{
  246. FieldNames: []string{"BranchName"},
  247. Classification: ErrGitRefName,
  248. Message: "GitRefName",
  249. },
  250. },
  251. },
  252. }
  253. func Test_GitRefNameValidation(t *testing.T) {
  254. AddBindingRules()
  255. for _, testCase := range gitRefNameValidationTestCases {
  256. t.Run(testCase.description, func(t *testing.T) {
  257. performValidationTest(t, testCase)
  258. })
  259. }
  260. }