Skip to content

Commit c93d5c5

Browse files
authored
add fuzz tests to all headers (#522)
1 parent 55fa72f commit c93d5c5

32 files changed

+177
-326
lines changed

pkg/headers/range_test.go

+29-130
Original file line numberDiff line numberDiff line change
@@ -131,136 +131,6 @@ func TestRangeUnmarshal(t *testing.T) {
131131
}
132132
}
133133

134-
func TestRangeUnmarshalErrors(t *testing.T) {
135-
for _, ca := range []struct {
136-
name string
137-
hv base.HeaderValue
138-
err string
139-
}{
140-
{
141-
"empty",
142-
base.HeaderValue{},
143-
"value not provided",
144-
},
145-
{
146-
"2 values",
147-
base.HeaderValue{"a", "b"},
148-
"value provided multiple times ([a b])",
149-
},
150-
{
151-
"invalid keys",
152-
base.HeaderValue{`key1="k`},
153-
"apexes not closed (key1=\"k)",
154-
},
155-
{
156-
"value not found",
157-
base.HeaderValue{``},
158-
"value not found ()",
159-
},
160-
{
161-
"smpte without values",
162-
base.HeaderValue{`smpte=`},
163-
"invalid value ()",
164-
},
165-
{
166-
"smtpe end invalid",
167-
base.HeaderValue{`smpte=00:00:01-123`},
168-
"invalid SMPTE time (123)",
169-
},
170-
{
171-
"smpte invalid 1",
172-
base.HeaderValue{`smpte=123-`},
173-
"invalid SMPTE time (123)",
174-
},
175-
{
176-
"smpte invalid 2",
177-
base.HeaderValue{`smpte=aa:00:00-`},
178-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
179-
},
180-
{
181-
"smpte invalid 3",
182-
base.HeaderValue{`smpte=00:aa:00-`},
183-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
184-
},
185-
{
186-
"smpte invalid 4",
187-
base.HeaderValue{`smpte=00:00:aa-`},
188-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
189-
},
190-
{
191-
"smpte invalid 5",
192-
base.HeaderValue{`smpte=00:00:00:aa-`},
193-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
194-
},
195-
{
196-
"smpte invalid 6",
197-
base.HeaderValue{`smpte=00:00:00:aa.00-`},
198-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
199-
},
200-
{
201-
"smpte invalid 7",
202-
base.HeaderValue{`smpte=00:00:00:00.aa-`},
203-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
204-
},
205-
{
206-
"npt without values",
207-
base.HeaderValue{`npt=`},
208-
"invalid value ()",
209-
},
210-
{
211-
"npt end invalid",
212-
base.HeaderValue{`npt=00:00:00-aa`},
213-
"strconv.ParseFloat: parsing \"aa\": invalid syntax",
214-
},
215-
{
216-
"npt invalid 1",
217-
base.HeaderValue{`npt=00:00:00:00-`},
218-
"invalid NPT time (00:00:00:00)",
219-
},
220-
{
221-
"npt invalid 2",
222-
base.HeaderValue{`npt=aa-`},
223-
"strconv.ParseFloat: parsing \"aa\": invalid syntax",
224-
},
225-
{
226-
"npt invalid 3",
227-
base.HeaderValue{`npt=aa:00-`},
228-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
229-
},
230-
{
231-
"npt invalid 4",
232-
base.HeaderValue{`npt=aa:00:00-`},
233-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
234-
},
235-
{
236-
"clock without values",
237-
base.HeaderValue{`clock=`},
238-
"invalid value ()",
239-
},
240-
{
241-
"clock end invalid",
242-
base.HeaderValue{`clock=20060102T150405Z-aa`},
243-
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
244-
},
245-
{
246-
"clock invalid 1",
247-
base.HeaderValue{`clock=aa-`},
248-
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
249-
},
250-
{
251-
"time invalid",
252-
base.HeaderValue{`time=aa`},
253-
"parsing time \"aa\" as \"20060102T150405Z\": cannot parse \"aa\" as \"2006\"",
254-
},
255-
} {
256-
t.Run(ca.name, func(t *testing.T) {
257-
var h Range
258-
err := h.Unmarshal(ca.hv)
259-
require.EqualError(t, err, ca.err)
260-
})
261-
}
262-
}
263-
264134
func TestRangeMarshal(t *testing.T) {
265135
for _, ca := range casesRange {
266136
t.Run(ca.name, func(t *testing.T) {
@@ -269,3 +139,32 @@ func TestRangeMarshal(t *testing.T) {
269139
})
270140
}
271141
}
142+
143+
func FuzzRangeUnmarshal(f *testing.F) {
144+
for _, ca := range casesRange {
145+
f.Add(ca.vin[0])
146+
}
147+
148+
f.Add("smtpe=")
149+
f.Add("npt=")
150+
f.Add("clock=")
151+
152+
f.Fuzz(func(t *testing.T, b string) {
153+
var h Range
154+
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
155+
})
156+
}
157+
158+
func TestRangeAdditionalErrors(t *testing.T) {
159+
func() {
160+
var h Range
161+
err := h.Unmarshal(base.HeaderValue{})
162+
require.Error(t, err)
163+
}()
164+
165+
func() {
166+
var h Range
167+
err := h.Unmarshal(base.HeaderValue{"a", "b"})
168+
require.Error(t, err)
169+
}()
170+
}

pkg/headers/rtpinfo.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
3939
return err
4040
}
4141

42+
urlReceived := false
43+
4244
for k, v := range kvs {
4345
switch k {
4446
case "url":
4547
e.URL = v
48+
urlReceived = true
4649

4750
case "seq":
4851
vi, err := strconv.ParseUint(v, 10, 16)
@@ -65,7 +68,7 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
6568
}
6669
}
6770

68-
if e.URL == "" {
71+
if !urlReceived {
6972
return fmt.Errorf("URL is missing")
7073
}
7174

pkg/headers/rtpinfo_test.go

+25-45
Original file line numberDiff line numberDiff line change
@@ -142,51 +142,6 @@ func TestRTPInfoUnmarshal(t *testing.T) {
142142
}
143143
}
144144

145-
func TestRTPInfoUnmarshalErrors(t *testing.T) {
146-
for _, ca := range []struct {
147-
name string
148-
hv base.HeaderValue
149-
err string
150-
}{
151-
{
152-
"empty",
153-
base.HeaderValue{},
154-
"value not provided",
155-
},
156-
{
157-
"2 values",
158-
base.HeaderValue{"a", "b"},
159-
"value provided multiple times ([a b])",
160-
},
161-
{
162-
"invalid key-value",
163-
base.HeaderValue{"test=\"a"},
164-
"apexes not closed (test=\"a)",
165-
},
166-
{
167-
"invalid sequence",
168-
base.HeaderValue{`url=rtsp://127.0.0.1/test.mkv/track1;seq=aa;rtptime=717574556`},
169-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
170-
},
171-
{
172-
"invalid rtptime",
173-
base.HeaderValue{`url=rtsp://127.0.0.1/test.mkv/track1;seq=35243;rtptime=aa`},
174-
"strconv.ParseUint: parsing \"aa\": invalid syntax",
175-
},
176-
{
177-
"missing URL",
178-
base.HeaderValue{`seq=35243;rtptime=717574556`},
179-
"URL is missing",
180-
},
181-
} {
182-
t.Run(ca.name, func(t *testing.T) {
183-
var h RTPInfo
184-
err := h.Unmarshal(ca.hv)
185-
require.EqualError(t, err, ca.err)
186-
})
187-
}
188-
}
189-
190145
func TestRTPInfoMarshal(t *testing.T) {
191146
for _, ca := range casesRTPInfo {
192147
t.Run(ca.name, func(t *testing.T) {
@@ -195,3 +150,28 @@ func TestRTPInfoMarshal(t *testing.T) {
195150
})
196151
}
197152
}
153+
154+
func FuzzRTPInfoUnmarshal(f *testing.F) {
155+
for _, ca := range casesRTPInfo {
156+
f.Add(ca.vin[0])
157+
}
158+
159+
f.Fuzz(func(t *testing.T, b string) {
160+
var h RTPInfo
161+
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
162+
})
163+
}
164+
165+
func TestRTPInfoAdditionalErrors(t *testing.T) {
166+
func() {
167+
var h RTPInfo
168+
err := h.Unmarshal(base.HeaderValue{})
169+
require.Error(t, err)
170+
}()
171+
172+
func() {
173+
var h RTPInfo
174+
err := h.Unmarshal(base.HeaderValue{"a", "b"})
175+
require.Error(t, err)
176+
}()
177+
}

pkg/headers/session_test.go

+27-35
Original file line numberDiff line numberDiff line change
@@ -53,41 +53,6 @@ func TestSessionUnmarshal(t *testing.T) {
5353
}
5454
}
5555

56-
func TestSessionUnmarshalErrors(t *testing.T) {
57-
for _, ca := range []struct {
58-
name string
59-
hv base.HeaderValue
60-
err string
61-
}{
62-
{
63-
"empty",
64-
base.HeaderValue{},
65-
"value not provided",
66-
},
67-
{
68-
"2 values",
69-
base.HeaderValue{"a", "b"},
70-
"value provided multiple times ([a b])",
71-
},
72-
{
73-
"invalid key-value",
74-
base.HeaderValue{"A3eqwsafq3rFASqew;test=\"a"},
75-
"apexes not closed (test=\"a)",
76-
},
77-
{
78-
"invalid timeout",
79-
base.HeaderValue{`A3eqwsafq3rFASqew;timeout=aaa`},
80-
"strconv.ParseUint: parsing \"aaa\": invalid syntax",
81-
},
82-
} {
83-
t.Run(ca.name, func(t *testing.T) {
84-
var h Session
85-
err := h.Unmarshal(ca.hv)
86-
require.EqualError(t, err, ca.err)
87-
})
88-
}
89-
}
90-
9156
func TestSessionMarshal(t *testing.T) {
9257
for _, ca := range casesSession {
9358
t.Run(ca.name, func(t *testing.T) {
@@ -96,3 +61,30 @@ func TestSessionMarshal(t *testing.T) {
9661
})
9762
}
9863
}
64+
65+
func FuzzSessionUnmarshal(f *testing.F) {
66+
for _, ca := range casesSession {
67+
f.Add(ca.vin[0])
68+
}
69+
70+
f.Add("timeout=")
71+
72+
f.Fuzz(func(t *testing.T, b string) {
73+
var h Session
74+
h.Unmarshal(base.HeaderValue{b}) //nolint:errcheck
75+
})
76+
}
77+
78+
func TestSessionAdditionalErrors(t *testing.T) {
79+
func() {
80+
var h Session
81+
err := h.Unmarshal(base.HeaderValue{})
82+
require.Error(t, err)
83+
}()
84+
85+
func() {
86+
var h Session
87+
err := h.Unmarshal(base.HeaderValue{"a", "b"})
88+
require.Error(t, err)
89+
}()
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("=\"")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("seq")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("rtptime")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("clock=-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte=0::-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte=0:0:0-0:0:0:0.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("=\"")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("npt=0-A")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte=0:0:0:-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("npt=-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("npt=::-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte=0:0:A-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("npt=:::-")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("clock=00000101T000000Z-0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("smpte=0:0:0-0")

0 commit comments

Comments
 (0)