1
// Copyright 2020 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
8
func TestShellEscape(t *testing.T) {
15
"Simplest case - nothing to escape",
19
"Prefixed tilde - with normal stuff - should not escape",
20
"~/src/go/gitea/gitea",
21
"~/src/go/gitea/gitea",
23
"Typical windows path with spaces - should get doublequote escaped",
24
`C:\Program Files\Gitea v1.13 - I like lots of spaces\gitea`,
25
`"C:\\Program Files\\Gitea v1.13 - I like lots of spaces\\gitea"`,
27
"Forward-slashed windows path with spaces - should get doublequote escaped",
28
"C:/Program Files/Gitea v1.13 - I like lots of spaces/gitea",
29
`"C:/Program Files/Gitea v1.13 - I like lots of spaces/gitea"`,
31
"Prefixed tilde - but then a space filled path",
32
"~git/Gitea v1.13/gitea",
33
`~git/"Gitea v1.13/gitea"`,
35
"Bangs are unfortunately not predictable so need to be singlequoted",
36
"C:/Program Files/Gitea!/gitea",
37
`'C:/Program Files/Gitea!/gitea'`,
39
"Newlines are just irritating",
40
"/home/git/Gitea\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea",
41
"'/home/git/Gitea\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea'",
43
"Similarly we should nicely handle multiple single quotes if we have to single-quote",
45
`\''!'\'\''!'\'\'\''!'\'\''!'\''!'\'`,
55
"Double quote and escape $ ...",
71
"Double quote and escape `...",
75
"Double quotes can handle a number of things without having to escape them but not everything ...",
76
"~/<gitea> ${gitea} `gitea` [gitea] (gitea) \"gitea\" \\gitea\\ 'gitea'",
77
"~/\"<gitea> \\${gitea} \\`gitea\\` [gitea] (gitea) \\\"gitea\\\" \\\\gitea\\\\ 'gitea'\"",
79
"Single quotes don't need to escape except for '...",
80
"~/<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ 'gitea'",
81
"~/'<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ '\\''gitea'\\'",
84
for _, tt := range tests {
85
t.Run(tt.name, func(t *testing.T) {
86
if got := ShellEscape(tt.toEscape); got != tt.want {
87
t.Errorf("ShellEscape(%q):\nGot: %s\nWanted: %s", tt.toEscape, got, tt.want)