mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 23:02:45 +08:00
base: improve coverage
This commit is contained in:
@@ -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())
|
||||||
|
}
|
||||||
|
@@ -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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -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" +
|
||||||
|
@@ -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"+
|
||||||
|
@@ -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,
|
||||||
|
Reference in New Issue
Block a user