-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathfuzz_test.go
103 lines (91 loc) · 1.71 KB
/
fuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//go:build go1.18
// +build go1.18
package mp4
import (
"bytes"
"context"
"errors"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/Eyevinn/mp4ff/bits"
)
func monitorMemory(ctx context.Context, t *testing.T, memoryLimit int) {
go func() {
timer := time.NewTicker(500 * time.Millisecond)
defer timer.Stop()
var m runtime.MemStats
for {
select {
case <-ctx.Done():
return
case <-timer.C:
runtime.ReadMemStats(&m)
if m.Alloc > uint64(memoryLimit) {
t.Logf("memory limit exceeded: %d > %d", m.Alloc, memoryLimit)
t.Fail()
return
}
}
}
}()
}
func FuzzDecodeBox(f *testing.F) {
entries, err := os.ReadDir("testdata")
if err != nil {
f.Fatal(err)
}
validExts := map[string]bool{
".mp4": true,
".m4s": true,
".cmfv": true,
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
if validExts[filepath.Ext(entry.Name())] {
testData, err := os.ReadFile("testdata/" + entry.Name())
if err != nil {
f.Fatal(err)
}
f.Add(testData)
}
}
f.Fuzz(func(t *testing.T, b []byte) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
monitorMemory(ctx, t, 500*1024*1024) // 500MB
r := bytes.NewReader(b)
var pos uint64 = 0
for {
box, err := DecodeBox(pos, r)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
break
}
}
if box == nil {
break
}
pos += box.Size()
}
pos = 0
sr := bits.NewFixedSliceReader(b)
for {
box, err := DecodeBoxSR(pos, sr)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
break
}
}
if box == nil {
break
}
pos += box.Size()
}
})
}