1
0

netutil_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package netutil
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestIsLocalHostname(t *testing.T) {
  7. tests := []struct {
  8. hostname string
  9. allowlist []string
  10. want bool
  11. }{
  12. {hostname: "localhost", want: true}, // #00
  13. {hostname: "127.0.0.1", want: true}, // #01
  14. {hostname: "::1", want: true}, // #02
  15. {hostname: "0:0:0:0:0:0:0:1", want: true}, // #03
  16. {hostname: "127.0.0.95", want: true}, // #04
  17. {hostname: "0.0.0.0", want: true}, // #05
  18. {hostname: "192.168.123.45", want: true}, // #06
  19. {hostname: "gogs.io", want: false}, // #07
  20. {hostname: "google.com", want: false}, // #08
  21. {hostname: "165.232.140.255", want: false}, // #09
  22. {hostname: "192.168.123.45", allowlist: []string{"10.0.0.17"}, want: true}, // #10
  23. {hostname: "gogs.local", allowlist: []string{"gogs.local"}, want: false}, // #11
  24. {hostname: "192.168.123.45", allowlist: []string{"*"}, want: false}, // #12
  25. }
  26. for _, test := range tests {
  27. t.Run("", func(t *testing.T) {
  28. assert.Equal(t, test.want, IsBlockedLocalHostname(test.hostname, test.allowlist))
  29. })
  30. }
  31. }