improve coverage (#168)

This commit is contained in:
Alessandro Ros
2023-01-06 23:34:10 +01:00
committed by GitHub
parent a759ba9d01
commit 2e88705875
195 changed files with 535 additions and 63 deletions

View File

@@ -13,6 +13,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
with:
go-version: "1.19"
- uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1

View File

@@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
go: ["1.17", "1.18", "1.19"]
go: ["1.18", "1.19"]
steps:
- uses: actions/checkout@v2

View File

@@ -1,4 +1,4 @@
BASE_IMAGE = golang:1.17-alpine3.14
BASE_IMAGE = golang:1.19-alpine3.16
LINT_IMAGE = golangci/golangci-lint:v1.50.1
.PHONY: $(shell ls)

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"sync"
@@ -86,6 +87,19 @@ func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (
sh.publisher.Close()
}
var forma *format.H265
medi := ctx.Medias.FindFormat(&forma)
if medi == nil {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, fmt.Errorf("H264 media not found")
}
for _, x := range forma.PPS {
fmt.Printf("0x%.2x, ", x)
}
fmt.Println("")
// create the stream and save the publisher
sh.stream = gortsplib.NewServerStream(ctx.Medias)
sh.publisher = ctx.Session

View File

@@ -47,7 +47,7 @@ outer:
}
if (n + 1) > MaxNALUsPerGroup {
return nil, fmt.Errorf("number of NALUs contained inside a single group (%d) is too big (maximum is %d)",
return nil, fmt.Errorf("NALU count (%d) exceeds maximum allowed (%d)",
n+1, MaxNALUsPerGroup)
}

View File

@@ -1,6 +1,7 @@
package h264
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
@@ -125,6 +126,11 @@ func TestAnnexBUnmarshalError(t *testing.T) {
[]byte{0x00, 0x00, 0x01, 0xaa, 0x00, 0x00, 0x01},
"empty NALU",
},
{
"too many nalus",
bytes.Repeat([]byte{0x00, 0x00, 0x01, 0x0a}, 21),
"NALU count (21) exceeds maximum allowed (20)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, err := AnnexBUnmarshal(ca.enc)

View File

@@ -27,7 +27,7 @@ func AVCCUnmarshal(buf []byte) ([][]byte, error) {
}
if (len(ret) + 1) > MaxNALUsPerGroup {
return nil, fmt.Errorf("number of NALUs contained inside a single group (%d) is too big (maximum is %d)",
return nil, fmt.Errorf("NALU count (%d) exceeds maximum allowed (%d)",
len(ret)+1, MaxNALUsPerGroup)
}

View File

@@ -1,6 +1,7 @@
package h264
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
@@ -63,23 +64,32 @@ func TestAVCCUnmarshalError(t *testing.T) {
for _, ca := range []struct {
name string
enc []byte
err string
}{
{
"empty",
[]byte{},
"invalid length",
},
{
"invalid length",
[]byte{0x01},
"invalid length",
},
{
"invalid length",
[]byte{0x00, 0x00, 0x00, 0x03},
"invalid length",
},
{
"too many nalus",
bytes.Repeat([]byte{0x00, 0x00, 0x00, 0x01, 0x0a}, 21),
"NALU count (21) exceeds maximum allowed (20)",
},
} {
t.Run(ca.name, func(t *testing.T) {
_, err := AVCCUnmarshal(ca.enc)
require.Error(t, err)
require.EqualError(t, err, ca.err)
})
}
}

View File

@@ -8,6 +8,10 @@ import (
)
func getPictureOrderCount(buf []byte, sps *SPS) (uint32, error) {
if len(buf) < 6 {
return 0, fmt.Errorf("not enough bits")
}
buf = EmulationPreventionRemove(buf[:6])
buf = buf[1:]

View File

@@ -165,3 +165,21 @@ func TestDTSExtractor(t *testing.T) {
})
}
}
func FuzzDTSExtractor(f *testing.F) {
ex := NewDTSExtractor()
f.Fuzz(func(t *testing.T, b []byte, p uint64) {
if len(b) < 1 {
return
}
ex.Extract([][]byte{
{ // SPS
0x27, 0x64, 0x00, 0x20, 0xac, 0x52, 0x18, 0x0f,
0x01, 0x17, 0xef, 0xff, 0x00, 0x01, 0x00, 0x01,
0x6a, 0x02, 0x02, 0x03, 0x6d, 0x85, 0x6b, 0xde,
0xf8, 0x08,
},
b,
}, time.Duration(p))
})
}

View File

@@ -450,3 +450,10 @@ func BenchmarkSPSUnmarshal(b *testing.B) {
})
}
}
func FuzzSPSUnmarshal(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
var sps SPS
sps.Unmarshal(b)
})
}

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A0\x00\x0000")
uint64(122)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("'000\xdc10")
uint64(23)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A00\x0000")
uint64(122)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A\x0001\x000")
uint64(188)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A00000")
uint64(122)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("%")
uint64(14)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("\x01\f")
uint64(60)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A\x00\x00\x0000")
uint64(70)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("A\x0001B0")
uint64(188)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("'000%9000")
uint64(23)

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf31Y08\xf7000000000\xd7")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff2\xff0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000B19")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z0010")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf60000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000772.B001")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z00002")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf6A")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z00$A0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017Y1")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A10")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000777Z\x0e2\x0e000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000%0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z007A")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017Y0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000177A000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z007B000000771B$00")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000\xff2")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000%A")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff2")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf6017")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z0017")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf61")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff0\xff0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z002")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000X")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000072")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017\xf60")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017\xf61")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017Y00")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf310\xff00007000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf68")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000\xff\xff\xff0a1000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A72")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff2&0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf310\xff00007")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A 0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf67")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000177Z100")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf30")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000A78X0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf60")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z00d")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000070")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000\xff0A0000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf31")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf6\xf60000000001")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00007\xb6\xf6018")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff20")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017\xf6")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000%\xf6")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\xf6\xf60\xf6000110")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0z00$002")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("00000A\xff2Z0")

View File

@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000017$")

View File

@@ -9,6 +9,10 @@ import (
)
func getPictureOrderCount(buf []byte, sps *SPS, pps *PPS) (uint32, uint32, error) {
if len(buf) < 12 {
return 0, 0, fmt.Errorf("not enough bits")
}
buf = h264.EmulationPreventionRemove(buf[:12])
typ := NALUType((buf[0] >> 1) & 0b111111)
@@ -90,9 +94,15 @@ func getPictureOrderCount(buf []byte, sps *SPS, pps *PPS) (uint32, uint32, error
if typ == NALUType_TRAIL_N || typ == NALUType_RASL_N {
v = sps.MaxNumReorderPics[0] - uint32(len(rps.DeltaPocS1Minus1))
} else if typ == NALUType_TRAIL_R || typ == NALUType_RASL_R {
if len(rps.DeltaPocS0Minus1) == 0 {
return 0, 0, fmt.Errorf("invalid delta_poc_s0_minus1")
}
v = rps.DeltaPocS0Minus1[0] + sps.MaxNumReorderPics[0] - 1
}
} else { // I or P-frame
if len(rps.DeltaPocS0Minus1) == 0 {
return 0, 0, fmt.Errorf("invalid delta_poc_s0_minus1")
}
v = rps.DeltaPocS0Minus1[0] + sps.MaxNumReorderPics[0]
}

View File

@@ -56,3 +56,26 @@ func TestDTSExtractor(t *testing.T) {
})
}
}
func FuzzDTSExtractor(f *testing.F) {
sps := []byte{
0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03,
0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
0x00, 0x78, 0xa0, 0x03, 0xc0, 0x80, 0x10, 0xe5,
0x96, 0x66, 0x69, 0x24, 0xca, 0xe0, 0x10, 0x00,
0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, 0x01,
0xe0, 0x80,
}
pps := []byte{
0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40,
}
ex := NewDTSExtractor()
f.Fuzz(func(t *testing.T, b []byte, p uint64) {
if len(b) < 1 {
return
}
ex.Extract([][]byte{sps, pps, b}, time.Duration(p))
})
}

View File

@@ -18,12 +18,12 @@ type PPS struct {
// Unmarshal decodes a PPS.
func (p *PPS) Unmarshal(buf []byte) error {
buf = h264.EmulationPreventionRemove(buf)
if len(buf) < 2 {
return fmt.Errorf("not enough bits")
}
buf = h264.EmulationPreventionRemove(buf)
buf = buf[2:]
pos := 0
@@ -38,21 +38,14 @@ func (p *PPS) Unmarshal(buf []byte) error {
return err
}
p.DependentSliceSegmentsEnabledFlag, err = bits.ReadFlag(buf, &pos)
err = bits.HasSpace(buf, pos, 5)
if err != nil {
return err
}
p.OutputFlagPresentFlag, err = bits.ReadFlag(buf, &pos)
if err != nil {
return err
}
tmp, err := bits.ReadBits(buf, &pos, 3)
if err != nil {
return err
}
p.NumExtraSliceHeaderBits = uint8(tmp)
p.DependentSliceSegmentsEnabledFlag = bits.ReadFlagUnsafe(buf, &pos)
p.OutputFlagPresentFlag = bits.ReadFlagUnsafe(buf, &pos)
p.NumExtraSliceHeaderBits = uint8(bits.ReadBitsUnsafe(buf, &pos, 3))
return nil
}

View File

@@ -0,0 +1,37 @@
package h265
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestPPSUnmarshal(t *testing.T) {
for _, ca := range []struct {
name string
byts []byte
pps PPS
}{
{
"default",
[]byte{
0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40,
},
PPS{},
},
} {
t.Run(ca.name, func(t *testing.T) {
var pps PPS
err := pps.Unmarshal(ca.byts)
require.NoError(t, err)
require.Equal(t, ca.pps, pps)
})
}
}
func FuzzPPSUnmarshal(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
var pps PPS
pps.Unmarshal(b)
})
}

View File

@@ -639,9 +639,9 @@ func (s *SPS) Unmarshal(buf []byte) error {
start = s.MaxSubLayersMinus1
}
s.MaxDecPicBufferingMinus1 = make([]uint32, s.MaxSubLayersMinus1-start+1)
s.MaxNumReorderPics = make([]uint32, s.MaxSubLayersMinus1-start+1)
s.MaxLatencyIncreasePlus1 = make([]uint32, s.MaxSubLayersMinus1-start+1)
s.MaxDecPicBufferingMinus1 = make([]uint32, s.MaxSubLayersMinus1+1)
s.MaxNumReorderPics = make([]uint32, s.MaxSubLayersMinus1+1)
s.MaxLatencyIncreasePlus1 = make([]uint32, s.MaxSubLayersMinus1+1)
for i := start; i <= s.MaxSubLayersMinus1; i++ {
s.MaxDecPicBufferingMinus1[i], err = bits.ReadGolombUnsigned(buf, &pos)

View File

@@ -382,3 +382,10 @@ func TestSPSUnmarshal(t *testing.T) {
})
}
}
func FuzzSPSUnmarshal(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
var sps SPS
sps.Unmarshal(b)
})
}

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*0\xc6000000000")
uint64(87)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*0\x80\x00\x00\x00\x0000000")
uint64(2)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("0")
uint64(0)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("\x000\x920!0000000")
uint64(156)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*")
uint64(34)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*0\x80\x00\x00\x000000 0")
uint64(3)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*0\xc6A00000000")
uint64(130)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("\x000\xb3000000000")
uint64(18)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*0\x80\x00\x00\x000000$0")
uint64(9)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("*00000000000")
uint64(6)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("0")
uint64(200)

Some files were not shown because too many files have changed in this diff Show More