backup_filename_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package service
  2. import (
  3. "regexp"
  4. "testing"
  5. )
  6. // getDb (controller) only accepts a Content-Disposition filename matching this
  7. // pattern, so every sanitizeBackupHost output must satisfy it.
  8. var backupFilenameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-.]+$`)
  9. func TestSanitizeBackupHost(t *testing.T) {
  10. cases := []struct {
  11. name string
  12. in string
  13. want string
  14. }{
  15. {"domain", "panel.example.com", "panel.example.com"},
  16. {"ipv4", "203.0.113.5", "203.0.113.5"},
  17. {"ipv6", "2001:db8::1", "2001-db8--1"},
  18. {"ipv6 bracketed", "[fe80::1]", "fe80--1"},
  19. {"domain with port", "example.com:8443", "example.com-8443"},
  20. {"trims edge dots and dashes", "-.example.com.-", "example.com"},
  21. {"empty falls back", "", "x-ui"},
  22. {"all invalid falls back", ":::", "x-ui"},
  23. }
  24. for _, tc := range cases {
  25. t.Run(tc.name, func(t *testing.T) {
  26. got := sanitizeBackupHost(tc.in)
  27. if got != tc.want {
  28. t.Errorf("sanitizeBackupHost(%q) = %q, want %q", tc.in, got, tc.want)
  29. }
  30. if !backupFilenameRegex.MatchString(got) {
  31. t.Errorf("sanitizeBackupHost(%q) = %q, not a valid download filename", tc.in, got)
  32. }
  33. })
  34. }
  35. }