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