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.6KB

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