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

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !js
     5  // +build !js
     6  
     7  package webrtc
     8  
     9  import "testing"
    10  
    11  func TestGenerateDataChannelID(t *testing.T) {
    12  	sctpTransportWithChannels := func(ids []uint16) *SCTPTransport {
    13  		ret := &SCTPTransport{dataChannels: []*DataChannel{}}
    14  
    15  		for i := range ids {
    16  			id := ids[i]
    17  			ret.dataChannels = append(ret.dataChannels, &DataChannel{id: &id})
    18  		}
    19  
    20  		return ret
    21  	}
    22  
    23  	testCases := []struct {
    24  		role   DTLSRole
    25  		s      *SCTPTransport
    26  		result uint16
    27  	}{
    28  		{DTLSRoleClient, sctpTransportWithChannels([]uint16{}), 0},
    29  		{DTLSRoleClient, sctpTransportWithChannels([]uint16{1}), 0},
    30  		{DTLSRoleClient, sctpTransportWithChannels([]uint16{0}), 2},
    31  		{DTLSRoleClient, sctpTransportWithChannels([]uint16{0, 2}), 4},
    32  		{DTLSRoleClient, sctpTransportWithChannels([]uint16{0, 4}), 2},
    33  		{DTLSRoleServer, sctpTransportWithChannels([]uint16{}), 1},
    34  		{DTLSRoleServer, sctpTransportWithChannels([]uint16{0}), 1},
    35  		{DTLSRoleServer, sctpTransportWithChannels([]uint16{1}), 3},
    36  		{DTLSRoleServer, sctpTransportWithChannels([]uint16{1, 3}), 5},
    37  		{DTLSRoleServer, sctpTransportWithChannels([]uint16{1, 5}), 3},
    38  	}
    39  	for _, testCase := range testCases {
    40  		idPtr := new(uint16)
    41  		err := testCase.s.generateAndSetDataChannelID(testCase.role, &idPtr)
    42  		if err != nil {
    43  			t.Errorf("failed to generate id: %v", err)
    44  			return
    45  		}
    46  		if *idPtr != testCase.result {
    47  			t.Errorf("Wrong id: %d expected %d", *idPtr, testCase.result)
    48  		}
    49  	}
    50  }