netutil_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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},
  16. {hostname: "127.0.0.1", want: true},
  17. {hostname: "::1", want: true},
  18. {hostname: "0:0:0:0:0:0:0:1", 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. {hostname: "192.168.123.45", allowlist: []string{"10.0.0.17"}, want: true}, // #11
  26. {hostname: "gogs.local", allowlist: []string{"gogs.local"}, want: false}, // #12
  27. {hostname: "192.168.123.45", allowlist: []string{"*"}, want: false}, // #13
  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. }