gitea源码

refname_test.go 6.0KB

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