Repository: pion/sctp
Pull request #305 (2024-02-28 15:06:05)

DRY and explain use of port 5000

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pion/sctp/pull/305?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 67.19%. Comparing base [(`a5583a6`)](https://app.codecov.io/gh/pion/sctp/commit/a5583a6c4ac8fb09154752c24df40d05b7ff47d9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) to head [(`7711f7a`)](https://app.codecov.io/gh/pion/sctp/pull/305?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           master     #305       +/-   ##
===========================================
- Coverage   80.81%   67.19%   -13.63%     
===========================================
  Files          49       49               
  Lines        4133     4133               
===========================================
- Hits         3340     2777      -563     
- Misses        649     1197      +548     
- Partials      144      159       +15     
```

| [Flag](https://app.codecov.io/gh/pion/sctp/pull/305/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [go](https://app.codecov.io/gh/pion/sctp/pull/305/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `?` | |
| [wasm](https://app.codecov.io/gh/pion/sctp/pull/305/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | `67.19% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#carryforward-flags-in-the-pull-request-comment) to find out more.


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pion/sctp/pull/305?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).


Patch:
diff --git a/association.go b/association.go
index 47c0d94..b4056a8 100644
--- a/association.go
+++ b/association.go
@@ -19,6 +19,12 @@ import (
 	"github.com/pion/randutil"
 )
 
+// Port 5000 shows up in examples for SDPs used by WebRTC. Since this implementation
+// assumes it will be used by DTLS over UDP, the port is only meaningful for de-multiplexing
+// but more-so verification.
+// Example usage: https://www.rfc-editor.org/rfc/rfc8841.html#section-13.1-2
+const defaultSCTPSrcDstPort = 5000
+
 // Use global random generator to properly seed by crypto grade random.
 var globalMathRandomGenerator = randutil.NewMathRandomGenerator() // nolint:gochecknoglobals
 
@@ -393,8 +399,8 @@ func (a *Association) sendInit() error {
 
 	outbound := &packet{}
 	outbound.verificationTag = a.peerVerificationTag
-	a.sourcePort = 5000      // Spec??
-	a.destinationPort = 5000 // Spec??
+	a.sourcePort = defaultSCTPSrcDstPort
+	a.destinationPort = defaultSCTPSrcDstPort
 	outbound.sourcePort = a.sourcePort
 	outbound.destinationPort = a.destinationPort
 


Test patch:
diff --git a/packet_test.go b/packet_test.go
index 1a270e5..40557f3 100644
--- a/packet_test.go
+++ b/packet_test.go
@@ -20,10 +20,10 @@ func TestPacketUnmarshal(t *testing.T) {
 	switch {
 	case err != nil:
 		t.Errorf("Unmarshal failed for SCTP packet with no chunks: %v", err)
-	case pkt.sourcePort != 5000:
-		t.Errorf("Unmarshal passed for SCTP packet, but got incorrect source port exp: %d act: %d", 5000, pkt.sourcePort)
-	case pkt.destinationPort != 5000:
-		t.Errorf("Unmarshal passed for SCTP packet, but got incorrect destination port exp: %d act: %d", 5000, pkt.destinationPort)
+	case pkt.sourcePort != defaultSCTPSrcDstPort:
+		t.Errorf("Unmarshal passed for SCTP packet, but got incorrect source port exp: %d act: %d", defaultSCTPSrcDstPort, pkt.sourcePort)
+	case pkt.destinationPort != defaultSCTPSrcDstPort:
+		t.Errorf("Unmarshal passed for SCTP packet, but got incorrect destination port exp: %d act: %d", defaultSCTPSrcDstPort, pkt.destinationPort)
 	case pkt.verificationTag != 0:
 		t.Errorf("Unmarshal passed for SCTP packet, but got incorrect verification tag exp: %d act: %d", 0, pkt.verificationTag)
 	}
diff --git a/rtx_timer_test.go b/rtx_timer_test.go
index 678103d..2a7da5a 100644
--- a/rtx_timer_test.go
+++ b/rtx_timer_test.go
@@ -120,7 +120,7 @@ func TestRtxTimer(t *testing.T) {
 		timerID := 0
 		var nCbs int32
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				atomic.AddInt32(&nCbs, 1)
 				// 30 : 1 (30)
 				// 60 : 2 (90)
@@ -128,7 +128,7 @@ func TestRtxTimer(t *testing.T) {
 				// 240: 4 (550) <== expected in 650 msec
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		assert.False(t, rt.isRunning(), "should not be running")
@@ -150,11 +150,11 @@ func TestRtxTimer(t *testing.T) {
 		var nCbs int32
 
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				atomic.AddInt32(&nCbs, 1)
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		interval := float64(30.0)
@@ -177,11 +177,11 @@ func TestRtxTimer(t *testing.T) {
 		var nCbs int32
 
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				atomic.AddInt32(&nCbs, 1)
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		interval := float64(30.0)
@@ -200,11 +200,11 @@ func TestRtxTimer(t *testing.T) {
 		timerID := 1
 		var nCbs int32
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				atomic.AddInt32(&nCbs, 1)
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		interval := float64(30.0)
@@ -226,12 +226,12 @@ func TestRtxTimer(t *testing.T) {
 		timerID := 2
 		var nCbs int32
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				atomic.AddInt32(&nCbs, 1)
 				t.Log("onRTO() called")
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		for i := 0; i < 1000; i++ {
@@ -305,7 +305,7 @@ func TestRtxTimer(t *testing.T) {
 					doneCh <- true
 				}
 			},
-			onRtxFailure: func(id int) {
+			onRtxFailure: func(_ int) {
 				assert.Fail(t, "timer should not fail")
 			},
 		}, 0, 0)
@@ -338,11 +338,11 @@ func TestRtxTimer(t *testing.T) {
 		doneCh := make(chan bool)
 
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(id int, _ uint) {
 				assert.Equal(t, timerID, id, "unexpted timer ID: %d", id)
 				doneCh <- true
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		for i := 0; i < 10; i++ {
@@ -362,10 +362,10 @@ func TestRtxTimer(t *testing.T) {
 		var rtoCount int
 		timerID := 6
 		rt := newRTXTimer(timerID, &testTimerObserver{
-			onRTO: func(id int, nRtos uint) {
+			onRTO: func(_ int, _ uint) {
 				rtoCount++
 			},
-			onRtxFailure: func(id int) {},
+			onRtxFailure: func(_ int) {},
 		}, pathMaxRetrans, 0)
 
 		ok := rt.start(20)
diff --git a/vnet_test.go b/vnet_test.go
index 6f225bb..9015217 100644
--- a/vnet_test.go
+++ b/vnet_test.go
@@ -202,8 +202,8 @@ func testRwndFull(t *testing.T, unordered bool) {
 		defer close(serverShutDown)
 		// connected UDP conn for server
 		conn, err := venv.net0.DialUDP("udp4",
-			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
-			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
+			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
+			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
 		)
 		if !assert.NoError(t, err, "should succeed") {
 			return
@@ -277,8 +277,8 @@ func testRwndFull(t *testing.T, unordered bool) {
 		defer close(clientShutDown)
 		// connected UDP conn for client
 		conn, err := venv.net1.DialUDP("udp4",
-			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
-			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
+			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
+			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
 		)
 		if !assert.NoError(t, err, "should succeed") {
 			return
@@ -435,8 +435,8 @@ func TestStreamClose(t *testing.T) {
 			defer close(serverShutDown)
 			// connected UDP conn for server
 			conn, innerErr := venv.net0.DialUDP("udp4",
-				&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
-				&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
+				&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
+				&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
 			)
 			if !assert.NoError(t, innerErr, "should succeed") {
 				return
@@ -485,8 +485,8 @@ func TestStreamClose(t *testing.T) {
 
 		// connected UDP conn for client
 		conn, err := venv.net1.DialUDP("udp4",
-			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
-			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
+			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
+			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
 		)
 		if !assert.NoError(t, err, "should succeed") {
 			return
@@ -620,8 +620,8 @@ func TestCookieEchoRetransmission(t *testing.T) {
 		defer close(serverShutDown)
 		// connected UDP conn for server
 		conn, err := venv.net0.DialUDP("udp4",
-			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
-			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
+			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
+			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
 		)
 		if !assert.NoError(t, err, "should succeed") {
 			return
@@ -650,8 +650,8 @@ func TestCookieEchoRetransmission(t *testing.T) {
 		defer close(clientShutDown)
 		// connected UDP conn for client
 		conn, err := venv.net1.DialUDP("udp4",
-			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: 5000},
-			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: 5000},
+			&net.UDPAddr{IP: net.ParseIP("2.2.2.2"), Port: defaultSCTPSrcDstPort},
+			&net.UDPAddr{IP: net.ParseIP("1.1.1.1"), Port: defaultSCTPSrcDstPort},
 		)
 		if !assert.NoError(t, err, "should succeed") {
 			return