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.

shellquote_test.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2020 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 util
  5. import "testing"
  6. func TestShellEscape(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. toEscape string
  10. want string
  11. }{
  12. {
  13. "Simplest case - nothing to escape",
  14. "a/b/c/d",
  15. "a/b/c/d",
  16. }, {
  17. "Prefixed tilde - with normal stuff - should not escape",
  18. "~/src/go/gitea/gitea",
  19. "~/src/go/gitea/gitea",
  20. }, {
  21. "Typical windows path with spaces - should get doublequote escaped",
  22. `C:\Program Files\Gitea v1.13 - I like lots of spaces\gitea`,
  23. `"C:\\Program Files\\Gitea v1.13 - I like lots of spaces\\gitea"`,
  24. }, {
  25. "Forward-slashed windows path with spaces - should get doublequote escaped",
  26. "C:/Program Files/Gitea v1.13 - I like lots of spaces/gitea",
  27. `"C:/Program Files/Gitea v1.13 - I like lots of spaces/gitea"`,
  28. }, {
  29. "Prefixed tilde - but then a space filled path",
  30. "~git/Gitea v1.13/gitea",
  31. `~git/"Gitea v1.13/gitea"`,
  32. }, {
  33. "Bangs are unforutunately not predictable so need to be singlequoted",
  34. "C:/Program Files/Gitea!/gitea",
  35. `'C:/Program Files/Gitea!/gitea'`,
  36. }, {
  37. "Newlines are just irritating",
  38. "/home/git/Gitea\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea",
  39. "'/home/git/Gitea\n\nWHY-WOULD-YOU-DO-THIS\n\nGitea/gitea'",
  40. }, {
  41. "Similarly we should nicely handle mutiple single quotes if we have to single-quote",
  42. "'!''!'''!''!'!'",
  43. `\''!'\'\''!'\'\'\''!'\'\''!'\''!'\'`,
  44. }, {
  45. "Double quote < ...",
  46. "~/<gitea",
  47. "~/\"<gitea\"",
  48. }, {
  49. "Double quote > ...",
  50. "~/gitea>",
  51. "~/\"gitea>\"",
  52. }, {
  53. "Double quote and escape $ ...",
  54. "~/$gitea",
  55. "~/\"\\$gitea\"",
  56. }, {
  57. "Double quote {...",
  58. "~/{gitea",
  59. "~/\"{gitea\"",
  60. }, {
  61. "Double quote }...",
  62. "~/gitea}",
  63. "~/\"gitea}\"",
  64. }, {
  65. "Double quote ()...",
  66. "~/(gitea)",
  67. "~/\"(gitea)\"",
  68. }, {
  69. "Double quote and escape `...",
  70. "~/gitea`",
  71. "~/\"gitea\\`\"",
  72. }, {
  73. "Double quotes can handle a number of things without having to escape them but not everything ...",
  74. "~/<gitea> ${gitea} `gitea` [gitea] (gitea) \"gitea\" \\gitea\\ 'gitea'",
  75. "~/\"<gitea> \\${gitea} \\`gitea\\` [gitea] (gitea) \\\"gitea\\\" \\\\gitea\\\\ 'gitea'\"",
  76. }, {
  77. "Single quotes don't need to escape except for '...",
  78. "~/<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ 'gitea'",
  79. "~/'<gitea> ${gitea} `gitea` (gitea) !gitea! \"gitea\" \\gitea\\ '\\''gitea'\\'",
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. if got := ShellEscape(tt.toEscape); got != tt.want {
  85. t.Errorf("ShellEscape(%q):\nGot: %s\nWanted: %s", tt.toEscape, got, tt.want)
  86. }
  87. })
  88. }
  89. }