Handle candidate: prefix with UnmarshalCandidate

Make UnmarshalCandidate able to handle candidate: prefix in the
candidate string.
This commit is contained in:
Joe Turki
2025-03-25 06:52:45 +02:00
parent 37fb5d2fc3
commit ef453b3fdd
2 changed files with 33 additions and 2 deletions
+3 -1
View File
@@ -693,8 +693,10 @@ func (c *candidateBase) setExtensions(extensions []CandidateExtension) {
// UnmarshalCandidate Parses a candidate from a string
// https://datatracker.ietf.org/doc/html/rfc5245#section-15.1
func UnmarshalCandidate(raw string) (Candidate, error) { //nolint:cyclop
pos := 0
// Handle candidates with the "candidate:" prefix as defined in RFC 5245 section 15.1.
raw = strings.TrimPrefix(raw, "candidate:")
pos := 0
// foundation ( 1*32ice-char ) But we allow for empty foundation,
foundation, pos, err := readCandidateCharToken(raw, pos, 32)
if err != nil {
+30 -1
View File
@@ -6,6 +6,7 @@ package ice
import (
"net"
"strconv"
"strings"
"testing"
"time"
@@ -465,6 +466,18 @@ func TestCandidateMarshal(t *testing.T) {
" 1 udp 500 " + localhostIPStr + " 80 typ host",
false,
},
// Missing Foundation
{
mustCandidateHost(t, &CandidateHostConfig{
Network: NetworkTypeUDP4.String(),
Address: localhostIPStr,
Port: 80,
Priority: 500,
Foundation: " ",
}),
"candidate: 1 udp 500 " + localhostIPStr + " 80 typ host",
false,
},
{
mustCandidateHost(t, &CandidateHostConfig{
Network: NetworkTypeUDP4.String(),
@@ -487,6 +500,17 @@ func TestCandidateMarshal(t *testing.T) {
"3359356140 1 tcp 1671430143 172.28.142.173 7686 typ host",
false,
},
{
mustCandidateHost(t, &CandidateHostConfig{
Network: NetworkTypeTCP4.String(),
Address: "172.28.142.173",
Port: 7686,
Priority: 1671430143,
Foundation: "+/3713fhi",
}),
"candidate:3359356140 1 tcp 1671430143 172.28.142.173 7686 typ host",
false,
},
// Invalid candidates
{nil, "", true},
@@ -562,7 +586,12 @@ func TestCandidateMarshal(t *testing.T) {
test.candidate.String(),
actualCandidate.String(),
)
require.Equal(t, test.marshaled, actualCandidate.Marshal())
if strings.HasPrefix(test.marshaled, "candidate:") {
require.Equal(t, test.marshaled[len("candidate:"):], actualCandidate.Marshal())
} else {
require.Equal(t, test.marshaled, actualCandidate.Marshal())
}
})
}
}