don’t panic if the STK doesn’t contain any data

This commit is contained in:
Marten Seemann
2017-05-26 23:26:42 +08:00
parent ef4699adef
commit 6144ebed21
2 changed files with 14 additions and 0 deletions

View File

@@ -89,6 +89,10 @@ func encodeRemoteAddr(remoteAddr net.Addr) []byte {
// decodeRemoteAddr decodes the remote address saved in the STK
func decodeRemoteAddr(data []byte) string {
// data will never be empty for an STK that we generated. Check it to be on the safe side
if len(data) == 0 {
return ""
}
if data[0] == stkPrefixIP {
return net.IP(data[1:]).String()
}

View File

@@ -63,6 +63,16 @@ var _ = Describe("STK Generator", func() {
Expect(err).To(MatchError("rest when unpacking token: 4"))
})
// we don't generate tokens that have no data, but we should be able to handle them if we receive one for whatever reason
It("doesn't panic if a tokens has no data", func() {
t, err := asn1.Marshal(token{Data: []byte("")})
Expect(err).ToNot(HaveOccurred())
enc, err := stkGen.stkSource.NewToken(t)
Expect(err).ToNot(HaveOccurred())
_, err = stkGen.DecodeToken(enc)
Expect(err).ToNot(HaveOccurred())
})
It("works with an IPv6 addresses ", func() {
addresses := []string{
"2001:db8::68",