netutil_test.go 1.3 KB

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