Skip to content

Commit

Permalink
fix HostAddress generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmturner committed Dec 28, 2018
1 parent 46e0011 commit f0f1114
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
8 changes: 3 additions & 5 deletions types/HostAddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,19 @@ func GetHostAddress(s string) (HostAddress, error) {
return h, fmt.Errorf("invalid format of client address: %v", err)
}
ip := net.ParseIP(cAddr)
hb, err := ip.MarshalText()
if err != nil {
return h, fmt.Errorf("could not marshal client's address into bytes: %v", err)
}
var ht int32
if ip.To4() != nil {
ht = addrtype.IPv4
ip = ip.To4()
} else if ip.To16() != nil {
ht = addrtype.IPv6
ip = ip.To16()
} else {
return h, fmt.Errorf("could not determine client's address types: %v", err)
}
h = HostAddress{
AddrType: ht,
Address: hb,
Address: ip,
}
return h, nil
}
Expand Down
29 changes: 29 additions & 0 deletions types/HostAddress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package types

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/jcmturner/gokrb5.v6/iana/addrtype"
)

func TestGetHostAddress(t *testing.T) {
tests := []struct {
str string
ipType int32
hex string
}{
{"192.168.1.100", addrtype.IPv4, "c0a80164"},
{"127.0.0.1", addrtype.IPv4, "7f000001"},
{"[fe80::1cf3:b43b:df29:d43e]", addrtype.IPv6, "fe800000000000001cf3b43bdf29d43e"},
}
for _, test := range tests {
h, err := GetHostAddress(test.str + ":1234")
if err != nil {
t.Errorf("error getting host for %s: %v", test.str, err)
}
assert.Equal(t, test.ipType, h.AddrType, "wrong address type for %s", test.str)
assert.Equal(t, test.hex, hex.EncodeToString(h.Address), "wrong address bytes for %s", test.str)
}
}

0 comments on commit f0f1114

Please sign in to comment.