improve coverage

This commit is contained in:
aler9
2021-05-12 16:03:15 +02:00
parent 4d1c2d1831
commit 8be64d9cf6
8 changed files with 123 additions and 19 deletions

View File

@@ -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"),

View File

@@ -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())
}

View File

@@ -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())
}

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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`},

View File

@@ -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)
}