diff --git a/pkg/base/header_test.go b/pkg/base/header_test.go index df8edf72..25c8a054 100644 --- a/pkg/base/header_test.go +++ b/pkg/base/header_test.go @@ -138,6 +138,16 @@ func TestHeaderReadErrors(t *testing.T) { []byte{}, "EOF", }, + { + "missing value", + []byte("Testing:"), + "EOF", + }, + { + "missing eol", + []byte("Testing: val"), + "EOF", + }, { "r without n", []byte("Testing: val\rTesting: val\r\n"), diff --git a/pkg/base/request_test.go b/pkg/base/request_test.go index a6ac549c..cd803b50 100644 --- a/pkg/base/request_test.go +++ b/pkg/base/request_test.go @@ -222,6 +222,16 @@ func TestRequestReadErrors(t *testing.T) { []byte("GET rtsp://testing123 RTSP/1.0\r\nContent-Length: 17\r\n\r\n123"), "unexpected EOF", }, + { + "invalid content-length", + []byte("GET rtsp://testing123 RTSP/1.0\r\nContent-Length: aaa\r\n\r\n123"), + "invalid Content-Length", + }, + { + "too big content-length", + []byte("GET rtsp://testing123 RTSP/1.0\r\nContent-Length: 1000000\r\n\r\n123"), + "Content-Length exceeds 131072 (it's 1000000)", + }, } { t.Run(ca.name, func(t *testing.T) { var req Request @@ -245,3 +255,13 @@ func TestRequestReadIgnoreFrames(t *testing.T) { err := req.ReadIgnoreFrames(rb, buf) require.NoError(t, err) } + +func TestRequestReadIgnoreFramesError(t *testing.T) { + byts := []byte{0x25} + + rb := bufio.NewReader(bytes.NewBuffer(byts)) + buf := make([]byte, 10) + var req Request + err := req.ReadIgnoreFrames(rb, buf) + require.Equal(t, "EOF", err.Error()) +} diff --git a/pkg/base/response_test.go b/pkg/base/response_test.go index b040199b..125f723c 100644 --- a/pkg/base/response_test.go +++ b/pkg/base/response_test.go @@ -179,6 +179,16 @@ func TestResponseReadErrors(t *testing.T) { []byte("RTSP/1.0 200 OK\r\nContent-Length: 17\r\n\r\n123"), "unexpected EOF", }, + { + "invalid content-length", + []byte("RTSP/1.0 200 OK\r\nContent-Length: aaa\r\n\r\n123"), + "invalid Content-Length", + }, + { + "too big content-length", + []byte("RTSP/1.0 200 OK\r\nContent-Length: 1000000\r\n\r\n123"), + "Content-Length exceeds 131072 (it's 1000000)", + }, } { t.Run(ca.name, func(t *testing.T) { var res Response @@ -230,3 +240,13 @@ func TestResponseReadIgnoreFrames(t *testing.T) { err := res.ReadIgnoreFrames(rb, buf) require.NoError(t, err) } + +func TestResponseReadIgnoreFramesError(t *testing.T) { + byts := []byte{0x25} + + rb := bufio.NewReader(bytes.NewBuffer(byts)) + buf := make([]byte, 10) + var res Response + err := res.ReadIgnoreFrames(rb, buf) + require.Equal(t, "EOF", err.Error()) +} diff --git a/pkg/headers/authorization_test.go b/pkg/headers/authorization_test.go index 7ddcbc3b..1aa37c62 100644 --- a/pkg/headers/authorization_test.go +++ b/pkg/headers/authorization_test.go @@ -85,6 +85,26 @@ func TestAuthorizationReadError(t *testing.T) { base.HeaderValue{"a", "b"}, "value provided multiple times ([a b])", }, + { + "invalid", + base.HeaderValue{`Invalid`}, + "invalid authorization header", + }, + { + "basic invalid 1", + base.HeaderValue{`Basic aaa`}, + "invalid value", + }, + { + "basic invalid 2", + base.HeaderValue{`Basic aW52YWxpZA==`}, + "invalid value", + }, + { + "digest invalid", + base.HeaderValue{`Basic`}, + "invalid authorization header", + }, } { t.Run(ca.name, func(t *testing.T) { var h Authorization diff --git a/pkg/headers/session.go b/pkg/headers/session.go index f66b2fbc..920079a3 100644 --- a/pkg/headers/session.go +++ b/pkg/headers/session.go @@ -46,17 +46,13 @@ func (h *Session) Read(v base.HeaderValue) error { } for k, v := range kvs { - switch k { - case "timeout": + if k == "timeout" { iv, err := strconv.ParseUint(v, 10, 64) if err != nil { return err } uiv := uint(iv) h.Timeout = &uiv - - default: - // ignore non-standard keys } } diff --git a/pkg/headers/session_test.go b/pkg/headers/session_test.go index f944a6f7..765248f5 100644 --- a/pkg/headers/session_test.go +++ b/pkg/headers/session_test.go @@ -84,6 +84,16 @@ func TestSessionReadError(t *testing.T) { base.HeaderValue{"a", "b"}, "value provided multiple times ([a b])", }, + { + "no keys", + base.HeaderValue{`A3eqwsafq3rFASqew;aaaa`}, + "unable to read key (aaaa)", + }, + { + "invalid timeout", + base.HeaderValue{`A3eqwsafq3rFASqew;timeout=aaa`}, + "strconv.ParseUint: parsing \"aaa\": invalid syntax", + }, } { t.Run(ca.name, func(t *testing.T) { var h Session diff --git a/pkg/headers/transport_test.go b/pkg/headers/transport_test.go index 71f5b671..b60db6e3 100644 --- a/pkg/headers/transport_test.go +++ b/pkg/headers/transport_test.go @@ -160,35 +160,40 @@ func TestTransportReadError(t *testing.T) { "invalid protocol (unicast;client_port=14186-14187)", }, { - "invalid client port 1", + "invalid ports 1", + base.HeaderValue{`RTP/AVP;unicast;client_port=aa`}, + "invalid ports (aa)", + }, + { + "invalid ports 2", + base.HeaderValue{`RTP/AVP;unicast;client_port=aa-bb-cc`}, + "invalid ports (aa-bb-cc)", + }, + { + "invalid ports 3", base.HeaderValue{`RTP/AVP;unicast;client_port=aa-14187`}, "invalid ports (aa-14187)", }, { - "invalid client port 2", + "invalid ports 4", base.HeaderValue{`RTP/AVP;unicast;client_port=14186-aa`}, "invalid ports (14186-aa)", }, { - "invalid server port 1", + "invalid client port", + base.HeaderValue{`RTP/AVP;unicast;client_port=aa-14187`}, + "invalid ports (aa-14187)", + }, + { + "invalid server port", base.HeaderValue{`RTP/AVP;unicast;server_port=aa-14187`}, "invalid ports (aa-14187)", }, { - "invalid server port 2", - base.HeaderValue{`RTP/AVP;unicast;server_port=14186-aa`}, - "invalid ports (14186-aa)", - }, - { - "invalid interleaved port 1", + "invalid interleaved port", base.HeaderValue{`RTP/AVP;unicast;interleaved=aa-14187`}, "invalid ports (aa-14187)", }, - { - "invalid interleaved port 2", - base.HeaderValue{`RTP/AVP;unicast;interleaved=14186-aa`}, - "invalid ports (14186-aa)", - }, { "invalid ttl", base.HeaderValue{`RTP/AVP;unicast;ttl=aa`}, diff --git a/pkg/multibuffer/multibuffer_test.go b/pkg/multibuffer/multibuffer_test.go new file mode 100644 index 00000000..518b47ae --- /dev/null +++ b/pkg/multibuffer/multibuffer_test.go @@ -0,0 +1,23 @@ +package multibuffer + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test(t *testing.T) { + mb := New(2, 4) + + b := mb.Next() + copy(b, []byte{0x01, 0x02, 0x03, 0x04}) + + b = mb.Next() + copy(b, []byte{0x05, 0x06, 0x07, 0x08}) + + b = mb.Next() + require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, b) + + b = mb.Next() + require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, b) +}