APL: adjust error handling and tests (#1302)

This commit is contained in:
Tom Sellers 2021-10-12 06:06:29 -05:00 committed by GitHub
parent 1630ffe2ca
commit 7318b01e11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View File

@ -781,6 +781,8 @@ func unpackDataAplPrefix(msg []byte, off int) (APLPrefix, int, error) {
if off+afdlen > len(msg) { if off+afdlen > len(msg) {
return APLPrefix{}, len(msg), &Error{err: "overflow unpacking APL address"} return APLPrefix{}, len(msg), &Error{err: "overflow unpacking APL address"}
} }
// Address MUST NOT contain trailing zero bytes per RFC3123 Sections 4.1 and 4.2.
off += copy(ip, msg[off:off+afdlen]) off += copy(ip, msg[off:off+afdlen])
if afdlen > 0 { if afdlen > 0 {
last := ip[afdlen-1] last := ip[afdlen-1]
@ -792,10 +794,6 @@ func unpackDataAplPrefix(msg []byte, off int) (APLPrefix, int, error) {
IP: ip, IP: ip,
Mask: net.CIDRMask(int(prefix), 8*len(ip)), Mask: net.CIDRMask(int(prefix), 8*len(ip)),
} }
network := ipnet.IP.Mask(ipnet.Mask)
if !network.Equal(ipnet.IP) {
return APLPrefix{}, len(msg), &Error{err: "invalid APL address length"}
}
return APLPrefix{ return APLPrefix{
Negation: (nlen & 0x80) != 0, Negation: (nlen & 0x80) != 0,

View File

@ -391,34 +391,42 @@ func TestUnpackDataAplPrefix_Errors(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
wire []byte wire []byte
want string
}{ }{
{ {
"incomplete header", "incomplete header",
[]byte{0x00, 0x01, 0x18}, []byte{0x00, 0x01, 0x18},
"dns: overflow unpacking APL prefix",
}, },
{ {
"unrecognized family", "unrecognized family",
[]byte{0x00, 0x03, 0x00, 0x00}, []byte{0x00, 0x03, 0x00, 0x00},
"dns: unrecognized APL address family",
}, },
{ {
"prefix length exceeded", "prefix too large for family IPv4",
[]byte{0x00, 0x01, 0x21, 0x04, 192, 0, 2, 0}, []byte{0x00, 0x01, 0x21, 0x04, 192, 0, 2, 0},
"dns: APL prefix too long",
}, },
{ {
"address with extra byte", "prefix too large for family IPv6",
[]byte{0x00, 0x01, 0x10, 0x03, 192, 0, 2}, []byte{0x00, 0x02, 0x81, 0x85, 0x20, 0x01, 0x0d, 0xb8, 0x80},
"dns: APL prefix too long",
}, },
{ {
"incomplete buffer", "afdlen too long for address family IPv4",
[]byte{0x00, 0x01, 0x10, 0x02, 192},
},
{
"extra bits set",
[]byte{0x00, 0x01, 22, 0x03, 192, 0, 2},
},
{
"afdlen invalid",
[]byte{0x00, 0x01, 22, 0x05, 192, 0, 2, 0, 0}, []byte{0x00, 0x01, 22, 0x05, 192, 0, 2, 0, 0},
"dns: APL length too long",
},
{
"overflow unpacking APL address",
[]byte{0x00, 0x01, 0x10, 0x02, 192},
"dns: overflow unpacking APL address",
},
{
"address included trailing zeros",
[]byte{0x00, 0x01, 0x10, 0x04, 192, 0, 2, 0},
"dns: extra APL address bits",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -427,6 +435,10 @@ func TestUnpackDataAplPrefix_Errors(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("expected error, got none") t.Fatal("expected error, got none")
} }
if err.Error() != tt.want {
t.Errorf("expected %s, got %s", tt.want, err.Error())
}
}) })
} }
} }