Skip to content

Commit 1ef6a1f

Browse files
authored
Fix parse port range (#205)
and also the error message
1 parent 04eeb63 commit 1ef6a1f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

unmarshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ func unmarshalMediaDescription(lex *lexer) (stateFn, error) { //nolint:cyclop
811811
parts := strings.Split(field, "/")
812812
newMediaDesc.MediaName.Port.Value, err = parsePort(parts[0])
813813
if err != nil {
814-
return nil, fmt.Errorf("%w `%v`", errSDPInvalidPortValue, parts[0])
814+
return nil, err
815815
}
816816

817817
if len(parts) > 1 {
@@ -986,10 +986,10 @@ func timeShorthand(b byte) int64 {
986986
func parsePort(value string) (int, error) {
987987
port, err := strconv.Atoi(value)
988988
if err != nil {
989-
return 0, fmt.Errorf("%w `%v`", errSDPInvalidPortValue, port)
989+
return 0, fmt.Errorf("%w `%v`", errSDPInvalidPortValue, value)
990990
}
991991

992-
if port < 0 || port > 65536 {
992+
if port < 0 || port > 65535 {
993993
return 0, fmt.Errorf("%w -- out of range `%v`", errSDPInvalidPortValue, port)
994994
}
995995

unmarshal_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package sdp
55

66
import (
7+
"fmt"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -395,6 +396,42 @@ func TestUnmarshalZeroValues(t *testing.T) {
395396
assert.Equal(t, in, string(out))
396397
}
397398

399+
func TestUnmarshalPortRange(t *testing.T) {
400+
for _, test := range []struct {
401+
In string
402+
ExpectError error
403+
}{
404+
{
405+
In: SessionAttributesSDP + "m=video -1 RTP/AVP 99\r\n",
406+
ExpectError: fmt.Errorf("%w -- out of range `%v`", errSDPInvalidPortValue, "-1"),
407+
},
408+
{
409+
In: SessionAttributesSDP + "m=video 65536 RTP/AVP 99\r\n",
410+
ExpectError: fmt.Errorf("%w -- out of range `%v`", errSDPInvalidPortValue, "65536"),
411+
},
412+
{
413+
In: SessionAttributesSDP + "m=video 0 RTP/AVP 99\r\n",
414+
ExpectError: nil,
415+
},
416+
{
417+
In: SessionAttributesSDP + "m=video 65535 RTP/AVP 99\r\n",
418+
ExpectError: nil,
419+
},
420+
{
421+
In: SessionAttributesSDP + "m=video --- RTP/AVP 99\r\n",
422+
ExpectError: fmt.Errorf("%w `%v`", errSDPInvalidPortValue, "---"),
423+
},
424+
} {
425+
var sd SessionDescription
426+
err := sd.UnmarshalString(test.In)
427+
if err != nil && test.ExpectError != nil {
428+
assert.Equal(t, test.ExpectError.Error(), err.Error())
429+
} else {
430+
assert.Equal(t, err, test.ExpectError)
431+
}
432+
}
433+
}
434+
398435
func BenchmarkUnmarshal(b *testing.B) {
399436
b.ReportAllocs()
400437
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)