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.

sanitize_test.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "errors"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSanitizeErrorCredentialURLs(t *testing.T) {
  10. err := errors.New("error with https://a@b.com")
  11. se := SanitizeErrorCredentialURLs(err)
  12. assert.Equal(t, "error with https://"+userPlaceholder+"@b.com", se.Error())
  13. }
  14. func TestSanitizeCredentialURLs(t *testing.T) {
  15. cases := []struct {
  16. input string
  17. expected string
  18. }{
  19. {
  20. "https://github.com/go-gitea/test_repo.git",
  21. "https://github.com/go-gitea/test_repo.git",
  22. },
  23. {
  24. "https://mytoken@github.com/go-gitea/test_repo.git",
  25. "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
  26. },
  27. {
  28. "https://user:password@github.com/go-gitea/test_repo.git",
  29. "https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
  30. },
  31. {
  32. "ftp://x@",
  33. "ftp://" + userPlaceholder + "@",
  34. },
  35. {
  36. "ftp://x/@",
  37. "ftp://x/@",
  38. },
  39. {
  40. "ftp://u@x/@", // test multiple @ chars
  41. "ftp://" + userPlaceholder + "@x/@",
  42. },
  43. {
  44. "😊ftp://u@x😊", // test unicode
  45. "😊ftp://" + userPlaceholder + "@x😊",
  46. },
  47. {
  48. "://@",
  49. "://@",
  50. },
  51. {
  52. "//u:p@h", // do not process URLs without explicit scheme, they are not treated as "valid" URLs because there is no scheme context in string
  53. "//u:p@h",
  54. },
  55. {
  56. "s://u@h", // the minimal pattern to be sanitized
  57. "s://" + userPlaceholder + "@h",
  58. },
  59. {
  60. "URLs in log https://u:b@h and https://u:b@h:80/, with https://h.com and u@h.com",
  61. "URLs in log https://" + userPlaceholder + "@h and https://" + userPlaceholder + "@h:80/, with https://h.com and u@h.com",
  62. },
  63. }
  64. for n, c := range cases {
  65. result := SanitizeCredentialURLs(c.input)
  66. assert.Equal(t, c.expected, result, "case %d: error should match", n)
  67. }
  68. }