base: improve coverage

This commit is contained in:
aler9
2021-05-21 18:52:55 +02:00
parent 9a7d0c56fa
commit ce83edbed1
6 changed files with 139 additions and 7 deletions

View File

@@ -3,11 +3,25 @@ package base
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type limitedBuffer struct {
cap int
n int
}
func (b *limitedBuffer) Write(p []byte) (int, error) {
b.n += len(p)
if b.n > b.cap {
return 0, fmt.Errorf("capacity reached")
}
return len(p), nil
}
var casesBody = []struct { var casesBody = []struct {
name string name string
h Header h Header
@@ -90,3 +104,9 @@ func TestBodyReadErrors(t *testing.T) {
}) })
} }
} }
func TestBodyWriteErrors(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: 3}, 1)
err := body([]byte("1234")).write(bw)
require.Equal(t, "capacity reached", err.Error())
}

View File

@@ -183,3 +183,27 @@ func TestHeaderReadErrors(t *testing.T) {
}) })
} }
} }
func TestHeaderWriteErrors(t *testing.T) {
for _, ca := range []struct {
name string
cap int
}{
{
"values",
3,
},
{
"final newline",
12,
},
} {
t.Run(ca.name, func(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: ca.cap}, 1)
err := Header{
"Value": HeaderValue{"key"},
}.write(bw)
require.Equal(t, "capacity reached", err.Error())
})
}
}

View File

@@ -116,14 +116,10 @@ func (f InterleavedFrame) Write(bw *bufio.Writer) error {
return uint8((f.TrackID * 2) + 1) return uint8((f.TrackID * 2) + 1)
}() }()
_, err := bw.Write([]byte{0x24, channel}) buf := []byte{0x24, channel, 0x00, 0x00}
if err != nil { binary.BigEndian.PutUint16(buf[2:], uint16(len(f.Payload)))
return err
}
buf := make([]byte, 2) _, err := bw.Write(buf)
binary.BigEndian.PutUint16(buf, uint16(len(f.Payload)))
_, err = bw.Write(buf)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -96,6 +96,32 @@ func TestInterleavedFrameReadErrors(t *testing.T) {
} }
} }
func TestInterleavedFrameWriteErrors(t *testing.T) {
for _, ca := range []struct {
name string
cap int
}{
{
"header",
3,
},
{
"content",
6,
},
} {
t.Run(ca.name, func(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: ca.cap}, 1)
err := InterleavedFrame{
TrackID: 3,
StreamType: StreamTypeRTP,
Payload: []byte{0x01, 0x02, 0x03, 0x04},
}.Write(bw)
require.Equal(t, "capacity reached", err.Error())
})
}
}
func TestReadInterleavedFrameOrRequest(t *testing.T) { func TestReadInterleavedFrameOrRequest(t *testing.T) {
byts := []byte("DESCRIBE rtsp://example.com/media.mp4 RTSP/1.0\r\n" + byts := []byte("DESCRIBE rtsp://example.com/media.mp4 RTSP/1.0\r\n" +
"Accept: application/sdp\r\n" + "Accept: application/sdp\r\n" +

View File

@@ -231,6 +231,39 @@ func TestRequestReadErrors(t *testing.T) {
} }
} }
func TestRequestWriteErrors(t *testing.T) {
for _, ca := range []struct {
name string
cap int
}{
{
"first line",
3,
},
{
"header",
20,
},
{
"body",
40,
},
} {
t.Run(ca.name, func(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: ca.cap}, 1)
err := Request{
Method: "ANNOUNCE",
URL: MustParseURL("rtsp://example.com/media.mp4"),
Header: Header{
"CSeq": HeaderValue{"7"},
},
Body: []byte("abc"),
}.Write(bw)
require.Equal(t, "capacity reached", err.Error())
})
}
}
func TestRequestReadIgnoreFrames(t *testing.T) { func TestRequestReadIgnoreFrames(t *testing.T) {
byts := []byte{0x24, 0x6, 0x0, 0x4, 0x1, 0x2, 0x3, 0x4} byts := []byte{0x24, 0x6, 0x0, 0x4, 0x1, 0x2, 0x3, 0x4}
byts = append(byts, []byte("OPTIONS rtsp://example.com/media.mp4 RTSP/1.0\r\n"+ byts = append(byts, []byte("OPTIONS rtsp://example.com/media.mp4 RTSP/1.0\r\n"+

View File

@@ -188,6 +188,39 @@ func TestResponseReadErrors(t *testing.T) {
} }
} }
func TestResponseWriteErrors(t *testing.T) {
for _, ca := range []struct {
name string
cap int
}{
{
"first line",
3,
},
{
"header",
20,
},
{
"body",
40,
},
} {
t.Run(ca.name, func(t *testing.T) {
bw := bufio.NewWriterSize(&limitedBuffer{cap: ca.cap}, 1)
err := Response{
StatusCode: 200,
StatusMessage: "OK",
Header: Header{
"CSeq": HeaderValue{"2"},
},
Body: []byte("abc"),
}.Write(bw)
require.Equal(t, "capacity reached", err.Error())
})
}
}
func TestResponseWriteAutoFillStatus(t *testing.T) { func TestResponseWriteAutoFillStatus(t *testing.T) {
res := &Response{ res := &Response{
StatusCode: StatusMethodNotAllowed, StatusCode: StatusMethodNotAllowed,