mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 15:46:51 +08:00
improve coverage
This commit is contained in:
@@ -40,10 +40,11 @@ func ReadBits(buf []byte, pos *int, n int) (uint64, error) {
|
|||||||
|
|
||||||
// ReadGolombUnsigned reads an unsigned golomb-encoded value.
|
// ReadGolombUnsigned reads an unsigned golomb-encoded value.
|
||||||
func ReadGolombUnsigned(buf []byte, pos *int) (uint32, error) {
|
func ReadGolombUnsigned(buf []byte, pos *int) (uint32, error) {
|
||||||
|
buflen := len(buf)
|
||||||
leadingZeroBits := uint32(0)
|
leadingZeroBits := uint32(0)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if (len(buf)*8 - *pos) == 0 {
|
if (buflen*8 - *pos) == 0 {
|
||||||
return 0, fmt.Errorf("not enough bits")
|
return 0, fmt.Errorf("not enough bits")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ func ReadGolombUnsigned(buf []byte, pos *int) (uint32, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len(buf)*8 - *pos) < int(leadingZeroBits) {
|
if (buflen*8 - *pos) < int(leadingZeroBits) {
|
||||||
return 0, fmt.Errorf("not enough bits")
|
return 0, fmt.Errorf("not enough bits")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,11 +37,40 @@ func TestReadGolombUnsigned(t *testing.T) {
|
|||||||
require.Equal(t, uint32(6), v)
|
require.Equal(t, uint32(6), v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadGolombUnsignedErrors(t *testing.T) {
|
||||||
|
buf := []byte{0x00}
|
||||||
|
pos := 0
|
||||||
|
_, err := ReadGolombUnsigned(buf, &pos)
|
||||||
|
require.EqualError(t, err, "not enough bits")
|
||||||
|
|
||||||
|
buf = []byte{0x00, 0x01}
|
||||||
|
pos = 0
|
||||||
|
_, err = ReadGolombUnsigned(buf, &pos)
|
||||||
|
require.EqualError(t, err, "not enough bits")
|
||||||
|
|
||||||
|
buf = []byte{0x00, 0x00, 0x00, 0x00, 0x01}
|
||||||
|
pos = 0
|
||||||
|
_, err = ReadGolombUnsigned(buf, &pos)
|
||||||
|
require.EqualError(t, err, "invalid value")
|
||||||
|
}
|
||||||
|
|
||||||
func TestReadGolombSigned(t *testing.T) {
|
func TestReadGolombSigned(t *testing.T) {
|
||||||
buf := []byte{0x38}
|
buf := []byte{0x38}
|
||||||
pos := 0
|
pos := 0
|
||||||
v, _ := ReadGolombSigned(buf, &pos)
|
v, _ := ReadGolombSigned(buf, &pos)
|
||||||
require.Equal(t, int32(-3), v)
|
require.Equal(t, int32(-3), v)
|
||||||
|
|
||||||
|
buf = []byte{0b00100100}
|
||||||
|
pos = 0
|
||||||
|
v, _ = ReadGolombSigned(buf, &pos)
|
||||||
|
require.Equal(t, int32(2), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadGolombSignedErrors(t *testing.T) {
|
||||||
|
buf := []byte{0x00}
|
||||||
|
pos := 0
|
||||||
|
_, err := ReadGolombSigned(buf, &pos)
|
||||||
|
require.EqualError(t, err, "not enough bits")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadFlag(t *testing.T) {
|
func TestReadFlag(t *testing.T) {
|
||||||
|
@@ -10,6 +10,14 @@ import (
|
|||||||
"github.com/aler9/gortsplib/pkg/url"
|
"github.com/aler9/gortsplib/pkg/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func mustParseURL(s string) *url.URL {
|
||||||
|
u, err := url.Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
||||||
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" +
|
||||||
@@ -170,3 +178,47 @@ func TestReadResponseIgnoreFramesErrors(t *testing.T) {
|
|||||||
_, err := conn.ReadResponseIgnoreFrames()
|
_, err := conn.ReadResponseIgnoreFrames()
|
||||||
require.EqualError(t, err, "EOF")
|
require.EqualError(t, err, "EOF")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteRequest(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
conn := NewConn(&buf)
|
||||||
|
err := conn.WriteRequest(&base.Request{
|
||||||
|
Method: "OPTIONS",
|
||||||
|
URL: mustParseURL("rtsp://example.com/media.mp4"),
|
||||||
|
Header: base.Header{
|
||||||
|
"CSeq": base.HeaderValue{"1"},
|
||||||
|
"Require": base.HeaderValue{"implicit-play"},
|
||||||
|
"Proxy-Require": base.HeaderValue{"gzipped-messages"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteResponse(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
conn := NewConn(&buf)
|
||||||
|
err := conn.WriteResponse(&base.Response{
|
||||||
|
StatusCode: base.StatusOK,
|
||||||
|
StatusMessage: "OK",
|
||||||
|
Header: base.Header{
|
||||||
|
"CSeq": base.HeaderValue{"2"},
|
||||||
|
"Session": base.HeaderValue{"645252166"},
|
||||||
|
"WWW-Authenticate": base.HeaderValue{
|
||||||
|
"Digest realm=\"4419b63f5e51\", nonce=\"8b84a3b789283a8bea8da7fa7d41f08b\", stale=\"FALSE\"",
|
||||||
|
"Basic realm=\"4419b63f5e51\"",
|
||||||
|
},
|
||||||
|
"Date": base.HeaderValue{"Sat, Aug 16 2014 02:22:28 GMT"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteInterleavedFrame(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
conn := NewConn(&buf)
|
||||||
|
err := conn.WriteInterleavedFrame(&base.InterleavedFrame{
|
||||||
|
Channel: 6,
|
||||||
|
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
||||||
|
}, make([]byte, 1024))
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
@@ -92,6 +92,25 @@ func TestURLClone(t *testing.T) {
|
|||||||
}, u2)
|
}, u2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestURLCloneWithoutCredentials(t *testing.T) {
|
||||||
|
u := mustParse("rtsp://user:pass@localhost:8554/test/stream")
|
||||||
|
u2 := u.CloneWithoutCredentials()
|
||||||
|
u.Host = "otherhost"
|
||||||
|
|
||||||
|
require.Equal(t, &URL{
|
||||||
|
Scheme: "rtsp",
|
||||||
|
Host: "otherhost",
|
||||||
|
Path: "/test/stream",
|
||||||
|
User: url.UserPassword("user", "pass"),
|
||||||
|
}, u)
|
||||||
|
|
||||||
|
require.Equal(t, &URL{
|
||||||
|
Scheme: "rtsp",
|
||||||
|
Host: "localhost:8554",
|
||||||
|
Path: "/test/stream",
|
||||||
|
}, u2)
|
||||||
|
}
|
||||||
|
|
||||||
func TestURLRTSPPathAndQuery(t *testing.T) {
|
func TestURLRTSPPathAndQuery(t *testing.T) {
|
||||||
for _, ca := range []struct {
|
for _, ca := range []struct {
|
||||||
u *URL
|
u *URL
|
||||||
|
Reference in New Issue
Block a user