av/codec/h264/h264dec/fuzz/fuzzParseLevelPrefix/fuzz.go

70 lines
2.1 KiB
Go
Raw Normal View History

/*
DESCRIPTION
fuzz.go provides a function with the required signature such that it may be
accessed by go-fuzz. The function will compare the output of C level_prefix
parser with a go version.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
// +build gofuzz
package fuzzParseLevelPrefix
/*
#include "../helpers.c"
#include "parse_level_prefix.h"
*/
import "C"
import (
"bytes"
"fmt"
"unsafe"
"bitbucket.org/ausocean/av/codec/h264/h264dec"
"bitbucket.org/ausocean/av/codec/h264/h264dec/bits"
)
func Fuzz(data []byte) int {
// Create C based BitReader based on data.
cbr := C.new_BitReader((*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)))
// Get the level_prefix from the C code. If got is < 0, then the C code
// doesn't like it and we shouldn't have that input in the corpus, so return -1.
got := C.read_levelprefix(cbr)
if got < 0 {
return -1
}
// Get the level_prefix from the go code. If the C code was okay with this
// input, but the go code isn't then that's bad, so panic - want crasher info.
want, err := h264dec.FuzzParseLevelPrefix(bits.NewBitReader(bytes.NewReader(data)))
if err != nil {
panic(fmt.Sprintf("error from go parseLevelPrefix: %v", err))
}
// Compare the result of the C and go code. If they are not the same then
// panic - our go code is not doing what it should.
if int(want) != int(got) {
panic(fmt.Sprintf("did not get expected results for input: %v\nGot: %v\nWant: %v\n", data, got, want))
}
return 1
}