Add fuzz test and fix panics found so far

This commit is contained in:
Josh Deprez 2024-04-24 12:18:22 +10:00
parent 9c3fddd7c6
commit d3e10d9aa0
Signed by: josh
SSH Key Fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
3 changed files with 30 additions and 4 deletions

25
aurp/aurp_test.go Normal file
View File

@ -0,0 +1,25 @@
/*
Copyright 2024 Josh Deprez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aurp
import "testing"
func FuzzParsePacket(f *testing.F) {
f.Fuzz(func(t *testing.T, p []byte) {
_, _, _ = ParsePacket(p)
})
}

View File

@ -109,12 +109,13 @@ func parseOptionTuple(p []byte) (OptionTuple, []byte, error) {
return OptionTuple{}, p, fmt.Errorf("insufficient input length %d for option tuple", len(p))
}
olen := int(p[0]) + 1
p = p[1:]
if len(p) < olen {
return OptionTuple{}, p, fmt.Errorf("insufficient input for option tuple data length %d", olen)
}
return OptionTuple{
Type: OptionType(p[1]),
Data: p[2:olen],
Type: OptionType(p[0]),
Data: p[1:olen],
}, p[olen:], nil
}

View File

@ -65,9 +65,9 @@ func parseZIReqPacket(p []byte) (*ZIReqPacket, error) {
return nil, fmt.Errorf("odd number of bytes %d for networks", len(p))
}
c := len(p) / 2
ns := make([]ddp.Network, 0, c)
ns := make([]ddp.Network, c)
for i := range c {
ns[i] = ddp.Network(binary.BigEndian.Uint16(p[i*2:][:2]))
ns[i] = ddp.Network(binary.BigEndian.Uint16(p[2*i:][:2]))
}
return &ZIReqPacket{
Subcode: SubcodeZoneInfoReq,