Skip to content

Commit

Permalink
APL: adjust error handling and tests (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsellers-r7 authored Oct 12, 2021
1 parent 1630ffe commit 7318b01
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 2 additions & 4 deletions msg_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ func unpackDataAplPrefix(msg []byte, off int) (APLPrefix, int, error) {
if off+afdlen > len(msg) {
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])
if afdlen > 0 {
last := ip[afdlen-1]
Expand All @@ -792,10 +794,6 @@ func unpackDataAplPrefix(msg []byte, off int) (APLPrefix, int, error) {
IP: 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{
Negation: (nlen & 0x80) != 0,
Expand Down
30 changes: 21 additions & 9 deletions msg_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,34 +391,42 @@ func TestUnpackDataAplPrefix_Errors(t *testing.T) {
tests := []struct {
name string
wire []byte
want string
}{
{
"incomplete header",
[]byte{0x00, 0x01, 0x18},
"dns: overflow unpacking APL prefix",
},
{
"unrecognized family",
[]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},
"dns: APL prefix too long",
},
{
"address with extra byte",
[]byte{0x00, 0x01, 0x10, 0x03, 192, 0, 2},
"prefix too large for family IPv6",
[]byte{0x00, 0x02, 0x81, 0x85, 0x20, 0x01, 0x0d, 0xb8, 0x80},
"dns: APL prefix too long",
},
{
"incomplete buffer",
[]byte{0x00, 0x01, 0x10, 0x02, 192},
"afdlen too long for address family IPv4",
[]byte{0x00, 0x01, 22, 0x05, 192, 0, 2, 0, 0},
"dns: APL length too long",
},
{
"extra bits set",
[]byte{0x00, 0x01, 22, 0x03, 192, 0, 2},
"overflow unpacking APL address",
[]byte{0x00, 0x01, 0x10, 0x02, 192},
"dns: overflow unpacking APL address",
},
{
"afdlen invalid",
[]byte{0x00, 0x01, 22, 0x05, 192, 0, 2, 0, 0},
"address included trailing zeros",
[]byte{0x00, 0x01, 0x10, 0x04, 192, 0, 2, 0},
"dns: extra APL address bits",
},
}
for _, tt := range tests {
Expand All @@ -427,6 +435,10 @@ func TestUnpackDataAplPrefix_Errors(t *testing.T) {
if err == nil {
t.Fatal("expected error, got none")
}

if err.Error() != tt.want {
t.Errorf("expected %s, got %s", tt.want, err.Error())
}
})
}
}
Expand Down

0 comments on commit 7318b01

Please sign in to comment.