Skip to content

Commit ef453b3

Browse files
committed
Handle candidate: prefix with UnmarshalCandidate
Make UnmarshalCandidate able to handle candidate: prefix in the candidate string.
1 parent 37fb5d2 commit ef453b3

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

candidate_base.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,10 @@ func (c *candidateBase) setExtensions(extensions []CandidateExtension) {
693693
// UnmarshalCandidate Parses a candidate from a string
694694
// https://datatracker.ietf.org/doc/html/rfc5245#section-15.1
695695
func UnmarshalCandidate(raw string) (Candidate, error) { //nolint:cyclop
696-
pos := 0
696+
// Handle candidates with the "candidate:" prefix as defined in RFC 5245 section 15.1.
697+
raw = strings.TrimPrefix(raw, "candidate:")
697698

699+
pos := 0
698700
// foundation ( 1*32ice-char ) But we allow for empty foundation,
699701
foundation, pos, err := readCandidateCharToken(raw, pos, 32)
700702
if err != nil {

candidate_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ice
66
import (
77
"net"
88
"strconv"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -465,6 +466,18 @@ func TestCandidateMarshal(t *testing.T) {
465466
" 1 udp 500 " + localhostIPStr + " 80 typ host",
466467
false,
467468
},
469+
// Missing Foundation
470+
{
471+
mustCandidateHost(t, &CandidateHostConfig{
472+
Network: NetworkTypeUDP4.String(),
473+
Address: localhostIPStr,
474+
Port: 80,
475+
Priority: 500,
476+
Foundation: " ",
477+
}),
478+
"candidate: 1 udp 500 " + localhostIPStr + " 80 typ host",
479+
false,
480+
},
468481
{
469482
mustCandidateHost(t, &CandidateHostConfig{
470483
Network: NetworkTypeUDP4.String(),
@@ -487,6 +500,17 @@ func TestCandidateMarshal(t *testing.T) {
487500
"3359356140 1 tcp 1671430143 172.28.142.173 7686 typ host",
488501
false,
489502
},
503+
{
504+
mustCandidateHost(t, &CandidateHostConfig{
505+
Network: NetworkTypeTCP4.String(),
506+
Address: "172.28.142.173",
507+
Port: 7686,
508+
Priority: 1671430143,
509+
Foundation: "+/3713fhi",
510+
}),
511+
"candidate:3359356140 1 tcp 1671430143 172.28.142.173 7686 typ host",
512+
false,
513+
},
490514

491515
// Invalid candidates
492516
{nil, "", true},
@@ -562,7 +586,12 @@ func TestCandidateMarshal(t *testing.T) {
562586
test.candidate.String(),
563587
actualCandidate.String(),
564588
)
565-
require.Equal(t, test.marshaled, actualCandidate.Marshal())
589+
590+
if strings.HasPrefix(test.marshaled, "candidate:") {
591+
require.Equal(t, test.marshaled[len("candidate:"):], actualCandidate.Marshal())
592+
} else {
593+
require.Equal(t, test.marshaled, actualCandidate.Marshal())
594+
}
566595
})
567596
}
568597
}

0 commit comments

Comments
 (0)