From f5493bc7fbdbf16de8d79ed8217984db8618fb5f Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Sat, 12 Sep 2020 20:50:07 -0700 Subject: [PATCH] Add TCPType Support Unmarshal/Marshal now supports TCPType --- candidate_base.go | 16 +++++++- candidate_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/candidate_base.go b/candidate_base.go index 128f7f1..fa9f9e9 100644 --- a/candidate_base.go +++ b/candidate_base.go @@ -313,6 +313,7 @@ func (c *candidateBase) Equal(other Candidate) bool { c.Type() == other.Type() && c.Address() == other.Address() && c.Port() == other.Port() && + c.TCPType() == other.TCPType() && c.RelatedAddress().Equal(other.RelatedAddress()) } @@ -380,6 +381,10 @@ func (c candidateBase) Marshal() string { c.Port(), c.Type()) + if c.tcpType != TCPTypeUnspecified { + val += fmt.Sprintf(" tcptype %s", c.tcpType.String()) + } + if c.RelatedAddress() != nil { val = fmt.Sprintf("%s raddr %s rport %d", val, @@ -428,6 +433,8 @@ func UnmarshalCandidate(raw string) (Candidate, error) { relatedAddress := "" relatedPort := 0 + tcpType := TCPTypeUnspecified + if len(split) > 8 { split = split[8:] @@ -445,13 +452,18 @@ func UnmarshalCandidate(raw string) (Candidate, error) { return nil, fmt.Errorf("could not parse port: %v", err) } relatedPort = int(rawRelatedPort) - } + } else if split[0] == "tcptype" { + if len(split) < 2 { + return nil, fmt.Errorf("could not parse typtype: incorrect length") + } + tcpType = NewTCPType(split[1]) + } } switch typ { case "host": - return NewCandidateHost(&CandidateHostConfig{foundation, protocol, address, port, uint16(component), TCPTypePassive}) + return NewCandidateHost(&CandidateHostConfig{foundation, protocol, address, port, uint16(component), tcpType}) case "srflx": return NewCandidateServerReflexive(&CandidateServerReflexiveConfig{foundation, protocol, address, port, uint16(component), relatedAddress, relatedPort}) case "prflx": diff --git a/candidate_test.go b/candidate_test.go index 509f807..857cd50 100644 --- a/candidate_test.go +++ b/candidate_test.go @@ -137,6 +137,75 @@ func TestCandidateLastReceived(t *testing.T) { assert.Equal(t, candidate.LastReceived(), now) } +func TestCandidateFoundation(t *testing.T) { + // All fields are the same + assert.Equal(t, + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation(), + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation()) + + // Different Address + assert.NotEqual(t, + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation(), + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "B", + }).Foundation()) + + // Different networkType + assert.NotEqual(t, + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation(), + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP6, + address: "A", + }).Foundation()) + + // Different candidateType + assert.NotEqual(t, + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation(), + (&candidateBase{ + candidateType: CandidateTypePeerReflexive, + networkType: NetworkTypeUDP4, + address: "A", + }).Foundation()) + + // Port has no effect + assert.Equal(t, + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + port: 8080, + }).Foundation(), + (&candidateBase{ + candidateType: CandidateTypeHost, + networkType: NetworkTypeUDP4, + address: "A", + port: 80, + }).Foundation()) +} + func TestCandidateMarshal(t *testing.T) { for _, test := range []struct { candidate Candidate @@ -152,7 +221,7 @@ func TestCandidateMarshal(t *testing.T) { }, "", }, - "1938809241 1 udp 2130706431 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host", + "121457893 1 udp 2130706431 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host", false, }, {&CandidateHost{ @@ -164,7 +233,7 @@ func TestCandidateMarshal(t *testing.T) { }, "", }, - "1986380506 1 udp 2130706431 10.0.75.1 53634 typ host", + "4273957277 1 udp 2130706431 10.0.75.1 53634 typ host", false, }, {&CandidateServerReflexive{ @@ -176,7 +245,7 @@ func TestCandidateMarshal(t *testing.T) { relatedAddress: &CandidateRelatedAddress{"192.168.0.274", 53991}, }, }, - "4207374051 1 udp 1694498815 191.228.238.68 53991 typ srflx raddr 192.168.0.274 rport 53991", + "647372371 1 udp 1694498815 191.228.238.68 53991 typ srflx raddr 192.168.0.274 rport 53991", false, }, {&CandidateRelay{ @@ -189,7 +258,20 @@ func TestCandidateMarshal(t *testing.T) { }, nil, }, - "4207374051 1 udp 16777215 50.0.0.1 5000 typ relay raddr 192.168.0.1 rport 5001", + "848194626 1 udp 16777215 50.0.0.1 5000 typ relay raddr 192.168.0.1 rport 5001", + false, + }, + {&CandidateHost{ + candidateBase{ + networkType: NetworkTypeTCP4, + candidateType: CandidateTypeHost, + address: "192.168.0.196", + port: 0, + tcpType: TCPTypeActive, + }, + "", + }, + "1052353102 1 tcp 2128609279 192.168.0.196 0 typ host tcptype active", false, }, @@ -201,14 +283,19 @@ func TestCandidateMarshal(t *testing.T) { {nil, "4207374051 1 udp 1685790463 191.228.238.68 99999999 typ srflx raddr 192.168.0.278 rport 53991 generation 0 network-id 3", true}, {nil, "4207374051 1 udp 1685790463 191.228.238.68 53991 typ srflx raddr", true}, {nil, "4207374051 1 udp 1685790463 191.228.238.68 53991 typ srflx raddr 192.168.0.278 rport 99999999 generation 0 network-id 3", true}, + {nil, "4207374051 INVALID udp 2130706431 10.0.75.1 53634 typ host", true}, + {nil, "4207374051 1 udp INVALID 10.0.75.1 53634 typ host", true}, + {nil, "4207374051 INVALID udp 2130706431 10.0.75.1 INVALID typ host", true}, + {nil, "4207374051 1 udp 2130706431 10.0.75.1 53634 typ INVALID", true}, } { actualCandidate, err := UnmarshalCandidate(test.marshaled) if test.expectError { assert.Error(t, err) - return + continue } assert.NoError(t, err) + assert.True(t, test.candidate.Equal(actualCandidate)) assert.Equal(t, test.marshaled, actualCandidate.Marshal()) }