portfwd_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package amneziawg
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. )
  9. func TestForwardedPortsInclude(t *testing.T) {
  10. cases := []struct {
  11. spec string
  12. port int
  13. want bool
  14. }{
  15. {"80,443", 80, true},
  16. {"80,443", 443, true},
  17. {"80,443", 8080, false},
  18. {"8000-8100", 8050, true},
  19. {"8000-8100", 7999, false},
  20. {"8000-8100", 8101, false},
  21. {"", 80, false},
  22. {"not-a-port", 80, false},
  23. }
  24. for _, c := range cases {
  25. if got := ForwardedPortsInclude(c.spec, c.port); got != c.want {
  26. t.Errorf("ForwardedPortsInclude(%q, %d) = %v, want %v", c.spec, c.port, got, c.want)
  27. }
  28. }
  29. }
  30. func TestExpandForwardedPorts(t *testing.T) {
  31. cases := []struct {
  32. name string
  33. spec string
  34. want []int
  35. }{
  36. {"empty", "", nil},
  37. {"malformed", "not-a-port", nil},
  38. {"single ports", "443,80", []int{80, 443}},
  39. {"a range", "8000-8003", []int{8000, 8001, 8002, 8003}},
  40. {
  41. "overlapping-but-distinct ranges dedupe and merge",
  42. "80-90,85-95",
  43. []int{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95},
  44. },
  45. {"mixed single ports and a range, unsorted input", "443,80-82,80", []int{80, 81, 82, 443}},
  46. }
  47. for _, c := range cases {
  48. t.Run(c.name, func(t *testing.T) {
  49. got := ExpandForwardedPorts(c.spec)
  50. if len(got) == 0 && len(c.want) == 0 {
  51. return
  52. }
  53. if !reflect.DeepEqual(got, c.want) {
  54. t.Errorf("ExpandForwardedPorts(%q) = %v, want %v", c.spec, got, c.want)
  55. }
  56. })
  57. }
  58. }
  59. func TestExpandForwardedPortsCapsAtMaxForwardedPorts(t *testing.T) {
  60. got := ExpandForwardedPorts("1-200")
  61. if len(got) != MaxForwardedPorts {
  62. t.Fatalf("len(ExpandForwardedPorts(\"1-200\")) = %d, want %d", len(got), MaxForwardedPorts)
  63. }
  64. for i, port := range got {
  65. if want := i + 1; port != want {
  66. t.Fatalf("ExpandForwardedPorts(\"1-200\")[%d] = %d, want %d (expansion must stop at the cap, not truncate after expanding fully)", i, port, want)
  67. }
  68. }
  69. }
  70. func TestExpandForwardedPortsCapAppliesAcrossMultipleSpecs(t *testing.T) {
  71. // A spec whose total span far exceeds the cap, split across many
  72. // individually-small tokens -- proves the cap is enforced cumulatively
  73. // across specs, not reset (or bypassed) per spec.
  74. tokens := make([]string, 150)
  75. for i := range tokens {
  76. tokens[i] = strconv.Itoa(10000 + i)
  77. }
  78. spec := strings.Join(tokens, ",")
  79. got := ExpandForwardedPorts(spec)
  80. if len(got) != MaxForwardedPorts {
  81. t.Fatalf("len(ExpandForwardedPorts(150 distinct single ports)) = %d, want %d", len(got), MaxForwardedPorts)
  82. }
  83. }
  84. func TestExceedsForwardedPortsCap(t *testing.T) {
  85. atCap := fmt.Sprintf("1-%d", MaxForwardedPorts)
  86. if ExceedsForwardedPortsCap(atCap) {
  87. t.Fatalf("a spec covering exactly %d ports is AT the cap, not over it", MaxForwardedPorts)
  88. }
  89. overCap := fmt.Sprintf("1-%d", MaxForwardedPorts+1)
  90. if !ExceedsForwardedPortsCap(overCap) {
  91. t.Fatalf("a spec covering %d ports must be reported as exceeding the cap", MaxForwardedPorts+1)
  92. }
  93. if ExceedsForwardedPortsCap("1-10") {
  94. t.Fatal("a small spec must not be reported as exceeding the cap")
  95. }
  96. }