github.com/pion/webrtc/v3@v3.2.24/dtlsrole_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/pion/sdp/v3"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestDTLSRole_String(t *testing.T) {
    15  	testCases := []struct {
    16  		role           DTLSRole
    17  		expectedString string
    18  	}{
    19  		{DTLSRole(Unknown), unknownStr},
    20  		{DTLSRoleAuto, "auto"},
    21  		{DTLSRoleClient, "client"},
    22  		{DTLSRoleServer, "server"},
    23  	}
    24  
    25  	for i, testCase := range testCases {
    26  		assert.Equal(t,
    27  			testCase.expectedString,
    28  			testCase.role.String(),
    29  			"testCase: %d %v", i, testCase,
    30  		)
    31  	}
    32  }
    33  
    34  func TestDTLSRoleFromRemoteSDP(t *testing.T) {
    35  	parseSDP := func(raw string) *sdp.SessionDescription {
    36  		parsed := &sdp.SessionDescription{}
    37  		if err := parsed.Unmarshal([]byte(raw)); err != nil {
    38  			panic(err)
    39  		}
    40  		return parsed
    41  	}
    42  
    43  	const noMedia = `v=0
    44  o=- 4596489990601351948 2 IN IP4 127.0.0.1
    45  s=-
    46  t=0 0
    47  `
    48  
    49  	const mediaNoSetup = `v=0
    50  o=- 4596489990601351948 2 IN IP4 127.0.0.1
    51  s=-
    52  t=0 0
    53  m=application 47299 DTLS/SCTP 5000
    54  c=IN IP4 192.168.20.129
    55  `
    56  
    57  	const mediaSetupDeclared = `v=0
    58  o=- 4596489990601351948 2 IN IP4 127.0.0.1
    59  s=-
    60  t=0 0
    61  m=application 47299 DTLS/SCTP 5000
    62  c=IN IP4 192.168.20.129
    63  a=setup:%s
    64  `
    65  
    66  	testCases := []struct {
    67  		test               string
    68  		sessionDescription *sdp.SessionDescription
    69  		expectedRole       DTLSRole
    70  	}{
    71  		{"nil SessionDescription", nil, DTLSRoleAuto},
    72  		{"No MediaDescriptions", parseSDP(noMedia), DTLSRoleAuto},
    73  		{"MediaDescription, no setup", parseSDP(mediaNoSetup), DTLSRoleAuto},
    74  		{"MediaDescription, setup:actpass", parseSDP(fmt.Sprintf(mediaSetupDeclared, "actpass")), DTLSRoleAuto},
    75  		{"MediaDescription, setup:passive", parseSDP(fmt.Sprintf(mediaSetupDeclared, "passive")), DTLSRoleServer},
    76  		{"MediaDescription, setup:active", parseSDP(fmt.Sprintf(mediaSetupDeclared, "active")), DTLSRoleClient},
    77  	}
    78  	for _, testCase := range testCases {
    79  		assert.Equal(t,
    80  			testCase.expectedRole,
    81  			dtlsRoleFromRemoteSDP(testCase.sessionDescription),
    82  			"TestDTLSRoleFromSDP (%s)", testCase.test,
    83  		)
    84  	}
    85  }