github.com/pion/webrtc/v3@v3.2.24/icecandidate_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  	"testing"
     8  
     9  	"github.com/pion/ice/v2"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestICECandidate_Convert(t *testing.T) {
    14  	testCases := []struct {
    15  		native ICECandidate
    16  
    17  		expectedType           ice.CandidateType
    18  		expectedNetwork        string
    19  		expectedAddress        string
    20  		expectedPort           int
    21  		expectedComponent      uint16
    22  		expectedRelatedAddress *ice.CandidateRelatedAddress
    23  	}{
    24  		{
    25  			ICECandidate{
    26  				Foundation: "foundation",
    27  				Priority:   128,
    28  				Address:    "1.0.0.1",
    29  				Protocol:   ICEProtocolUDP,
    30  				Port:       1234,
    31  				Typ:        ICECandidateTypeHost,
    32  				Component:  1,
    33  			},
    34  
    35  			ice.CandidateTypeHost,
    36  			"udp",
    37  			"1.0.0.1",
    38  			1234,
    39  			1,
    40  			nil,
    41  		},
    42  		{
    43  			ICECandidate{
    44  				Foundation:     "foundation",
    45  				Priority:       128,
    46  				Address:        "::1",
    47  				Protocol:       ICEProtocolUDP,
    48  				Port:           1234,
    49  				Typ:            ICECandidateTypeSrflx,
    50  				Component:      1,
    51  				RelatedAddress: "1.0.0.1",
    52  				RelatedPort:    4321,
    53  			},
    54  
    55  			ice.CandidateTypeServerReflexive,
    56  			"udp",
    57  			"::1",
    58  			1234,
    59  			1,
    60  			&ice.CandidateRelatedAddress{
    61  				Address: "1.0.0.1",
    62  				Port:    4321,
    63  			},
    64  		},
    65  		{
    66  			ICECandidate{
    67  				Foundation:     "foundation",
    68  				Priority:       128,
    69  				Address:        "::1",
    70  				Protocol:       ICEProtocolUDP,
    71  				Port:           1234,
    72  				Typ:            ICECandidateTypePrflx,
    73  				Component:      1,
    74  				RelatedAddress: "1.0.0.1",
    75  				RelatedPort:    4321,
    76  			},
    77  
    78  			ice.CandidateTypePeerReflexive,
    79  			"udp",
    80  			"::1",
    81  			1234,
    82  			1,
    83  			&ice.CandidateRelatedAddress{
    84  				Address: "1.0.0.1",
    85  				Port:    4321,
    86  			},
    87  		},
    88  	}
    89  
    90  	for i, testCase := range testCases {
    91  		var expectedICE ice.Candidate
    92  		var err error
    93  		switch testCase.expectedType { // nolint:exhaustive
    94  		case ice.CandidateTypeHost:
    95  			config := ice.CandidateHostConfig{
    96  				Network:    testCase.expectedNetwork,
    97  				Address:    testCase.expectedAddress,
    98  				Port:       testCase.expectedPort,
    99  				Component:  testCase.expectedComponent,
   100  				Foundation: "foundation",
   101  				Priority:   128,
   102  			}
   103  			expectedICE, err = ice.NewCandidateHost(&config)
   104  		case ice.CandidateTypeServerReflexive:
   105  			config := ice.CandidateServerReflexiveConfig{
   106  				Network:    testCase.expectedNetwork,
   107  				Address:    testCase.expectedAddress,
   108  				Port:       testCase.expectedPort,
   109  				Component:  testCase.expectedComponent,
   110  				Foundation: "foundation",
   111  				Priority:   128,
   112  				RelAddr:    testCase.expectedRelatedAddress.Address,
   113  				RelPort:    testCase.expectedRelatedAddress.Port,
   114  			}
   115  			expectedICE, err = ice.NewCandidateServerReflexive(&config)
   116  		case ice.CandidateTypePeerReflexive:
   117  			config := ice.CandidatePeerReflexiveConfig{
   118  				Network:    testCase.expectedNetwork,
   119  				Address:    testCase.expectedAddress,
   120  				Port:       testCase.expectedPort,
   121  				Component:  testCase.expectedComponent,
   122  				Foundation: "foundation",
   123  				Priority:   128,
   124  				RelAddr:    testCase.expectedRelatedAddress.Address,
   125  				RelPort:    testCase.expectedRelatedAddress.Port,
   126  			}
   127  			expectedICE, err = ice.NewCandidatePeerReflexive(&config)
   128  		}
   129  		assert.NoError(t, err)
   130  
   131  		// first copy the candidate ID so it matches the new one
   132  		testCase.native.statsID = expectedICE.ID()
   133  		actualICE, err := testCase.native.toICE()
   134  		assert.NoError(t, err)
   135  
   136  		assert.Equal(t, expectedICE, actualICE, "testCase: %d ice not equal %v", i, actualICE)
   137  	}
   138  }
   139  
   140  func TestConvertTypeFromICE(t *testing.T) {
   141  	t.Run("host", func(t *testing.T) {
   142  		ct, err := convertTypeFromICE(ice.CandidateTypeHost)
   143  		if err != nil {
   144  			t.Fatal("failed coverting ice.CandidateTypeHost")
   145  		}
   146  		if ct != ICECandidateTypeHost {
   147  			t.Fatal("should be converted to ICECandidateTypeHost")
   148  		}
   149  	})
   150  	t.Run("srflx", func(t *testing.T) {
   151  		ct, err := convertTypeFromICE(ice.CandidateTypeServerReflexive)
   152  		if err != nil {
   153  			t.Fatal("failed coverting ice.CandidateTypeServerReflexive")
   154  		}
   155  		if ct != ICECandidateTypeSrflx {
   156  			t.Fatal("should be converted to ICECandidateTypeSrflx")
   157  		}
   158  	})
   159  	t.Run("prflx", func(t *testing.T) {
   160  		ct, err := convertTypeFromICE(ice.CandidateTypePeerReflexive)
   161  		if err != nil {
   162  			t.Fatal("failed coverting ice.CandidateTypePeerReflexive")
   163  		}
   164  		if ct != ICECandidateTypePrflx {
   165  			t.Fatal("should be converted to ICECandidateTypePrflx")
   166  		}
   167  	})
   168  }
   169  
   170  func TestICECandidate_ToJSON(t *testing.T) {
   171  	candidate := ICECandidate{
   172  		Foundation: "foundation",
   173  		Priority:   128,
   174  		Address:    "1.0.0.1",
   175  		Protocol:   ICEProtocolUDP,
   176  		Port:       1234,
   177  		Typ:        ICECandidateTypeHost,
   178  		Component:  1,
   179  	}
   180  
   181  	candidateInit := candidate.ToJSON()
   182  
   183  	assert.Equal(t, uint16(0), *candidateInit.SDPMLineIndex)
   184  	assert.Equal(t, "candidate:foundation 1 udp 128 1.0.0.1 1234 typ host", candidateInit.Candidate)
   185  }