HostAddress_test.go 777 B

1234567891011121314151617181920212223242526272829
  1. package types
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "gopkg.in/jcmturner/gokrb5.v7/iana/addrtype"
  7. )
  8. func TestGetHostAddress(t *testing.T) {
  9. tests := []struct {
  10. str string
  11. ipType int32
  12. hex string
  13. }{
  14. {"192.168.1.100", addrtype.IPv4, "c0a80164"},
  15. {"127.0.0.1", addrtype.IPv4, "7f000001"},
  16. {"[fe80::1cf3:b43b:df29:d43e]", addrtype.IPv6, "fe800000000000001cf3b43bdf29d43e"},
  17. }
  18. for _, test := range tests {
  19. h, err := GetHostAddress(test.str + ":1234")
  20. if err != nil {
  21. t.Errorf("error getting host for %s: %v", test.str, err)
  22. }
  23. assert.Equal(t, test.ipType, h.AddrType, "wrong address type for %s", test.str)
  24. assert.Equal(t, test.hex, hex.EncodeToString(h.Address), "wrong address bytes for %s", test.str)
  25. }
  26. }