netutil_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. // Copyright 2025 Huan-Gogs Authors. All rights reserved.
  5. // Use of this source code is governed by a MIT-style
  6. // license that can be found in the LICENSE file.
  7. package netutil
  8. import (
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestIsLocalHostname(t *testing.T) {
  13. tests := []struct {
  14. hostname string
  15. allowlist []string
  16. want bool
  17. }{
  18. {hostname: "localhost", want: true}, // #00
  19. {hostname: "127.0.0.1", want: true}, // #01
  20. {hostname: "::1", want: true}, // #02
  21. {hostname: "0:0:0:0:0:0:0:1", want: true}, // #03
  22. {hostname: "127.0.0.95", want: true}, // #04
  23. {hostname: "0.0.0.0", want: true}, // #05
  24. {hostname: "192.168.123.45", want: true}, // #06
  25. {hostname: "gogs.io", want: false}, // #07
  26. {hostname: "google.com", want: false}, // #08
  27. {hostname: "165.232.140.255", want: false}, // #09
  28. {hostname: "192.168.123.45", allowlist: []string{"10.0.0.17"}, want: true}, // #10
  29. {hostname: "gogs.local", allowlist: []string{"gogs.local"}, want: false}, // #11
  30. {hostname: "192.168.123.45", allowlist: []string{"*"}, want: false}, // #12
  31. }
  32. for _, test := range tests {
  33. t.Run("", func(t *testing.T) {
  34. assert.Equal(t, test.want, IsBlockedLocalHostname(test.hostname, test.allowlist))
  35. })
  36. }
  37. }