netutil_test.go 918 B

123456789101112131415161718192021222324252627282930313233343536
  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. want bool
  13. }{
  14. {hostname: "localhost", want: true},
  15. {hostname: "127.0.0.1", want: true},
  16. {hostname: "::1", want: true},
  17. {hostname: "0:0:0:0:0:0:0:1", want: true},
  18. {hostname: "fuf.me", want: true},
  19. {hostname: "127.0.0.95", want: true},
  20. {hostname: "0.0.0.0", want: true},
  21. {hostname: "192.168.123.45", want: true},
  22. {hostname: "gogs.io", want: false},
  23. {hostname: "google.com", want: false},
  24. {hostname: "165.232.140.255", want: false},
  25. }
  26. for _, test := range tests {
  27. t.Run("", func(t *testing.T) {
  28. assert.Equal(t, test.want, IsLocalHostname(test.hostname))
  29. })
  30. }
  31. }