diff --git a/candidate_base.go b/candidate_base.go index e7660d6..66fb776 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -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 { diff --git a/candidate_test.go b/candidate_test.go index 4694cec..ac7b642 100644 --- a/candidate_test.go +++ b/candidate_test.go @@ -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()) + } }) } }