File: dtlsrole_test.go

package info (click to toggle)
golang-github-pion-webrtc.v3 3.1.56-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: javascript: 595; sh: 28; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 1,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package webrtc

import (
	"fmt"
	"testing"

	"github.com/pion/sdp/v3"
	"github.com/stretchr/testify/assert"
)

func TestDTLSRole_String(t *testing.T) {
	testCases := []struct {
		role           DTLSRole
		expectedString string
	}{
		{DTLSRole(Unknown), unknownStr},
		{DTLSRoleAuto, "auto"},
		{DTLSRoleClient, "client"},
		{DTLSRoleServer, "server"},
	}

	for i, testCase := range testCases {
		assert.Equal(t,
			testCase.expectedString,
			testCase.role.String(),
			"testCase: %d %v", i, testCase,
		)
	}
}

func TestDTLSRoleFromRemoteSDP(t *testing.T) {
	parseSDP := func(raw string) *sdp.SessionDescription {
		parsed := &sdp.SessionDescription{}
		if err := parsed.Unmarshal([]byte(raw)); err != nil {
			panic(err)
		}
		return parsed
	}

	const noMedia = `v=0
o=- 4596489990601351948 2 IN IP4 127.0.0.1
s=-
t=0 0
`

	const mediaNoSetup = `v=0
o=- 4596489990601351948 2 IN IP4 127.0.0.1
s=-
t=0 0
m=application 47299 DTLS/SCTP 5000
c=IN IP4 192.168.20.129
`

	const mediaSetupDeclared = `v=0
o=- 4596489990601351948 2 IN IP4 127.0.0.1
s=-
t=0 0
m=application 47299 DTLS/SCTP 5000
c=IN IP4 192.168.20.129
a=setup:%s
`

	testCases := []struct {
		test               string
		sessionDescription *sdp.SessionDescription
		expectedRole       DTLSRole
	}{
		{"nil SessionDescription", nil, DTLSRoleAuto},
		{"No MediaDescriptions", parseSDP(noMedia), DTLSRoleAuto},
		{"MediaDescription, no setup", parseSDP(mediaNoSetup), DTLSRoleAuto},
		{"MediaDescription, setup:actpass", parseSDP(fmt.Sprintf(mediaSetupDeclared, "actpass")), DTLSRoleAuto},
		{"MediaDescription, setup:passive", parseSDP(fmt.Sprintf(mediaSetupDeclared, "passive")), DTLSRoleServer},
		{"MediaDescription, setup:active", parseSDP(fmt.Sprintf(mediaSetupDeclared, "active")), DTLSRoleClient},
	}
	for _, testCase := range testCases {
		assert.Equal(t,
			testCase.expectedRole,
			dtlsRoleFromRemoteSDP(testCase.sessionDescription),
			"TestDTLSRoleFromSDP (%s)", testCase.test,
		)
	}
}